Using promise based dynamic remotes with a self reference

427 views Asked by At

I've been using promised based dynamic remotes

https://webpack.js.org/concepts/module-federation/#promise-based-dynamic-remotes

successfully for a little while now. However, I've run into a problem. When a module both exposes some functionality and consumes it(self) as a remote (this is "home" in the example), and I'm using promised based dynamic remotes, I ran into a loop where the module tries to load itself indefinitely. When using the static URL based remote, I don't run into this problem. I think I need to adjust the promised based remote in some manner, but I don't know how.

I've put together a relatively simple example on github, based on an example of self referencing micro frontend that Jack Herrington and Zack Jackson put together in their book, but I modified it slightly to attempt (but failing) to use promise based dynamic remotes. Promise based dynamic remotes are working in general for me. It's just when having a self reference that it's falling apart. Any help from anyone? Here's the example code:

https://github.com/trevorgithub/promise-based-dynamic-remotes-self-reference

The fact that the self reference leads to an infinite loop is probably not surprising; the code essentially add a script tag to load the remoteEntry.js file, waits for it to load, and then resolves the promise with an object that satisfies the init/get API. But in working out one of the modules dependencies, in this case "home" is a self reference which kicks off the process in a cycle. I think I need some way to signal that the promise is resolved already, rather than calling it repeatedly.

1

There are 1 answers

0
Notre On

I figured out a solution to my problem. I introduced a cache that is used to resolve repeated requests for a component's content, which was causing the infinite loop. The fix is demonstrated on the fixed-infinite-loop-with-cache branch: https://github.com/trevorgithub/promise-based-dynamic-remotes-self-reference/tree/fixed-infinite-loop-with-cache