-
-
Notifications
You must be signed in to change notification settings - Fork 518
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support Next.js 13 (App Router) #1644
Comments
It's also worth mentioning that this is not an issue with MSW. The library works in an extremely straightforward way in any browser or Node.js process. This issue is here simply to track the progress of this Next.js integration as many developers have voiced their concerns and questions about it. |
Does it need to have a persistent process for some reason or can it just repatch each time if there was a place to do it? How did this work in Next.js Pages for API routes since A workaround is to just have a module that’s imported from every layout, page or custom route that uses data. Such as in a share api layer:
We’re considering a global place to inject it too but not sure the process can be guaranteed to live on forever. That’s pretty limited. |
@sebmarkbage, hi 👋 Thank you for reaching out!
It can certainly re-patch on every hot update. We are doing precisely that for Remix and Svelte examples. I had trouble doing that with Next due to those dual processes running (a Node.js patch on the root layout doesn't apply to the server-side logic of individual pages since those seem to be evaluated in a different process).
I suppose it was fine because the main use case is to support client- and server-side development of Next apps, which came down to:
Right now, the second one doesn't work due to the lack of
I have two concerns regarding this workaround:
It would be really nice to have |
We already have There's an idea to expand that to include more features and to have different variants for the different module/scopes processes so that it can patch the module in the Server Components scoped, SSR scope and Client side scope. Not sure if what is already there might be sufficient for your use case. |
Thanks, @sebmarkbage. At first glance, it looks like it could work. I will give it a try in a new Next example repository and let you know. |
@sebmarkbage, do you know if the // ./with-next/instrumentation.ts
export async function register() {
// I've tried a regular top-level import, it matters not.
const { server } = await import('./mocks/node')
server.listen()
} Module not found: Package path ./node is not exported from package /new-examples/examples/with-next/node_modules/msw (see exports field in /new-examples/examples/with-next/node_modules/msw/package.json)
> 1 | import { setupServer } from 'msw/node'
2 | import { handlers } from './handlers'
3 |
4 | export const server = setupServer(...handlers) While {
"exports": {
"./node": {
"browser": null,
"types": "./lib/node/index.d.ts",
"require": "./lib/node/index.js",
"import": "./lib/node/index.mjs",
"default": "./lib/node/index.mjs"
},
}
} My first hunch was maybe the hook runs in the browser too, thus it's unable to resolve the This is really odd because MSW doesn't ship ESM exclusively, it comes as a dual CJS/ESM package, which means it has both the You can reproduce this behavior in here: https://github.com/mswjs/examples-new/pull/7 What can be going wrong here? |
I strongly suspect Next.js is trying to resolve the Moreover, it then fails on a bunch of other 4th-party imports ( Do I have to mark the imported module in |
Hi 👋 Firstly, thanks for the fantastic library. I don't know if this is something that you're already aware of, but MSW doesn't appear to work with Next.js 13 full stop, not just with the app directory. It doesn't appear to work with the pages directory; the official example is also broken. Is the issue with the pages directory encapsulated by this issue too? Thanks 😄 |
Hi, @louis-young. Thanks for reaching out. I'm aware of the library not working with Next 13 in general (I believe there are also issues on Next 12; I suspect there were some architectural changes merged to Next 12 prior to Next 13 which may have caused this). However, to reduce an already complex issue, I'd like to track the App router exclusively here and approach it first. As I understand, the App router is the main direction the Next is going to take, and |
Thanks for getting back to me so quickly. That's fair enough and a very reasonable and pragmatic approach, I just wanted to check that it was something that you were aware of. Thanks again and keep up the great work 😄 |
@louis-young We have msw working with the pages directory just fine. Make sure you have this code at the top of your pages component's index.tsx:
|
You can create a client side only component that you include in your root "use client";
import { useEffect } from "react";
export const MSWComponent = () => {
useEffect(() => {
if (process.env.NEXT_PUBLIC_API_MOCKING === "enabled") {
// eslint-disable-next-line global-require
require("~/mocks");
}
}, []);
return null;
}; edit: sorry I mistakenly assumed not working "full stop" meant client side too |
@darrylblake, that would work but the main thing we're trying to solve here is the server-side integration. |
Any news on this? Is there any ongoing task? |
And to add on, can we help at all? Anything you can point us to look at or do need help from the Vercel team? |
Our team absolutely loves msw and we really want to use it on a green field we started last week |
Module resolution in
|
I tried this in an existing project opted into instrumentation and found that HMR changes didn't trigger the console log. I saw one console log with pid I then two more console logs, both with pid Finally I saw a console log with I made a bunch of |
@kettanaito Next.js maintainer here, I did not verify your use case but what I think is going on there is that:
|
Hi, @feedthejim. Thanks for the info. I've tried logging I see webpack in the stack trace. Are there any flags I can use to debug what webpack is trying to do regarding module resolution here? |
On instrumentation hook@sebmarkbage, I don't think the instrumentation hook works in a way to implement Node.js module patching. An example below. Consider the following root layout and a page component (both fetching data on the server because that's the point): // app/layout.tsx
async function getRootData() {
return fetch('https://api.example.com/root').catch(() => null)
}
export default async function RootLayout() {
const data = await getRootData()
return (
<html lang="en">
<body className={inter.className}>{children}</body>
</html>
)
} // app/page.tsx
async function getData() {
const response = await fetch('https://example.com')
return response.text()
}
export default async function Home() {
const data = await getData()
return <p>{data}</p>
} And the export async function register() {
console.log('[i] %s %d', process.env.NEXT_RUNTIME, process.pid)
globalThis.fetch = new Proxy(globalThis.fetch, {
apply(target, thisArg, args) {
console.log('[i] fetch', args)
return Reflect.apply(target, thisArg, args)
},
})
} Expected behaviorThe instrumentation hook run as a part of the Node.js server of Next and executes its Current behaviorNothing gets printed. I suspected that patching
On a relevant note, I've checked how this dual Node.js process architecture is handled with the instrumentation hook, and it seems like this:
It seems that the hook shares at least 1 process with the layout and the page ( I don't have the insight to say what is that random port used for. If by any chance Next evaluates the components as a part of that port process, then I don't see how MSW or any other third-party can establish any server-side side effects to enable features like API mocking—there's no shared process to apply those effects in. Can we have something like |
Exactly the same results - 13.4.9 |
https://remix.run/docs/en/main/file-conventions/entry.server Should we start asking Vercel for a similar feature? (You know they don't want the folks over at Remix "one 'upping" them) |
Does anyone created an issue on the NextJS repo? Would love to have the link so I can +1 |
It seems for the dev server at least, https://github.com/vercel/next.js/blob/673107551c3466da6d68660b37198eee0a2c85f7/packages/next/src/server/dev/next-dev-server.ts#L1759 is restoring fetch to the un-patched original. When running with Update: This was running nextjs 13.4.9 in classic mode (as apposed to app mode) so maybe not as much help here. |
In app mode (https://github.com/Jaesin/with-msw-app), I added some logging and patched
13-15 repeat for subsequent requests. It looks to me that the third process that handles all of the requests is instantiating
Running in prod mode:
MSW v1.2.2 |
Thank you for doing all that research @Jaesin! |
Did you have any example about it? |
Nothing complete, it was just me playing around. If I ever come back to it, I will try to make an example repo, or possibly try to submit a PR to |
We had to use MWS as a stand-alone http server. |
I have found a way to mock API requests without using
This approach is very simple. First, define a handler that catches This approach does not conflict with monkey patching by Next.js for the fetch API because it does not use |
@mizdra - how does that mock responses for your endpoints? Presumably you'd have to add/remove the real endpoints each time (Next, afaik, will only give you one response if you have such a setup - and I believe it prefers static routes to the catchall routes (i.e. the catchall is a fallback, not an override)). It also seems like this would only work for routes defined in your application (e.g. in your example, myapp.com/api/my-endpoint), excluding the ability to fetch from e.g. https://jsonplaceholder.com/ or real external endpoints. Edit: As I tried to play with the example, I also couldn't get the fetch call to reload correctly (I think perhaps this relates to Next.js' caching - but using the second argument to Next's fetch and then |
Hi there |
@chrisb2244 Your understanding is correct. My approach cannnot mock external API requests. However, if you configure your project to request
This is because I forgot to add
I am sorry. I did not understand what you are saying. Could you please explain in more detail? |
@mizdra - using your new example I can see the behaviour you describe.
Now it reloads for me - I didn't know about this mechanism for setting a global default, thank you.
I think you addressed this with the environment variable. I think if you wanted to do this, you could also pass headers on your requests (for example with Playwright, or similar) and then parse those to determine handling in |
That's right. You must take care to configure your API endpoints for mocking and any other API endpoints so that they do not conflict. One of msw's philosophies is that you can mock without changing the application code at all. msw makes use of monkey patches and ServiceWorker to intercept requests in order to achieve this. My approach avoids the problem of monkey patches in Next.js, but loses that philosophy. The user may have to rewrite the application code, such as #1644 (comment), to mock the request. This is an important tradeoff. |
seems fetch on serverside is not working. finally i came up with a solution that think works. I created an interceptor for fetch and then inside that i put an if statement that says if env is development use axios otherwise use fetch. and inside your root layout you put :
this is also my fetch interceptor :
please ignore auth processes inside fetch instance. its specific for my application |
Shouldn't MSW intercept all those fetchings? |
This has worked for me:
With the usage from layout.tsx:
|
@kettanaito I made next-app-router-with-msw-vitest-playwright based on the Draft Example. Can I add a page for Next.js App Router Integration to the MSW documentation, or open a PR in the Next.js repo to create a template with MSW pre-configured using The reason is that there are currently no official resources on integrating the Next.js App Router with MSW, leading to incomplete setup projects and blog posts. If you have time, I'd appreciate it if you could check if there are any improvements needed in my repo setup! |
UpdateI am collaborating closely with @feedthejim to conclude the Next.js + MSW usage example. He has shared some vital improvements to the server-side instantiation, which enabled proper support for HMR. There are still some issues pending around HMR and Next tearing down any previously existing patches (addressing in vercel/next.js#68193), and also one client-side HMR issue. This is where you stay up-to-date: mswjs/examples#101 Please do not design custom interceptors/request handling logic. MSW already covers you, just be patient while we are finalizing the integration. Thanks. |
UpdateI'm going to close this issue. No action is needed on MSW side, never has been. Every step in making this support happen required changes or bug fixes in Next.js. Currently, vercel/next.js#69098 is the remaining blocker to have a proper HMR in the browser. The server-side integration, including HMR, works great. I trust the Next.js team will address the remaining issue to the best of their availability. Please be respectful and don't spam (instead, give 👍 on the issue, that's enough). As always, here's your integration reference: mswjs/examples#101. Once we have a proper HMR in place, I will merge that example. |
Thanks for the effort and dedicated time! 🍻 |
@kettanaito Did you try my suggestion in the vercel issue of telling the HMR system how to close the worker when the module is disposed? Even though it looks dodgy, it works, makes sense, and doesn't require framework specific code inside MSW's code base. |
@sebws, I saw that one, thanks for proposing. It's still a workaround. Somehow, every other framework cleans up modules before HMR without additional instructions. That's a good experience. Asking developers to introduce manual cleanup is a bad experience. The worst part, what if there's more to the issue? Will we ask the developer to keep piling onto that workaround in that case? I don't think so. I suggest you mention it in mswjs/examples#101 and let people decide if they want it in their setups. I'm not comfortable recommending it. |
@kettanaito Fair enough. I've added it here: mswjs/examples#101 (comment) I was having more issues afterwards related to accessing headers on the server, and they didn't seem to be connected to HMR. I'll raise an issue if I can't figure that out / if that doesn't sound familiar to you |
Do the NextJS fixes that are re-enabling MSW have tests to avoid regressing again in future versions of Next? |
@votemike, they do. |
Scope
Adds a new behavior
Compatibility
Feature description
As of 1.2.2, MSW does not support the newest addition to Next.js being the App directory in Next.js 13. I'd like to use this issue to track the implementation of this support since many people are currently blocked by this.
Prior investigation
I looked into this a month ago and discovered that there's difficulty to establish any process-wide logic in Next.js 13 due to the way Next itself is implemented internally. Very briefly, Next keeps two Node.js processes while it runs:
This process separation is evident when you issue a hot reload to any of the layout components.
Reloading a root layout
When a hot update is issued through the root layout component, we can see the persistent Node.js process update. I don't remember any changes to the intermittent process.
Reloading a nested layout
When a hot update is issued for a nested layout, i.e. a certain page's layout, the intermittent process gets killed, then a new one spawns at its place (on a different port), likely evaluates the update, and keeps pending.
What's the problem with the two processes then?
In Node.js, MSW works by patching global Node modules, like
http
andhttps
. Those patches must persist in the process in order for the mocking to work. Since Next uses this fluctuating process structure, it introduces two main issues:What happens next?
MSW is blocked by the way Next.js is implemented internally. I don't like this state of things but that's what we get—patching Node globals is not the most reliable of things and it will, inevitably, differ across frameworks depending on how those frameworks are implemented.
I would pretty much like for the Next.js team to assist us in this. There's very little we can do on the MSW's side to resolve this. In fact, I believe there's nothing we can do until there's a way to establish a module patch in Node.js for all Next.js processes at the same time.
If you're blocked by this, reach out to the Next.js team on Twitter or other platforms, letting them know this issue is important. I hope that would help the team prioritize it and help us resolve it together. Thanks.
The text was updated successfully, but these errors were encountered: