Unverified Commit 094dccb2 authored by Sebastian "Sebbie" Silbermann's avatar Sebastian "Sebbie" Silbermann Committed by GitHub

[instant] Let dev-server requests bypass the fetch lock (#95761)

parent 163e45e4
......@@ -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`
......
......@@ -1100,6 +1100,33 @@ describe('instant-navigation-testing-api', () => {
expect(await fetchedData.textContent()).toContain('api response')
})
if (isNextDev) {
it('lets dev-server requests through during instant scope', async () => {
const page = await openPage(next, '/')
await instant(page, async () => {
await page.click('#link-to-client-fetch')
// The out-of-band fetch to /api/data is blocked by the lock
const loading = page.locator('[data-testid="fetched-data-loading"]')
await loading.waitFor({ state: 'visible' })
// But dev-server requests (hot-reloader middleware endpoints like the
// error overlay and source maps) bypass the lock and resolve while the
// scope is still active. Without the bypass this evaluate would hang
// until the scope ends.
const status = await page.evaluate(() =>
fetch('/__nextjs_server_status').then((res) => res.status)
)
expect(status).toBe(200)
// The blocked fetch still hasn't resolved
const fetchedData = page.locator('[data-testid="fetched-data"]')
expect(await fetchedData.count()).toBe(0)
})
})
}
it('clears cookie even when callback throws', async () => {
const page = await openPage(next, '/')
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment