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.

Unable to intercept requests sent during pre-rendering in Next.js

See original GitHub issue

Prerequisites

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

  1. 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
  1. Open http://localhost:3000

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:open
  • Created a year ago
  • Reactions:1
  • Comments:10 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
andykenwardcommented, Nov 22, 2022

@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 from ES5 to ESNext

So I could change _app.tsx from using

  require('../mocks')

to

  await import('../mocks')

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

error - FetchError: request to https://my.backend/book failed, reason: getaddrinfo ENOTFOUND my.backend
    at ClientRequest.<anonymous> (/Users/andykenward/Documents/github/andykenward/next-with-msw/with-msw-pages/node_modules/next/dist/compiled/node-fetch/index.js:1:65756)
    at ClientRequest.emit (node:events:513:28)
    at TLSSocket.socketErrorListener (node:_http_client:494:9)
    at TLSSocket.emit (node:events:513:28)
    at emitErrorNT (node:internal/streams/destroy:157:8)
    at emitErrorCloseNT (node:internal/streams/destroy:122:3)
    at processTicksAndRejections (node:internal/process/task_queues:83:21) {
  type: 'system',
  errno: 'ENOTFOUND',
  code: 'ENOTFOUND',
  page: '/'
}

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.

2reactions
andykenwardcommented, Nov 22, 2022

@kettanaito I’ve tested the NextJS MSW template and it doesn’t work at all, I’ve reported it to them, but if you have any idea how to make it work, that would be awesome 😁 vercel/next.js#43221

Thanks!

@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 version 0.49.0 it should work. I’ve fixed the example in this PR https://github.com/vercel/next.js/pull/43224

Read more comments on GitHub >

github_iconTop 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 >

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