Skip to content

Commit d6f4cd3

Browse files
authored
Fix Navigation Inspector in Safari (#95329)
Fixes the Navigation Inspector in Safari/WebKit on localhost. Safari 18.6 exposes the Cookie Store API, but WebKit on localhost can resolve `cookieStore.set()` and emit a change event without committing a cookie that is visible to `document.cookie` or sent with requests. Since the instant navigation lock depends on a real request cookie, the inspector was not working correctly. This switches instant-nav cookie writes/deletes to `document.cookie`, keeps Cookie Store for observing changes, and makes Resume wait for the delete event before starting the next pending capture.
1 parent 6c5f3d5 commit d6f4cd3

3 files changed

Lines changed: 95 additions & 38 deletions

File tree

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

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,31 @@ function parseCookieValue(raw: string): InstantNavCookieState {
4444
return 'pending'
4545
}
4646

47+
function writeDocumentCookie(
48+
value: InstantCookie,
49+
options: { domain?: string | null; path?: string | null }
50+
): void {
51+
if (typeof document === 'undefined') {
52+
return
53+
}
54+
let cookie = `${NEXT_INSTANT_TEST_COOKIE}=${JSON.stringify(value)}; Path=${
55+
options.path ?? '/'
56+
}`
57+
if (options.domain) {
58+
cookie += `; Domain=${options.domain}`
59+
}
60+
document.cookie = cookie
61+
}
62+
4763
function writeCookieValue(value: InstantCookie): void {
4864
if (typeof cookieStore === 'undefined') {
4965
return
5066
}
51-
// Read the existing cookie to preserve its attributes (domain, path),
52-
// then write back with the new value. This updates the same cookie
53-
// entry that the external actor created, regardless of how it was
54-
// scoped.
67+
// Read the existing cookie to preserve its attributes (domain, path), then
68+
// 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.
5572
//
5673
// Capture the current lockState and compare it in the callback so we
5774
// only write if the lock we observed at call time is still held. This
@@ -63,15 +80,7 @@ function writeCookieValue(value: InstantCookie): void {
6380
const lockAtCall = lockState
6481
cookieStore.get(NEXT_INSTANT_TEST_COOKIE).then((existing: any) => {
6582
if (existing && lockState === lockAtCall && lockAtCall !== null) {
66-
const options: any = {
67-
name: NEXT_INSTANT_TEST_COOKIE,
68-
value: JSON.stringify(value),
69-
path: existing.path ?? '/',
70-
}
71-
if (existing.domain) {
72-
options.domain = existing.domain
73-
}
74-
cookieStore.set(options)
83+
writeDocumentCookie(value, existing)
7584
}
7685
})
7786
}

‎packages/next/src/next-devtools/dev-overlay/components/instant-navs/instant-navs-panel.tsx‎

Lines changed: 47 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,45 @@ function createPendingInstantNavCookie(): InstantCookie {
9393
return [0, `p${Math.random()}`]
9494
}
9595

96-
function setPendingInstantNavCookie(): void {
96+
function setInstantNavCookie(value: InstantCookie): void {
97+
if (typeof document === 'undefined') {
98+
return
99+
}
100+
document.cookie = `${COOKIE_NAME}=${JSON.stringify(value)}; Path=/`
101+
}
102+
103+
function deleteInstantNavCookie(): void {
104+
if (typeof document === 'undefined') {
105+
return
106+
}
107+
document.cookie = `${COOKIE_NAME}=; Path=/; Max-Age=0`
108+
}
109+
110+
function waitForInstantNavCookieDeleted(): Promise<void> {
97111
if (typeof cookieStore !== 'undefined') {
98-
cookieStore.set({
99-
name: COOKIE_NAME,
100-
value: JSON.stringify(createPendingInstantNavCookie()),
101-
path: '/',
112+
return new Promise((resolve) => {
113+
function done(): void {
114+
cookieStore.removeEventListener('change', handler)
115+
resolve()
116+
}
117+
118+
function handler(event: CookieChangeEvent): void {
119+
for (const cookie of event.deleted) {
120+
if (cookie.name === COOKIE_NAME) {
121+
done()
122+
return
123+
}
124+
}
125+
}
126+
127+
cookieStore.addEventListener('change', handler)
102128
})
103129
}
130+
return Promise.resolve()
131+
}
132+
133+
function setPendingInstantNavCookie(): void {
134+
setInstantNavCookie(createPendingInstantNavCookie())
104135
}
105136

106137
function getCurrentLocationUrl(): string {
@@ -114,9 +145,7 @@ function getCurrentLocationUrl(): string {
114145
// navigation-testing-lock.ts, which releases the lock and soft-refreshes to real data.
115146
function clearInstantNavCaptureCookie(): void {
116147
setInstantNavTransientStatus('idle')
117-
if (typeof cookieStore !== 'undefined') {
118-
cookieStore.delete(COOKIE_NAME)
119-
}
148+
deleteInstantNavCookie()
120149
}
121150

122151
export function InstantNavsPanel() {
@@ -199,23 +228,16 @@ export function InstantNavsPanel() {
199228
const spaToUrl = currentSpaToUrl ?? lastSpaToUrl
200229

201230
async function resume(): Promise<void> {
202-
if (typeof cookieStore !== 'undefined') {
203-
// Resume: delete the cookie (which releases the
204-
// lock and triggers a soft refresh for real data via the lock
205-
// listener), then restart capture for the next navigation by
206-
// writing a fresh pending cookie. Chaining the writes keeps
207-
// them as two ordered cookie events (delete -> refresh, then
208-
// restart) rather than coalescing into one, so the refresh runs
209-
// and snapshots the released lock before the restart re-acquires it.
210-
setInstantNavTransientStatus('restarting')
211-
const pendingCookie: InstantCookie = [0, `p${Math.random()}`]
212-
await cookieStore.delete(COOKIE_NAME)
213-
cookieStore.set({
214-
name: COOKIE_NAME,
215-
value: JSON.stringify(pendingCookie),
216-
path: '/',
217-
})
218-
}
231+
// Resume: delete the cookie (which releases the lock and triggers a soft
232+
// refresh for real data via the lock listener), then restart capture for
233+
// the next navigation by writing a fresh pending cookie. Waiting for the
234+
// delete event keeps this as two ordered cookie transitions.
235+
setInstantNavTransientStatus('restarting')
236+
const pendingCookie = createPendingInstantNavCookie()
237+
const deleted = waitForInstantNavCookieDeleted()
238+
deleteInstantNavCookie()
239+
await deleted
240+
setInstantNavCookie(pendingCookie)
219241
}
220242

221243
function togglePaused(): void {

‎test/development/app-dir/instant-navs-devtools/instant-navs-devtools.test.ts‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,17 @@ describe('instant-nav-panel', () => {
5858
.click()
5959
}
6060

61+
async function disableCookieStoreSet(browser: Playwright) {
62+
await browser.eval(() => {
63+
if (typeof cookieStore !== 'undefined') {
64+
const prototype = Object.getPrototypeOf(cookieStore) as {
65+
set: typeof cookieStore.set
66+
}
67+
prototype.set = async () => undefined
68+
}
69+
})
70+
}
71+
6172
async function clickLink(browser: Playwright, href: string) {
6273
await browser.eval((page) => {
6374
document.querySelector<HTMLAnchorElement>(`[href="${page}"]`)!.click()
@@ -558,6 +569,21 @@ describe('instant-nav-panel', () => {
558569
await clearInstantModeCookie(browser)
559570
})
560571

572+
it('should capture when CookieStore writes are not reflected in document.cookie', async () => {
573+
const browser = await openHomeWithTargetPageWarmup()
574+
await disableCookieStoreSet(browser)
575+
576+
await openInstantNavPanel(browser)
577+
await clickStartCapturing(browser)
578+
await expectPendingPanel(browser)
579+
580+
await clickLink(browser, '/target-page/my-post?search=foo')
581+
await expectSpaPanel(browser)
582+
await expectTargetPageSpaShell(browser)
583+
584+
await clearInstantModeCookie(browser)
585+
})
586+
561587
it('should show loading skeleton during SPA navigation after clicking Start', async () => {
562588
const browser = await openHomeWithTargetPageWarmup()
563589

0 commit comments

Comments
 (0)