Skip to content

Commit f15aa4e

Browse files
authored
Clear a resurrected instant cookie on unlock so a hard reload recovers (#95398)
Under the Instant Navigation Testing lock, an MPA page load's bootstrap writes the captured cookie value asynchronously via `cookieStore`. When a scope is released (the external actor deletes the cookie), that pending write can land in the narrow gap between the delete and the deleted-event handler that tears the lock down, resurrecting the cookie after the scope has ended. If the unlock then falls back to a hard reload, which happens when the freshly loaded shell has not hydrated the router yet, the reload carries the resurrected cookie, the server serves the shell again, and the page re-enters instant mode with no scope left to release it, so the deferred content never streams in and the navigation hangs. Once the lock is released, `writeCookieValue`'s guard rejects any further captured write, so the deleted-event handler now clears the cookie right after `releaseLock` to neutralize a write that resurrected it in the gap, and an added guard ignores the re-entrant change event that the clear itself produces. This removes a family of flaky timeouts in `instant-navigation-testing-api.test.ts`, where a full-page (MPA) navigation inside an `instant()` scope would intermittently never stream in its deferred content after the scope exits. [Example failing run](https://github.com/vercel/next.js/actions/runs/28572283366/job/84712848241?pr=95227) --- <sub>Stack created with <a href="GitHub">https://github.com/github/gh-stack">GitHub Stacks CLI</a> • <a href="Give" rel="nofollow">https://gh.io/stacks-feedback">Give Feedback 💬</a></sub>
1 parent 0d4df38 commit f15aa4e

1 file changed

Lines changed: 34 additions & 10 deletions

File tree

‎packages/next/src/client/components/segment-cache/navigation-testing-lock.ts‎

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -66,17 +66,23 @@ function writeCookieValue(value: InstantCookie): void {
6666
}
6767
// Read the existing cookie to preserve its attributes (domain, path), then
6868
// write back with the new value. This updates the same cookie entry that the
69-
// external actor created, regardless of how it was scoped. Use document.cookie
70-
// for the write because WebKit exposes Cookie Store on localhost but does not
71-
// commit cookies written through cookieStore.set() there.
69+
// external actor created, regardless of how it was scoped. The read goes
70+
// through `cookieStore.get` because `document.cookie` exposes only names and
71+
// values, not the domain/path we need to preserve. The write goes through
72+
// document.cookie because WebKit exposes Cookie Store on localhost but does
73+
// not commit cookies written through cookieStore.set() there.
7274
//
73-
// Capture the current lockState and compare it in the callback so we
74-
// only write if the lock we observed at call time is still held. This
75-
// guards against two races: (a) the scope ended between get and set
76-
// (lockState is now null), and (b) the scope ended and a new one was
77-
// acquired in the same gap (lockState is a different object). In
78-
// either case we must not write — doing so would leak stale state
79-
// into the next scope or outlive the current one.
75+
// Capture the current lockState and compare it in the callback so we only
76+
// write if the lock we observed at call time is still held. This guards
77+
// against two races: (a) the scope ended between get and set (lockState is
78+
// now null), and (b) the scope ended and a new one was acquired in the same
79+
// gap (lockState is a different object). In either case we must not write —
80+
// doing so would leak stale state into the next scope or outlive the current
81+
// one. It cannot close one window, though: the callback can run after an
82+
// external delete but before the deleted-event handler nulls lockState, so
83+
// the guard still passes and we resurrect the cookie. The deleted handler
84+
// clears any such entry once the lock is released (see the `event.deleted`
85+
// loop below).
8086
const lockAtCall = lockState
8187
cookieStore.get(NEXT_INSTANT_TEST_COOKIE).then((existing: any) => {
8288
if (existing && lockState === lockAtCall && lockAtCall !== null) {
@@ -369,7 +375,25 @@ export function startListeningForInstantNavigationCookie(): void {
369375

370376
for (const cookie of event.deleted) {
371377
if (cookie.name === NEXT_INSTANT_TEST_COOKIE) {
378+
if (lockState === null) {
379+
// Either no lock is active, or this is the re-entrant change event
380+
// from the defensive clear below (which runs after releaseLock).
381+
// Nothing to release either way.
382+
return
383+
}
372384
releaseLock()
385+
// A captured write from this page's bootstrap can resurrect the
386+
// cookie in the narrow gap between the external delete and this
387+
// handler: writeCookieValue's guard only rejects the write once the
388+
// lock is torn down, which happens here. Now that the lock is
389+
// released, no further captured write can re-add the cookie, so clear
390+
// any entry that was resurrected in that gap. Otherwise an unlock
391+
// that falls back to a hard reload (when the shell has not yet
392+
// hydrated) would carry the stale cookie, be served the shell again,
393+
// and re-enter instant mode with no scope left to release it.
394+
if (typeof document !== 'undefined') {
395+
document.cookie = `${NEXT_INSTANT_TEST_COOKIE}=; Path=/; Max-Age=0`
396+
}
373397
refreshOnInstantNavigationUnlock()
374398
return
375399
}

0 commit comments

Comments
 (0)