File size: 19,418 Bytes
b2cad4f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 | diff --git a/test/e2e/app-dir/back-before-hydration/app/layout.tsx b/test/e2e/app-dir/back-before-hydration/app/layout.tsx
new file mode 100644
index 00000000..e6f5a153
--- /dev/null
+++ b/test/e2e/app-dir/back-before-hydration/app/layout.tsx
@@ -0,0 +1,16 @@
+import { ThirdPartyPush } from './third-party-push'
+
+export default function RootLayout({
+ children,
+}: {
+ children: React.ReactNode
+}) {
+ return (
+ <html lang="en">
+ <body>
+ <ThirdPartyPush />
+ {children}
+ </body>
+ </html>
+ )
+}
diff --git a/test/e2e/app-dir/back-before-hydration/app/page.tsx b/test/e2e/app-dir/back-before-hydration/app/page.tsx
new file mode 100644
index 00000000..3f0394ba
--- /dev/null
+++ b/test/e2e/app-dir/back-before-hydration/app/page.tsx
@@ -0,0 +1,12 @@
+import Link from 'next/link'
+
+export default function Home() {
+ return (
+ <>
+ <h1 id="home">Home</h1>
+ <Link id="to-post" href="/post">
+ To post
+ </Link>
+ </>
+ )
+}
diff --git a/test/e2e/app-dir/back-before-hydration/app/post/page.tsx b/test/e2e/app-dir/back-before-hydration/app/post/page.tsx
new file mode 100644
index 00000000..f900d106
--- /dev/null
+++ b/test/e2e/app-dir/back-before-hydration/app/post/page.tsx
@@ -0,0 +1,16 @@
+import Link from 'next/link'
+
+export default function Post() {
+ return (
+ <>
+ <h1 id="post">Post</h1>
+ <Link id="to-home" href="/">
+ To home
+ </Link>
+ <a id="hash-link" href="#section">
+ Jump to section
+ </a>
+ <div id="section">Section</div>
+ </>
+ )
+}
diff --git a/test/e2e/app-dir/back-before-hydration/app/search/page.tsx b/test/e2e/app-dir/back-before-hydration/app/search/page.tsx
new file mode 100644
index 00000000..bb71d4df
--- /dev/null
+++ b/test/e2e/app-dir/back-before-hydration/app/search/page.tsx
@@ -0,0 +1,28 @@
+import Link from 'next/link'
+import { Suspense } from 'react'
+
+async function CurrentPage({
+ searchParams,
+}: {
+ searchParams: Promise<{ page?: string }>
+}) {
+ const { page = '1' } = await searchParams
+ return <h1 id="search">Page {page}</h1>
+}
+
+export default function Search({
+ searchParams,
+}: {
+ searchParams: Promise<{ page?: string }>
+}) {
+ return (
+ <>
+ <Suspense fallback={null}>
+ <CurrentPage searchParams={searchParams} />
+ </Suspense>
+ <Link id="to-page-2" href="/search?page=2">
+ To page 2
+ </Link>
+ </>
+ )
+}
diff --git a/test/e2e/app-dir/back-before-hydration/app/suspense/layout.tsx b/test/e2e/app-dir/back-before-hydration/app/suspense/layout.tsx
new file mode 100644
index 00000000..4323c9f5
--- /dev/null
+++ b/test/e2e/app-dir/back-before-hydration/app/suspense/layout.tsx
@@ -0,0 +1,9 @@
+import { Suspense } from 'react'
+
+export default function SuspenseLayout({
+ children,
+}: {
+ children: React.ReactNode
+}) {
+ return <Suspense>{children}</Suspense>
+}
diff --git a/test/e2e/app-dir/back-before-hydration/app/suspense/page.tsx b/test/e2e/app-dir/back-before-hydration/app/suspense/page.tsx
new file mode 100644
index 00000000..2641df90
--- /dev/null
+++ b/test/e2e/app-dir/back-before-hydration/app/suspense/page.tsx
@@ -0,0 +1,12 @@
+import Link from 'next/link'
+
+export default function Home() {
+ return (
+ <>
+ <h1 id="home">Home</h1>
+ <Link id="to-post" href="/suspense/post">
+ To post
+ </Link>
+ </>
+ )
+}
diff --git a/test/e2e/app-dir/back-before-hydration/app/suspense/post/page.tsx b/test/e2e/app-dir/back-before-hydration/app/suspense/post/page.tsx
new file mode 100644
index 00000000..2b6ab0d6
--- /dev/null
+++ b/test/e2e/app-dir/back-before-hydration/app/suspense/post/page.tsx
@@ -0,0 +1,16 @@
+import Link from 'next/link'
+
+export default function Post() {
+ return (
+ <>
+ <h1 id="post">Post</h1>
+ <Link id="to-home" href="/suspense">
+ To home
+ </Link>
+ <a id="hash-link" href="#section">
+ Jump to section
+ </a>
+ <div id="section">Section</div>
+ </>
+ )
+}
diff --git a/test/e2e/app-dir/back-before-hydration/app/suspense/search/page.tsx b/test/e2e/app-dir/back-before-hydration/app/suspense/search/page.tsx
new file mode 100644
index 00000000..94a58b0c
--- /dev/null
+++ b/test/e2e/app-dir/back-before-hydration/app/suspense/search/page.tsx
@@ -0,0 +1,28 @@
+import Link from 'next/link'
+import { Suspense } from 'react'
+
+async function CurrentPage({
+ searchParams,
+}: {
+ searchParams: Promise<{ page?: string }>
+}) {
+ const { page = '1' } = await searchParams
+ return <h1 id="search">Page {page}</h1>
+}
+
+export default function Search({
+ searchParams,
+}: {
+ searchParams: Promise<{ page?: string }>
+}) {
+ return (
+ <>
+ <Suspense fallback={null}>
+ <CurrentPage searchParams={searchParams} />
+ </Suspense>
+ <Link id="to-page-2" href="/suspense/search?page=2">
+ To page 2
+ </Link>
+ </>
+ )
+}
diff --git a/test/e2e/app-dir/back-before-hydration/app/third-party-push.tsx b/test/e2e/app-dir/back-before-hydration/app/third-party-push.tsx
new file mode 100644
index 00000000..b40a2328
--- /dev/null
+++ b/test/e2e/app-dir/back-before-hydration/app/third-party-push.tsx
@@ -0,0 +1,19 @@
+'use client'
+
+import { useEffect } from 'react'
+
+// Simulates a third-party script writing to history between the router's
+// traversal detection and its popstate replay: this component's effect runs
+// after all insertion effects but before the parent router's effects.
+export function ThirdPartyPush() {
+ useEffect(() => {
+ if ((window as any).__injectThirdPartyPush) {
+ window.history.pushState(
+ { thirdParty: true },
+ '',
+ window.location.pathname + '?tp=1'
+ )
+ }
+ }, [])
+ return null
+}
diff --git a/test/e2e/app-dir/back-before-hydration/back-before-hydration.test.ts b/test/e2e/app-dir/back-before-hydration/back-before-hydration.test.ts
new file mode 100644
index 00000000..ab9c8d3c
--- /dev/null
+++ b/test/e2e/app-dir/back-before-hydration/back-before-hydration.test.ts
@@ -0,0 +1,335 @@
+import { nextTestSetup } from 'e2e-utils'
+import { retry } from 'next-test-utils'
+import type * as Playwright from 'playwright'
+
+// Reproduces a URL/content desync when the browser's Back button is pressed
+// while a reloaded page has committed but not yet hydrated.
+//
+// Setup: navigate client-side so both history entries are same-document
+// entries created via pushState, then reload. Chrome preserves the
+// same-document association across a reload, so a Back traversal that happens
+// before the new document hydrates is an *instant same-document* traversal:
+// the URL bar changes and popstate fires, but no router is attached yet, so
+// the rendered content stays on the reloaded page. When hydration then
+// completes, the router assumes the current URL matches its payload and calls
+// replaceState() with the payload tree onto the traversed entry. From that
+// point the URL bar and the content permanently disagree, and subsequent
+// Back/Forward traversals update the URL bar without ever changing what is
+// rendered.
+//
+// In manual testing this is easiest to hit with a hard reload (shift+cmd+R)
+// because the widened commit-to-hydration window makes the race human-sized;
+// here we make it deterministic by stalling the static scripts instead.
+//
+// Every scenario runs twice: against pages with no Suspense boundary above
+// them, and against pages below a Suspense-wrapped layout. These exercise
+// different recovery codepaths: without a boundary, a suspended traversal
+// holds the transition until its data is ready; with one, it can commit the
+// boundary's fallback and then depends on React retrying when the data
+// resolves. Under cacheComponents the boundary variant regressed into a
+// permanently blank page (#95848) while the boundary-less variant recovered.
+describe('back navigation before hydration after reload', () => {
+ const { next } = nextTestSetup({ files: __dirname })
+
+ // Stalls every static script on the page so a committed document cannot
+ // start hydrating until released. (Routing a pattern also disables the
+ // browser HTTP cache for it, so cached scripts are stalled too.)
+ async function stallScripts(page: Playwright.Page) {
+ let stalling = true
+ const stalled: Array<() => void> = []
+ await page.route('**/_next/static/**', async (route) => {
+ if (stalling && route.request().resourceType() === 'script') {
+ await new Promise<void>((resolve) => stalled.push(resolve))
+ }
+ await route.continue()
+ })
+ return function releaseScripts() {
+ stalling = false
+ for (const release of stalled) release()
+ }
+ }
+
+ // Navigates client-side (creating a same-document sibling entry), then
+ // reloads with scripts stalled, returning as soon as the new document
+ // commits. `window.__stayed` is set on the committed document so tests can
+ // assert that hydration did not cause a full reload.
+ //
+ // NOTE: while scripts are stalled, only the raw Playwright `page` may be
+ // used — most `browser.*` helpers wait for the `load` event, which the
+ // stalled scripts block.
+ async function clickThenReloadStalled(
+ startPath: string,
+ linkId: string,
+ headingAfterClick: string
+ ) {
+ let page: Playwright.Page
+ const browser = await next.browser(startPath, {
+ beforePageLoad(p: Playwright.Page) {
+ page = p
+ },
+ })
+
+ await browser.elementById(linkId).click()
+ await retry(async () => {
+ expect(await browser.elementByCss('h1').text()).toBe(headingAfterClick)
+ })
+
+ const releaseScripts = await stallScripts(page)
+ await browser.refresh({ waitUntil: 'commit' })
+ await page.evaluate('window.__stayed = true')
+
+ return { browser, page, releaseScripts }
+ }
+
+ // Loads a path directly with scripts stalled from the start, so the
+ // initial document is parsed but not hydrated until released. Waits for
+ // `readySelector` since document events are blocked by the stalled scripts.
+ async function loadStalled(startPath: string, readySelector: string) {
+ let page: Playwright.Page
+ let releaseScripts: () => void
+ const browser = await next.browser(startPath, {
+ waitUntil: 'commit',
+ waitHydration: false,
+ async beforePageLoad(p: Playwright.Page) {
+ page = p
+ releaseScripts = await stallScripts(p)
+ },
+ })
+ await page.waitForSelector(readySelector)
+ await page.evaluate('window.__stayed = true')
+ return { browser, page, releaseScripts }
+ }
+
+ describe.each([
+ { label: 'no Suspense boundary above the page', prefix: '' },
+ { label: 'Suspense boundary above the page', prefix: '/suspense' },
+ ])('$label', ({ prefix }) => {
+ const homePath = prefix === '' ? '/' : prefix
+ const postPath = `${prefix}/post`
+ const searchPath = `${prefix}/search`
+
+ it('reconciles the URL with the rendered content once hydration completes', async () => {
+ const { browser, releaseScripts } = await clickThenReloadStalled(
+ homePath,
+ 'to-post',
+ 'Post'
+ )
+
+ // Back while the reloaded document is not hydrated: an instant
+ // same-document traversal handled by nobody.
+ await browser.back({ waitUntil: 'commit' })
+ expect(new URL(await browser.url()).pathname).toBe(homePath)
+
+ releaseScripts()
+
+ // We traversed back, so once the router is up it must render the home
+ // page (or otherwise bring URL and content back in sync).
+ await retry(async () => {
+ expect(new URL(await browser.url()).pathname).toBe(homePath)
+ expect(await browser.elementByCss('h1').text()).toBe('Home')
+ })
+
+ // History traversal must still work after recovery.
+ await browser.forward()
+ await retry(async () => {
+ expect(new URL(await browser.url()).pathname).toBe(postPath)
+ expect(await browser.elementByCss('h1').text()).toBe('Post')
+ })
+
+ await browser.back()
+ await retry(async () => {
+ expect(new URL(await browser.url()).pathname).toBe(homePath)
+ expect(await browser.elementByCss('h1').text()).toBe('Home')
+ })
+ })
+
+ it('reconciles when the traversed entry differs only in search params', async () => {
+ const { browser, releaseScripts } = await clickThenReloadStalled(
+ `${searchPath}?page=1`,
+ 'to-page-2',
+ 'Page 2'
+ )
+
+ await browser.back({ waitUntil: 'commit' })
+ expect(new URL(await browser.url()).search).toBe('?page=1')
+
+ releaseScripts()
+
+ await retry(async () => {
+ expect(new URL(await browser.url()).search).toBe('?page=1')
+ expect(await browser.elementByCss('h1').text()).toBe('Page 1')
+ })
+
+ await browser.forward()
+ await retry(async () => {
+ expect(new URL(await browser.url()).search).toBe('?page=2')
+ expect(await browser.elementByCss('h1').text()).toBe('Page 2')
+ })
+ })
+
+ // History changes before hydration that are NOT missed traversals must
+ // not trigger any recovery: the router should adopt the current entry on
+ // its first history write, and in particular never cause a full reload.
+ describe('other history changes before hydration', () => {
+ it('adopts a third-party pushState', async () => {
+ const { browser, page, releaseScripts } = await loadStalled(
+ postPath,
+ '#post'
+ )
+
+ // e.g. analytics/consent tooling running before the framework.
+ await page.evaluate(
+ `window.history.pushState({ thirdParty: true }, '', '${postPath}?tp=1')`
+ )
+ releaseScripts()
+
+ await retry(async () => {
+ expect(await browser.eval('window.__stayed')).toBe(true)
+ expect(await browser.elementByCss('h1').text()).toBe('Post')
+ expect(new URL(await browser.url()).search).toBe('?tp=1')
+ })
+
+ // The router still navigates.
+ await browser.elementById('to-home').click()
+ await retry(async () => {
+ expect(await browser.elementByCss('h1').text()).toBe('Home')
+ })
+ })
+
+ it('keeps an in-page anchor jump on a fresh load', async () => {
+ const { browser, page, releaseScripts } = await loadStalled(
+ postPath,
+ '#post'
+ )
+
+ await page.click('#hash-link')
+ expect(new URL(page.url()).hash).toBe('#section')
+ releaseScripts()
+
+ await retry(async () => {
+ expect(await browser.eval('window.__stayed')).toBe(true)
+ expect(await browser.elementByCss('h1').text()).toBe('Post')
+ expect(new URL(await browser.url()).hash).toBe('#section')
+ })
+
+ // Hash traversals keep behaving like same-page jumps.
+ await browser.back()
+ await retry(async () => {
+ expect(new URL(await browser.url()).hash).toBe('')
+ expect(await browser.elementByCss('h1').text()).toBe('Post')
+ })
+ await browser.forward()
+ await retry(async () => {
+ expect(new URL(await browser.url()).hash).toBe('#section')
+ expect(await browser.elementByCss('h1').text()).toBe('Post')
+ })
+ })
+
+ it('keeps an in-page anchor jump between a reload and hydration', async () => {
+ const { browser, page, releaseScripts } = await clickThenReloadStalled(
+ homePath,
+ 'to-post',
+ 'Post'
+ )
+
+ await page.click('#hash-link')
+ expect(new URL(page.url()).hash).toBe('#section')
+ releaseScripts()
+
+ await retry(async () => {
+ expect(await browser.eval('window.__stayed')).toBe(true)
+ expect(await browser.elementByCss('h1').text()).toBe('Post')
+ expect(new URL(await browser.url()).hash).toBe('#section')
+ })
+
+ // Traversing over the hash entry and the pushState entry still works.
+ await browser.back() // -> post
+ await browser.back() // -> home
+ await retry(async () => {
+ expect(new URL(await browser.url()).pathname).toBe(homePath)
+ expect(await browser.elementByCss('h1').text()).toBe('Home')
+ })
+ })
+
+ it('handles a pushState followed by back', async () => {
+ const { browser, page, releaseScripts } = await loadStalled(
+ postPath,
+ '#post'
+ )
+
+ await page.evaluate(
+ `window.history.pushState({ thirdParty: true }, '', '${postPath}?tp=1')`
+ )
+ await page.evaluate(`window.history.back()`)
+ await retry(async () => {
+ expect(new URL(page.url()).search).toBe('')
+ })
+ releaseScripts()
+
+ await retry(async () => {
+ expect(await browser.eval('window.__stayed')).toBe(true)
+ expect(await browser.elementByCss('h1').text()).toBe('Post')
+ })
+
+ await browser.elementById('to-home').click()
+ await retry(async () => {
+ expect(await browser.elementByCss('h1').text()).toBe('Home')
+ })
+ })
+
+ it('leaves the traversal unhandled when a third-party write lands before the replay', async () => {
+ const { browser, page, releaseScripts } = await clickThenReloadStalled(
+ homePath,
+ 'to-post',
+ 'Post'
+ )
+
+ await browser.back({ waitUntil: 'commit' })
+ expect(new URL(await browser.url()).pathname).toBe(homePath)
+
+ // Arms an effect in the fixture that pushes a third-party history
+ // entry between the router's traversal detection and its replay.
+ await page.evaluate('window.__injectThirdPartyPush = true')
+ releaseScripts()
+
+ await retry(async () => {
+ // The traversal cannot be replayed onto the third-party entry. The
+ // content stays on the reloaded page, like before the fix — and in
+ // particular the router must not reload a page that just loaded.
+ expect(await browser.eval('window.__stayed')).toBe(true)
+ expect(new URL(await browser.url()).search).toBe('?tp=1')
+ expect(await browser.elementByCss('h1').text()).toBe('Post')
+ })
+
+ await browser.elementById('to-home').click()
+ await retry(async () => {
+ expect(await browser.elementByCss('h1').text()).toBe('Home')
+ })
+ })
+
+ it('handles a traversal onto a third-party entry', async () => {
+ const { browser, page, releaseScripts } = await loadStalled(
+ postPath,
+ '#post'
+ )
+
+ await page.evaluate(
+ `window.history.pushState({ a: 1 }, '', '${postPath}?tp=1')`
+ )
+ await page.evaluate(
+ `window.history.pushState({ b: 2 }, '', '${postPath}?tp=2')`
+ )
+ await page.evaluate(`window.history.back()`)
+ await retry(async () => {
+ expect(new URL(page.url()).search).toBe('?tp=1')
+ })
+ releaseScripts()
+
+ await retry(async () => {
+ expect(await browser.eval('window.__stayed')).toBe(true)
+ expect(await browser.elementByCss('h1').text()).toBe('Post')
+ })
+ })
+ })
+ })
+})
|