diff --git a/test/e2e/app-dir/segment-cache/static-shell-vary-params-regression/app/docs/[[...slug]]/page.tsx b/test/e2e/app-dir/segment-cache/static-shell-vary-params-regression/app/docs/[[...slug]]/page.tsx new file mode 100644 index 0000000000000000000000000000000000000000..dd9ad409e52d6e8d39efcaece3fc8d26a395afdd --- /dev/null +++ b/test/e2e/app-dir/segment-cache/static-shell-vary-params-regression/app/docs/[[...slug]]/page.tsx @@ -0,0 +1,36 @@ +import { LinkAccordion } from '../../../components/link-accordion' + +type Params = { slug?: string[] } + +export function generateStaticParams(): Params[] { + return [{ slug: [] }, { slug: ['alpha'] }, { slug: ['beta'] }] +} + +/** + * An optional catch-all where the index (empty slug) and the named pages are + * the same route. The page reads `params`, so every one of these segments + * varies by `slug` — the server reports that in the response's vary params. + */ +export default async function DocsPage({ + params, +}: { + params: Promise +}) { + const { slug } = await params + const name = slug && slug.length > 0 ? slug.join('/') : 'index' + return ( +
+ {/* + The id is slug-specific so a test can wait for the navigation to + actually commit — an id shared by every slug would match the + pre-navigation DOM and read stale content. + + One interpolated string so the text is a single node in the RSC + payload, which lets the test assert on response contents. + */} +
{`Docs: ${name}`}
+ alpha + beta +
+ ) +} diff --git a/test/e2e/app-dir/segment-cache/static-shell-vary-params-regression/app/layout.tsx b/test/e2e/app-dir/segment-cache/static-shell-vary-params-regression/app/layout.tsx new file mode 100644 index 0000000000000000000000000000000000000000..08eaa94fdc8896acfd343a0dbb61c713eec93a55 --- /dev/null +++ b/test/e2e/app-dir/segment-cache/static-shell-vary-params-regression/app/layout.tsx @@ -0,0 +1,11 @@ +export default function RootLayout({ + children, +}: { + children: React.ReactNode +}) { + return ( + + {children} + + ) +} diff --git a/test/e2e/app-dir/segment-cache/static-shell-vary-params-regression/app/page.tsx b/test/e2e/app-dir/segment-cache/static-shell-vary-params-regression/app/page.tsx new file mode 100644 index 0000000000000000000000000000000000000000..ef3f0b14c0a9145a09a4c0f2acb45d9065805aeb --- /dev/null +++ b/test/e2e/app-dir/segment-cache/static-shell-vary-params-regression/app/page.tsx @@ -0,0 +1,15 @@ +import { LinkAccordion } from '../components/link-accordion' + +export default function HomePage() { + return ( +
+
Home
+ {/* + Revealing this link prefetches /docs — the fully static index of the + optional catch-all route. That prefetch is what used to poison the + param-wildcard slot for every other slug. + */} + Docs index +
+ ) +} diff --git a/test/e2e/app-dir/segment-cache/static-shell-vary-params-regression/components/link-accordion.tsx b/test/e2e/app-dir/segment-cache/static-shell-vary-params-regression/components/link-accordion.tsx new file mode 100644 index 0000000000000000000000000000000000000000..2b1e68daafd32988e83f7b40b2365d36dd387b5f --- /dev/null +++ b/test/e2e/app-dir/segment-cache/static-shell-vary-params-regression/components/link-accordion.tsx @@ -0,0 +1,29 @@ +'use client' + +import Link from 'next/link' +import { useState } from 'react' + +export function LinkAccordion({ + href, + children, +}: { + href: string + children: React.ReactNode +}) { + const [isVisible, setIsVisible] = useState(false) + return ( + <> + setIsVisible(!isVisible)} + data-link-accordion={href} + /> + {isVisible ? ( + {children} + ) : ( + <>{children} (link is hidden) + )} + + ) +} diff --git a/test/e2e/app-dir/segment-cache/static-shell-vary-params-regression/next.config.js b/test/e2e/app-dir/segment-cache/static-shell-vary-params-regression/next.config.js new file mode 100644 index 0000000000000000000000000000000000000000..62e0151ab5ef666e250c00fa21208dfddd544ad3 --- /dev/null +++ b/test/e2e/app-dir/segment-cache/static-shell-vary-params-regression/next.config.js @@ -0,0 +1,13 @@ +/** + * @type {import('next').NextConfig} + */ +const nextConfig = { + cacheComponents: true, + // Partial Prefetching enables the Shell prefetch phase, where the bug lived. + partialPrefetching: true, + experimental: { + varyParams: process.env.TEST_VARY_PARAMS === 'true', + }, +} + +module.exports = nextConfig diff --git a/test/e2e/app-dir/segment-cache/static-shell-vary-params-regression/static-shell-vary-params-regression.test.ts b/test/e2e/app-dir/segment-cache/static-shell-vary-params-regression/static-shell-vary-params-regression.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..8b7b8174363aa6cb833e186de3832a5c0f06faf7 --- /dev/null +++ b/test/e2e/app-dir/segment-cache/static-shell-vary-params-regression/static-shell-vary-params-regression.test.ts @@ -0,0 +1,91 @@ +import { nextTestSetup } from 'e2e-utils' +import type * as Playwright from 'playwright' +import { createRouterAct } from 'router-act' + +describe.each([ + ['vary params enabled', 'true'], + ['vary params disabled', 'false'], +])( + 'segment cache - static shell vary params regression (%s)', + (_, varyParams) => { + const { next, isNextDev } = nextTestSetup({ + files: __dirname, + env: { TEST_VARY_PARAMS: varyParams }, + }) + + if (isNextDev) { + // Depends on build-time prerenders, which don't exist in dev. + test('skipped in dev mode', () => {}) + return + } + + // Regression test for a segment cache keying bug. + // + // During the Shell prefetch phase the client walks at the StaticShell + // strategy, and it used to key whatever came back at `tree.shellVaryPath`, + // which replaces every non-root param with Fallback. That's only correct + // when the payload really is the param-independent shell. + // + // When a route renders with no dynamic hole, the response has no shell/full + // split — the "shell" payload IS the concrete render for the params that + // were requested. Keying it at the wildcard path published one param's page + // as the answer for every other param value, and recorded it at the highest + // tier, so later navigations rendered the wrong page and skipped the network + // entirely. + // + // On an optional catch-all, the index (empty slug) is the case that hits + // this: prefetching /docs poisoned /docs/alpha and /docs/beta. + // + // The fix makes the server-reported vary params authoritative, so the shell + // path is used exactly when the segment really is param-independent. + it('does not serve the catch-all index page for a different slug', async () => { + let page: Playwright.Page + const browser = await next.browser('/', { + beforePageLoad(p: Playwright.Page) { + page = p + }, + }) + const act = createRouterAct(page, { includeAppShellRequests: true }) + + // Prefetch the fully static index of the optional catch-all route. Its + // response has no shell/full split, so this is the write that used to land + // in the param-wildcard slot. + await act( + async () => { + await browser + .elementByCss('input[data-link-accordion="/docs"]') + .click() + }, + { includes: 'Docs: index' } + ) + + // Navigate there. Fully prefetched, so nothing should be requested. + await act(async () => { + await browser.elementByCss('a[href="/docs"]').click() + expect(await browser.elementById('docs-page-index').text()).toBe( + 'Docs: index' + ) + }, 'no-requests') + + // Now prefetch a different slug on the same route. Before the fix the + // index's page segment was sitting in the wildcard slot marked complete, + // so this fired no request at all for the page content and this + // expectation would time out. + await act( + async () => { + await browser + .elementByCss('input[data-link-accordion="/docs/alpha"]') + .click() + }, + { includes: 'Docs: alpha' } + ) + + await act(async () => { + await browser.elementByCss('a[href="/docs/alpha"]').click() + expect(await browser.elementById('docs-page-alpha').text()).toBe( + 'Docs: alpha' + ) + }, 'no-requests') + }) + } +)