core: Next.js: Dynamic Remotes Singleton sharing not working

Describe the bug

When using dynamic remotes in the Next.js module federation plugin, it looks like singletons are not loaded (or loaded too late) which is causing components to crash. In our use case, we’re e.g. sharing @tanstack/react-query and only set the QueryClientProvider in the host application.

When using “static” remotes in the NextFederationPlugin and the remote components are imported with a regular dynamic import, the sharing is working fine.

When using module federation with dynamic remotes, the singletons are not (yet) loaded and the component is crashing.

I tried to find the root cause for that, but the only thing I found out is that __webpack_share_scopes__.default is missing the loaded flag on the singleton remotes:

{
  "react": {
    "0": {
      "from": "nextjs-react_remote-app",
      "eager": false
    },
    "18.2.0": {
      "loaded": 1,
      "from": "roothost"
    }
  },
  "next/router": {
    "13.5.6": {
      "loaded": 1,
      "from": "roothost"
    }
  },
  "react-dom": {
    "0": {
      "from": "nextjs-react_remote-app",
      "eager": false
    },
    "18.2.0": {
      "loaded": 1,
      "from": "roothost"
    }
  },
  "@tanstack/react-query": {
    "5.13.4": {
      "from": "nextjs-react_remote-app",
      "eager": false,
-     "loaded": 1
    }
  }
}

How to reproduce

  1. Checkout the reproduction repo and install dependencies
  2. pnpm start and open the http://localhost:3000 This page is using the standard dynamic import and is loading fine ✅
  3. Now open http://localhost:3000/dynamic-remote
  4. Restart the dev-server (otherwise the singletons are somehow cached?)
  5. Reload http://localhost:3000/dynamic-remote --> see the error 💥

Reproduction

https://github.com/MarcusNotheis/module-federation-dynamic-nextjs-repro

Used Package Manager

pnpm

System Info

System:
    OS: macOS 14.1.1
    CPU: (12) arm64 Apple M2 Pro
    Memory: 398.00 MB / 32.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 20.10.0 - ~/.nvm/versions/node/v20.10.0/bin/node
    Yarn: 1.22.21 - ~/.nvm/versions/node/v20.10.0/bin/yarn
    npm: 10.2.3 - ~/.nvm/versions/node/v20.10.0/bin/npm
    pnpm: 8.10.5 - ~/.nvm/versions/node/v20.10.0/bin/pnpm
  Browsers:
    Chrome: 120.0.6099.71
    Safari: 17.1

Dependencies:
next: 13.5.6
@module-federation/nextjs-mf: 8.1.0-canary.7

Validations

About this issue

  • Original URL
  • State: closed
  • Created 7 months ago
  • Reactions: 4
  • Comments: 16 (14 by maintainers)

Most upvoted comments

Read here: https://github.com/module-federation/universe/tree/feat_apply_bundler_runtime/packages/runtime

You can dynamically defined containers as needed.

Or you dynamically control what remotes are loaded or from where, you should use the new runtimePlugins capability:

https://github.com/module-federation/universe/blob/feat_apply_bundler_runtime/packages/runtime/src/core.md

https://github.com/module-federation/universe/blob/feat_apply_bundler_runtime/packages/nextjs-mf/src/plugins/container/runtimePlugin.ts

new NextFederationPlugin({
runtimePlugins: [
        require.resolve(path.join(__dirname, '../container/runtimePlugin')),
      ],
})

We are encountering issues when trying to share react-query as a singleton with dynamic remotes. This problem is preventing us from updating our next and @module-federation/nextjs-mf packages.

Versions Tried

We attempted to use the following versions but encountered the same issue:

Current Versions

Currently, we are using the following versions without this issue:

  • @module-federation/nextjs-mf: 7.0.8
  • next: 13.4.19

We would appreciate any guidance or assistance in resolving this issue. Thank you.

published new nightlys

Stay pinned there!! Thanks for letting me know.