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

Block prefetch task until sufficient response is received (#96017)

This is a refactor of the prefetching algorithm to block the prefetch
task from completing until the response is received.

Currently, a prefetch task is fire-and-forget: once a prefetch's
requests are spawned, the scheduler marks the originating task as
complete regardless of what the server ends up returning. However,
certain planned optimizations require us to verify the response from the
server to determine whether additional work is necessary. For example,
we may attempt to do a static prefetch instead of a runtime prefetch if
we optimistically assume that the static prefetch does not vary on
runtime data. We need to verify that assumption is correct before
marking the task as complete.

Now, whenever a pass over the task spawns a segment request, or
encounters an entry whose response hasn't arrived yet, the task
registers itself on the entry and exits as Blocked instead of Done. When
the entry resolves, the blocked task is pinged and the pass re-runs
against the received data. Only a pass that observes every response it
cares about advances the task to the next phase or completes it.

Rejected entries are handled per segment: a rejection counts as an
observed response, so the pass skips that segment and keeps prefetching
the rest. The task never registers on a rejected entry, since nothing
ever pings one. Retries are governed by the existing per-entry logic.

Also fixes a cache bug this exposed: when a revalidation response is
keyed at a more generic vary path than the stale partial entry that
prompted it (because the segment turned out not to vary on some param),
the stale entry shadowed the result on every lookup — the upgraded data
was unreachable and the revalidation was wasted. With blocking, that
dead end became an infinite loop: each fulfillment re-pinged the blocked
task, which re-read the same stale entry and spawned another
revalidation. Upserting (or re-keying) a segment now deletes settled
entries at more specific vary paths that the incoming entry supersedes.

The router-act test helper's "at least one request initiated" watchdog
is adjusted to account for the new blocking behavior: countable requests
can now be legitimately gated on an in-flight App Shell response (which
the watchdog intentionally doesn't count). On expiry it now proceeds to
response processing if the act scope intercepted a shell request, and
keeps waiting while shell requests are in flight elsewhere on the page,
instead of timing out.
parent a619237b
......@@ -6,23 +6,25 @@ import { equals } from '@jest/expect-utils'
// prefetches carry the value '3' (FetchStrategy.RuntimeShell). The App Shell is
// the param/searchParam-independent chrome of a route — conceptually part of the
// route itself, not prefetch data in the way we normally think of it. By default
// we therefore exclude App Shell requests from all `act` assertion logic
// (`includes` matching, `no-requests`, `block: 'reject'`, and the "at least one
// request" check). They are still intercepted, fulfilled, and awaited so that the
// browser caches the shell and no requests are left in flight. Pass
// `includeAppShellRequests: true` to `createRouterAct` to assert on them directly
// (e.g. when testing App Shell behavior specifically).
// we therefore exclude App Shell requests from `act` assertion logic
// (`includes` matching, `no-requests`, and `block: 'reject'`). They are still
// intercepted, fulfilled, and awaited so that the browser caches the shell and
// no requests are left in flight — and they do satisfy the "at least one
// request" check, since they prove the router reacted to the scope. Pass
// `includeAppShellRequests: true` to `createRouterAct` to assert on them
// directly (e.g. when testing App Shell behavior specifically).
const NEXT_ROUTER_PREFETCH_HEADER = 'next-router-prefetch'
const APP_SHELL_PREFETCH_VALUE = '3'
type Batch = {
pendingRequestChecks: Set<Promise<void>>
pendingRequests: Set<PendingRSCRequest>
// The number of pending requests in `pendingRequests` that are NOT App Shell
// requests. App Shell requests don't count toward the "at least one request"
// check, so we track this separately rather than scanning the set. Maintained
// in lockstep with `pendingRequests` membership.
pendingNonAppShellRequests: number
// Whether any router request — including an App Shell request — was
// received by this batch. Used by the "at least one request" watchdog:
// any router request proves the router reacted to the `act` scope, even
// ones that are excluded from the assertion logic. Also set when an inner
// `act`'s blocked responses are transferred to this batch.
didReceiveRouterRequest: boolean
}
type PendingRSCRequest = {
......@@ -98,6 +100,41 @@ export function createRouterAct(
}
): <T>(scope: () => Promise<T> | T, config?: ActConfig) => Promise<T> {
const includeAppShellRequests = options?.includeAppShellRequests ?? false
// Track App Shell requests that are currently in flight anywhere on the
// page — including ones initiated outside an `act` scope, like viewport
// prefetches during the initial page load. The "at least one request"
// watchdog consults this set: the prefetch scheduler's Shell phase does not
// complete until its shell responses have arrived, so while a shell request
// is in flight, follow-up requests (which ARE countable) may be
// legitimately waiting on it. In that case the watchdog keeps waiting
// instead of timing out. See the watchdog logic inside `act`.
const inFlightAppShellRequests = new Set<Playwright.Request>()
const onRequestStarted = (request: Playwright.Request) => {
if (
request.headers()[NEXT_ROUTER_PREFETCH_HEADER] ===
APP_SHELL_PREFETCH_VALUE
) {
inFlightAppShellRequests.add(request)
}
}
const onRequestSettled = (request: Playwright.Request) => {
inFlightAppShellRequests.delete(request)
}
// A hard navigation can orphan in-flight requests: Playwright doesn't
// reliably emit `requestfinished`/`requestfailed` for requests aborted by
// page teardown, which would leave stale entries in the set for the rest
// of the test — and a stale entry keeps the watchdog alive forever,
// turning what should be a clean timeout error into a hang. The destroyed
// document's shell requests can't gate anything anymore, and the new
// document's requests re-add themselves, so drop them all.
const onFrameDetached = () => {
inFlightAppShellRequests.clear()
}
page.on('request', onRequestStarted)
page.on('requestfinished', onRequestSettled)
page.on('requestfailed', onRequestSettled)
page.on('framedetached', onFrameDetached)
/**
* Helper function to wait for requestIdleCallback with retry logic.
* Retries up to 3 times if "Execution context was destroyed" error occurs.
......@@ -296,14 +333,14 @@ export function createRouterAct(
})(),
didProcess: false,
})
// App Shell requests don't count toward the "at least one request"
// check, so only track and signal for non-App-Shell requests.
if (!isAppShell) {
batch.pendingNonAppShellRequests++
if (onDidIssueFirstRequest !== null) {
onDidIssueFirstRequest()
onDidIssueFirstRequest = null
}
// Any router request — including an App Shell request, which is
// otherwise excluded from assertion logic — satisfies the "at least
// one request" watchdog, since it proves the router reacted to the
// `act` scope.
batch.didReceiveRouterRequest = true
if (onDidIssueFirstRequest !== null) {
onDidIssueFirstRequest()
onDidIssueFirstRequest = null
}
return
}
......@@ -327,7 +364,7 @@ export function createRouterAct(
const orphanedRequests = batch.pendingRequests
batch.pendingRequests = new Set()
batch.pendingRequestChecks = new Set()
batch.pendingNonAppShellRequests = 0
batch.didReceiveRouterRequest = false
await Promise.all(
Array.from(orphanedRequests).map((item) => item.route?.continue())
)
......@@ -345,7 +382,7 @@ export function createRouterAct(
const batch: Batch = {
pendingRequestChecks: new Set(),
pendingRequests: new Set(),
pendingNonAppShellRequests: 0,
didReceiveRouterRequest: false,
}
currentBatch = batch
await page.route('**/*', routeHandler)
......@@ -354,17 +391,26 @@ export function createRouterAct(
// Call the user-provided scope function
const returnValue = await scope()
// Wait until the first request is initiated, up to some timeout. App Shell
// requests don't count, so check the non-App-Shell pending request count.
if (
expectedResponses !== null &&
batch.pendingNonAppShellRequests === 0
) {
// Wait until the first request is initiated, up to some timeout.
if (expectedResponses !== null && !batch.didReceiveRouterRequest) {
await new Promise<void>((resolve, reject) => {
const timerId = setTimeout(() => {
let timerId: ReturnType<typeof setTimeout>
const onExpiry = () => {
// Before timing out, check for App Shell requests in flight
// elsewhere on the page (e.g. spawned by a viewport prefetch
// during page load, before this `act` scope began, so it was
// never intercepted here). The prefetch scheduler's Shell phase
// doesn't complete until its shell responses arrive, so the
// first observable request may be legitimately gated on one.
// Keep the watchdog alive until it settles.
if (inFlightAppShellRequests.size > 0) {
timerId = setTimeout(onExpiry, 500)
return
}
error.message = 'Timed out waiting for a request to be initiated.'
reject(error)
}, 500)
}
timerId = setTimeout(onExpiry, 500)
onDidIssueFirstRequest = () => {
clearTimeout(timerId)
resolve()
......@@ -402,14 +448,6 @@ export function createRouterAct(
const route = item.route
const url = item.url
// This request is being removed from `pendingRequests` for
// processing. Keep the non-App-Shell counter in lockstep. (If it ends
// up blocked and transferred to the outer batch, that batch's counter
// is incremented when the transfer happens, below.)
if (!item.isAppShell) {
batch.pendingNonAppShellRequests--
}
let shouldBlock = false
const fulfilled = await item.result
if (item.didProcess) {
......@@ -606,9 +644,7 @@ ${fulfilled.body}
// Shell prefetch.
isAppShell: false,
})
// Keep the counter in lockstep with the add above (drained
// and decremented on the next iteration of the while loop).
batch.pendingNonAppShellRequests++
batch.didReceiveRouterRequest = true
page.off('response', handleResponse)
page.off('requestfailed', handleFailure)
resolve()
......@@ -681,10 +717,10 @@ ${fulfilled.body}
if (remaining.size !== 0 && prevBatch !== null) {
for (const item of remaining) {
prevBatch.pendingRequests.add(item)
if (!item.isAppShell) {
prevBatch.pendingNonAppShellRequests++
}
}
// The outer batch inherits pending work, so its "at least one
// request" check is satisfied.
prevBatch.didReceiveRouterRequest = true
}
return returnValue
......
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