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

  1. Clone the repo, check out the PR’s branch.
  2. pnpm install.
  3. cd examples/with-next.
  4. pnpm dev
  5. Open the application URL in the browser.
  6. Open the DevTools, select the “Console” tab.
  7. 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.
  8. Go to src/mocks/handlers.ts. Change the payload of the graphql.query() handler (e.g. remove any word from a movie title).
  9. Save the changes.
  10. Back in the browser, click “Fetch movies” again.
  11. 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

Most upvoted comments

@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.

Looks like it’s still an issue in next@15.0.1

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 mockingEnabledPromise after the call to worker.start

module.hot?.dispose(() => { worker.stop(); });

as well as moving the fallback value in the Suspense from null to false. 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 worker instance somehow retains the “old” handlers (from before HMR), and worker.use() simply prepends updated handlers to the overall list:

Screenshot 2024-09-06 at 11 42 59

The first two are the updated handlers I explicitly import in msw-provider.tsx and provide to worker.use(). The previous four handlers are outdated handlers that somehow survive HMR.

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.ts module (the one importing handlers.ts and providing them to the worker instance).

If I apply this diff, the issue is fixed:

// mocks/browser.ts
import { setupWorker } from 'msw/browser'
-import { handlers } from './handlers'

-export const worker = setupWorker(...handlers)
+export const worker = setupWorker()
// msw-provider.tsx
'use client'

import { Suspense, use } from 'react'
+import { handlers } from '../mocks/handlers'

// ...

await worker.start()
+worker.use(...handlers)

This effectively moves handlers.ts so they are directly imported by msw-provider.tsx.

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.tsx

But 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).

Screenshot 2024-08-29 at 15 14 19

In this example, I’ve decorated the handler instances with the ID property that is random on every evaluation. The expected result here is that each HMR produces a new ID and a single handler. The issue is that it keeps producing more handlers for more hot updates (see my theory on why that happens in the next comment).

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.

you are viewing an old worker instance, even after HMR

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 setupWorker would keep piling that object in memory between hot updates.

so adding a resetHandlers call in the mockingEnabledPromise works.

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.

just wondering, is starting up a new worker preferable?

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

  1. React Strict Mode doesn’t seem to be affecting this behavior. I’ve tried disabling it in next.config.js and the issue can still be reproduced:
// next.config.js
module.exports = {
  reactStrictMode: false,
}
  1. Removing use() that awaits the worker start doesn’t change anything.
  2. Unregistering the worker manually before making a change and triggering HMR does not affect anything. The worker persisting HMR doesn’t seem to be a problem (it should, normally).
  3. If I introduce any other side effect, like a navigator.serviceWorker.addEventListener('message') in the msw-provider.tsx module, 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 in layout.tsx, it will not.

The last point makes me believe that the way msw-provider.tsx is 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?