From c0fbd08996dc46cd2c5bcfc815822eead4925467 Mon Sep 17 00:00:00 2001 From: Punyapal Shah <53343069+MrPunyapal@users.noreply.github.com> Date: Wed, 18 Nov 2026 20:04:20 +0530 Subject: [PATCH 1/3] fix: refresh stale page data after browser history restores --- resources/js/lib/current-page.ts | 64 +++++++++- tests/Unit/CurrentPageHistory.test.tsx | 157 +++++++++++++++++++++++++ 2 files changed, 220 insertions(+), 1 deletion(-) create mode 100644 tests/Unit/CurrentPageHistory.test.tsx diff --git a/resources/js/lib/current-page.ts b/resources/js/lib/current-page.ts index 248315ff09..e3fc2b33a1 100644 --- a/resources/js/lib/current-page.ts +++ b/resources/js/lib/current-page.ts @@ -1,14 +1,76 @@ +import type { VisitOptions } from '@inertiajs/core'; import { router } from '@inertiajs/react'; // Toasts render outside the Inertia page context, so they can't call usePage(). let component = ''; +// Live props and moderation already catch up after history restores. Only +// non-live props need a separate reload, including when returning from bfcache. +const historyReloads: Record< + string, + Pick +> = { + 'operations/features': { only: ['features'], preserveScroll: true }, + 'operations/emails': { only: ['recipients'], preserveScroll: true }, + 'starred/index': { + only: ['commits'], + reset: ['commits'], + preserveScroll: false, + }, +}; + export function currentPageComponent(): string { return component; } +function refreshRestoredPage(page: { component: string; url: string }): void { + const options = historyReloads[page.component]; + + if (!options) { + return; + } + + // Photo overlays show their own URL over a background page. Their existing + // before-visit handler redirects async GETs to that background without closing the photo. + router.visit(page.url, { + ...options, + method: 'get', + async: true, + preserveState: true, + replace: true, + onHttpException: ({ status }) => { + if (status === 403 || status === 404) { + window.location.reload(); + + return false; + } + }, + }); +} + export function trackCurrentPage(): VoidFunction { - return router.on('navigate', (event) => { + let page: { component: string; url: string } | null = null; + + const stop = router.on('navigate', (event) => { component = event.detail.page.component; + page = { component, url: event.detail.page.url }; + + if (event.detail.type === 'history') { + refreshRestoredPage(page); + } }); + + const browserWindow = typeof window === 'undefined' ? null : window; + const onPageShow = (event: PageTransitionEvent) => { + if (event.persisted && page !== null) { + refreshRestoredPage(page); + } + }; + + browserWindow?.addEventListener('pageshow', onPageShow); + + return () => { + stop(); + browserWindow?.removeEventListener('pageshow', onPageShow); + }; } diff --git a/tests/Unit/CurrentPageHistory.test.tsx b/tests/Unit/CurrentPageHistory.test.tsx new file mode 100644 index 0000000000..7e147d9de1 --- /dev/null +++ b/tests/Unit/CurrentPageHistory.test.tsx @@ -0,0 +1,157 @@ +import assert from 'node:assert/strict'; +import { test, vi } from 'vite-plus/test'; + +type NavigationType = 'initial' | 'visit' | 'history'; +type NavigationEvent = { + detail: { + type: NavigationType; + page: { component: string; url: string }; + }; +}; + +const visit = vi.fn(); +let navigate: ((event: NavigationEvent) => void) | undefined; + +vi.doMock('@inertiajs/react', () => ({ + router: { + on: (event: string, listener: (event: NavigationEvent) => void) => { + assert.equal(event, 'navigate'); + navigate = listener; + + return () => { + navigate = undefined; + }; + }, + visit, + }, +})); + +const { currentPageComponent, trackCurrentPage } = await import( + '../../resources/js/lib/current-page' +); + +test('refreshes only non-live props after history and bfcache restores', () => { + visit.mockClear(); + const reloadDocument = vi.fn(); + let pageShow: ((event: { persisted: boolean }) => void) | undefined; + vi.stubGlobal('window', { + location: { reload: reloadDocument }, + addEventListener: ( + name: string, + listener: (event: { persisted: boolean }) => void, + ) => { + assert.equal(name, 'pageshow'); + pageShow = listener; + }, + removeEventListener: ( + name: string, + listener: (event: { persisted: boolean }) => void, + ) => { + assert.equal(name, 'pageshow'); + assert.equal(pageShow, listener); + pageShow = undefined; + }, + }); + + try { + const stop = trackCurrentPage(); + const dispatch = ( + type: NavigationType, + component: string, + url = `/${component}`, + ) => + navigate?.({ + detail: { type, page: { component, url } }, + }); + + pageShow?.({ persisted: true }); + dispatch('initial', 'device/index'); + dispatch('visit', 'safety/edit'); + dispatch('history', 'log/show'); + assert.equal(currentPageComponent(), 'log/show'); + assert.equal(visit.mock.calls.length, 0); + + // #1458 already refreshes live props, and moderation has its own catch-up. + [ + 'device/index', + 'safety/edit', + 'organization-member/index', + 'moderation/show', + ].forEach((name) => { + dispatch('history', name); + pageShow?.({ persisted: true }); + }); + assert.equal(visit.mock.calls.length, 0); + + const expected = [ + { only: ['features'], preserveScroll: true }, + { only: ['recipients'], preserveScroll: true }, + { only: ['commits'], reset: ['commits'], preserveScroll: false }, + ]; + const components = [ + 'operations/features', + 'operations/emails', + 'starred/index', + ]; + + components.forEach((name) => dispatch('history', name)); + assert.deepEqual( + visit.mock.calls.map(([url, options]) => ({ + url, + only: options.only, + ...(options.reset === undefined ? {} : { reset: options.reset }), + preserveScroll: options.preserveScroll, + method: options.method, + async: options.async, + preserveState: options.preserveState, + replace: options.replace, + onHttpException: typeof options.onHttpException, + })), + expected.map((options, index) => ({ + url: `/${components[index]}`, + ...options, + method: 'get', + async: true, + preserveState: true, + replace: true, + onHttpException: 'function', + })), + ); + + pageShow?.({ persisted: false }); + assert.equal(visit.mock.calls.length, expected.length); + + // A bfcache restore doesn't cause an Inertia history navigation. + dispatch('visit', 'starred/index'); + pageShow?.({ persisted: true }); + assert.equal(visit.mock.calls.length, expected.length + 1); + assert.equal(visit.mock.calls.at(-1)?.[0], '/starred/index'); + assert.deepEqual(visit.mock.calls.at(-1)?.[1].reset, ['commits']); + + const onHttpException = visit.mock.calls[0][1].onHttpException; + assert.equal(onHttpException?.({ status: 403 } as never), false); + assert.equal(onHttpException?.({ status: 404 } as never), false); + assert.equal(onHttpException?.({ status: 503 } as never), undefined); + assert.equal(reloadDocument.mock.calls.length, 2); + + // When a photo is open over Starred, this must be an async GET to the + // visible photo URL: trackCommitPhotoPage redirects it to the starred + // background and preserves the photo URL and overlay. + const photoUrl = '/@author/commits/123/photos/1'; + dispatch('history', 'starred/index', photoUrl); + assert.equal(visit.mock.lastCall?.[0], photoUrl); + assert.equal(visit.mock.lastCall?.[1].async, true); + assert.deepEqual(visit.mock.lastCall?.[1].only, ['commits']); + pageShow?.({ persisted: true }); + assert.equal(visit.mock.lastCall?.[0], photoUrl); + assert.equal(visit.mock.lastCall?.[1].async, true); + + const beforeStop = visit.mock.calls.length; + stop(); + dispatch('history', 'starred/index'); + pageShow?.({ persisted: true }); + assert.equal(visit.mock.calls.length, beforeStop); + } finally { + vi.unstubAllGlobals(); + } +}); From 29b821f9ac2f9260b2e35b076aa3fef715c121ef Mon Sep 17 00:00:00 2001 From: Punyapal Shah <53343069+MrPunyapal@users.noreply.github.com> Date: Wed, 18 Nov 2026 20:53:03 +0530 Subject: [PATCH 2/3] test: run TSX unit tests in the standard test command --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 3e2ef2c79a..267b928d09 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,7 @@ "lint": "NODE_OPTIONS='--experimental-strip-types' vp fmt resources/ && NODE_OPTIONS='--experimental-strip-types' vp lint --fix", "test:lint": "NODE_OPTIONS='--experimental-strip-types' vp fmt --check resources/ && NODE_OPTIONS='--experimental-strip-types' vp lint", "test:types": "tsc --noEmit && tsc --noEmit --project tests/tsconfig.json", - "test:unit": "php artisan wayfinder:generate --with-form && node --experimental-strip-types --test tests/Unit/*.test.ts" + "test:unit": "php artisan wayfinder:generate --with-form && node --experimental-strip-types --test tests/Unit/*.test.ts && vp test --config tests/draft-images.config.ts" }, "dependencies": { "@daypicker/react": "^10.0.2", From 79e808b4d77f7ba6fb57539707f565b98033a4df Mon Sep 17 00:00:00 2001 From: Punyapal Shah Date: Wed, 25 Nov 2026 19:18:58 +0530 Subject: [PATCH 3/3] fix: preserve starred photo during history refresh --- resources/js/lib/current-page.ts | 30 +++++++++++++++++++----- tests/Unit/CurrentPageHistory.test.tsx | 32 +++++++++++++++++++------- 2 files changed, 48 insertions(+), 14 deletions(-) diff --git a/resources/js/lib/current-page.ts b/resources/js/lib/current-page.ts index e3fc2b33a1..6593918c04 100644 --- a/resources/js/lib/current-page.ts +++ b/resources/js/lib/current-page.ts @@ -23,20 +23,31 @@ export function currentPageComponent(): string { return component; } -function refreshRestoredPage(page: { component: string; url: string }): void { +type RestoredPage = { + component: string; + url: string; + photoBackgroundUrl?: string; +}; + +function refreshRestoredPage(page: RestoredPage): void { const options = historyReloads[page.component]; if (!options) { return; } - // Photo overlays show their own URL over a background page. Their existing - // before-visit handler redirects async GETs to that background without closing the photo. - router.visit(page.url, { + const photoBackgroundUrl = + page.component === 'starred/index' + ? page.photoBackgroundUrl + : undefined; + + // Refresh the starred feed behind an open photo without replacing its URL. + router.visit(photoBackgroundUrl ?? page.url, { ...options, method: 'get', async: true, preserveState: true, + preserveUrl: photoBackgroundUrl !== undefined, replace: true, onHttpException: ({ status }) => { if (status === 403 || status === 404) { @@ -49,11 +60,18 @@ function refreshRestoredPage(page: { component: string; url: string }): void { } export function trackCurrentPage(): VoidFunction { - let page: { component: string; url: string } | null = null; + let page: RestoredPage | null = null; const stop = router.on('navigate', (event) => { component = event.detail.page.component; - page = { component, url: event.detail.page.url }; + const photo = event.detail.page.props.photo as + | { backgroundUrl?: string } + | undefined; + page = { + component, + url: event.detail.page.url, + photoBackgroundUrl: photo?.backgroundUrl, + }; if (event.detail.type === 'history') { refreshRestoredPage(page); diff --git a/tests/Unit/CurrentPageHistory.test.tsx b/tests/Unit/CurrentPageHistory.test.tsx index 7e147d9de1..357428a3e5 100644 --- a/tests/Unit/CurrentPageHistory.test.tsx +++ b/tests/Unit/CurrentPageHistory.test.tsx @@ -5,7 +5,11 @@ type NavigationType = 'initial' | 'visit' | 'history'; type NavigationEvent = { detail: { type: NavigationType; - page: { component: string; url: string }; + page: { + component: string; + url: string; + props?: { photo?: { backgroundUrl?: string } }; + }; }; }; @@ -59,9 +63,19 @@ test('refreshes only non-live props after history and bfcache restores', () => { type: NavigationType, component: string, url = `/${component}`, + backgroundUrl?: string, ) => navigate?.({ - detail: { type, page: { component, url } }, + detail: { + type, + page: { + component, + url, + props: backgroundUrl + ? { photo: { backgroundUrl } } + : {}, + }, + }, }); pageShow?.({ persisted: true }); @@ -104,6 +118,7 @@ test('refreshes only non-live props after history and bfcache restores', () => { method: options.method, async: options.async, preserveState: options.preserveState, + preserveUrl: options.preserveUrl, replace: options.replace, onHttpException: typeof options.onHttpException, })), @@ -113,6 +128,7 @@ test('refreshes only non-live props after history and bfcache restores', () => { method: 'get', async: true, preserveState: true, + preserveUrl: false, replace: true, onHttpException: 'function', })), @@ -134,17 +150,17 @@ test('refreshes only non-live props after history and bfcache restores', () => { assert.equal(onHttpException?.({ status: 503 } as never), undefined); assert.equal(reloadDocument.mock.calls.length, 2); - // When a photo is open over Starred, this must be an async GET to the - // visible photo URL: trackCommitPhotoPage redirects it to the starred - // background and preserves the photo URL and overlay. + // Refresh the starred background directly while retaining the visible photo URL. const photoUrl = '/@author/commits/123/photos/1'; - dispatch('history', 'starred/index', photoUrl); - assert.equal(visit.mock.lastCall?.[0], photoUrl); + dispatch('history', 'starred/index', photoUrl, '/starred?sort=recent'); + assert.equal(visit.mock.lastCall?.[0], '/starred?sort=recent'); assert.equal(visit.mock.lastCall?.[1].async, true); + assert.equal(visit.mock.lastCall?.[1].preserveUrl, true); assert.deepEqual(visit.mock.lastCall?.[1].only, ['commits']); pageShow?.({ persisted: true }); - assert.equal(visit.mock.lastCall?.[0], photoUrl); + assert.equal(visit.mock.lastCall?.[0], '/starred?sort=recent'); assert.equal(visit.mock.lastCall?.[1].async, true); + assert.equal(visit.mock.lastCall?.[1].preserveUrl, true); const beforeStop = visit.mock.calls.length; stop();