Running MSW in SSR apps #1209
-
So far I have run MSW with simple React apps that call APIs from the client-side.
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 7 replies
-
Hey, @nareshbhatia.
Yes, that's possible. You can see the Next.js usage example where the same mocks are used for the client- and server-side requests.
Your app can make requests from both sides and still get them intercepted. This is achieved by different interceptors though—you use LimitationsThere are, however, limitations to this approach. As you instantiate interception in different environments, there's no established way to communicate between those environments. You may experience this limitation if you want to mutate some data on the client and expect the server request to respond with the next state of the data. That won't be possible. We've introduced the concept of a "remote" interceptor for some time now, which should allow facilitating this issue in theory. This hasn't been added to MSW for now. I've tried it on a Next.js project and, for some reason, Next.js doesn't spawn the client process as a child of the server process. Without this parent-child relation, this remote interception won't work. There are really no options if there's no connection between the two processes. I've asked about this behavior in this discussion but, sadly, nobody has answered me yet. |
Beta Was this translation helpful? Give feedback.
-
Any update on the concept of remote interceptor? I would like to be able to have msw intercept requests from nextjs that is running in another node process. |
Beta Was this translation helpful? Give feedback.
Hey, @nareshbhatia.
Yes, that's possible. You can see the Next.js usage example where the same mocks are used for the client- and server-side requests.
Your app can make requests from both sides and still get them intercepted. This is achieved by different interceptors though—you use
setupWorker
in the client andsetupServer
in the server process.Limitations
There are, however, limitations to this approach. As you instantiate interception in different environments, there's no …