[Module Federation] Support Eager Sharing of React #27508
Replies: 3 comments 6 replies
-
I might have a solution for sharing modules like react without a eager: true flag When sharing a module, give it a fake key and manually set the import and shareKey. This seems to keep react in the module graph as it should be, but allow sharing between federated |
Beta Was this translation helpful? Give feedback.
-
Any solution with |
Beta Was this translation helpful? Give feedback.
-
We are close to solving this problem. 1- we use normal module replacement, and rewrite the imports to next/router internally. Wrapping it in additional logic and exporting it back out. |
Beta Was this translation helpful? Give feedback.
-
Describe the feature you'd like to request
Make sure that react is shared via webpack share scopes when module federation is active.
When using a sidecar build(webpack plugin that creates a third build for any expose modules) https://github.com/module-federation/nextjs-mf
You can inspect the code and see how I've been able to make it work with a loader-plugin combo
Describe the solution you'd like
I was able to put the following in
_app
and share React eagerly with MF plugin
After upgrading to next 11 and react 17, i was no longer able to use
eager:true
to share react properly from nextjs, I end up getting hooks errors (more than one react instance is getting initialized)I was able to work around the problem by adding this object to the "shimmed" share scope, however I'm unable to utilize the nice
import('otherapp/moduleIwant')
because webpack errors saying that react cannot be found on share scope, likely due to import hoisting and the fact that I'm assigning share scope later on in the app boot sequence.By shimming react into share scope, I'm able to use federated modules but only if I am using the low-level window.scope.get(someModuleIwant);
You can see a full example (in broken state) here https://github.com/module-federation/module-federation-examples/tree/master/nextjs
Run yarn in the monorepo root, then yarn in the
/nextjs
folder, then yarn start and visitlocalhost:3001
and you'll see hooks errors.If next could internally eager share react or wrap its entrypoints behind a dynamic import (before react is first imported) then it would work.
Describe alternatives you've considered
I own the alternatives :P tried everything
Beta Was this translation helpful? Give feedback.
All reactions