-
Notifications
You must be signed in to change notification settings - Fork 26.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix routing bug when bfcache is hit following an mpa navigation (#54081)
When an mpa navigation takes place, we currently push the user to the new route and suspend the page indefinitely (x-ref: #49058). When navigating back, if the browser opts into using the [bfcache](https://web.dev/bfcache/), it will remain suspended and `pushRef.mpaNavigation` will be true. This means that anything that would cause the component to re-render will trigger the mpa navigation again (such as hovering over another `Link`, as reported in #53347) This PR checks to see if bfcache is being used by observing `PageTransitionEvent.persisted` and if so, resets the router state to clear out `pushRef`. Closes NEXT-1511 Fixes #53347
- Loading branch information
Showing
9 changed files
with
131 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
export default function Layout({ children }) { | ||
return ( | ||
<html> | ||
<head></head> | ||
<body>{children}</body> | ||
</html> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
'use client' | ||
import React from 'react' | ||
import Link from 'next/link' | ||
|
||
export default function Page() { | ||
const [counter, setCounter] = React.useState(0) | ||
return ( | ||
<div> | ||
<button onClick={() => setCounter((c) => c + 1)}> | ||
Trigger Re-Render | ||
</button> | ||
<div id="counter">{counter}</div> | ||
<Link href="https://example.vercel.sh">External Page</Link> | ||
</div> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { Server } from 'http' | ||
import { | ||
findPort, | ||
nextBuild, | ||
startStaticServer, | ||
stopApp, | ||
} from 'next-test-utils' | ||
import webdriver from 'next-webdriver' | ||
import { join } from 'path' | ||
|
||
describe('export-routing', () => { | ||
let port: number | ||
let app: Server | ||
|
||
beforeAll(async () => { | ||
const appDir = __dirname | ||
const exportDir = join(appDir, 'out') | ||
|
||
await nextBuild(appDir, undefined, { cwd: appDir }) | ||
port = await findPort() | ||
app = await startStaticServer(exportDir, undefined, port) | ||
}) | ||
|
||
afterAll(() => { | ||
stopApp(app) | ||
}) | ||
|
||
it('should not suspend indefinitely when page is restored from bfcache after an mpa navigation', async () => { | ||
// bfcache is not currently supported by CDP, so we need to run this particular test in headed mode | ||
// https://bugs.chromium.org/p/chromium/issues/detail?id=1317959 | ||
if (!process.env.CI && process.env.HEADLESS) { | ||
console.warn('This test can only run in headed mode. Skipping...') | ||
return | ||
} | ||
|
||
const browser = await webdriver(port, '/index.html', { headless: false }) | ||
|
||
await browser.elementByCss('a[href="https://example.vercel.sh"]').click() | ||
await browser.waitForCondition( | ||
'window.location.origin === "https://example.vercel.sh"' | ||
) | ||
|
||
// this will never resolve in the failure case, as the page will be suspended indefinitely | ||
await browser.back() | ||
|
||
expect(await browser.elementByCss('#counter').text()).toBe('0') | ||
|
||
// click the button | ||
await browser.elementByCss('button').click() | ||
|
||
// counter should be 1 | ||
expect(await browser.elementByCss('#counter').text()).toBe('1') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
/** @type {import('next').NextConfig} */ | ||
const nextConfig = { | ||
output: 'export', | ||
} | ||
|
||
module.exports = nextConfig |