Unverified Commit fdcb7386 authored by Andrew Clark's avatar Andrew Clark Committed by GitHub

Fix prefetch loop when a predicted route redirects (#98998)

A full or runtime prefetch (e.g. `router.prefetch(url, { kind: 'full'
})` or `<Link prefetch={true}>`) of a URL that matched a learned route
pattern but is redirected by the server (e.g. `/docs/changelog` →
`/changelog` while `/docs/alpha` renders `[collection]/[...slug]`)
caused the client to re-issue the same prefetch indefinitely, at
whatever rate the network allowed, until the tab was closed.

When the server contradicts a route prediction, the client is supposed
to remember that the URL shape can't be predicted and resolve it on the
server from then on. We were storing this information
(`hasDynamicRewrite`) in the wrong place: on the predicted route cache
entry, rather than the optimistic routing table that's used to generate
predictions.

The fix moves the `hasDynamicRewrite` flag to the optimistic routing
table so it applies to all subsequent optimistic routing predictions.

Fixes #98345.

<!-- NEXT_JS_LLM -->
parent a18b9a58
......@@ -34,7 +34,6 @@ import {
convertReusedFlightRouterStateToRouteTree,
readSegmentCacheEntryForNavigation,
waitForSegmentCacheEntry,
markRouteEntryAsDynamicRewrite,
invalidateRouteCacheEntries,
spawnStaticStageCacheWrite,
writeRuntimePrefetchStreamIntoCache,
......@@ -1712,10 +1711,15 @@ function dispatchRetryDueToTreeMismatch(
| FreshnessPolicy.RefreshAll
| FreshnessPolicy.HistoryTraversal
) {
// If the navigation used a route prediction, mark it as having a dynamic
// rewrite since it resulted in a mismatch.
// If the navigation used a route prediction, mark the node it was predicted
// from as having a dynamic rewrite since it resulted in a mismatch. A route
// entry the server resolved has nothing to mark: nothing was predicted
// from it.
if (routeCacheEntry !== null) {
markRouteEntryAsDynamicRewrite(routeCacheEntry)
const predictedFrom = routeCacheEntry.predictedFrom
if (predictedFrom !== null) {
predictedFrom.hasDynamicRewrite = true
}
} else if (seed !== null) {
// Even without a direct reference to the route cache entry, we can still
// mark the route as having a dynamic rewrite by traversing the known route
......@@ -1740,8 +1744,9 @@ function dispatchRetryDueToTreeMismatch(
}
}
// Invalidate all route cache entries. Other entries may have been derived
// from the template before we knew it had a dynamic rewrite. This also
// Invalidate all route cache entries. If the navigation used a route entry
// the server resolved, its tree is what the server just contradicted, so
// the retry must re-fetch it rather than navigate with it again. This also
// triggers re-prefetching of visible links.
invalidateRouteCacheEntries(retryNextUrl, baseTree)
......
......@@ -130,6 +130,16 @@ type KnownRoutePartBase = {
// tree, like we do for static siblings, and attempting to match both.
hasConflictingDynamicChildren: boolean
// True once a prediction made from this node's pattern was contradicted by
// the server: the URL has a rewrite or redirect that behaves dynamically
// (e.g. /docs/changelog redirects to /changelog while /docs/alpha renders
// [collection]/[...slug]). Since rewrite behavior can vary by param value,
// no URL terminating at this node can be predicted safely, so matching
// bails out to server resolution. Lives on the node rather than on the
// pattern entry so it survives the pattern being evicted (by staleness or
// cache invalidation) and re-learned from a later response.
hasDynamicRewrite: boolean
// TODO: For prefix rewrite support. When true, this part may not appear in
// the candidate URL because it was injected by a rewrite. Today, discovery
// refuses to store a pattern for such routes (see the cache key comparison
......@@ -151,7 +161,7 @@ type KnownRoutePartWithDynamicChild = KnownRoutePartBase & {
dynamicChildParamType: DynamicParamTypesShort
}
type KnownRoutePart =
export type KnownRoutePart =
| KnownRoutePartWithoutDynamicChild
| KnownRoutePartWithDynamicChild
......@@ -197,6 +207,7 @@ function createEmptyPart(): KnownRoutePart {
dynamicChildParamType: null,
pattern: null,
hasConflictingDynamicChildren: false,
hasDynamicRewrite: false,
}
}
......@@ -214,9 +225,9 @@ let knownRouteTreeRoot: KnownRoutePart = createEmptyPart()
* When pendingEntry is provided, it's fulfilled and used. When null, an entry
* is created and inserted into the route cache map.
*
* When hasDynamicRewrite is true, the route entry is marked as having a
* dynamic rewrite, which prevents it from being used as a template for future
* predictions. This is set when we detect a mismatch between what we predicted
* When hasDynamicRewrite is true, the node the route terminates at is marked
* as having a dynamic rewrite, which prevents URLs of its shape from being
* predicted. This is set when we detect a mismatch between what we predicted
* and what the server returned.
*
* Returns the fulfilled route cache entry.
......@@ -249,9 +260,6 @@ export function discoverKnownRoute(
canonicalUrl,
supportsPerSegmentPrefetching
)
if (hasDynamicRewrite) {
fulfilledEntry.hasDynamicRewrite = true
}
// Populate the known route tree (handles rewrite detection internally).
// The entry is already in the cache; this just stores it as a pattern
// if the URL matches the route structure.
......@@ -680,14 +688,15 @@ function discoverKnownRoutePart(
)
}
// Reached a page node. Create/get the route cache entry and store as a
// pattern. First, check if there's already a pattern for this route.
// Reached a page node: the URL terminates here.
if (hasDynamicRewrite) {
knownRoutePart.hasDynamicRewrite = true
}
// Create/get the route cache entry and store as a pattern. First, check if
// there's already a pattern for this route.
const existingPattern = readPattern(now, knownRoutePart)
if (existingPattern !== null) {
// If this route has a dynamic rewrite, mark the existing pattern.
if (hasDynamicRewrite) {
existingPattern.hasDynamicRewrite = true
}
return existingPattern
}
......@@ -712,10 +721,6 @@ function discoverKnownRoutePart(
)
}
if (hasDynamicRewrite) {
entry.hasDynamicRewrite = true
}
// Store as pattern
knownRoutePart.pattern = entry
return entry
......@@ -800,13 +805,12 @@ export function matchKnownRoute(
reifiedTree.prefetchHints
)
// Create a synthetic (predicted) entry and store it as the new pattern.
//
// Why replace the pattern? We intentionally update the pattern with this
// synthetic entry so that if our prediction was wrong (server returns a
// different pathname due to dynamic rewrite), the entry gets marked with
// hasDynamicRewrite. Future predictions for this route will see the flag
// and bail out to server resolution instead of making the same mistake.
// Create a synthetic (predicted) entry. It's not inserted into the route
// cache; it lives only as long as the task or navigation that read it. It
// records the node it was predicted from so that if the prediction turns
// out to be wrong (the server renders a different tree due to a dynamic
// rewrite), the node can be marked and the shape is never predicted again
// (see KnownRoutePart.hasDynamicRewrite).
const syntheticEntry: FulfilledRouteCacheEntry = {
canonicalUrl: pathname + search,
status: EntryStatus.Fulfilled,
......@@ -815,7 +819,7 @@ export function matchKnownRoute(
metadata: reifiedMetadata,
couldBeIntercepted: pattern.couldBeIntercepted,
supportsPerSegmentPrefetching: pattern.supportsPerSegmentPrefetching,
hasDynamicRewrite: false,
predictedFrom: matchedPart,
renderedSearch: search,
ref: null,
size: pattern.size,
......@@ -823,15 +827,13 @@ export function matchKnownRoute(
version: pattern.version,
}
matchedPart.pattern = syntheticEntry
return syntheticEntry
}
/**
* Result of a successful match: the matched tree node and its pattern.
* We return both because the caller needs to update the pattern after
* creating a synthetic entry (for dynamic rewrite detection).
* We return both because the synthetic entry records the node it was
* predicted from (for dynamic rewrite detection).
*/
type KnownRouteMatch = {
part: KnownRoutePart
......@@ -867,9 +869,9 @@ function matchKnownRoutePart(
// /blog/featured would incorrectly match /blog/[slug].
if (part.staticChildren === null) {
// The only safe match is a direct pattern when no URL parts remain.
if (urlPart === null) {
if (urlPart === null && !part.hasDynamicRewrite) {
const pattern = readPattern(now, part)
if (pattern !== null && !pattern.hasDynamicRewrite) {
if (pattern !== null) {
return { part, pattern }
}
}
......@@ -919,16 +921,16 @@ function matchKnownRoutePart(
const dynamicPart = part.dynamicChild
const paramName = part.dynamicChildParamName
const paramType = part.dynamicChildParamType
const dynamicPattern = readPattern(now, dynamicPart)
// Catch-alls terminate at the dynamic child, so a dynamic rewrite marked
// there rules out predicting any URL that would end in it.
const dynamicPattern = dynamicPart.hasDynamicRewrite
? null
: readPattern(now, dynamicPart)
switch (paramType) {
case 'c':
// Required catch-all [...param]: consumes 1+ URL parts
if (
dynamicPattern !== null &&
!dynamicPattern.hasDynamicRewrite &&
urlPart !== null
) {
if (dynamicPattern !== null && urlPart !== null) {
resolvedParams.set(
paramName,
pathnameParts.slice(partIndex).map(canonicalizeURLPart).join('/')
......@@ -938,7 +940,7 @@ function matchKnownRoutePart(
break
case 'oc': {
// Optional catch-all [[...param]]: consumes 0+ URL parts
if (dynamicPattern !== null && !dynamicPattern.hasDynamicRewrite) {
if (dynamicPattern !== null) {
if (urlPart !== null) {
resolvedParams.set(
paramName,
......@@ -948,8 +950,10 @@ function matchKnownRoutePart(
}
// urlPart is null - can match with zero parts, but a direct pattern
// (e.g., page.tsx alongside [[...param]]) takes precedence.
const directPattern = readPattern(now, part)
if (directPattern === null || directPattern.hasDynamicRewrite) {
const directPattern = part.hasDynamicRewrite
? null
: readPattern(now, part)
if (directPattern === null) {
resolvedParams.set(paramName, '')
return { part: dynamicPart, pattern: dynamicPattern }
}
......@@ -990,9 +994,9 @@ function matchKnownRoutePart(
// No children matched. If we've consumed all URL parts, check for a direct
// pattern at this node (the route terminates here).
if (urlPart === null) {
if (urlPart === null && !part.hasDynamicRewrite) {
const pattern = readPattern(now, part)
if (pattern !== null && !pattern.hasDynamicRewrite) {
if (pattern !== null) {
return { part, pattern }
}
}
......
import { connection } from 'next/server'
import { Suspense } from 'react'
import { LinkAccordion } from '../../../components/link-accordion'
import { PrefetchButton } from '../../../components/prefetch-button'
type Params = Promise<{ collection: string; slug: string[] }>
// A sidebar of links under the same route, like the reported site.
const SIDEBAR = ['beta', 'gamma', 'delta', 'epsilon', 'zeta', 'eta']
// A static shell with one dynamic hole, so a full prefetch of a page under
// this route needs a runtime request rather than just the static segment data.
export default function DocsPage({ params }: { params: Params }) {
return (
<>
<h1>A page under [collection]/[...slug]</h1>
<Suspense fallback={<p>Loading docs...</p>}>
<Body params={params} />
</Suspense>
<ul>
{SIDEBAR.map((slug) => (
<li key={slug}>
<LinkAccordion href={`/docs/${slug}`}>/docs/{slug}</LinkAccordion>
</li>
))}
</ul>
<PrefetchButton href="/docs/changelog" />
</>
)
}
async function Body({ params }: { params: Params }) {
const { collection, slug } = await params
await connection()
return (
<p id="docs-params">
{collection}/{slug.join('/')}
</p>
)
}
export default function ChangelogPage() {
return (
<>
<h1 id="changelog-page">Changelog</h1>
<p>A static route. /docs/changelog redirects here with a 308.</p>
</>
)
}
import type { ReactNode } from 'react'
export default function RootLayout({ children }: { children: ReactNode }) {
return (
<html lang="en">
<body>{children}</body>
</html>
)
}
'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']
}) {
// This component controls when prefetching happens by hiding the Link until
// the checkbox is clicked. Prefetch triggers when the Link enters the viewport.
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)`
)}
</>
)
}
'use client'
import { useRouter } from 'next/navigation'
type Router = ReturnType<typeof useRouter>
type PrefetchOptions = Parameters<Router['prefetch']>[1]
export function PrefetchButton({ href }: { href: string }) {
const router = useRouter()
return (
<button
data-prefetch-full={href}
onClick={() =>
router.prefetch(href, {
kind: 'full' as NonNullable<PrefetchOptions>['kind'],
})
}
>
router.prefetch({href}, {'{ kind: "full" }'})
</button>
)
}
/** @type {import('next').NextConfig} */
module.exports = {
cacheComponents: true,
partialPrefetching: true,
async redirects() {
return [
{
// A URL under the dynamic [collection]/[...slug] route permanently
// redirects to a static route whose tree has a different shape.
source: '/docs/changelog',
destination: '/changelog',
permanent: true,
},
]
},
}
import { nextTestSetup } from 'e2e-utils'
import { createRouterAct } from 'router-act'
import type { Page, Request } from 'playwright'
// Prefetching is disabled in dev, and route prediction only happens in a
// production build.
// @force-gate prefetching
describe('optimistic routing - redirect prefetch loop regression', () => {
const { next } = nextTestSetup({
files: __dirname,
})
it('does not loop when a full prefetch of a predicted route is redirected', async () => {
let act: ReturnType<typeof createRouterAct>
// Pre-fix, a single router.prefetch of the redirecting URL made the router
// request that URL again and again, indefinitely. Under that loop, `act`'s
// flush keeps observing new requests and never settles — so instead of
// waiting for a jest timeout, each `act` is raced against a promise that
// rejects once the URL has been requested more times than any correct
// sequence needs. This fails fast with a legible error on a buggy build
// and is a no-op on a correct one.
const REDIRECTING_URL = '/docs/changelog'
const MAX_REQUESTS_TO_REDIRECTING_URL = 5
let requestsToRedirectingUrl = 0
let rejectOnLoop!: (error: Error) => void
const loopDetected = new Promise<never>((_, reject) => {
rejectOnLoop = reject
})
// Mark the rejection as handled in case it fires while no race is
// pending; the final assertions still catch it.
loopDetected.catch(() => {})
async function actExpectingNoLoop<T>(
scope: () => Promise<T> | T,
config?: Parameters<typeof act>[1]
): Promise<unknown> {
const actPromise = act(scope, config)
// If the race rejects first, `act` keeps running until the page is torn
// down; swallow its eventual rejection so it doesn't surface as an
// unhandled promise rejection.
actPromise.catch(() => {})
return Promise.race([actPromise, loopDetected])
}
const browser = await next.browser('/docs/alpha', {
beforePageLoad(page: Page) {
act = createRouterAct(page)
page.on('request', (request: Request) => {
if (new URL(request.url()).pathname !== REDIRECTING_URL) {
return
}
requestsToRedirectingUrl++
if (requestsToRedirectingUrl > MAX_REQUESTS_TO_REDIRECTING_URL) {
rejectOnLoop(
new Error(
`${REDIRECTING_URL} was requested more than ` +
`${MAX_REQUESTS_TO_REDIRECTING_URL} times after a single ` +
'prefetch. The URL redirects, so one attempt is enough to ' +
'learn that; a client that keeps asking is looping.'
)
)
}
})
},
})
// The initial page load renders /docs/alpha via [collection]/[...slug],
// which is when the client learns the pattern for that URL shape.
expect(await browser.elementById('docs-params').text()).toBe('docs/alpha')
// Reveal the sidebar links. Their routes are predicted from the pattern
// and their shell is already cached, so revealing them makes no requests —
// which is also what proves the pattern was learned.
//
// They matter for the pre-fix behavior: the redirected response
// invalidated the whole route cache, and the visible links re-fetched
// their route trees ahead of the prefetch task, re-learning the pattern
// the task then predicted the redirecting URL from again.
await actExpectingNoLoop(async () => {
for (const slug of ['beta', 'gamma', 'delta', 'epsilon', 'zeta', 'eta']) {
const toggle = await browser.elementByCss(
`input[data-link-accordion="/docs/${slug}"]`
)
await toggle.click()
}
}, 'no-requests')
// Imperatively prefetch the redirecting URL with `kind: 'full'`. The URL
// matches the learned pattern, so the client predicts its route; the
// server redirects to /changelog and renders a different tree. The client
// must take the redirected response, resolve the route for real, and
// settle. `act` returning at all proves the prefetch queue settled
// instead of looping.
await actExpectingNoLoop(async () => {
const button = await browser.elementByCss(
`button[data-prefetch-full="${REDIRECTING_URL}"]`
)
await button.click()
})
// The prefetch completed: navigating to the redirecting URL is served
// entirely from the cache, and lands on the redirect target.
await actExpectingNoLoop(async () => {
await browser.eval(
`window.next.router.push(${JSON.stringify(REDIRECTING_URL)})`
)
}, 'no-requests')
expect(await browser.elementById('changelog-page').text()).toBe('Changelog')
expect(await browser.url()).toBe(`${next.url}/changelog`)
})
})
......@@ -705,6 +705,79 @@ ${fulfilled.body}
} else {
if (route !== null) {
const request = route.request()
// When fulfilling a redirect, for some reason, the page.route()
// handler installed earlier will not intercept the redirect
// request. Install an event listener to wait for the redirected
// request to finish. This works for this case because we don't
// need to modify or delay the response; we only need to observe
// when it has finished. The listener must be installed before
// fulfilling: the browser can issue the redirected request as
// soon as it receives the redirect, and a listener installed
// afterwards would miss it and latch onto whatever router
// request happens next — a request this act scope is itself
// holding, which would never receive a response.
// TODO: Because this request cannot be intercepted, it's
// incompatible with the "block" option. I haven't yet figured out
// a strategy to make that work. In the meantime, attempting to
// write a test that blocks a redirect will result in an error
// (see error above).
const redirectedRequestFinished =
fulfilled.status === 307 || fulfilled.status === 308
? new Promise<void>((resolve, reject) => {
const handleRequest = (req: Playwright.Request) => {
if (req.redirectedFrom()?.url() !== request.url()) {
return
}
page.off('request', handleRequest)
const handleResponse = (res: Playwright.Response) => {
if (res.url() === req.url()) {
batch.pendingRequests.add({
url: req.url(),
route: null,
result: (async () => {
return {
// For redirects, body may not be available,
// so catch the error and return an empty
// string.
text: await res.text().catch(() => ''),
body: await res
.body()
.catch(() => Buffer.from('')),
headers: res.headers(),
status: res.status(),
}
})(),
didProcess: false,
// The target of a redirect is a navigation, not
// an App Shell prefetch.
isAppShell: false,
// Navigations have no prefetch kind.
kind: undefined,
})
batch.didReceiveRouterRequest = true
page.off('response', handleResponse)
page.off('requestfailed', handleFailure)
resolve()
}
}
const handleFailure = (
failedReq: Playwright.Request
) => {
if (failedReq.url() === req.url()) {
page.off('response', handleResponse)
page.off('requestfailed', handleFailure)
error.message = `Request failed: ${failedReq.failure()?.errorText || 'Unknown error'}\n\nURL: ${req.url()}`
reject(error)
}
}
page.on('response', handleResponse)
page.on('requestfailed', handleFailure)
}
page.on('request', handleRequest)
})
: null
await route.fulfill({
body: fulfilled.body,
headers: fulfilled.headers,
......@@ -718,63 +791,11 @@ ${fulfilled.body}
await browserResponse.finished()
}
}
}
}
if (fulfilled.status === 307 || fulfilled.status === 308) {
// When fulfilling a redirect, for some reason, the page.route()
// handler installed earlier will not intercept the
// redirect request. Install a one-off event listener to wait for
// the redirected request to finish. This works for this case
// because we don't need to modify to delay the response; we only
// need to observe when it has finished.
// TODO: Because this request cannot be intercepted, it's
// incompatible with the "block" option. I haven't yet figured out
// a strategy to make that work. In the meantime, attempting to
// write a test that blocks a redirect will result in an error
// (see error above).
await new Promise<void>((resolve, reject) => {
page.once('request', (req) => {
const handleResponse = (res: Playwright.Response) => {
if (res.url() === req.url()) {
batch.pendingRequests.add({
url: req.url(),
route: null,
result: (async () => {
return {
// For redirects, body may not be available, so catch
// the error and return an empty string.
text: await res.text().catch(() => ''),
body: await res.body().catch(() => Buffer.from('')),
headers: res.headers(),
status: res.status(),
}
})(),
didProcess: false,
// The target of a redirect is a navigation, not an App
// Shell prefetch.
isAppShell: false,
// Navigations have no prefetch kind.
kind: undefined,
})
batch.didReceiveRouterRequest = true
page.off('response', handleResponse)
page.off('requestfailed', handleFailure)
resolve()
}
}
const handleFailure = (failedReq: Playwright.Request) => {
if (failedReq.url() === req.url()) {
page.off('response', handleResponse)
page.off('requestfailed', handleFailure)
error.message = `Request failed: ${failedReq.failure()?.errorText || 'Unknown error'}\n\nURL: ${req.url()}`
reject(error)
}
}
page.on('response', handleResponse)
page.on('requestfailed', handleFailure)
})
})
if (redirectedRequestFinished !== null) {
await redirectedRequestFinished
}
}
}
}
......
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