-
-
Notifications
You must be signed in to change notification settings - Fork 9.1k
fix: allow multiple runtime instances to receive HMR updates #19543
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
base: main
Are you sure you want to change the base?
Conversation
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, it will work in some cases, but there is a limitation - both can have the same module and it will a problem, because both will accept update, and there is a problem with runtime too
Why you need this?
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. So you mean that we would be getting the same instance of the module for both instances over HMR? I wasn't aware of this limitation. Do you think there might be another approach that we are ignoring? |
Yes, to solve this you need to give a unique name - https://webpack.js.org/configuration/output/#outputuniquename |
The problem is that we couldn't find any way to make the uniqueName (and thus the JSONP loaders) dynamic. We need to compile the microfrontend once and run it multiple times within the same window context, but the chunk loaders and the HMR loaders seem to have a fixed name always. Is there anything like a wrapper of the entrypoint that allows to set a unique name before actually executing the runtime and attaching the JSONP loaders? Edit: also, I've been thinking about the same instance of the module being reused by both runtimes. Is it really like that? I might be completely wrong here, but doesn't the HMR chunk update contains module factories? My guess is that we are just passing the module factories to each runtime and that they will instantiate different modules, isn't it? I just made a small test by using Math.random() as the value of the hot-replaced module, and seems to be called twice: 2025-05-20.01-33-08.mp4 |
a6c59ac
to
74161a0
Compare
it is impossible, you can't have multiple on the same page, we don't support bidirectional multiple HMR, it is very hard task, yes it can work in some cases, but I would strictly say that this is not the best idea Also this fix handle only jsonp, but we have more types
Because it is a simple case, in the real world you can have multiple async chunks and in some you can have module in some not. I would say you need to rethink your architecture. You can look at our source code and see:
Based on your changes I assume you overridden it or don't provide a value at all, that is why you faced with it, I can provide more feedback if you provide small reproducible example. |
What kind of change does this PR introduce?
The current webpack 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.
The changes aim to implement a way to propagate HMR updates through all instances of the same runtime, if they exist.
Did you add tests for your changes?
Yes. There's a new hotCase at
hotCases/multiple-runtime-instances
. I used webpack-dev-server to run it, but since I couldn't find any similar example, my testing approach might not be appropriate for the repo, in which case I'll be glad to help making it suitable.Before fix:
2025-05-16.20-37-06.mp4
After fix:
2025-05-16.20-37-31.mp4
Does this PR introduce a breaking change?
No, runtime keeps its current behavior, except HMR updates are propagated in the singular case where more than one instance of the same runtime is running.
What needs to be documented once your changes are merged?
Since I couldn't find this behavior mentioned anywhere, no changes to the documentation are needed, AFAIK.