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 { ...@@ -34,7 +34,6 @@ import {
convertReusedFlightRouterStateToRouteTree, convertReusedFlightRouterStateToRouteTree,
readSegmentCacheEntryForNavigation, readSegmentCacheEntryForNavigation,
waitForSegmentCacheEntry, waitForSegmentCacheEntry,
markRouteEntryAsDynamicRewrite,
invalidateRouteCacheEntries, invalidateRouteCacheEntries,
spawnStaticStageCacheWrite, spawnStaticStageCacheWrite,
writeRuntimePrefetchStreamIntoCache, writeRuntimePrefetchStreamIntoCache,
...@@ -1712,10 +1711,15 @@ function dispatchRetryDueToTreeMismatch( ...@@ -1712,10 +1711,15 @@ function dispatchRetryDueToTreeMismatch(
| FreshnessPolicy.RefreshAll | FreshnessPolicy.RefreshAll
| FreshnessPolicy.HistoryTraversal | FreshnessPolicy.HistoryTraversal
) { ) {
// If the navigation used a route prediction, mark it as having a dynamic // If the navigation used a route prediction, mark the node it was predicted
// rewrite since it resulted in a mismatch. // 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) { if (routeCacheEntry !== null) {
markRouteEntryAsDynamicRewrite(routeCacheEntry) const predictedFrom = routeCacheEntry.predictedFrom
if (predictedFrom !== null) {
predictedFrom.hasDynamicRewrite = true
}
} else if (seed !== null) { } else if (seed !== null) {
// Even without a direct reference to the route cache entry, we can still // 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 // mark the route as having a dynamic rewrite by traversing the known route
...@@ -1740,8 +1744,9 @@ function dispatchRetryDueToTreeMismatch( ...@@ -1740,8 +1744,9 @@ function dispatchRetryDueToTreeMismatch(
} }
} }
// Invalidate all route cache entries. Other entries may have been derived // Invalidate all route cache entries. If the navigation used a route entry
// from the template before we knew it had a dynamic rewrite. This also // 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. // triggers re-prefetching of visible links.
invalidateRouteCacheEntries(retryNextUrl, baseTree) invalidateRouteCacheEntries(retryNextUrl, baseTree)
......
...@@ -130,6 +130,16 @@ type KnownRoutePartBase = { ...@@ -130,6 +130,16 @@ type KnownRoutePartBase = {
// tree, like we do for static siblings, and attempting to match both. // tree, like we do for static siblings, and attempting to match both.
hasConflictingDynamicChildren: boolean 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 // 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 // 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 // refuses to store a pattern for such routes (see the cache key comparison
...@@ -151,7 +161,7 @@ type KnownRoutePartWithDynamicChild = KnownRoutePartBase & { ...@@ -151,7 +161,7 @@ type KnownRoutePartWithDynamicChild = KnownRoutePartBase & {
dynamicChildParamType: DynamicParamTypesShort dynamicChildParamType: DynamicParamTypesShort
} }
type KnownRoutePart = export type KnownRoutePart =
| KnownRoutePartWithoutDynamicChild | KnownRoutePartWithoutDynamicChild
| KnownRoutePartWithDynamicChild | KnownRoutePartWithDynamicChild
...@@ -197,6 +207,7 @@ function createEmptyPart(): KnownRoutePart { ...@@ -197,6 +207,7 @@ function createEmptyPart(): KnownRoutePart {
dynamicChildParamType: null, dynamicChildParamType: null,
pattern: null, pattern: null,
hasConflictingDynamicChildren: false, hasConflictingDynamicChildren: false,
hasDynamicRewrite: false,
} }
} }
...@@ -214,9 +225,9 @@ let knownRouteTreeRoot: KnownRoutePart = createEmptyPart() ...@@ -214,9 +225,9 @@ let knownRouteTreeRoot: KnownRoutePart = createEmptyPart()
* When pendingEntry is provided, it's fulfilled and used. When null, an entry * When pendingEntry is provided, it's fulfilled and used. When null, an entry
* is created and inserted into the route cache map. * is created and inserted into the route cache map.
* *
* When hasDynamicRewrite is true, the route entry is marked as having a * When hasDynamicRewrite is true, the node the route terminates at is marked
* dynamic rewrite, which prevents it from being used as a template for future * as having a dynamic rewrite, which prevents URLs of its shape from being
* predictions. This is set when we detect a mismatch between what we predicted * predicted. This is set when we detect a mismatch between what we predicted
* and what the server returned. * and what the server returned.
* *
* Returns the fulfilled route cache entry. * Returns the fulfilled route cache entry.
...@@ -249,9 +260,6 @@ export function discoverKnownRoute( ...@@ -249,9 +260,6 @@ export function discoverKnownRoute(
canonicalUrl, canonicalUrl,
supportsPerSegmentPrefetching supportsPerSegmentPrefetching
) )
if (hasDynamicRewrite) {
fulfilledEntry.hasDynamicRewrite = true
}
// Populate the known route tree (handles rewrite detection internally). // Populate the known route tree (handles rewrite detection internally).
// The entry is already in the cache; this just stores it as a pattern // The entry is already in the cache; this just stores it as a pattern
// if the URL matches the route structure. // if the URL matches the route structure.
...@@ -680,14 +688,15 @@ function discoverKnownRoutePart( ...@@ -680,14 +688,15 @@ function discoverKnownRoutePart(
) )
} }
// Reached a page node. Create/get the route cache entry and store as a // Reached a page node: the URL terminates here.
// 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) { if (hasDynamicRewrite) {
existingPattern.hasDynamicRewrite = true 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) {
return existingPattern return existingPattern
} }
...@@ -712,10 +721,6 @@ function discoverKnownRoutePart( ...@@ -712,10 +721,6 @@ function discoverKnownRoutePart(
) )
} }
if (hasDynamicRewrite) {
entry.hasDynamicRewrite = true
}
// Store as pattern // Store as pattern
knownRoutePart.pattern = entry knownRoutePart.pattern = entry
return entry return entry
...@@ -800,13 +805,12 @@ export function matchKnownRoute( ...@@ -800,13 +805,12 @@ export function matchKnownRoute(
reifiedTree.prefetchHints reifiedTree.prefetchHints
) )
// Create a synthetic (predicted) entry and store it as the new pattern. // 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
// Why replace the pattern? We intentionally update the pattern with this // records the node it was predicted from so that if the prediction turns
// synthetic entry so that if our prediction was wrong (server returns a // out to be wrong (the server renders a different tree due to a dynamic
// different pathname due to dynamic rewrite), the entry gets marked with // rewrite), the node can be marked and the shape is never predicted again
// hasDynamicRewrite. Future predictions for this route will see the flag // (see KnownRoutePart.hasDynamicRewrite).
// and bail out to server resolution instead of making the same mistake.
const syntheticEntry: FulfilledRouteCacheEntry = { const syntheticEntry: FulfilledRouteCacheEntry = {
canonicalUrl: pathname + search, canonicalUrl: pathname + search,
status: EntryStatus.Fulfilled, status: EntryStatus.Fulfilled,
...@@ -815,7 +819,7 @@ export function matchKnownRoute( ...@@ -815,7 +819,7 @@ export function matchKnownRoute(
metadata: reifiedMetadata, metadata: reifiedMetadata,
couldBeIntercepted: pattern.couldBeIntercepted, couldBeIntercepted: pattern.couldBeIntercepted,
supportsPerSegmentPrefetching: pattern.supportsPerSegmentPrefetching, supportsPerSegmentPrefetching: pattern.supportsPerSegmentPrefetching,
hasDynamicRewrite: false, predictedFrom: matchedPart,
renderedSearch: search, renderedSearch: search,
ref: null, ref: null,
size: pattern.size, size: pattern.size,
...@@ -823,15 +827,13 @@ export function matchKnownRoute( ...@@ -823,15 +827,13 @@ export function matchKnownRoute(
version: pattern.version, version: pattern.version,
} }
matchedPart.pattern = syntheticEntry
return syntheticEntry return syntheticEntry
} }
/** /**
* Result of a successful match: the matched tree node and its pattern. * Result of a successful match: the matched tree node and its pattern.
* We return both because the caller needs to update the pattern after * We return both because the synthetic entry records the node it was
* creating a synthetic entry (for dynamic rewrite detection). * predicted from (for dynamic rewrite detection).
*/ */
type KnownRouteMatch = { type KnownRouteMatch = {
part: KnownRoutePart part: KnownRoutePart
...@@ -867,9 +869,9 @@ function matchKnownRoutePart( ...@@ -867,9 +869,9 @@ function matchKnownRoutePart(
// /blog/featured would incorrectly match /blog/[slug]. // /blog/featured would incorrectly match /blog/[slug].
if (part.staticChildren === null) { if (part.staticChildren === null) {
// The only safe match is a direct pattern when no URL parts remain. // 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) const pattern = readPattern(now, part)
if (pattern !== null && !pattern.hasDynamicRewrite) { if (pattern !== null) {
return { part, pattern } return { part, pattern }
} }
} }
...@@ -919,16 +921,16 @@ function matchKnownRoutePart( ...@@ -919,16 +921,16 @@ function matchKnownRoutePart(
const dynamicPart = part.dynamicChild const dynamicPart = part.dynamicChild
const paramName = part.dynamicChildParamName const paramName = part.dynamicChildParamName
const paramType = part.dynamicChildParamType 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) { switch (paramType) {
case 'c': case 'c':
// Required catch-all [...param]: consumes 1+ URL parts // Required catch-all [...param]: consumes 1+ URL parts
if ( if (dynamicPattern !== null && urlPart !== null) {
dynamicPattern !== null &&
!dynamicPattern.hasDynamicRewrite &&
urlPart !== null
) {
resolvedParams.set( resolvedParams.set(
paramName, paramName,
pathnameParts.slice(partIndex).map(canonicalizeURLPart).join('/') pathnameParts.slice(partIndex).map(canonicalizeURLPart).join('/')
...@@ -938,7 +940,7 @@ function matchKnownRoutePart( ...@@ -938,7 +940,7 @@ function matchKnownRoutePart(
break break
case 'oc': { case 'oc': {
// Optional catch-all [[...param]]: consumes 0+ URL parts // Optional catch-all [[...param]]: consumes 0+ URL parts
if (dynamicPattern !== null && !dynamicPattern.hasDynamicRewrite) { if (dynamicPattern !== null) {
if (urlPart !== null) { if (urlPart !== null) {
resolvedParams.set( resolvedParams.set(
paramName, paramName,
...@@ -948,8 +950,10 @@ function matchKnownRoutePart( ...@@ -948,8 +950,10 @@ function matchKnownRoutePart(
} }
// urlPart is null - can match with zero parts, but a direct pattern // urlPart is null - can match with zero parts, but a direct pattern
// (e.g., page.tsx alongside [[...param]]) takes precedence. // (e.g., page.tsx alongside [[...param]]) takes precedence.
const directPattern = readPattern(now, part) const directPattern = part.hasDynamicRewrite
if (directPattern === null || directPattern.hasDynamicRewrite) { ? null
: readPattern(now, part)
if (directPattern === null) {
resolvedParams.set(paramName, '') resolvedParams.set(paramName, '')
return { part: dynamicPart, pattern: dynamicPattern } return { part: dynamicPart, pattern: dynamicPattern }
} }
...@@ -990,9 +994,9 @@ function matchKnownRoutePart( ...@@ -990,9 +994,9 @@ function matchKnownRoutePart(
// No children matched. If we've consumed all URL parts, check for a direct // No children matched. If we've consumed all URL parts, check for a direct
// pattern at this node (the route terminates here). // pattern at this node (the route terminates here).
if (urlPart === null) { if (urlPart === null && !part.hasDynamicRewrite) {
const pattern = readPattern(now, part) const pattern = readPattern(now, part)
if (pattern !== null && !pattern.hasDynamicRewrite) { if (pattern !== null) {
return { part, pattern } 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,36 +705,31 @@ ${fulfilled.body} ...@@ -705,36 +705,31 @@ ${fulfilled.body}
} else { } else {
if (route !== null) { if (route !== null) {
const request = route.request() const request = route.request()
await route.fulfill({
body: fulfilled.body,
headers: fulfilled.headers,
status: fulfilled.status,
})
const browserResponse = await request.response()
if (browserResponse !== null) {
// For error responses (>= 400), the browser may not consume the body
// in the same way, so we skip waiting for finished() to avoid hanging
if (fulfilled.status < 400) {
await browserResponse.finished()
}
}
}
}
if (fulfilled.status === 307 || fulfilled.status === 308) {
// When fulfilling a redirect, for some reason, the page.route() // When fulfilling a redirect, for some reason, the page.route()
// handler installed earlier will not intercept the // handler installed earlier will not intercept the redirect
// redirect request. Install a one-off event listener to wait for // request. Install an event listener to wait for the redirected
// the redirected request to finish. This works for this case // request to finish. This works for this case because we don't
// because we don't need to modify to delay the response; we only // need to modify or delay the response; we only need to observe
// need to observe when it has finished. // 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 // TODO: Because this request cannot be intercepted, it's
// incompatible with the "block" option. I haven't yet figured out // incompatible with the "block" option. I haven't yet figured out
// a strategy to make that work. In the meantime, attempting to // a strategy to make that work. In the meantime, attempting to
// write a test that blocks a redirect will result in an error // write a test that blocks a redirect will result in an error
// (see error above). // (see error above).
await new Promise<void>((resolve, reject) => { const redirectedRequestFinished =
page.once('request', (req) => { 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) => { const handleResponse = (res: Playwright.Response) => {
if (res.url() === req.url()) { if (res.url() === req.url()) {
batch.pendingRequests.add({ batch.pendingRequests.add({
...@@ -742,17 +737,20 @@ ${fulfilled.body} ...@@ -742,17 +737,20 @@ ${fulfilled.body}
route: null, route: null,
result: (async () => { result: (async () => {
return { return {
// For redirects, body may not be available, so catch // For redirects, body may not be available,
// the error and return an empty string. // so catch the error and return an empty
// string.
text: await res.text().catch(() => ''), text: await res.text().catch(() => ''),
body: await res.body().catch(() => Buffer.from('')), body: await res
.body()
.catch(() => Buffer.from('')),
headers: res.headers(), headers: res.headers(),
status: res.status(), status: res.status(),
} }
})(), })(),
didProcess: false, didProcess: false,
// The target of a redirect is a navigation, not an App // The target of a redirect is a navigation, not
// Shell prefetch. // an App Shell prefetch.
isAppShell: false, isAppShell: false,
// Navigations have no prefetch kind. // Navigations have no prefetch kind.
kind: undefined, kind: undefined,
...@@ -763,7 +761,9 @@ ${fulfilled.body} ...@@ -763,7 +761,9 @@ ${fulfilled.body}
resolve() resolve()
} }
} }
const handleFailure = (failedReq: Playwright.Request) => { const handleFailure = (
failedReq: Playwright.Request
) => {
if (failedReq.url() === req.url()) { if (failedReq.url() === req.url()) {
page.off('response', handleResponse) page.off('response', handleResponse)
page.off('requestfailed', handleFailure) page.off('requestfailed', handleFailure)
...@@ -773,8 +773,29 @@ ${fulfilled.body} ...@@ -773,8 +773,29 @@ ${fulfilled.body}
} }
page.on('response', handleResponse) page.on('response', handleResponse)
page.on('requestfailed', handleFailure) page.on('requestfailed', handleFailure)
}
page.on('request', handleRequest)
}) })
: null
await route.fulfill({
body: fulfilled.body,
headers: fulfilled.headers,
status: fulfilled.status,
}) })
const browserResponse = await request.response()
if (browserResponse !== null) {
// For error responses (>= 400), the browser may not consume the body
// in the same way, so we skip waiting for finished() to avoid hanging
if (fulfilled.status < 400) {
await browserResponse.finished()
}
}
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