How to configure Module Federation in React Router with Vite? #13444
Replies: 2 comments 1 reply
-
|
any updates on this? |
Beta Was this translation helpful? Give feedback.
-
Solution: Runtime-Only Module FederationBoth errors you're encountering stem from the same root cause:
The The Fix: Use
|
| With Plugin (Broken) | Runtime-Only (Working) |
|---|---|
| Plugin hijacks all shared imports | No build-time transformation |
| Creates CJS virtual modules | Pure ESM throughout |
| Breaks Vite SSR evaluation | SSR works normally |
require is not defined errors |
No SSR interference |
Trade-offs
⚠️ Remote components won't be SSR'd (they render a placeholder during SSR)⚠️ Slightly slower first load (manifest fetched at runtime)⚠️ Remote URL configuration moves from build-time to runtime
Summary
The @module-federation/vite plugin has fundamental incompatibilities with SSR frameworks. The runtime-only approach sidesteps these issues by:
- Not using the Vite plugin at all
- Initializing MF only in client code (
entry.client.tsx) - Using
loadRemote()with proper client-side guards - Rendering fallbacks during SSR
This is currently the most reliable way to use Module Federation with React Router in framework mode until the MF Vite plugin gains proper SSR support.
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I am typically trying to set up a React Router (framework mode) project with Module Federation enabled.
You can explore a demo repo here: https://github.com/neetigyachahar/react-router-vite-module-federation
There are two apps,
hostandremote-1in it. First, I'm trying to configureremote-1app work properly.Here is a snippet of my
vite.config.tsfile in theremote-1app.The build is successfully created but if I run it, I get the following error:
Also, for the host app, I am getting the error as mentioned in this repo: https://github.com/Hunga1/module-federation-react-router-framework-reprex/tree/main
[vite] (ssr) Error when evaluating SSR module virtual:react-router/server-build: require is not defined at eval (/Users/myuser/module-federation-react-router-framework-reprex/react-router-framework-host/node_modules/__mf__virtual/host__loadShare__react_mf_2_router__loadShare__.js:8:25) [vite] Internal server error: require is not defined at eval (/Users/myuser/module-federation-react-router-framework-reprex/react-router-framework-host/node_modules/__mf__virtual/host__loadShare__react_mf_2_router__loadShare__.js:8:25)What could be the possible fixes for these?
Beta Was this translation helpful? Give feedback.
All reactions