Skip to content

How do I implement MSW in a NextJS backend? #2137

Answered by kettanaito
moonman239 asked this question in Q&A
Discussion options

You must be logged in to vote

Hi, @moonman239!

MSW on the server will follow the Node.js integration instructions. You have to call server.listen() preferably in your server entrpoint.

Now, Next.js doesn't really expose you one. The next best place I found is the instrumentation hook. It will indeed instantiate MSW server-side and allow it to intercept the requests you make on the server.

Take a look at the in-progress Next.js + MSW example that features both client-side and server-side integrations using the instrumentation hooks I've mentioned. It's incomplete but it can serve you as a reference to get started.

Note that when you initiate MSW server-side, you will be intercepting requests your server makes. For exam…

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@ghost
Comment options

@kettanaito
Comment options

@ghost
Comment options

Answer selected by kettanaito
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #2136 on April 20, 2024 17:21.