question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

MSW + NextJS 12+ : doesn't intercept first request, waitUntilReady not working

See original GitHub issue

Prerequisites

Environment check

  • I’m using the latest msw version
  • I’m using Node.js version 14 or higher

Browsers

Chromium (Chrome, Brave, etc.), Firefox, Safari

Reproduction repository

https://github.com/watch-janick/msw-nextjs12-issue

Reproduction steps

  1. Clone the repo
  2. Run yarn install
  3. Run yarn dev
  4. Open http://localhost:3000 in your browser
  5. Open the browser console
  6. Refresh the page

Current behavior

Right now MSW isn’t intercepting the first request.

Expected behavior

The first request should be delayed until MSW is ready according to waitUntilReady.

Issue Analytics

  • State:closed
  • Created 10 months ago
  • Reactions:2
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
watch-janickcommented, Nov 22, 2022

@kettanaito Awesome, it’s working now 🚀

Thank you very much! 🙏

1reaction
kettanaitocommented, Nov 22, 2022

Hey, @watch-janick. Thanks for reporting this.

May I ask you to try using require() here instead of import()?

https://github.com/watch-janick/msw-nextjs12-issue/blob/881d9606cc77b20fa61cf798d966c4f6d1f506b6/mocks/index.js#L6

What I think happens now is this:

  • The initMocks function is async but it’s never awaited.
  • This means it fires when you require('../mocks') and then runs in parallel to the rest of your app.
  • This creates a race condition between await import('./browser') and your fetch() call on the page.

I’d suggest trying to require those browser and server parts synchronously using require().

Please give this a try and let me know.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Dynamic "import" creates a race condition with initial load ...
Originally reported in MSW + NextJS 12+ : doesn't intercept first request, waitUntilReady not working mswjs/msw#1474 (I describe what/why happens here).
Read more >
MSW cannot intercept requests when using testcafe as the ...
The error implies that the worker is not registered at the root. Note that a service worker defaults to the scope of the...
Read more >
Next.js by Vercel - The React Framework
Production grade React applications that scale. The world's leading companies use Next.js by Vercel to build static and dynamic websites and web applications....
Read more >
msw: Versions - Openbase
Rebuilds the Request Handler API from ground-up (#561). Does not introduce any breaking changes in how you use the handlers. Fixes the auto-imports...
Read more >
Fearless demos, controlled tests: Next.js & MSW tutorial
Take your demos and tests to the next level, by bringing in Mock Service Worker!Are you nervous doing live demos because you always...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found