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

[Bug]: Module Federation Runtime with Pages + useDataLoader crashes #6855

Open
zackarychapple opened this issue Feb 21, 2025 · 4 comments
Open

Comments

@zackarychapple
Copy link

Version

System:
    OS: macOS 15.3.1
    CPU: (16) arm64 Apple M4 Max
    Memory: 2.57 GB / 64.00 GB
    Shell: 5.9 - /bin/zsh
  Browsers:
    Chrome: 133.0.6943.127
    Safari: 18.3
  npmPackages:
    @modern-js/app-tools: 2.65.1 => 2.65.1 
    @modern-js/runtime: 2.65.1 => 2.65.1 
    @modern-js/tsconfig: 2.65.1 => 2.65.1

Details

We have a goal of using Module Federation together with ModernJS. The repository I am creating is trying to leverage enhanced to attempt to demonstrate several capabilities.

  1. We can do basic module federation of components using enhanced/runtime (this works)
  2. We can do advanced module federation of routes that have server side rendered data (this does not)

Reproduce link

https://github.com/zackarychapple/modern-mf-example2

Reproduce Steps

  1. start the host app
  2. start the ssr-profile app
  3. start the header app

When we import this remote into the header we are able to see the Async boundary as well as the delay in loading. Our initial understanding was that because this is wrapped in Await, the data fetch to the Pokemon api would be performed on the server (especially since this is being run with ssr streaming mode enabled.

const SsrProfile = React.lazy(() =>
  loadRemote('ssrprofile/ssr-profile').then((m) => {
    return m;
  }),
);

After some further digging we made an attempt to expose another federated remote that has useLoaderData which does in fact do SSR and only renders the data when pulled up manually. However when we try to federate it into the header we get an error.

const PreRenderedPokemon = React.lazy(() =>
  loadRemote('ssrprofile/rendered').then((m) => {
    return m;
  }),
);

The error

Unexpected Application Error!
useLoaderData must be used within a data router. See https://reactrouter.com/routers/picking-a-router.

Questions

  1. What is the proper mechanism to do pure SSR functionality that does not expose the requests to the frontend if not useLoaderData?
  2. If useLoaderData is the proper mechanism what are we missing?
  3. It seems there are some documentation and example discrepancies around using the enhanced runtime and the modernjs configs.
    Should we be using this methodology in the components themselves
registerRemotes([
  {
    name: 'ssrprofile',
    entry: 'http://localhost:4002/remoteEntry.js',
  }
]);

const SsrProfile = React.lazy(() =>
  loadRemote('ssrprofile/ssr-profile').then((m) => {
    return m;
  }),
);

Or should we be using this in the module-federation.config.ts that is loaded via the moduleFederationPlugin in modern.config.ts

  remotes: {
    ssrprofile: 'ssrprofile@http://localhost:4002/remoteEntry.js',
  },
@zackarychapple zackarychapple added the bug Something isn't working label Feb 21, 2025
@zackarychapple
Copy link
Author

Wondering if this is related to the work @2heal1 is doing in this PR

@zllkjc
Copy link
Member

zllkjc commented Feb 24, 2025

Oh, I know what you want. But we have not support get Data in server with module federation together now.

It's still on design phase, and we do some attempt in internal framework, maybe the feature will expose in Modern.js in a few months.

@zackarychapple
Copy link
Author

Should we keep this open to track?

@zllkjc zllkjc added ✨ feature request and removed bug Something isn't working labels Feb 26, 2025
@zllkjc
Copy link
Member

zllkjc commented Feb 26, 2025

Keep open ! I added a new tag as feature request~

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

No branches or pull requests

2 participants