Skip to content

fix(nextjs-mf): await async factories in server onLoad hook - #4973

Closed
shashank-u03 wants to merge 1 commit into
module-federation:mainfrom
shashank-u03:fix/nextjs-mf-onload-async-factory
Closed

fix(nextjs-mf): await async factories in server onLoad hook#4973
shashank-u03 wants to merge 1 commit into
module-federation:mainfrom
shashank-u03:fix/nextjs-mf-onload-async-factory

Conversation

@shashank-u03

Copy link
Copy Markdown
Contributor

Description

Remote containers in @module-federation/runtime expose async module factories (RemoteEntryExports.get returns () => Promise<Module>).

During the webpack build/SSR path, runtime-core loads remotes with loadFactory: false and from: 'build' (see packages/webpack-bundler-runtime/src/remotes.ts), passing the unexecuted factory to nextjs-mf's onLoad hook as exposeModuleFactory.

On the server, onLoad synchronously invoked that factory and Proxy-wrapped the return value for chunk-usage tracking. When the factory is async, the sync invocation returns a raw Promise. Proxy-wrapping that Promise causes webpack's async module runtime to fail when calling .then():

TypeError: Method Promise.prototype.then called on incompatible receiver [object Promise]

runtime-core catches this in loadRemote and routes it to errorLoadRemote with lifecycle: 'onLoad' and from: 'build'.

Solution: Make the server-side onLoad hook async-safe by awaiting Promise results from moduleOrFactory() before applying the existing Proxy wrapper. runtime-core already handles async factories in module.wraperFactory; this aligns nextjs-mf onLoad with that behavior.

Added regression tests in runtimePlugin.test.ts for the async exposeModuleFactory server path.

Related Issue

#2218

Types of changes

  • Docs change / refactoring / dependency upgrade
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)

Checklist

  • I have added tests to cover my changes.
  • All new and existing tests passed.
  • I have updated the documentation.

@changeset-bot

changeset-bot Bot commented Aug 8, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: d3f72c9

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@module-federation/nextjs-mf Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@shashank-u03
shashank-u03 force-pushed the fix/nextjs-mf-onload-async-factory branch from e9a20c2 to a3d351e Compare August 8, 2026 17:33

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: e9a20c22ce

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread packages/nextjs-mf/src/plugins/container/runtimePlugin.ts
MF v2 remote containers expose async module factories. When webpack
loads remotes with loadFactory:false during the build/SSR path,
nextjs-mf onLoad synchronously invoked the factory and Proxy-wrapped
the resulting Promise, which breaks Promise.prototype.then brand
checks and routes to errorLoadRemote.

Await Promise results before proxy-wrapping on the server.

Related: module-federation#2218
Co-authored-by: Cursor <cursoragent@cursor.com>

test(nextjs-mf): cover client onLoad path and async factory rejection

Add regression tests for browser-path passthrough and server-side
rejection propagation after making onLoad async-safe.

Co-authored-by: Cursor <cursoragent@cursor.com>
@shashank-u03
shashank-u03 force-pushed the fix/nextjs-mf-onload-async-factory branch from 075f1a1 to d3f72c9 Compare August 8, 2026 17:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant