You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Component modifications are not applied in module federation #394
I'm having an issue getting react refresh working on a project with module fedration. The ws connection is working, also in the console it seems its working, but the modifications are not applied to the component.
[webpack-dev-server] App updated. Recompiling...
index.js:3210 [webpack-dev-server] App hot update...
log.js:24 [HMR] Checking for updates on the server...
log.js:24 [HMR] Updated modules:
log.js:24 [HMR] - ./src/Button.js
log.js:24 [HMR] App is up to date.
react-refresh should be injecting before react-dom this seem to work at app1;
Also react-refresh should be executed only once, or if executed more than once (it seems like the case on app 2), the react-dom should be injected again, that it's not possible right now because this plugin only injects it once.
Put this piece of code at the start of your app2 entry and it should work right now.
A PR would be welcome to fix it, maybe I'll do it :).
//A reference to the react refresh injectorconst{ injectIntoGlobalHook }=require("react-refresh/cjs/react-refresh-runtime.development")//Injects the react refresh replacing the one from the app1injectIntoGlobalHook(window);//Injects the react-dom instance again, that will now be referenced by the refresher from app2(windowasany).__REACT_DEVTOOLS_GLOBAL_HOOK__.inject(ReactDOM);
Reacted by zhanfang, Ariel Alvarado Atahuichi, nyqykk and Artem
HMR cannot work with federated modules, you can use @module-federation/fmr as an alternative solution.
We would have to rewrite HMR graphs in webpack in order to achieve this. Webpack needs to be aware of all module and parent modules in the graph which means we would have to build a way for federated webpack graphs to be interconnected as well. Changing a module in a remote doesn't mean that the container knows where its being used inside a host.
Does anyone have an example how react-refresh etc. should be loaded on the page in order for hot reload to work?
In my module federation setup, hot reload works occasionally, which suggests there's some indeterminism going on in the order that scripts are loading on the page.
The suggestion of bundling react-refresh separately, placing it in window and then making it external in MF libs does not seem to work.
Reacted by Vaughan Rouesnel, Octavio Tosta, 郑贤锋 and Nicolas Moser
@danieloprado, thanks for the suggestion. I've tried that and it does indeed fix this issue for the host application. The problem, however, is that we're using bi-directional module federation and when we set runtimeChunk: single it breaks any applications that are consuming a component from the host.
If you want to configure react-refresh to shared, you need react-refresh-webpack-plugin to change the import method of source code from react-refresh/runtime to react-refresh, and you also need to configure "react-refresh-webpack-plugin" include and exclude to avoid shared being referenced in the entry chunk, even so there will be other problems, so I also tend to force react-refresh to become a singleton to solve it quickly
As module federation becomes more popular I can imagine this will become a more common issue.
I cannot really offer much help without any example to look into. For the patch, my stance still stands that polluting global and binding this package to browser only is a bad thing to do. I personally do not use module federation and is not knoledgeable with it by any means, so help would be appreciated.
As someone else who is experiencing issues here with module federation, I am hoping to re-up this conversation. @pmmmwh, would you be open to applying the changes described here: #516 (comment)
As module federation becomes more popular I can imagine this will become a more common issue.
I cannot really offer much help without any example to look into. For the patch, my stance still stands that polluting global and binding this package to browser only is a bad thing to do. I personally do not use module federation and is not knoledgeable with it by any means, so help would be appreciated.
Workaround for us is to opt-into Module Federation when starting the dev server (via a command line arg), so that most developers are not impacted by this when developing the standalone application. When consuming a component via Module Federation, Fast Refresh doesn't work anyway as stated by @ScriptedAlchemy, so we are not losing anything.
Maybe you could use the library option to ensure react refresh is injected for both apps?
It seems like RefreshRuntime.injectIntoGlobalHook is not executed in MF, because the flag RefreshInjected has Injected by host app,but MF also require a refresh-runtime.So when refresh happend,MF can't make a connection with React.InjectIntoGlobalHook injects some hooks to make sure react refresh ready to perform.
constsafeThis=require('core-js-pure/features/global-this');constRefreshRuntime=require('react-refresh/runtime');if(process.env.NODE_ENV!=='production'){if(typeofsafeThis!=='undefined'){var$RefreshInjected$='__reactRefreshInjected';// Namespace the injected flag (if necessary) for monorepo compatibilityif(typeof__react_refresh_library__!=='undefined'&&__react_refresh_library__){$RefreshInjected$+='_'+__react_refresh_library__;}// Only inject the runtime if it hasn't been injectedif(!safeThis[$RefreshInjected$]){// Inject refresh runtime into global scopeRefreshRuntime.injectIntoGlobalHook(safeThis);// Mark the runtime as injected to prevent double-injectionsafeThis[$RefreshInjected$]=true;}}}
If our host app is mounting React components from secondary MF container with the React instance bundled and shared by the host app MF container, should we also put react-refresh/runtime in the MF shared dependencies?
EDIT: okay yeah I was finally able to get it working by doing that
Activity
Ridermansb commentedon Jun 22, 2021
Same with me
pmmmwh commentedon Jun 27, 2021
Are you externalising React?
kabel2 commentedon Jun 28, 2021
React is shared from App1 via the ModuleFederation plugin. I tried remove react from the list of shared components, but the problem remains.
Ridermansb commentedon Jul 7, 2021
Any news on this?
pmmmwh commentedon Jul 8, 2021
No. This is likely due to externalising and injection order of stuff. I currently do not have much time to check this, but I can revisit this later.
Muritavo commentedon Jul 10, 2021
There are 2 key events for it to work correctly.
Put this piece of code at the start of your app2 entry and it should work right now.
A PR would be welcome to fix it, maybe I'll do it :).
pmmmwh commentedon Jul 11, 2021
Maybe you could use the
libraryoption to ensure react refresh is injected for both apps?ScriptedAlchemy commentedon Sep 8, 2021
HMR cannot work with federated modules, you can use
@module-federation/fmras an alternative solution.We would have to rewrite HMR graphs in webpack in order to achieve this. Webpack needs to be aware of all module and parent modules in the graph which means we would have to build a way for federated webpack graphs to be interconnected as well. Changing a module in a remote doesn't mean that the container knows where its being used inside a host.
ije commentedon Sep 28, 2021
i figured out how resovle to this in #516, maybe not a greate idea, but it works
raine commentedon Apr 11, 2022
Does anyone have an example how react-refresh etc. should be loaded on the page in order for hot reload to work?
In my module federation setup, hot reload works occasionally, which suggests there's some indeterminism going on in the order that scripts are loading on the page.
The suggestion of bundling react-refresh separately, placing it in window and then making it external in MF libs does not seem to work.
ccambournac commentedon Jun 13, 2022
Indeed. Seems like using Webpack 5's
output.uniqueNamehelped me solve this same issue with 2 separate builds used within the same page.pmmmwh commentedon Jun 13, 2022
I would be happy to curate examples where React Refresh can be used properly with MF if anyone can provide a small sample repo.
ccambournac commentedon Jun 13, 2022
I'll definitely try to find time to do so @pmmmwh.
ruanyl commentedon Aug 19, 2022
For anyone who runs into this issue, the solution from @ije and @danieloprado in #516 works perfectly for me.
I end up with
pnpm patchto patch the package, you can also do it withyarn patchif you're usingyarn.@pmmmwh would you reconsider applying the changes suggested by @ije and @danieloprado?
ESoch commentedon Mar 10, 2023
As someone else who is experiencing issues here with module federation, I am hoping to re-up this conversation.
@pmmmwh, would you be open to applying the changes described here: #516 (comment)
As module federation becomes more popular I can imagine this will become a more common issue.
2 remaining items
ESoch commentedon Mar 10, 2023
@danieloprado, thanks for the suggestion. I've tried that and it does indeed fix this issue for the host application. The problem, however, is that we're using bi-directional module federation and when we set
runtimeChunk: singleit breaks any applications that are consuming a component from the host.zhangHongEn commentedon Mar 16, 2023
If you want to configure react-refresh to shared, you need react-refresh-webpack-plugin to change the import method of source code from react-refresh/runtime to react-refresh, and you also need to configure "react-refresh-webpack-plugin" include and exclude to avoid shared being referenced in the entry chunk, even so there will be other problems, so I also tend to force react-refresh to become a singleton to solve it quickly
zhangHongEn commentedon Mar 16, 2023
https://github.com/zhangHongEn/universal-module-federation-plugin/tree/main/packages/single-react-refresh-plugin
pmmmwh commentedon Apr 12, 2023
I cannot really offer much help without any example to look into. For the patch, my stance still stands that polluting global and binding this package to browser only is a bad thing to do. I personally do not use module federation and is not knoledgeable with it by any means, so help would be appreciated.
zhangHongEn commentedon Apr 17, 2023
@pmmmwh You can refer to this example, app2 achieves hmr by mounting react-refresh/runtime to the global maintain singleton, app3's react-refresh/runtime is not a singleton so it cannot hmr
https://github.com/wpmjs/examples/tree/main/module-federation-react-hmr
fxmkdev commentedon May 2, 2023
We also have this issue.
Workaround for us is to opt-into Module Federation when starting the dev server (via a command line arg), so that most developers are not impacted by this when developing the standalone application. When consuming a component via Module Federation, Fast Refresh doesn't work anyway as stated by @ScriptedAlchemy, so we are not losing anything.
alexander-akait commentedon May 2, 2023
@felixmokross Maybe you can provide small reproducible example? It helps, thank you
dtyyy commentedon Jul 16, 2024
It seems like
RefreshRuntime.injectIntoGlobalHookis not executed in MF, because the flagRefreshInjectedhas Injected by host app,but MF also require a refresh-runtime.So when refresh happend,MF can't make a connection with React.InjectIntoGlobalHookinjects some hooks to make sure react refresh ready to perform.as the code show, we can use
libraryor other options to config the__react_refresh_library__.ScriptedAlchemy commentedon Sep 29, 2024
module federation v2 supports hmr now. https://module-federation.io/
russellfoster commentedon Mar 26, 2025
Nice! Is there a way to get v2 in webpack that works with this plugin?
ScriptedAlchemy commentedon Mar 26, 2025
Replace webpacks old plugin with the new one via PR, or use the official v2 that's not coupled to webpack.
russellfoster commentedon Mar 27, 2025
Thanks for responding so quickly! By the official v2 plugin, are you talking about "@module-federation/enhanced"?
https://www.npmjs.com/package/@module-federation/enhanced
ScriptedAlchemy commentedon Mar 27, 2025
Yes that's the one
jedwards1211 commentedon Nov 2, 2025
If our host app is mounting React components from secondary MF container with the React instance bundled and shared by the host app MF container, should we also put
react-refresh/runtimein the MF shared dependencies?EDIT: okay yeah I was finally able to get it working by doing that
Add migration guide from react-hot-loader to Fast Refresh