next.js: Module incorrectly persists between hot updates (HMR) in the browser
Link to the code that reproduces this issue
https://github.com/mswjs/examples/pull/101
To Reproduce
- Clone the repo, check out the PR’s branch.
pnpm install.cd examples/with-next.pnpm dev- Open the application URL in the browser.
- Open the DevTools, select the “Console” tab.
- Click the “Fetch movies” button on the page. See the list of fetched movies (these are coming from mocks). See a single log output from MSW about the intercepted GraphQL query.
- Go to
src/mocks/handlers.ts. Change the payload of thegraphql.query()handler (e.g. remove any word from a movie title). - Save the changes.
- Back in the browser, click “Fetch movies” again.
- See two logs for the same GraphQL request.
Current vs. Expected behavior
Current behavior
The entire MovieList component gets re-rendered a bunch of times on hot update to handlers.ts. Re-rendering is expected, but it looks like Next.js re-applies event listeners to the same button multiple times.
This is not an MSW issue. You can log something in the MovieList component manually, and see that it re-renders quite a lot. I suspect during those re-renderings, the onClick listener gets applied more than it needs to.
The number of times the listener is excessively applied is directly proportionate to the number of HMR changes issued (e.g. 1 change = 2 listeners; 2 changes = 3 listeners; etc).
Provide environment information
Operating System:
Platform: darwin
Arch: x64
Version: Darwin Kernel Version 23.3.0: Wed Dec 20 21:28:58 PST 2023; root:xnu-10002.81.5~7/RELEASE_X86_64
Available memory (MB): 65536
Available CPU cores: 16
Binaries:
Node: 18.19.0
npm: 10.2.3
Yarn: 1.22.10
pnpm: 9.6.0
Relevant Packages:
next: 15.0.0-canary.121 // Latest available version is detected (15.0.0-canary.121).
eslint-config-next: N/A
react: 19.0.0-rc-14a4699f-20240725
react-dom: 19.0.0-rc-14a4699f-20240725
typescript: 5.3.3
Next.js Config:
output: N/A
Which area(s) are affected? (Select all that apply)
Developer Experience, Runtime
Which stage(s) are affected? (Select all that apply)
next dev (local)
Additional context
No response
About this issue
- Original URL
- State: open
- Created 2 years ago
- Reactions: 50
- Comments: 23 (11 by maintainers)
Commits related to this issue
- next용 msw 컴포넌트 - [이슈 있음](https://github.com/vercel/next.js/issues/69098) — committed to yponion/z-com by yponion 2 years ago
@sebws, such an interesting find, thank you for looking into this! I am surprised HMR doesn’t kill that interval though. It should be a part of garbage collection from the previous “frame”, and it’s not even something the framework is doing.
Context: the interval is there to keep the worker-client channel alive (it gets killed if nothing has been transferred on it in some time). We won’t be removing that interval, it’s intentional and needed.
Since HMR destroys the previous execution context and its worker, I’d expect it to clear that interval as well. The fact that this doesn’t cause issues in other frameworks still circles out Next.js and, likely webpack handling HMR.
It will be solved in 16 XD.
I’m having some luck 😃 It’s another funny workaround/dodgy type of thing, however I added the following to
mockingEnabledPromiseafter the call toworker.startas well as moving the fallback value in the Suspense from
nulltofalse. Otherwise on HMR the browser would occasionally stay empty.With this, on HMR you get just the one handler persisting!
I don’t think this is a sustainable solution but might hopefully help in seeing what is happening
Gave the proposed workaround a try, and it doesn’t actually solve the issue. What ends up happening, the
workerinstance somehow retains the “old” handlers (from before HMR), andworker.use()simply prepends updated handlers to the overall list:Darn, I thought this can be a viable workaround for now, but it’s not. If some values incorrectly persist across HMR, this can lead to bigger behavior issues for users.
I would love to get some help on this!
Workaround
As I suspected, the “cleanup” of handlers is not happening somewhere in memory in the
mocks/browser.tsmodule (the one importinghandlers.tsand providing them to theworkerinstance).If I apply this diff, the issue is fixed:
Can it be that webpack incorrectly updates the modules in HMR? I think Remix used to have the exact issue, but the they’ve fixed it somehow (cc @pcattori).
It must not matter where you import the handlers. If the hot update originates in
handlers.ts, I expect the bundler to correctly understand the update path:handlers.ts->browser.ts->msw-provider.tsxBut this update path seems to be faulty, resulting in the old and new handlers array persisting at the same time across hot updates.
Okay, I think I understand better what’s happening now. It looks like HRM doesn’t terminate previously attached request handlers (see the screenshot below).
This is a screenshot of a duplicate request log in the console after HMR. You can see that although HMR has happened, and I’ve modified the handler, the “old” handler is still firing (the first one), taking precedence over the updated handler (the second one).
This is not a bug in MSW. If you observe the handlers between hot updates, you will see that
worker.listHandlers()after HMR correctly lists the updated GraphQL handler.The fact that the old handler persists and still works is likely related to how the module is re-evaluated. @feedthejim @eps1lon, with this new intel, do you have any thoughts on what may be causing this? Suggestions on how to debug this further are also welcome.
Seems like we’re saying the same thing then, because that’s what I’m saying. That you can see old handlers but it’s because the old worker has persisted (with old handlers), not one worker with x handlers per refresh.
The thing is, your module is supposed to produce the same result upon evaluation during HMR, normally. We’ve gathered multiple examples of how other frameworks handle a nearly identical server-side and client-side setups: Remix, Svelte, Vue. There’s no need to perform any magic around worker or server. When HMR comes, the old module gets thrown away, the changed module takes place. The other frameworks also act as an additional proof that the issue doesn’t lie with MSW, otherwise, anywhere you use
setupWorkerwould keep piling that object in memory between hot updates.It works because it “remediates” the problem by clearing the handlers that persisted between hot updates before attaching new handlers. That’s a workaround, not a solution I can recommend.
Well, it’s the initial browser integration so, yes, it’s not only preferable but is the only way to enable MSW in the browser.
Exploration
next.config.jsand the issue can still be reproduced:use()that awaits the worker start doesn’t change anything.navigator.serviceWorker.addEventListener('message')in themsw-provider.tsxmodule, it will be executed twice as well (growing on each HMR). If I move that side effect to a new module, mark it as'use client', and import inlayout.tsx, it will not.The last point makes me believe that the way
msw-provider.tsxis re-evaluated during HMR can be faulty.The only unusual thing that may be related is the MSW setup:
https://github.com/mswjs/examples/pull/101/files#diff-e0bbff5ddfef9a67e6e818bdf0575be8737af37c859effc946ee40e8dc6b30c2
Can some of these hooks/functions lead to component rendering being messed up?