diff --git a/packages/next/src/client/components/segment-cache/navigation-testing-lock.ts b/packages/next/src/client/components/segment-cache/navigation-testing-lock.ts index 6153b44e0628514eb9a96896641264066ca3f9f2..ed07e3cfb7d7d978e32660d926cee377f7459f7e 100644 --- a/packages/next/src/client/components/segment-cache/navigation-testing-lock.ts +++ b/packages/next/src/client/components/segment-cache/navigation-testing-lock.ts @@ -364,6 +364,32 @@ export function resetNavigationLockToPending(): void { writeCookieValue([0, `c${Math.random()}`]) } +/** + * Returns true if the request targets a dev-server endpoint — one of the + * hot-reloader middleware routes (error overlay, source maps, launch-editor, + * devtools). They all share the `/__nextjs_` path prefix and are always + * requested root-relative on the same origin. + */ +function isDevServerRequest(input: RequestInfo | URL): boolean { + let url: URL + try { + url = new URL( + typeof input === 'string' + ? input + : input instanceof URL + ? input + : input.url, + window.location.href + ) + } catch { + return false + } + return ( + url.origin === window.location.origin && + url.pathname.startsWith('/__nextjs_') + ) +} + /** * Global fetch override * @@ -386,6 +412,15 @@ function globalFetchOverride( // scope and invoked it after release. return fetch(input, init) } + if (process.env.__NEXT_DEV_SERVER && isDevServerRequest(input)) { + // Dev-server requests must not be gated on the testing lock — blocking + // them would break the error overlay, source maps, and devtools for the + // whole scope. Dispatch immediately through the pre-lock fetch. Copy to a + // local so the call doesn't bind `this` to the lock state object (native + // fetch throws "Illegal invocation" for a foreign receiver). + const preLockFetch = lockState.fetch + return preLockFetch(input, init) + } // Block user-initiated fetches until the lock is released, then dispatch // through the fetch captured at acquire time. Reading from `lockState` // (rather than `window.fetch`) pins to the capture even if `window.fetch`