diff --git a/packages/next/src/client/components/app-router.tsx b/packages/next/src/client/components/app-router.tsx index 0aec66a37146e..2eabaa8787c7b 100644 --- a/packages/next/src/client/components/app-router.tsx +++ b/packages/next/src/client/components/app-router.tsx @@ -449,6 +449,11 @@ function Router({ return } + // Clear the pendingMpaPath value so that a subsequent MPA navigation to the same URL can be triggered. + // This is necessary because if the browser restored from bfcache, the pendingMpaPath would still be set to the value + // of the last MPA navigation. + globalMutable.pendingMpaPath = undefined + dispatch({ type: ACTION_RESTORE, url: new URL(window.location.href), diff --git a/test/production/bfcache-routing/index.test.ts b/test/production/bfcache-routing/index.test.ts index f813c720bd9ab..4df1876bbabc8 100644 --- a/test/production/bfcache-routing/index.test.ts +++ b/test/production/bfcache-routing/index.test.ts @@ -69,5 +69,15 @@ describe('bfcache-routing', () => { // we should be back on the test page with no errors html = await browser.evalAsync('document.documentElement.innerHTML') expect(html).toContain('BFCache Test') + + // After restoring from bfcache, a subsequent mpa navigation to the same URL should work + // We trigger the click via `evalAsync` because when restoring from bfcache, our internal + // 'waitForElementByCss' method doesn't think the element is attached to the DOM. + await browser.evalAsync( + `document.querySelector('a[href="https://example.vercel.sh"]').click()` + ) + await browser.waitForCondition( + 'window.location.origin === "https://example.vercel.sh"' + ) }) })