Skip to content

fix: allow multiple runtime instances to receive HMR updates #10426

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

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

Conversation

GuilleX7
Copy link

Summary

The current rspack runtime assigns a global HMR JSONP loader that replaces any existing loader with the same name. While usually a single instance of the same runtime is running on the same context, we found some cases in Module Federation where running the same remoteEntry more than once (and therefore the webpack runtime) in the same page allows to have coexisting instances of the same runtime. However, since the HMR JSONP loader is overwritten by the latest instance, only the latest instance gets the updates, causing errors in the earlier instances.

See the following scenario.

Before the fix:

2025-05-20.13-29-12.mp4

After the fix:

2025-05-20.13-27-38.mp4

The proposed fix consists of installing a JSONP loader that calls previous existing JSONP loaders in the scope. Since all instances trigger HMR updates sequentially, we also rely on currentUpdate to determine which instance needs the HMR update (if an old instance is waiting for the HMR update and the JSONP loader is executed before newer instances have requested the update, the newer instances might not even have set currentUpdate up at that time)

Our real use case is:

We have a microfrontend that we load using Module Federation and we want to have multiple instances of it running at the same time. The problem is that it uses singleton libraries, so mounting it twice is not enough, as the runtime would be the same and both would share the same instance.

What we are doing right now is to run remoteEntry multiple times, creating different runtimes so multiple instances of the library are instantiated. However, with this approach we only get HMR in the latest instance that has been created.

For completeness, here's the discussion I've had on webpack. Same changes have been proposed, since the runtime seems to be the same.

webpack/webpack#19543

Checklist

  • Tests updated (or not required): E2E test added
  • Documentation updated (or not required): likely not needed

Copy link

netlify bot commented May 20, 2025

Deploy Preview for rspack canceled.

Built without sensitive environment variables

Name Link
🔨 Latest commit 3c7a3d8
🔍 Latest deploy log https://app.netlify.com/projects/rspack/deploys/682c6c920c43df00083ad02e

@CLAassistant
Copy link

CLAassistant commented May 20, 2025

CLA assistant check
All committers have signed the CLA.

@GuilleX7 GuilleX7 force-pushed the fix/multiple-hmr branch from cc222c6 to 85666b5 Compare May 20, 2025 11:36
@github-actions github-actions bot added the release: bug fix release: bug related release(mr only) label May 20, 2025
@GuilleX7 GuilleX7 force-pushed the fix/multiple-hmr branch from 85666b5 to 3c7a3d8 Compare May 20, 2025 11:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release: bug fix release: bug related release(mr only)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants