Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use StreamingTargetPlugin instead of @module-federation/enhanced when running MFE in SSR mode #30128

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

vamsi-pandari
Copy link

@vamsi-pandari vamsi-pandari commented Feb 20, 2025

Current Behavior

@nrwl/nx-pipelines-reviewers This PR addresses issues encountered when using Module Federation with Server-Side Rendering (SSR) in new Micro Frontend (MFE) workspaces created following the Nx documentation (https://nx.dev/recipes/react/module-federation-with-ssr).

Currently, creating a new MFE workspace results in the following errors:

1) The error below occurs due to the server attempting to directly load modules using @module-federation/enhanced, which is designed for browser environments and cannot fetch remote modules on the server.

[Error: ENOENT: no such file or directory, open '/server/__federation_expose_Module.js'
while loading "./Module" from 28] {
  errno: -2,
  code: 'ENOENT',
  syscall: 'open',
  path: '/server/__federation_expose_Module.js'
}

2) the error below arises when attempting to hardcode the remoteEntry.js file path in the ModuleFederationConfig, indicating a problem with how remote entries are handled in SSR.

Error: [ Federation Runtime ]: remoteEntryExports is undefined 
 {
  "alias": "checkout",
  "name": "checkout",
  "entry": "http://localhost:4202/remoteEntry.js",
  "shareScope": "default",
  "type": "global",
  "entryGlobalName": "checkout"
}

This PR resolves these issues by utilizing the StreamingTargetPlugin from @module-federation/node within withModuleFederationForSSR. This approach enables remote module loading during SSR, ensuring proper loading on the server-side. Remote modules are now correctly loaded in SSR mode, without errors, though re-rendering still occurs on the client if you don't hydrate root.

Expected Behavior

When using Server-Side Rendering (SSR) with Module Federation, remote modules must be loaded by the server. Currently, this process fails, resulting in errors and a fallback to browser-side loading, which defeats the purpose of SSR. This PR corrects this behavior.

@vamsi-pandari vamsi-pandari requested a review from a team as a code owner February 20, 2025 19:52
Copy link

vercel bot commented Feb 20, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

1 Skipped Deployment
Name Status Preview Updated (UTC)
nx-dev ⬜️ Ignored (Inspect) Visit Preview Feb 20, 2025 7:55pm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant