Unable to intercept requests sent during pre-rendering in Next.js
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
Node.js version
v18.3.0
Reproduction repository
https://github.com/ajfAfg/msw-operation-research
Reproduction steps
- Run the following commands
git clone https://github.com/ajfAfg/msw-operation-research.git
cd msw-operation-research/nextjs-rest
npm install
npm run dev
Current behavior
In src/pages/index.tsx
, getServerSideProps
fetches data and outputs the received data to the console. Currently, the output is as follows.
server: {"answer":"yes","forced":false,"image":"https://yesno.wtf/assets/yes/14-b57c6dc03aa15a4b18f53eb50d6197ee.gif"}
The data being fetched here is as follows.
$ curl https://yesno.wtf/api
{"answer":"yes","forced":false,"image":"https://yesno.wtf/assets/yes/11-a23cbde4ae018bbda812d2d8b2b8fc6c.gif"}
Expected behavior
The expected output is as follows, since the request is intercepted by the definition in src/mocks/handlers.ts
.
server: {"foo":"bar"}
Issue Analytics
- State:
- Created a year ago
- Reactions:1
- Comments:10 (3 by maintainers)
Top Results From Across the Web
How do I stop auth middleware in NextJS applying to server ...
I thought the issue was down to the server-side pre-rendered page always being created while unauthenticated, but I'm no longer sure. Middleware ...
Read more >Pre-rendering and Data Fetching | Learn Next.js
When a page is loaded by the browser, its JavaScript code runs and makes the page fully interactive. (This process is called hydration.)...
Read more >Testing and error handling patterns in Next.js - LogRocket Blog
Check out these different ways to run tests and handle errors in Next.js apps using Cypress, a JavaScript-based testing framework.
Read more >Getting Started With Apollo Client in Next.js
We used a custom component to ensure we only request the countries from the client to avoid sending duplicate requests during page rendering....
Read more >Rewrite HTML and transform page props in Next.js - Netlify
In this tutorial, I show you how to use Next.js Advanced Middleware on Netlify to intercept a request to a statically pre-generated page,...
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 The issue with NextJS and MSW being enabled to intercept client side requests still exists.
Using node:
v16.18.1
npm:8.19.2
I’ve managed to fix the issue. But it involved changing a few things in the Next.JS setup. Had to enable webpack experiments of topLevelAwait along with changing TypeScript
tsconfig.json
target
fromES5
toESNext
So I could change
_app.tsx
from usingto
You can see all the changes in this commit https://github.com/andykenward/next-with-msw/commit/91d9bbda12baff864913dc9eb95e7d921d75fb27
To reproduce the client side error before the above changes, checkout commit https://github.com/andykenward/next-with-msw/commit/3d00db8f66b12e5496881b18bbd3b903a479f0df
You should get this error on the server from the client side fetch happening in a
useEffect
I’ve started to also look into using the new Next.JS app directory changes they are currently working on. But haven’t managed to get it working yet. https://github.com/andykenward/next-with-msw/tree/main/with-msw-app . Will probably hold off until msw native fetch support version is released.
@watch-janick The NextJs MSW example is using an exact version of msw
0.47.3
and doesn’t install the latest version. If you update it to the latest version0.49.0
it should work. I’ve fixed the example in this PR https://github.com/vercel/next.js/pull/43224