Unverified Commit 8980f3a7 authored by Andrew Clark's avatar Andrew Clark Committed by GitHub

Fix: Improved rewrite detection during optimistic routing (#93619)

When optimistic routing is enabled, the client uses responses it
receives from the server (during a navigation or a prefetch) to resolve
the routes of structurally similar URLs.

However, if the server rewrites a URL to a different path than the one
requested by the client, then we disable optimistic routing for that
pattern.

We detect a rewrite by comparing the route tree returned from the server
to the requested URL. If the route tree does not match, according to the
Next.js routing algorithm, then the route must have been rewritten; we
do not write this pattern into the client's route table.

The basic mechanism for this was already implemented, but there were
some cases we weren't handling correctly:

- A static or non-optional dynamic segment is reached with no URL part
to consume.
- A page leaf is reached with URL parts still pending.
- A dynamic segment whose URL part matches one of its known static
siblings (which would have taken precedence under normal routing).

These cases have been fixed. Regression tests live in
`optimistic-routing-rewrite-detection-regression/` and
`optimistic-routing-parallel-slot-catchall-regression/`.

---

Replaces #93578 (re-opened from an `origin` branch to satisfy CI
requirements).
parent c2062dc6
...@@ -209,8 +209,6 @@ export function discoverKnownRoute( ...@@ -209,8 +209,6 @@ export function discoverKnownRoute(
const tree = routeTree const tree = routeTree
const pathnameParts = pathname.split('/').filter((p) => p !== '') const pathnameParts = pathname.split('/').filter((p) => p !== '')
const firstPart = pathnameParts.length > 0 ? pathnameParts[0] : null
const remainingParts = pathnameParts.length > 0 ? pathnameParts.slice(1) : []
if (pendingEntry !== null) { if (pendingEntry !== null) {
// Fulfill the pending entry first // Fulfill the pending entry first
...@@ -232,8 +230,8 @@ export function discoverKnownRoute( ...@@ -232,8 +230,8 @@ export function discoverKnownRoute(
discoverKnownRoutePart( discoverKnownRoutePart(
knownRouteTreeRoot, knownRouteTreeRoot,
tree, tree,
firstPart, pathnameParts,
remainingParts, 0,
fulfilledEntry, fulfilledEntry,
now, now,
pathname, pathname,
...@@ -253,8 +251,8 @@ export function discoverKnownRoute( ...@@ -253,8 +251,8 @@ export function discoverKnownRoute(
return discoverKnownRoutePart( return discoverKnownRoutePart(
knownRouteTreeRoot, knownRouteTreeRoot,
tree, tree,
firstPart, pathnameParts,
remainingParts, 0,
null, null,
now, now,
pathname, pathname,
...@@ -268,6 +266,38 @@ export function discoverKnownRoute( ...@@ -268,6 +266,38 @@ export function discoverKnownRoute(
) )
} }
/**
* Bail out of populating the known route tree when discovery detects that the
* URL doesn't match the route structure (a rewrite). The route entry is still
* inserted into the cache for direct lookup — we just don't store it as a
* pattern, since the URL and the tree describe different shapes.
*/
function handleMismatchDueToRewrite(
existingEntry: FulfilledRouteCacheEntry | null,
now: number,
pathname: string,
nextUrl: string | null,
fullTree: RouteTree,
metadataVaryPath: PageVaryPath,
couldBeIntercepted: boolean,
canonicalUrl: string,
supportsPerSegmentPrefetching: boolean
): FulfilledRouteCacheEntry {
if (existingEntry !== null) {
return existingEntry
}
return writeRouteIntoCache(
now,
pathname as NormalizedPathname,
nextUrl,
fullTree,
metadataVaryPath,
couldBeIntercepted,
canonicalUrl,
supportsPerSegmentPrefetching
)
}
/** /**
* Gets or creates the dynamic child node for a KnownRoutePart. * Gets or creates the dynamic child node for a KnownRoutePart.
* A node can have at most one dynamic child (you can't have both [slug] and * A node can have at most one dynamic child (you can't have both [slug] and
...@@ -308,8 +338,8 @@ function discoverDynamicChild( ...@@ -308,8 +338,8 @@ function discoverDynamicChild(
function discoverKnownRoutePart( function discoverKnownRoutePart(
parentKnownRoutePart: KnownRoutePart, parentKnownRoutePart: KnownRoutePart,
routeTree: RouteTree, routeTree: RouteTree,
urlPart: string | null, pathnameParts: readonly string[],
remainingParts: string[], partIndex: number,
existingEntry: FulfilledRouteCacheEntry | null, existingEntry: FulfilledRouteCacheEntry | null,
// These are passed through unchanged for entry creation at the leaf // These are passed through unchanged for entry creation at the leaf
now: number, now: number,
...@@ -323,38 +353,82 @@ function discoverKnownRoutePart( ...@@ -323,38 +353,82 @@ function discoverKnownRoutePart(
hasDynamicRewrite: boolean hasDynamicRewrite: boolean
): FulfilledRouteCacheEntry { ): FulfilledRouteCacheEntry {
const segment = routeTree.segment const segment = routeTree.segment
const urlPart =
partIndex < pathnameParts.length ? pathnameParts[partIndex] : null
let segmentAppearsInURL: boolean let knownRoutePart: KnownRoutePart = parentKnownRoutePart
let paramName: string | null = null let nextPartIndex = partIndex
let paramType: DynamicParamTypesShort | null = null
let staticSiblings: readonly string[] | null = null
if (typeof segment === 'string') { if (typeof segment === 'string') {
segmentAppearsInURL = doesStaticSegmentAppearInURL(segment) if (doesStaticSegmentAppearInURL(segment)) {
// A visible static segment must consume exactly one URL part that
// equals the segment. If the URL is exhausted or the URL part doesn't
// match, the URL doesn't fit the route shape — the response was
// rewrite-affected. Bail out.
if (urlPart === null || urlPart !== segment) {
return handleMismatchDueToRewrite(
existingEntry,
now,
pathname,
nextUrl,
fullTree,
metadataVaryPath,
couldBeIntercepted,
canonicalUrl,
supportsPerSegmentPrefetching
)
}
if (parentKnownRoutePart.staticChildren === null) {
parentKnownRoutePart.staticChildren = new Map()
}
let existingChild = parentKnownRoutePart.staticChildren.get(urlPart)
if (existingChild === undefined) {
existingChild = createEmptyPart()
parentKnownRoutePart.staticChildren.set(urlPart, existingChild)
}
knownRoutePart = existingChild
// Advance to next URL part.
nextPartIndex = partIndex + 1
}
// else: Transparent segment (route group, __PAGE__, etc.)
// Stay at the same known route part, don't advance URL parts
} else { } else {
// Dynamic segment tuple: [paramName, paramCacheKey, paramType, staticSiblings] // Dynamic segment tuple: [paramName, paramCacheKey, paramType, staticSiblings]
paramName = segment[0] const paramName: string = segment[0]
paramType = segment[2] const paramType: DynamicParamTypesShort = segment[2]
staticSiblings = segment[3] const staticSiblings: readonly string[] | null = segment[3]
segmentAppearsInURL = true
if (paramType !== 'oc' && urlPart === null) {
// Every dynamic segment except the optional catch-all (`[[...param]]`)
// must consume at least one URL part at runtime. If discovery reached
// this segment with no URL parts left to consume, the URL doesn't fit
// the route shape — the response was rewrite-affected. Bail out.
return handleMismatchDueToRewrite(
existingEntry,
now,
pathname,
nextUrl,
fullTree,
metadataVaryPath,
couldBeIntercepted,
canonicalUrl,
supportsPerSegmentPrefetching
)
} }
let knownRoutePart: KnownRoutePart = parentKnownRoutePart if (
let nextUrlPart: string | null = urlPart staticSiblings !== null &&
let nextRemainingParts: string[] = remainingParts urlPart !== null &&
staticSiblings.includes(urlPart)
if (segmentAppearsInURL) { ) {
// Check for mismatch: if this is a static segment, the URL part must match // The route tree says this is a dynamic sibling, but the canonical URL
if (paramName === null && urlPart !== segment) { // is a known static sibling. This is a mismatch.
// URL doesn't match route structure (likely a rewrite). return handleMismatchDueToRewrite(
// Don't populate the known route tree, just write the route into the existingEntry,
// cache and return immediately.
if (existingEntry !== null) {
return existingEntry
}
return writeRouteIntoCache(
now, now,
pathname as NormalizedPathname, pathname,
nextUrl, nextUrl,
fullTree, fullTree,
metadataVaryPath, metadataVaryPath,
...@@ -365,8 +439,6 @@ function discoverKnownRoutePart( ...@@ -365,8 +439,6 @@ function discoverKnownRoutePart(
} }
// URL matches route structure. Build the known route tree. // URL matches route structure. Build the known route tree.
if (paramName !== null && paramType !== null) {
// Dynamic segment
knownRoutePart = discoverDynamicChild( knownRoutePart = discoverDynamicChild(
parentKnownRoutePart, parentKnownRoutePart,
paramName, paramName,
...@@ -390,27 +462,17 @@ function discoverKnownRoutePart( ...@@ -390,27 +462,17 @@ function discoverKnownRoutePart(
} }
} }
} }
// Advance to next URL part. Catch-all segments (`[...param]` and
// `[[...param]]`) absorb every remaining URL part at runtime (see
// `matchKnownRoutePart`, which slices the rest of `pathnameParts`).
if (paramType === 'c' || paramType === 'oc') {
nextPartIndex = pathnameParts.length
} else { } else {
// Static segment nextPartIndex = partIndex + 1
if (parentKnownRoutePart.staticChildren === null) {
parentKnownRoutePart.staticChildren = new Map()
} }
let existingChild = parentKnownRoutePart.staticChildren.get(urlPart!)
if (existingChild === undefined) {
existingChild = createEmptyPart()
parentKnownRoutePart.staticChildren.set(urlPart!, existingChild)
}
knownRoutePart = existingChild
} }
// Advance to next URL part
nextUrlPart = remainingParts.length > 0 ? remainingParts[0] : null
nextRemainingParts =
remainingParts.length > 0 ? remainingParts.slice(1) : []
}
// else: Transparent segment (route group, __PAGE__, etc.)
// Stay at the same known route part, don't advance URL parts
// Recurse into child routes. A route tree can have multiple parallel routes // Recurse into child routes. A route tree can have multiple parallel routes
// (e.g., @modal alongside children). Each parallel route is a separate // (e.g., @modal alongside children). Each parallel route is a separate
// branch, but they all share the same URL - we just need to traverse all // branch, but they all share the same URL - we just need to traverse all
...@@ -429,8 +491,8 @@ function discoverKnownRoutePart( ...@@ -429,8 +491,8 @@ function discoverKnownRoutePart(
const result = discoverKnownRoutePart( const result = discoverKnownRoutePart(
knownRoutePart, knownRoutePart,
childRouteTree, childRouteTree,
nextUrlPart, pathnameParts,
nextRemainingParts, nextPartIndex,
existingEntry, existingEntry,
now, now,
pathname, pathname,
...@@ -451,12 +513,27 @@ function discoverKnownRoutePart( ...@@ -451,12 +513,27 @@ function discoverKnownRoutePart(
} }
// Defensive fallback: no children returned a result. This shouldn't happen // Defensive fallback: no children returned a result. This shouldn't happen
// for valid route trees, but handle it gracefully. // for valid route trees, but handle it gracefully.
if (existingEntry !== null) { return handleMismatchDueToRewrite(
return existingEntry existingEntry,
now,
pathname,
nextUrl,
fullTree,
metadataVaryPath,
couldBeIntercepted,
canonicalUrl,
supportsPerSegmentPrefetching
)
} }
return writeRouteIntoCache(
// Reached a page node (`__PAGE__` leaf). If there are still URL parts
// left to consume, the route tree is shorter than the URL, which means
// the URL doesn't match the route structure (likely a rewrite).
if (nextPartIndex < pathnameParts.length) {
return handleMismatchDueToRewrite(
existingEntry,
now, now,
pathname as NormalizedPathname, pathname,
nextUrl, nextUrl,
fullTree, fullTree,
metadataVaryPath, metadataVaryPath,
......
import { connection } from 'next/server' import { connection } from 'next/server'
import { LinkAccordion } from '../../../components/link-accordion'
export default async function ActualPage({ export default async function ActualPage({
params, params,
...@@ -12,6 +13,7 @@ export default async function ActualPage({ ...@@ -12,6 +13,7 @@ export default async function ActualPage({
<div id="actual-page"> <div id="actual-page">
<h1>Actual page</h1> <h1>Actual page</h1>
<p data-slug={slug}>Slug: {slug}</p> <p data-slug={slug}>Slug: {slug}</p>
<LinkAccordion href="/hub">Hub</LinkAccordion>
</div> </div>
) )
} }
import { Suspense } from 'react'
import { connection } from 'next/server'
import { LinkAccordion } from '../../components/link-accordion'
async function HubContent() {
await connection()
return <div id="hub-content">Hub</div>
}
// Hub page used by rewrite-detection tests as a navigation target instead of
// `browser.back()`. Each visit lands on a fresh page where all accordions
// start closed, so previously-revealed Links can't be re-mounted by BFCache
// and trigger uncontrolled prefetches outside any `act` scope.
export default function HubPage() {
return (
<div>
<Suspense fallback="Loading hub...">
<HubContent />
</Suspense>
<ul>
<li>
<LinkAccordion href="/rewritten/second" prefetch={false}>
Rewritten Second
</LinkAccordion>
</li>
<li>
<LinkAccordion href="/search-rewrite?v=beta" prefetch={false}>
Search Rewrite Beta (v=beta → content=beta)
</LinkAccordion>
</li>
</ul>
</div>
)
}
import { LinkAccordion } from '../../components/link-accordion'
// Fully static page - no dynamic data access // Fully static page - no dynamic data access
export default function RewriteAlphaPage() { export default function RewriteAlphaPage() {
return ( return (
...@@ -6,6 +8,7 @@ export default function RewriteAlphaPage() { ...@@ -6,6 +8,7 @@ export default function RewriteAlphaPage() {
<p id="rewrite-content" data-content="alpha"> <p id="rewrite-content" data-content="alpha">
Content: alpha Content: alpha
</p> </p>
<LinkAccordion href="/hub">Hub</LinkAccordion>
</div> </div>
) )
} }
import { LinkAccordion } from '../../components/link-accordion'
// Fully static page - no dynamic data access // Fully static page - no dynamic data access
export default function RewriteBetaPage() { export default function RewriteBetaPage() {
return ( return (
...@@ -6,6 +8,7 @@ export default function RewriteBetaPage() { ...@@ -6,6 +8,7 @@ export default function RewriteBetaPage() {
<p id="rewrite-content" data-content="beta"> <p id="rewrite-content" data-content="beta">
Content: beta Content: beta
</p> </p>
<LinkAccordion href="/hub">Hub</LinkAccordion>
</div> </div>
) )
} }
...@@ -346,14 +346,26 @@ describe('optimistic-routing', () => { ...@@ -346,14 +346,26 @@ describe('optimistic-routing', () => {
// Wait for navigation to complete // Wait for navigation to complete
await browser.elementById('actual-page') await browser.elementById('actual-page')
// Step 2: Navigate back to home using browser back button // Step 2: Navigate forward to /hub. We use a hub page rather than
await browser.back() // browser.back() so that the previously-revealed /rewritten/first
await browser.elementById('rendered-route-history') // accordion can't be re-mounted from BFCache and trigger an
// uncontrolled prefetch outside any `act` scope. See
// Step 3: Navigate to /rewritten/second. // .claude/skills/router-act/SKILL.md.
// This link has prefetch={false}. Even though we've "learned" the route await act(async () => {
// from step 1, the route should be marked as having a dynamic rewrite, const revealHub = await browser.elementByCss(
// so we should NOT use the cached pattern. 'input[data-link-accordion="/hub"]'
)
await revealHub.click()
const linkHub = await browser.elementByCss('a[href="/hub"]')
await linkHub.click()
})
await browser.elementById('hub-content')
// Step 3: From /hub, reveal /rewritten/second. This link has
// prefetch={false}. Even though /rewritten/first was visited in
// step 1, that response was marked as a dynamic rewrite, so the
// router must not reuse it as a prediction for /rewritten/second —
// meaning no prefetch should fire on reveal.
await act(async () => { await act(async () => {
const revealSecond = await browser.elementByCss( const revealSecond = await browser.elementByCss(
'input[data-link-accordion="/rewritten/second"]' 'input[data-link-accordion="/rewritten/second"]'
...@@ -370,13 +382,12 @@ describe('optimistic-routing', () => { ...@@ -370,13 +382,12 @@ describe('optimistic-routing', () => {
await browser.elementById('actual-page') await browser.elementById('actual-page')
// Verify using rendered route history that no wrong params were rendered. // Verify using rendered route history that no wrong params were rendered.
// If route prediction incorrectly used a cached pattern, we'd see "first" // If the router had reused step 1's response as a prediction, we'd see
// briefly flash before "second". // "first" briefly flash before "second".
expect(await getRenderedRouteHistory(browser)).toEqual([ expect(await getRenderedRouteHistory(browser)).toEqual([
{ url: '/', params: {} }, { url: '/', params: {} },
{ url: '/rewritten/first', params: { slug: 'first' } }, { url: '/rewritten/first', params: { slug: 'first' } },
// Back to home { url: '/hub', params: {} },
{ url: '/', params: {} },
// Should go directly to "second" with no intermediate wrong params // Should go directly to "second" with no intermediate wrong params
{ url: '/rewritten/second', params: { slug: 'second' } }, { url: '/rewritten/second', params: { slug: 'second' } },
]) ])
...@@ -408,14 +419,23 @@ describe('optimistic-routing', () => { ...@@ -408,14 +419,23 @@ describe('optimistic-routing', () => {
const contentAlpha = await browser.elementById('rewrite-content') const contentAlpha = await browser.elementById('rewrite-content')
expect(await contentAlpha.getAttribute('data-content')).toBe('alpha') expect(await contentAlpha.getAttribute('data-content')).toBe('alpha')
// Step 2: Go back to home // Step 2: Navigate forward to /hub instead of using browser.back() to
await browser.back() // avoid BFCache restoring previously-opened accordions and triggering
await browser.elementById('rendered-route-history') // uncontrolled prefetches. See .claude/skills/router-act/SKILL.md.
await act(async () => {
const revealHub = await browser.elementByCss(
'input[data-link-accordion="/hub"]'
)
await revealHub.click()
const linkHub = await browser.elementByCss('a[href="/hub"]')
await linkHub.click()
})
await browser.elementById('hub-content')
// Step 3: Navigate to /search-rewrite?v=beta. // Step 3: From /hub, reveal /search-rewrite?v=beta.
// This link has prefetch={false} - if the route was incorrectly cached as // This link has prefetch={false} - if the router had reused step 1's
// predictable, we'd see "alpha" instead of "beta" because the static page // response as a prediction, we'd see "alpha" instead of "beta" because
// would be served from cache. // the static page would be served from cache.
await act(async () => { await act(async () => {
const revealBeta = await browser.elementByCss( const revealBeta = await browser.elementByCss(
'input[data-link-accordion="/search-rewrite?v=beta"]' 'input[data-link-accordion="/search-rewrite?v=beta"]'
...@@ -430,8 +450,9 @@ describe('optimistic-routing', () => { ...@@ -430,8 +450,9 @@ describe('optimistic-routing', () => {
await linkBeta.click() await linkBeta.click()
}) })
// Verify we see "beta", not "alpha" // Verify we see "beta", not "alpha".
// If this shows "alpha", the route was incorrectly using a cached pattern. // If this shows "alpha", the router incorrectly reused step 1's
// response as a prediction for /search-rewrite?v=beta.
const contentBeta = await browser.elementById('rewrite-content') const contentBeta = await browser.elementById('rewrite-content')
expect(await contentBeta.getAttribute('data-content')).toBe('beta') expect(await contentBeta.getAttribute('data-content')).toBe('beta')
}) })
......
import { Suspense } from 'react'
import { connection } from 'next/server'
async function DynamicProject() {
await connection()
return <h1>PROJECT PAGE</h1>
}
export default function ProjectPage() {
return (
<main>
<Suspense fallback={<div id="project-loading">Loading project...</div>}>
<DynamicProject />
</Suspense>
</main>
)
}
import { ReactNode } from 'react'
export default function TeamLayout({
children,
actions,
}: {
children: ReactNode
actions: ReactNode
}) {
return (
<>
<div>{children}</div>
<div>{actions}</div>
</>
)
}
import { Suspense } from 'react'
import { connection } from 'next/server'
import { LinkAccordion } from '../../../components/link-accordion'
async function DynamicOverview({
params,
}: {
params: Promise<{ teamSlug: string }>
}) {
await connection()
const { teamSlug } = await params
return (
<>
<h1>TEAM OVERVIEW</h1>
<ul>
<li>
<LinkAccordion href={`/${teamSlug}/myproject`}>Project</LinkAccordion>
</li>
</ul>
</>
)
}
export default function OverviewPage({
params,
}: {
params: Promise<{ teamSlug: string }>
}) {
return (
<main>
<Suspense fallback={<div id="overview-loading">Loading overview...</div>}>
<DynamicOverview params={params} />
</Suspense>
</main>
)
}
import { ReactNode } from 'react'
export default function RootLayout({ children }: { children: ReactNode }) {
return (
<html>
<body>{children}</body>
</html>
)
}
import { LinkAccordion } from '../components/link-accordion'
export default function Home() {
return (
<main>
<h1>HOME</h1>
<ul>
<li>
<LinkAccordion href="/myteam">Team</LinkAccordion>
</li>
</ul>
</main>
)
}
'use client'
import Link, { type LinkProps } from 'next/link'
import { useState } from 'react'
export function LinkAccordion({
href,
children,
prefetch,
}: {
href: LinkProps['href']
children: React.ReactNode
prefetch?: LinkProps['prefetch']
}) {
const [isVisible, setIsVisible] = useState(false)
return (
<>
<input
type="checkbox"
checked={isVisible}
onChange={() => setIsVisible(!isVisible)}
data-link-accordion={href}
/>
{isVisible ? (
<Link href={href} prefetch={prefetch}>
{children}
</Link>
) : (
`${children} (link is hidden)`
)}
</>
)
}
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {
cacheComponents: true,
experimental: {
optimisticRouting: true,
},
// The bug being regressed requires the response for /[teamSlug] to
// exercise its `@actions/[...catchAll]` parallel slot even though the
// canonical URL has no parts left to feed the catch-all. The simplest
// reproducer is a rewrite that maps the single-segment URL into a
// longer destination: the response describes a route shape that
// includes the catch-all slot, but the canonical URL is short enough
// that no parts reach the catch-all under normal routing.
async rewrites() {
return [
{
source: '/:teamSlug',
destination: '/:teamSlug/overview',
},
]
},
}
module.exports = nextConfig
import { nextTestSetup } from 'e2e-utils'
import { createRouterAct } from 'router-act'
describe('optimistic routing - parallel slot catch-all regression', () => {
const { next, isNextDev } = nextTestSetup({
files: __dirname,
})
if (isNextDev) {
// Optimistic routing is a production-build feature; in dev mode the
// router does not have complete information about which routes
// exist, so prediction is disabled.
test('skipped in dev mode', () => {})
return
}
// Regression test: a route with a parallel slot whose route is a
// required catch-all (e.g., /[teamSlug] with an
// `@actions/[...catchAll]` slot) used to poison route prediction.
// After visiting /myteam, navigating to a longer sibling URL like
// /myteam/myproject would reuse a prediction derived from the
// @actions slot's catch-all, resolving the URL to the team-page
// content even though /myteam/myproject is a separate route. The
// router skipped the server fetch entirely under the (incorrect)
// assumption that the URL was already known.
//
// Detection: visit /myteam (which would teach the bad prediction).
// Then click /myteam/myproject and wait for a response containing
// "PROJECT PAGE". With the bug, no server fetch happens, so "PROJECT
// PAGE" never arrives and the `act` waiter rejects. With the fix, no
// prediction matches /myteam/myproject and a real server fetch
// delivers the project page.
it('does not match a parallel-slot catch-all for an unrelated sibling URL', async () => {
let act: ReturnType<typeof createRouterAct>
const browser = await next.browser('/', {
beforePageLoad(page) {
act = createRouterAct(page)
},
})
// Reveal + click /myteam (a /[teamSlug] route). Its layout
// includes an `@actions/[...catchAll]` parallel slot. Visiting
// /myteam would, under the bug, teach the router a prediction
// that wrongly matches longer sibling URLs.
await act(
async () => {
const toggle = await browser.elementByCss(
'input[data-link-accordion="/myteam"]'
)
await toggle.click()
const link = await browser.elementByCss('a[href="/myteam"]')
await link.click()
},
{ includes: 'TEAM OVERVIEW' }
)
expect(await browser.elementByCss('h1').text()).toBe('TEAM OVERVIEW')
// Reveal + click /myteam/myproject — a sibling URL whose data is
// not already cached. Without the fix, the router uses the
// incorrect prediction taught by the previous step and skips the
// server fetch; "PROJECT PAGE" never arrives and `act` rejects.
// With the fix, no prediction matches /myteam/myproject and a
// real server fetch delivers the project page.
await act(
async () => {
const toggle = await browser.elementByCss(
'input[data-link-accordion="/myteam/myproject"]'
)
await toggle.click()
const link = await browser.elementByCss('a[href="/myteam/myproject"]')
await link.click()
},
{ includes: 'PROJECT PAGE' }
)
expect(await browser.elementByCss('h1').text()).toBe('PROJECT PAGE')
})
})
import { Suspense } from 'react'
import { connection } from 'next/server'
async function DynamicOverview({
params,
}: {
params: Promise<{ teamSlug: string }>
}) {
await connection()
const { teamSlug } = await params
return (
<h1 id="overview-page" data-slug={teamSlug}>
Overview: {teamSlug}
</h1>
)
}
// Static visible deeper segment. Used by the static-visible-null test:
// the rewrite /team-shorter → /team-shorter/overview produces a response
// that walks through this segment with no URL part to feed it.
export default function OverviewPage({
params,
}: {
params: Promise<{ teamSlug: string }>
}) {
return (
<main>
<Suspense fallback={<div id="team-loading">Loading team...</div>}>
<DynamicOverview params={params} />
</Suspense>
</main>
)
}
import { Suspense } from 'react'
import { connection } from 'next/server'
async function DynamicTeam({
params,
}: {
params: Promise<{ teamSlug: string }>
}) {
await connection()
const { teamSlug } = await params
return (
<h1 id="team-page" data-slug={teamSlug}>
Team: {teamSlug}
</h1>
)
}
export default function TeamPage({
params,
}: {
params: Promise<{ teamSlug: string }>
}) {
return (
<main>
<Suspense fallback={<div id="team-loading">Loading team...</div>}>
<DynamicTeam params={params} />
</Suspense>
</main>
)
}
// The real /featured static page. At runtime the `beforeFiles` rewrite
// hijacks /featured requests and sends them through /[teamSlug], so this
// page is never actually served — but its presence on the filesystem
// matters: it makes /featured a static sibling of /[teamSlug], which is
// what makes the rewrite case observable to the router (a request for
// /featured could not have arrived at /[teamSlug] under normal routing).
export default function FeaturedPage() {
return (
<main>
<h1 id="featured-page">FEATURED</h1>
</main>
)
}
import { ReactNode } from 'react'
export default function RootLayout({ children }: { children: ReactNode }) {
return (
<html>
<body>{children}</body>
</html>
)
}
import { LinkAccordion } from '../components/link-accordion'
export default function Home() {
return (
<main>
<h1>HOME</h1>
<ul>
{/* The four prefetch URLs below each arrive via a rewrite that
produces a route shape the URL would not naturally route to.
Each test reveals one of them (default prefetch=true) to
populate the router's prediction state, then clicks one of
the click-targets below to check that no incorrect prediction
was learned. */}
<li>
<LinkAccordion href="/myteam/garbage">Tree-shorter</LinkAccordion>
</li>
<li>
<LinkAccordion href="/featured">Static-sibling</LinkAccordion>
</li>
<li>
<LinkAccordion href="/team-shorter">
Static-visible-null
</LinkAccordion>
</li>
{/* Click target — prefetch={false} so revealing it fires no
request. Inside an act scope, the test clicks this and
asserts that no /[teamSlug] loading boundary appears
instantly (which would only happen if the router had a
cached prediction for it). */}
<li>
<LinkAccordion href="/yourteam" prefetch={false}>
Your team
</LinkAccordion>
</li>
</ul>
</main>
)
}
'use client'
import Link, { type LinkProps } from 'next/link'
import { useState } from 'react'
export function LinkAccordion({
href,
children,
prefetch,
}: {
href: LinkProps['href']
children: React.ReactNode
prefetch?: LinkProps['prefetch']
}) {
const [isVisible, setIsVisible] = useState(false)
return (
<>
<input
type="checkbox"
checked={isVisible}
onChange={() => setIsVisible(!isVisible)}
data-link-accordion={href}
/>
{isVisible ? (
<Link href={href} prefetch={prefetch}>
{children}
</Link>
) : (
`${children} (link is hidden)`
)}
</>
)
}
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {
cacheComponents: true,
experimental: {
optimisticRouting: true,
varyParams: true,
},
// Each rewrite below crafts one of the rewrite-affected response shapes
// we want to verify. They live in `beforeFiles` so they take precedence
// over the filesystem dynamic route — otherwise a 1-part URL like
// /featured would route directly to the filesystem (matched by the
// dynamic /[teamSlug]) without going through the rewrite, and we'd
// never observe the rewrite-affected response shape under test.
async rewrites() {
return {
beforeFiles: [
// Case "static-sibling": URL matches a known static sibling, but
// the rewrite sends it through the dynamic /[teamSlug] route.
{ source: '/featured', destination: '/some-team' },
// Case "static-visible-null": URL is shorter than the response,
// and the deeper segment is a visible static segment.
{ source: '/team-shorter', destination: '/team-shorter/overview' },
// Case "tree-shorter-than-url": URL is longer than the response.
{ source: '/:teamSlug/garbage', destination: '/:teamSlug' },
],
}
},
}
module.exports = nextConfig
import { nextTestSetup } from 'e2e-utils'
import { createRouterAct } from 'router-act'
describe('optimistic routing - rewrite detection regression', () => {
const { next, isNextDev } = nextTestSetup({
files: __dirname,
})
if (isNextDev) {
// Optimistic routing is a production-build feature; in dev mode the
// router does not have complete information about which routes
// exist, so prediction is disabled.
test('skipped in dev mode', () => {})
return
}
// The shape of every test below is the same:
//
// 1. Reveal a "prefetch" link whose URL arrives via a rewrite. The
// response describes a route shape the URL would not naturally
// route to. The router must not learn from this response as a
// prediction template — otherwise future navigations to other
// URLs of the same dynamic shape would receive an incorrect
// "free" prediction and could render a stale loading state
// instantly.
// 2. Reveal one of the click-target links (prefetch={false}).
// 3. Click it inside an act scope. With the bug, the router uses the
// rewrite-affected response as a prediction and the cached
// /[teamSlug] loading boundary appears synchronously. With the
// fix, no prediction is available and the loading boundary only
// appears once the server responds.
async function setupBrowser() {
let act: ReturnType<typeof createRouterAct>
const browser = await next.browser('/', {
beforePageLoad(page) {
act = createRouterAct(page)
},
})
return { browser, act: act! }
}
async function revealPrefetch(
browser: Awaited<ReturnType<typeof next.browser>>,
act: ReturnType<typeof createRouterAct>,
href: string
) {
await act(
async () => {
const toggle = await browser.elementByCss(
`input[data-link-accordion="${href}"]`
)
await toggle.click()
},
// Wait for the prefetch to complete by matching the loading
// boundary text in the response.
{ includes: 'Loading team' }
)
}
async function revealAndClick(
browser: Awaited<ReturnType<typeof next.browser>>,
act: ReturnType<typeof createRouterAct>,
href: string
) {
await act(async () => {
const toggle = await browser.elementByCss(
`input[data-link-accordion="${href}"]`
)
await toggle.click()
}, 'no-requests')
const link = await browser.elementByCss(`a[href="${href}"]`)
await act(async () => {
await link.click()
const teamLoading = await browser
.elementById('team-loading')
.catch(() => null)
expect(teamLoading).toBeNull()
})
}
it('does not predict from a rewrite that shortens the URL', async () => {
// The rewrite /myteam/garbage → /myteam produces a response whose
// route shape is one part shorter than the canonical URL.
const { browser, act } = await setupBrowser()
await revealPrefetch(browser, act, '/myteam/garbage')
await revealAndClick(browser, act, '/yourteam')
expect(
await (await browser.elementById('team-page')).getAttribute('data-slug')
).toBe('yourteam')
})
it('does not predict from a rewrite to a static-sibling URL', async () => {
// The rewrite /featured → /some-team produces a response that
// resolves through /[teamSlug] for a URL that, under normal
// routing, would have matched the static /featured page (a known
// sibling of /[teamSlug]).
const { browser, act } = await setupBrowser()
await revealPrefetch(browser, act, '/featured')
await revealAndClick(browser, act, '/yourteam')
expect(
await (await browser.elementById('team-page')).getAttribute('data-slug')
).toBe('yourteam')
})
it('does not predict when the rewrite hides a deeper static segment', async () => {
// The rewrite /team-shorter → /team-shorter/overview produces a
// response whose deeper visible-static segment (`overview`) has no
// URL part to match against under the canonical URL `/team-shorter`.
const { browser, act } = await setupBrowser()
await revealPrefetch(browser, act, '/team-shorter')
await revealAndClick(browser, act, '/yourteam')
expect(
await (await browser.elementById('team-page')).getAttribute('data-slug')
).toBe('yourteam')
})
// Note: a fourth case where the deeper segment is a regular dynamic
// (e.g. /team-dyn → /team-dyn/proj-1, response for /[teamSlug]/[project])
// does not have a regression test here. The pattern would land at the
// [project] trie node, which is only reachable via a 2-part click URL,
// and the cached page shell at that depth is rendered for the click
// regardless of whether the prediction was stored — so the loading
// boundary visible / not visible signal does not distinguish with-fix
// from without-fix at the UI level. The bailout for this case is still
// covered defensively by the same `paramType !== 'oc' && urlPart === null`
// check as case 2.
})
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment