MSW is not intercepting in development in nextjs and graphql api #1049
-
Hey, |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 15 replies
-
Hey, @themashcodee. Thanks for reporting this. So the query is not intercepted because by the time it happens the worker is not done registering. I've spotted this by debugging the Lines 261 to 266 in 527add2 As the condition above states, if there are no clients (tabs where the worker is ready), bypass the request. That's precisely what's happening. The race condition between the worker registration and whichever requests an app may perform is still something we're working on solving. Currently, there's the request deferring logic that should capture and replay any requests happening in that race condition timeframe. That's clearly not what's happening in your case. At first, I suspected the custom For now, what I did is deferred the app's mounting (as described in the Deferred mounting page) so that your app mounts after the worker is ready: import 'styles/globals.css'
import type { AppProps } from 'next/app'
import { Apollo } from 'services/apollo-client'
import { useEffect, useState } from 'react'
const { API_MOCKING } = process.env
const MyApp = ({ Component, pageProps: pageProps }: AppProps) => {
const [shouldRender, setShouldRender] = useState(!API_MOCKING)
useEffect(() => {
async function initMocks() {
const { setupMocks } = await import('mocks')
await setupMocks()
setShouldRender(true)
}
if (API_MOCKING) {
initMocks()
}
}, [])
if (!shouldRender) {
return null
}
return (
<>
<Apollo>
<Component {...pageProps} />
</Apollo>
</>
)
}
export default MyApp I'd much prefer for this to be handled by the library but for the lack of such handling at the moment this is what I'd recommend you try. |
Beta Was this translation helpful? Give feedback.
-
I'm using |
Beta Was this translation helpful? Give feedback.
-
Still a big issue in 14 version! |
Beta Was this translation helpful? Give feedback.
Hey, @themashcodee. Thanks for reporting this.
So the query is not intercepted because by the time it happens the worker is not done registering. I've spotted this by debugging the
activeClientsIds
in themockServiceWorker.js
file:msw/src/mockServiceWorker.js
Lines 261 to 266 in 527add2
As the condition above states, if there are no clients (tabs where the worker is ready), bypass the request. That's precisely wh…