MSW + NextJS 12+ : doesn't intercept first request, waitUntilReady not working
See original GitHub issuePrerequisites
- I confirm my issue is not in the opened issues
- I confirm the Frequently Asked Questions didn’t contain the answer to my issue
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
- Clone the repo
- Run
yarn install
- Run
yarn dev
- Open
http://localhost:3000
in your browser - Open the browser console
- 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:
- Created 10 months ago
- Reactions:2
- Comments:5 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@kettanaito Awesome, it’s working now 🚀
Thank you very much! 🙏
Hey, @watch-janick. Thanks for reporting this.
May I ask you to try using
require()
here instead ofimport()
?https://github.com/watch-janick/msw-nextjs12-issue/blob/881d9606cc77b20fa61cf798d966c4f6d1f506b6/mocks/index.js#L6
What I think happens now is this:
initMocks
function is async but it’s never awaited.require('../mocks')
and then runs in parallel to the rest of your app.await import('./browser')
and yourfetch()
call on the page.I’d suggest trying to require those
browser
andserver
parts synchronously usingrequire()
.Please give this a try and let me know.