Unverified Commit e1131ee7 authored by Janka Uryga's avatar Janka Uryga Committed by GitHub

[PPF] Separate tracking of runtime data accesses for shells and prefetches (#98129)

Expands the static tracking from #96095 to track shells and prefetches
separately. We split the `ShouldAttemptStaticPrefetch` hint into
`ShouldAttemptStaticShell` and `ShouldAttemptStaticPrefetch`, which the
client router considers separately depending on whether we need a shell
or a prefetch.

The server-side tracking of `needsRuntimeRequest` becomes a bit more
subtle.

First, a runtime data access should only affect the shell if we're in
the `ShellStatic` render stage. If we're already in the `PrefetchStatic`
stage, then that content is not conceptually part of the shell, and
should not deopt the shell to runtime, so we only unset
`shouldAttemptStaticShell` if we're still in the shell stage.

Second, accessing data that would only resolve in the prefetch (params,
search params, short-stale caches) should **not** affect whether the
shell can be static. `makeRuntimeDataHangingPromise` has been split into
variants that match this:
- `makeSessionDataHangingPromise` - data that affects the shell (and the
prefetch), e.g. `cookies()`
- `makeURLDataHangingPromise` - data that only affects the prefetch,
e.g. `params`
- `makeUnknownRuntimeDataHangingPromise`, used for private caches -- we
omit these during build so we don't know whether they should affect the
shell or only the prefetch, so we conservatively affect both (unless a
private cache is only used in the prefetch stage)
  
The server-side `needsRuntimeRequest` promise is now resolved such that
data that only resolves in the prefetch does not affect the shell. If
we're accessing data that only resolves in the prefetch, we will delay
the promise's resolution to the `PrefetchStatic` stage, which means that
when the client rewinds the static response to a shell, it will see an
unresolved promise and consider the response as not having used runtime
data. (see `markRuntimeDataAccessWhenStageReached`)
parent 51603630
import type React from 'react'
import type { Segment as FlightRouterStateSegment } from '../../../shared/lib/app-router-types'
import { PrefetchHint } from '../../../shared/lib/app-router-types'
import {
PrefetchHint,
StaticAttemptHints,
} from '../../../shared/lib/app-router-types'
import type { VaryParams } from '../../../shared/lib/segment-cache/vary-params-decoding'
import { readFulfilledValue } from '../../../shared/lib/rsc-transport'
import {
......@@ -1443,13 +1446,13 @@ export function createMetadataRouteTree(
// one. If this logic ever gets more complex we can change this to an enum.
isPage: true,
slots: null,
// Only the static-attempt bit applies to the head: it's a route-level
// Only the static-attempt bits apply to the head: it's a route-level
// fact ("static per-segment responses may exist for this route"), and
// it's what lets a shell-tier cached head attempt a static head fetch
// before deopting to a runtime request (see the shell-tier eligibility
// check in pingSegmentBundle). The other bits describe tree structure
// the head doesn't participate in.
prefetchHints: rootPrefetchHints & PrefetchHint.ShouldAttemptStaticPrefetch,
prefetchHints: rootPrefetchHints & StaticAttemptHints,
}
return metadata
}
......
......@@ -1100,10 +1100,9 @@ function pingStaticHead(
if (
headRequiresRuntimeCompleteness &&
// The head is not a tree node — it hangs off the route root — so the
// static-attempt hint is read from the root's node. (Segments read the
// bit from their own node; see the decision point in
// pingNewPartOfCacheComponentsTree.)
(route.tree.prefetchHints & PrefetchHint.ShouldAttemptStaticPrefetch) === 0
// static-attempt hints are read from the root's node. (Segments read the
// hints from their own node; see `pingNewPartOfCacheComponentsTree.`)
!shouldSegmentAttemptStaticRequest(fetchStrategy, route.tree)
) {
// No static attempt: the head arrives via the runtime request instead.
addSpawnedRuntimePrefetch(task, route.metadata.requestKey)
......@@ -1170,7 +1169,7 @@ function pingStaticHead(
* dynamic holes are filled by the navigation-time request.
*
* Note: The runtime contract is affordable because most
* routes carry the ShouldAttemptStaticPrefetch hint: their segments are
* routes carry the ShouldAttemptStatic{Shell,Prefetch} hints: their segments are
* prefetched statically and the responses' own sufficiency signal makes a
* runtime request rare. On a hint-unset route, a walked segment deopts
* directly to the batched runtime request.
......@@ -1184,12 +1183,40 @@ function walkRequiresRuntimeCompleteness(
staticWalkStrategy: FetchStrategy.PPR | FetchStrategy.StaticShell,
route: FulfilledRouteCacheEntry
): boolean {
if (staticWalkStrategy === FetchStrategy.StaticShell) {
// `FetchStrategy.StaticShell` is only used on PPF routes.
return true
}
// `FetchStrategy.PPR` can only use runtime requests if PPF is enabled on the route.
return (
staticWalkStrategy === FetchStrategy.StaticShell ||
(route.tree.prefetchHints & PrefetchHint.SubtreeHasPartialPrefetching) !== 0
)
}
/**
* Whether a static request for this segment should be attempted.
* This may vary on the static walk's strategy, because we might e.g.
* have a shell that's static, but a prefetch that requires runtime requests.
*
* NOTE: Should only be used on Partial Prefetching routes, where
* `walkCanUseRuntimeRequests` is true.
* */
function shouldSegmentAttemptStaticRequest(
staticWalkStrategy: FetchStrategy.PPR | FetchStrategy.StaticShell,
tree: RouteTree<any>
): boolean {
const { prefetchHints } = tree
switch (staticWalkStrategy) {
case FetchStrategy.StaticShell:
return (prefetchHints & PrefetchHint.ShouldAttemptStaticShell) !== 0
case FetchStrategy.PPR:
return (prefetchHints & PrefetchHint.ShouldAttemptStaticPrefetch) !== 0
default:
staticWalkStrategy satisfies never
return false
}
}
/**
* The runtime counterpart of a pass's static walk strategy: the strategy the
* batched runtime request uses if this walk deopts. Each phase has exactly one
......@@ -1256,9 +1283,9 @@ function isShellEntryEligibleForStaticAttempt(
!(
(entry.fetchStrategy === FetchStrategy.StaticShell ||
entry.fetchStrategy === FetchStrategy.RuntimeShell) &&
(tree.prefetchHints & PrefetchHint.ShouldAttemptStaticPrefetch) !== 0 &&
// A StaticShell walk's static attempt is the shell tier itself, so it
// only applies when the walk's static strategy outranks the entry.
shouldSegmentAttemptStaticRequest(fetchStrategy, tree) &&
// if the `fetchStrategy` is `FetchStrategy.PPR`, it might provide more content
// (e.g. static params that this shell doesn't have)
canNewFetchStrategyProvideMoreContent(entry.fetchStrategy, fetchStrategy)
)
) {
......@@ -1455,7 +1482,7 @@ function pingNewPartOfCacheComponentsTree(
// complete as a runtime response for every segment before the prefetch
// can complete (see walkRequiresRuntimeCompleteness) — this function is
// also the per-segment decision point. If the segment's node carries the
// ShouldAttemptStaticPrefetch hint (the build-time prerender accessed no
// ShouldAttemptStatic{Shell,Prefetch} hints (the build-time prerender accessed no
// runtime data), its subtree is prefetched statically first,
// and the responses themselves decide whether that was enough: every
// fulfilled entry carries a needsRuntimeRequest signal. Pending responses
......@@ -1482,27 +1509,21 @@ function pingNewPartOfCacheComponentsTree(
// Constant for the whole pass; recomputed here only because the walk is
// recursive and the check is cheap.
const segmentRequiresRuntimeCompleteness = walkRequiresRuntimeCompleteness(
const requiresRuntimeCompleteness = walkRequiresRuntimeCompleteness(
fetchStrategy,
route
)
// TODO: The static-attempt hint reflects the build-time prerender's whole
// runtime-data tracking, so a page that always accesses
// runtime data after the shell stage never attempts a static prefetch —
// even though its shell variant is rewindable at the shell boundary and
// perfectly reusable. The server could emit a second bit derived from the
// shell-stage value ("a static SHELL attempt is worthwhile even though
// the page accesses runtime data post-shell") to let such pages attempt
// static, too.
// A force-disabled segment deliberately does NOT deopt here: disabling
// prefetch is passive. It never initiates a request — its accumulation
// below contributes nothing — and must never be the reason a runtime
// prefetch spawns, though it may ride along in a runtime response issued
// on another segment's behalf.
const attemptStaticPrefetchOfSegment =
(tree.prefetchHints & PrefetchHint.ShouldAttemptStaticPrefetch) !== 0
if (segmentRequiresRuntimeCompleteness && !attemptStaticPrefetchOfSegment) {
if (
requiresRuntimeCompleteness &&
!shouldSegmentAttemptStaticRequest(fetchStrategy, tree)
) {
// Deopt directly to a runtime prefetch, without a static attempt.
addSpawnedRuntimePrefetch(task, tree.requestKey)
// If there's a pending static bundle from a parent, we need to finish
......@@ -1533,7 +1554,7 @@ function pingNewPartOfCacheComponentsTree(
)
const bundleInProgress = accumulation.bundle
if (segmentRequiresRuntimeCompleteness && accumulation.needsRuntimeRequest) {
if (requiresRuntimeCompleteness && accumulation.needsRuntimeRequest) {
// The static attempt for this segment was insufficient. Stop the walk
// and deopt — the runtime prefetch covers the whole subtree. (Unlike the
// direct deopt above, any open bundle is dropped rather than finished: a
......
......@@ -337,6 +337,8 @@ import { isInstantValidationError } from './instant-validation/instant-validatio
import { createPromiseWithResolvers } from '../../shared/lib/promise-with-resolvers'
import {
RENDER_STAGES_BY_DATA_KIND,
createPrerenderDataTracking,
finishPrerenderDataTracking,
trackIncompatibleShellContent,
} from '../dynamic-rendering-utils'
import type {
......@@ -8893,8 +8895,7 @@ async function prerenderToStream(
hmrRefreshHash: undefined,
// We don't track vary params during initial prerender, only the final one
varyParamsAccumulator: null,
runtimeDataAccessed: null,
shouldAttemptStaticPrefetch: null,
prerenderDataTracking: null,
isFallbackUpgradeable: renderOpts.isFallbackUpgradeable === true,
}
......@@ -8930,8 +8931,7 @@ async function prerenderToStream(
hmrRefreshHash: undefined,
// We don't track vary params during initial prerender, only the final one
varyParamsAccumulator: null,
runtimeDataAccessed: null,
shouldAttemptStaticPrefetch: null,
prerenderDataTracking: null,
isFallbackUpgradeable: renderOpts.isFallbackUpgradeable === true,
})
......@@ -9157,25 +9157,7 @@ async function prerenderToStream(
finalStage: RenderStage.Static,
})
// Records runtime data accesses from the payload and render stores
// below into the RSC payload (as `u`), resolved `true` at the moment
// of first access so the fulfillment row is serialized at the stream
// position where it happened. Used when generating per-segment
// prefetch responses. Request data props (params, searchParams) are
// created while the RSC payload is constructed, under the payload
// store; both stores share the same promise so it observes accesses
// from both.
const runtimeDataAccessed = createPromiseWithResolvers<boolean>()
// Companion cell holding this prerender's static-prefetch measurement
// directly — the value that becomes the route's build-constant hint:
// starts true, and a disqualifying runtime-data access flips it false
// — fallback-param accesses on an upgradeable route don't (see
// trackRuntimeDataAccessed, which applies the rule at access time).
// Read after the prerender settles by
// collectSegmentData below. Shared between both stores for the same
// reason as the promise.
const shouldAttemptStaticPrefetch = { current: true }
const prerenderDataTracking = createPrerenderDataTracking()
const finalServerPayloadPrerenderStore: PrerenderStoreModernServer = {
type: 'prerender',
......@@ -9203,8 +9185,7 @@ async function prerenderToStream(
resumeDataCache,
hmrRefreshHash: undefined,
varyParamsAccumulator,
runtimeDataAccessed,
shouldAttemptStaticPrefetch,
prerenderDataTracking,
isFallbackUpgradeable: renderOpts.isFallbackUpgradeable === true,
}
......@@ -9233,7 +9214,7 @@ async function prerenderToStream(
// Embed the runtime data access tracking in the payload so
// collectSegmentData can replay it per stage. Only needed when the
// Flight data will be decomposed into segment prefetches below.
finalServerPayload.u = runtimeDataAccessed.promise
finalServerPayload.u = prerenderDataTracking.runtimeDataAccessed.promise
const serverDynamicTracking = createDynamicTrackingState(
isDebugDynamicAccesses
......@@ -9259,8 +9240,7 @@ async function prerenderToStream(
resumeDataCache,
hmrRefreshHash: undefined,
varyParamsAccumulator,
runtimeDataAccessed,
shouldAttemptStaticPrefetch,
prerenderDataTracking,
isFallbackUpgradeable: renderOpts.isFallbackUpgradeable === true,
})
......@@ -9317,7 +9297,6 @@ async function prerenderToStream(
if (staleTimeIterable !== undefined) {
staleTimeIterable.close()
}
runtimeDataAccessed.resolve(false)
finishAccumulatingVaryParams(varyParamsAccumulator)
}
}
......@@ -9410,9 +9389,7 @@ async function prerenderToStream(
if (staleTimeIterable !== undefined) {
staleTimeIterable.close()
}
// Idempotent: a no-op if a runtime data access already resolved it
// `true`. The `false` row lands here, after all stage content.
runtimeDataAccessed.resolve(false)
finishPrerenderDataTracking(prerenderDataTracking)
finishAccumulatingVaryParams(varyParamsAccumulator)
shellByteLengthDeferred.resolve(
......@@ -9467,6 +9444,16 @@ async function prerenderToStream(
? metadata.flightData.subarray(1)
: metadata.flightData
if (process.env.NEXT_PRIVATE_DEBUG_RUNTIME_DATA) {
const {
shouldAttemptStaticShell: shellIsStatic,
shouldAttemptStaticPrefetch: prefetchIsStatic,
} = prerenderDataTracking
const label = (isStatic: boolean) => (isStatic ? 'static' : 'runtime')
console.log(
`Route ${workStore.route} :: hints: shell=${label(shellIsStatic)}, prefetch=${label(prefetchIsStatic)}`
)
}
await collectSegmentData(
flightData,
finalServerPrerenderStore,
......@@ -9979,8 +9966,7 @@ async function prerenderToStream(
resumeDataCache: originalResumeDataCache,
hmrRefreshHash: undefined,
varyParamsAccumulator: null,
runtimeDataAccessed: null,
shouldAttemptStaticPrefetch: null,
prerenderDataTracking: null,
isFallbackUpgradeable: renderOpts.isFallbackUpgradeable === true,
}
......@@ -10642,20 +10628,22 @@ async function collectSegmentData(
let hints: PrefetchHints | null
const prefetchInlining = renderOpts.experimental.prefetchInlining
if (renderOpts.isBuildTimePrerendering) {
// Whether the client should attempt a static prefetch for this route
// (PrefetchHint.ShouldAttemptStaticPrefetch): the prerender store's
// cell holds the hint value directly — true iff the build-time
// prerender accessed no runtime data that disqualifies a static
// attempt. The fallback-param upgradeability rule is applied at access
// time — see trackRuntimeDataAccessed — so only the settled value is
// read here. Only the modern (cacheComponents) prerender tracks
// accesses; legacy prerenders conservatively never set the hint.
const hintCell =
// Whether the client should attempt to use static requests for
// shells and prefetches. Only applies in routes with Partial Prefetching.
const prerenderDataTracking =
prerenderStore.type === 'prerender'
? prerenderStore.shouldAttemptStaticPrefetch
? prerenderStore.prerenderDataTracking
: null
const shouldAttemptStaticPrefetch = hintCell !== null && hintCell.current
if (prefetchInlining || shouldAttemptStaticPrefetch) {
const {
shouldAttemptStaticShell = false,
shouldAttemptStaticPrefetch = false,
} = prerenderDataTracking ?? {}
if (
prefetchInlining ||
shouldAttemptStaticShell ||
shouldAttemptStaticPrefetch
) {
// Build time: compute fresh hints and store in metadata for the
// manifest. When prefetch inlining is disabled there are no sizes to
// measure, but the static-prefetch hint still rides the manifest —
......@@ -10667,6 +10655,7 @@ async function collectSegmentData(
clientModules,
serverConsumerManifest,
prefetchInlining,
shouldAttemptStaticShell,
shouldAttemptStaticPrefetch
)
metadata.prefetchHints = hints
......
......@@ -364,7 +364,7 @@ export async function collectSegmentData(
* decisions can be fed back into collectSegmentData to control which segments
* are output as separate entries vs. inlined into their parent.
*
* `shouldAttemptStaticPrefetch` (computed by the caller from the prerender's
* `shouldAttemptStatic{Shell,Prefetch}` (computed by the caller from the prerender's
* runtime-data tracking) is folded onto every node of the result, so the
* manifest delivers it to every response like the other hint bits. It's
* independent of the inlining feature: when `inlining` is false the sizing
......@@ -384,6 +384,7 @@ export async function collectPrefetchHints(
clientModules: ManifestNode,
serverConsumerManifest: any,
inlining: { maxSize: number; maxBundleSize: number } | false,
shouldAttemptStaticShell: boolean,
shouldAttemptStaticPrefetch: boolean
): Promise<PrefetchHints> {
// Warm up the module cache, same as collectSegmentData.
......@@ -409,13 +410,18 @@ export async function collectPrefetchHints(
const buildId = initialRSCPayload.b
const head = transportData.h.r
// The hints every node starts from. The static-prefetch-attempt hint is
// page-global (the tracking that feeds it is), so it goes on every node,
// The hints every node starts from. The static-prefetch-attempt hints are
// page-global (the tracking that creates them is), so they go on every node,
// non-propagating — the client reads it per segment, and the runtime
// hint merging walks the manifest tree node-by-node.
const baseHints = shouldAttemptStaticPrefetch
? PrefetchHint.ShouldAttemptStaticPrefetch
: 0
// (NOTE: if the prefetch is static, then the shell must be as well)
let baseHints = 0
if (shouldAttemptStaticShell) {
baseHints |= PrefetchHint.ShouldAttemptStaticShell
}
if (shouldAttemptStaticPrefetch) {
baseHints |= PrefetchHint.ShouldAttemptStaticPrefetch
}
if (inlining === false) {
// Prefetch inlining is disabled: nothing to measure, and no inlining
......@@ -1030,11 +1036,10 @@ function collectSegmentDataImpl(
// response as-is. Root params are forwarded separately, once per response.
const varyParams = node.d.v
// If static prefetching is disabled for this segment
// (prefetch: 'force-disabled' / instant = false), it still participates in
// the bundle chain but with null data. Its node in the response tree
// carries identity only, so the client skips creating a cache entry
// for it.
// If static prefetching is disabled for this segment (prefetch: 'force-disabled'),
// it still participates in the bundle chain but with null data.
// Its node in the response tree carries identity only, so the client skips
// creating a cache entry for it.
//
// Partial Prefetching segments are NOT disabled even though they may need
// a runtime prefetch: their static data is emitted UNCONDITIONALLY, so the
......
......@@ -35,10 +35,7 @@ import React from 'react'
import { DynamicServerError } from '../../client/components/hooks-server-context'
import { StaticGenBailoutError } from '../../client/components/static-generation-bailout'
import { getStagedRenderingController } from './work-unit-async-storage.external'
import {
isClientHookDynamicError,
trackRuntimeDataAccessed,
} from '../dynamic-rendering-utils'
import { isClientHookDynamicError } from '../dynamic-rendering-utils'
import {
METADATA_BOUNDARY_NAME,
VIEWPORT_BOUNDARY_NAME,
......@@ -348,15 +345,6 @@ export function abortAndThrowOnSynchronousRequestDataAccess(
errorWithStack: Error,
prerenderStore: PrerenderStoreModern
): never {
// The synchronously accessed request data would have been available during
// a runtime prerender, which would have rendered past this point instead of
// aborting — so a runtime prefetch would produce more content than this
// render. Record that, same as when request data access creates a hanging
// promise (see makeRuntimeHangingPromise). Unlike
// `abortOnSynchronousPlatformIOAccess`, which aborts a runtime prerender
// all the same and therefore must not record anything.
trackRuntimeDataAccessed(prerenderStore, expression)
const prerenderSignal = prerenderStore.controller.signal
if (prerenderSignal.aborted === false) {
// TODO it would be better to move this aborted check into the callsite so we can avoid making
......
......@@ -113,6 +113,11 @@ export class StagedRenderingController {
}
}
/**
* Schedules a callback that will execute synchronously when the controller is advanced
* to `stage` or past it, before any promises for that stage are resolved.
* If the controller is already past `stage`, the callback is executed immediately.
* */
onStage(stage: AdvanceableRenderStage, callback: () => void) {
addSyncTriggerListener(this.triggers[stage], callback)
}
......
......@@ -22,6 +22,7 @@ import type {
} from './staged-rendering'
import type { ValidationBoundaryTracking } from './instant-validation/boundary-tracking'
import type { InstantValidationSampleTracking } from './instant-validation/instant-samples'
import type { PrerenderDataTracking } from '../dynamic-rendering-utils'
export type WorkUnitPhase = 'action' | 'render' | 'after'
......@@ -177,53 +178,23 @@ export interface PrerenderStoreModernServer
readonly stagedRendering: StagedRenderingController | null
/**
* When not null, records whether the render has accessed a data source
* that hangs during a static prerender but would resolve during a runtime
* prerender — cookies, headers, fallback params, searchParams, and cache
* entries excluded only from static prerenders. Call sites go through
* `trackRuntimeDataAccessed`, which resolves the promise `true` on the
* first access; it's resolved `false` when the prerender completes without
* one. Promise resolution is idempotent, so the flag is monotonic with no
* extra state.
/*
* Records usages of non-static data during a prerender to determine
* whether it should be statically optimized.
* Includes:
* - session data (`cookies()`, `headers()`)
* - URL data (`params`, `searchParams`)
*
* The promise is embedded in the RSC payload (`InitialRSCPayload['u']`)
* so the fulfillment row's stream position records the stage the access
* happened in; the per-segment prefetch encoding (`collectSegmentData`)
* extracts it from the page data to tell the client whether a runtime
* prefetch request could be skipped. Tracking is page-global: an access
* anywhere in the page poisons all segments (per-segment granularity is
* recovered downstream for segments whose content is provably complete).
* Shared between the payload prerender store and the render store because
* request-data props are created during payload construction, before the
* render store exists. Null for warmup, route-handler, and error prerender
* stores.
*/
readonly runtimeDataAccessed: PromiseWithResolvers<boolean> | null
/**
* Mutable single-boolean companion to `runtimeDataAccessed`, holding this
* prerender's `PrefetchHint.ShouldAttemptStaticPrefetch` measurement
* directly — the value that becomes the route's build-constant hint:
* starts `true`, and a disqualifying runtime-data access flips it to
* `false`. Not every access that resolves the promise disqualifies —
* fallback-param accesses on a fallback-upgradeable route are transient
* and leave the hint intact (see `trackRuntimeDataAccessed`, which applies
* the rule at access time using `isFallbackUpgradeable` below). A plain
* boolean suffices because the hint needs no stream positioning: unlike
* `runtimeDataAccessed`, whose fulfillment position encodes which stage
* the access happened in, this is read once after the prerender settles.
* Held in a cell so it can be shared. Same sharing and null rules as
* `runtimeDataAccessed`.
*/
readonly shouldAttemptStaticPrefetch: { current: boolean } | null
* Only intended to be used during the final prerender of a page.
* */
readonly prerenderDataTracking: PrerenderDataTracking | null
/**
* Whether a fallback shell produced by this prerender could later be
* upgraded to a concrete prerender (`renderOpts.isFallbackUpgradeable`:
* at least one fallback param is a `generateStaticParams` candidate).
* Consulted by `trackRuntimeDataAccessed` to decide whether a
* fallback-param access disqualifies the static-prefetch hint.
* Used to decide whether a fallback-param access indicates that a
* runtime prefetch should be used.
*/
readonly isFallbackUpgradeable: boolean
}
......
......@@ -21,7 +21,7 @@ import {
import { StaticGenBailoutError } from '../../client/components/static-generation-bailout'
import {
makeDevtoolsIOAwarePromise,
makeRuntimeHangingPromise,
makeSessionDataHangingPromise,
RENDER_STAGES_BY_DATA_KIND,
} from '../dynamic-rendering-utils'
import { createDedupedByCallsiteServerErrorLoggerDev } from '../create-deduped-by-callsite-server-error-logger'
......@@ -165,7 +165,7 @@ function makeHangingCookies(
return cachedPromise
}
const promise = makeRuntimeHangingPromise<ReadonlyRequestCookies>(
const promise = makeSessionDataHangingPromise<ReadonlyRequestCookies>(
prerenderStore.renderSignal,
workStore.route,
'`cookies()`',
......
......@@ -19,7 +19,7 @@ import {
import { StaticGenBailoutError } from '../../client/components/static-generation-bailout'
import {
makeDevtoolsIOAwarePromise,
makeRuntimeHangingPromise,
makeSessionDataHangingPromise,
RENDER_STAGES_BY_DATA_KIND,
} from '../dynamic-rendering-utils'
import { createDedupedByCallsiteServerErrorLoggerDev } from '../create-deduped-by-callsite-server-error-logger'
......@@ -169,7 +169,7 @@ function makeHangingHeaders(
return cachedHeaders
}
const promise = makeRuntimeHangingPromise<ReadonlyHeaders>(
const promise = makeSessionDataHangingPromise<ReadonlyHeaders>(
prerenderStore.renderSignal,
workStore.route,
'`headers()`',
......
......@@ -95,9 +95,7 @@ function createPrerenderPathname(
case 'prerender': {
const fallbackParams = prerenderStore.fallbackRouteParams
if (fallbackParams && fallbackParams.size > 0) {
// The pathname only hangs when there are fallback params, and a
// concrete (ISR-upgraded) prerender resolves it — so this access is
// fallback-param data for the static-prefetch hint.
// The pathname depends on params, so we track it like a fallback params access.
return makeFallbackParamsHangingPromise<string>(
prerenderStore.renderSignal,
workStore.route,
......
......@@ -28,9 +28,9 @@ import {
import { InvariantError } from '../../shared/lib/invariant-error'
import {
makeDevtoolsIOAwarePromise,
makeRuntimeHangingPromise,
makeURLDataHangingPromise,
makePromiseFromTrigger,
trackRuntimeDataAccessed,
trackURLDataAccessed,
RENDER_STAGES_BY_DATA_KIND,
} from '../dynamic-rendering-utils'
import { createDedupedByCallsiteServerErrorLoggerDev } from '../create-deduped-by-callsite-server-error-logger'
......@@ -175,7 +175,7 @@ export function createPrerenderSearchParamsForClientPage(): Promise<SearchParams
case 'prerender-client':
// We're prerendering in a mode that aborts (cacheComponents) and should stall
// the promise to ensure the RSC side is considered dynamic
return makeRuntimeHangingPromise(
return makeURLDataHangingPromise(
workUnitStore.renderSignal,
workStore.route,
'`searchParams`',
......@@ -221,9 +221,10 @@ function createStaticPrerenderSearchParams(
switch (prerenderStore.type) {
case 'prerender':
case 'prerender-client':
case 'prerender-client': {
// We are in a cacheComponents (PPR or otherwise) prerender
return makeHangingSearchParams(workStore, prerenderStore)
}
case 'prerender-legacy':
// We are in a legacy static generation and need to interrupt the
// prerender when search params are accessed.
......@@ -382,7 +383,7 @@ function makeHangingSearchParams(
return cachedSearchParams
}
const promise = makeRuntimeHangingPromise<SearchParams>(
const promise = makeURLDataHangingPromise<SearchParams>(
prerenderStore.renderSignal,
workStore.route,
'`searchParams`',
......@@ -397,7 +398,7 @@ function makeHangingSearchParams(
// created while the RSC payload is constructed, but typically accessed
// later, during the render, under a different store.
const workUnitStore = workUnitAsyncStorage.getStore()
trackRuntimeDataAccessed(workUnitStore ?? prerenderStore, '`searchParams`')
trackURLDataAccessed(workUnitStore ?? prerenderStore, '`searchParams`')
}
const proxyHandler: ProxyHandler<Promise<SearchParams>> = {
......
......@@ -597,8 +597,7 @@ export class AppRouteRouteModule extends RouteModule<
resumeDataCache: prerenderResumeDataCache,
hmrRefreshHash: undefined,
varyParamsAccumulator: null,
runtimeDataAccessed: null,
shouldAttemptStaticPrefetch: null,
prerenderDataTracking: null,
isFallbackUpgradeable: false,
})
......@@ -696,8 +695,7 @@ export class AppRouteRouteModule extends RouteModule<
resumeDataCache: prerenderResumeDataCache,
hmrRefreshHash: undefined,
varyParamsAccumulator: null,
runtimeDataAccessed: null,
shouldAttemptStaticPrefetch: null,
prerenderDataTracking: null,
isFallbackUpgradeable: false,
})
......
......@@ -42,9 +42,11 @@ import {
applyOwnerStack,
makeDevtoolsIOAwarePromise,
makeDynamicHangingPromise,
makeRuntimeHangingPromise,
makePrefetchHangingPromise,
makeUntrackedHangingPromise,
makeSessionDataHangingPromise,
makeURLDataHangingPromise,
makePrefetchHangingPromise,
makeUnknownRuntimeDataHangingPromise,
RENDER_STAGES_BY_DATA_KIND,
trackIncompatibleShellContent,
} from '../dynamic-rendering-utils'
......@@ -1457,8 +1459,9 @@ async function generateCacheEntryImpl(
// If the prerender is aborted because of dynamic access (e.g. reading
// fallback params), we return a hanging promise. This essentially makes
// the "use cache" function dynamic.
// The dynamic access is a fallback params read, which is runtime data.
const hangingPromise = makeRuntimeHangingPromise<never>(
// The dynamic access means reading either params or searchParams,
// both of which are URL data.
const hangingPromise = makeURLDataHangingPromise<never>(
outerWorkUnitStore.renderSignal,
workStore.route,
'dynamic "use cache"',
......@@ -1883,8 +1886,11 @@ export async function cache(
switch (workUnitStore.type) {
// "use cache: private" is dynamic in prerendering contexts.
case 'prerender':
// Private caches can read request data, which is runtime data.
return makeRuntimeHangingPromise(
// Private caches can read request data. At this point we don't know
// Whether it's going to use session data or URL data.
// TODO(app-shells): This could be narrowed (and thus optimized) if we checked whether URL data
// is included in the cache key, because it can only be passed in as arguments.
return makeUnknownRuntimeDataHangingPromise(
workUnitStore.renderSignal,
workStore.route,
expression,
......@@ -2194,9 +2200,9 @@ export async function cache(
)
if (dynamicAccessAbortController.signal.aborted) {
// The dynamic access is a fallback params read, which is runtime
// data.
return makeRuntimeHangingPromise(
// The dynamic access is means the arguments contain either
// params or searchParams, which are URL data.
return makeURLDataHangingPromise(
workUnitStore.renderSignal,
workStore.route,
'dynamic "use cache"',
......@@ -2340,8 +2346,10 @@ export async function cache(
case 'prerender':
case 'prerender-runtime':
// The cache key was marked dynamic because it depends on fallback
// params, which are runtime data.
return makeRuntimeHangingPromise(
// params or search params, which are URL data.
// (Note that this can still occur in a runtime prerender for a runtime shell,
// which does not have access to params or searchParams)
return makeURLDataHangingPromise(
workUnitStore.renderSignal,
workStore.route,
'dynamic "use cache"',
......@@ -2462,9 +2470,9 @@ export async function cache(
cacheSignal.endRead()
}
// The entry is only excluded from *static* prerenders — the
// 'prerender-runtime' case below serves it — so a runtime
// prefetch would include this content.
return makeRuntimeHangingPromise(
// 'prerender-runtime' case below serves it.
// TODO(app-shells): Does this handle the MIN_SHELL_STALE case below correctly?
return makeSessionDataHangingPromise(
workUnitStore.renderSignal,
workStore.route,
'dynamic "use cache"',
......@@ -2762,9 +2770,8 @@ export async function cache(
// also do here, this covers the case where params are transformed
// with an async function before being passed into the "use cache"
// function, which escapes the instrumentation.
// The cache key depends on fallback params, which are runtime
// data.
return makeRuntimeHangingPromise(
// The cache key depends on fallback params, which are URL data.
return makeURLDataHangingPromise(
workUnitStore.renderSignal,
workStore.route,
'dynamic "use cache"',
......@@ -2784,10 +2791,8 @@ export async function cache(
// broken cache entry that gets aborted.
console.warn(new UnexpectedCacheMissError(workStore.route))
// This is an anomaly (non-deterministic cache key), so we can't
// know whether a runtime prerender would resolve it. Treat it as
// runtime data, conservatively: the cost is at most a redundant
// runtime prefetch request.
return makeRuntimeHangingPromise(
// know whether a runtime prerender would resolve it.
return makeUnknownRuntimeDataHangingPromise(
workUnitStore.renderSignal,
workStore.route,
'dynamic "use cache"',
......@@ -3197,10 +3202,10 @@ export async function cache(
cacheSignal.endRead()
}
// The entry is only excluded from *static* prerenders — the
// 'prerender-runtime' case below serves it — so a runtime
// prefetch would include this content.
const hangingPromise = makeRuntimeHangingPromise<never>(
// The entry is only excluded from *static* prerenders. A runtime
// shell or prefetch would include this content.
// TODO(app-shells): Does this handle the MIN_SHELL_STALE case below correctly?
const hangingPromise = makeSessionDataHangingPromise<never>(
workUnitStore.renderSignal,
workStore.route,
'dynamic "use cache"',
......
......@@ -277,15 +277,16 @@ export const enum PrefetchHint {
// navigations alike. (Routes missing from the manifest — see the #91407
// fallbacks — simply never carry it.) Set on every node of the tree, but
// does not propagate.
ShouldAttemptStaticPrefetch = 0b100000000000000,
ShouldAttemptStaticShell = 0b100000000000000,
ShouldAttemptStaticPrefetch = 0b1000000000000000,
// This dynamic segment only accepts build-time parameter values. Kept on
// the affected node, without propagating or copying it to other segments.
// Until the client knows the allowed values, any such node prevents route
// prediction, even when the route's components never read the parameter.
IsClosedParam = 0b1000000000000000,
IsClosedParam = 0b10000000000000000,
// A descendant segment (but not this one) has a closed parameter. Propagates
// upward so route prediction can check the root instead of walking the tree.
SubtreeHasClosedParams = 0b10000000000000000,
SubtreeHasClosedParams = 0b100000000000000000,
}
/**
......@@ -297,17 +298,21 @@ export const enum PrefetchHint {
* Static prefetching is disabled ONLY by `prefetch: 'force-disabled'`
* (PrefetchDisabled). Notably, Partial Prefetching segments DO have static
* data even though they require runtime completeness: the server emits it
* unconditionally — it can't be gated on the ShouldAttemptStaticPrefetch
* hint, because which segments carry static data must be deterministic
* unconditionally — it can't be gated on the ShouldAttemptStatic{Shell,Prefetch}
* hints, because which segments carry static data must be deterministic
* from build-time config. A runtime request may still be needed for the
* segment, but the scheduler may attempt a static prefetch first (per the
* ShouldAttemptStaticPrefetch hint) and skip the runtime request if the
* ShouldAttemptStatic{Shell,Prefetch} hints) and skip the runtime request if the
* static response proves sufficient.
*
* Usage: `(hints & StaticPrefetchDisabled) !== 0`
*/
export const StaticPrefetchDisabled = PrefetchHint.PrefetchDisabled
export const StaticAttemptHints =
PrefetchHint.ShouldAttemptStaticShell |
PrefetchHint.ShouldAttemptStaticPrefetch
/**
* The subset of PrefetchHint bits that propagate upward from a child segment to
* its ancestors (as opposed to segment-local bits like SegmentHasLoadingBoundary
......@@ -438,11 +443,9 @@ export type InitialRSCPayload = {
* Unlike an async iterable, a pending promise costs Flight no abort
* listener on the render. Used when generating per-segment prefetch
* responses (forwarded as each response's own `u`).
* The build-constant `PrefetchHint.ShouldAttemptStaticPrefetch` is
* tracked directly on the prerender store instead (its
* `shouldAttemptStaticPrefetch` cell) — it needs neither stream
* positioning nor this flag's param/non-param blindness. Only present
* for static prerenders when Cache Components is enabled.
* The build-time constant `PrefetchHint.ShouldAttemptStatic{Shell,Prefetch}`
* hint is tracked separately.
* Only present for static prerenders when Cache Components is enabled.
*/
u?: Promise<boolean>
/** staticStageByteLength - Resolves when the static stage ends. */
......
import cheerio from 'cheerio'
import { nextTestSetup } from 'e2e-utils'
import type { PrerenderManifest } from 'next/dist/build'
import type { FlightRouterState } from 'next/dist/shared/lib/app-router-types'
import {
type FlightRouterState,
PrefetchHint,
} from 'next/dist/shared/lib/app-router-types'
import { createRouterAct } from 'router-act'
describe('segment cache closed params (dynamicParams = false)', () => {
......@@ -14,33 +17,43 @@ describe('segment cache closed params (dynamicParams = false)', () => {
) {
// If this private tree representation changes, reconstruct the regression
// rather than preserving or exposing router internals just for this test.
return browser.eval(() => {
const closedSegments: string[] = []
function visit(tree: FlightRouterState) {
// PrefetchHint.IsClosedParam is a const enum bit. Check every node so
// stamping the root, static segments, or page nodes also fails.
if (((tree[4] ?? 0) & 0b1000000000000000) !== 0) {
const segment = tree[0]
closedSegments.push(
typeof segment === 'string' ? segment : `[${segment[0]}]`
)
return browser.eval(
(IsClosedParam: number) => {
const closedSegments: string[] = []
function visit(tree: FlightRouterState) {
// PrefetchHint.IsClosedParam is a const enum bit. Check every node so
// stamping the root, static segments, or page nodes also fails.
if (((tree[4] ?? 0) & IsClosedParam) !== 0) {
const segment = tree[0]
closedSegments.push(
typeof segment === 'string' ? segment : `[${segment[0]}]`
)
}
for (const child of Object.values(tree[1])) visit(child)
}
for (const child of Object.values(tree[1])) visit(child)
}
visit(window.history.state.__PRIVATE_NEXTJS_INTERNALS_TREE.tree)
return closedSegments
})
visit(window.history.state.__PRIVATE_NEXTJS_INTERNALS_TREE.tree)
return closedSegments
},
// Client code in the closure cannot access this imported value,
// so we have to pass it in
PrefetchHint.IsClosedParam
)
}
async function hasClosedParamDescendants(
browser: Awaited<ReturnType<typeof next.browser>>
) {
return browser.eval(() => {
const tree: FlightRouterState =
window.history.state.__PRIVATE_NEXTJS_INTERNALS_TREE.tree
// PrefetchHint.SubtreeHasClosedParams summarizes descendants at the root.
return ((tree[4] ?? 0) & 0b10000000000000000) !== 0
})
return browser.eval(
(SubtreeHasClosedParams: number) => {
const tree: FlightRouterState =
window.history.state.__PRIVATE_NEXTJS_INTERNALS_TREE.tree
// PrefetchHint.SubtreeHasClosedParams summarizes descendants at the root.
return ((tree[4] ?? 0) & SubtreeHasClosedParams) !== 0
},
// Client code in the closure cannot access this imported value,
// so we have to pass it in
PrefetchHint.SubtreeHasClosedParams
)
}
// @force-gate prefetching
......
......@@ -47,6 +47,44 @@ export default async function Page() {
</LinkAccordion>
</li>
</ul>
<h2>First cookies only used in the prefetch, then also in the shell</h2>
<ul>
<li data-prefetch="auto">
<LinkAccordion href="/partial-conditional-cookies-in-both/first-prefetch-then-shell">
/partial-conditional-cookies-in-both/first-prefetch-then-shell (auto
prefetch)
</LinkAccordion>
</li>
<li data-prefetch="true">
<LinkAccordion
href="/partial-conditional-cookies-in-both/first-prefetch-then-shell"
prefetch={true}
>
/partial-conditional-cookies-in-both/first-prefetch-then-shell
(prefetch=true)
</LinkAccordion>
</li>
</ul>
<h2>First cookies used in the shell, then also in the prefetch</h2>
<ul>
<li data-prefetch="auto">
<LinkAccordion href="/partial-conditional-cookies-in-both/first-shell-then-prefetch">
/partial-conditional-cookies-in-both/first-shell-then-prefetch (auto
prefetch)
</LinkAccordion>
</li>
<li data-prefetch="true">
<LinkAccordion
href="/partial-conditional-cookies-in-both/first-shell-then-prefetch"
prefetch={true}
>
/partial-conditional-cookies-in-both/first-shell-then-prefetch
(prefetch=true)
</LinkAccordion>
</li>
</ul>
</main>
)
}
import { getCachedValue, CookieData } from '../../../cached-value'
import { Suspense } from 'react'
import { connection } from 'next/server'
import { unstable_prefetch } from 'next/cache'
export default async function Page() {
const cachedValue = await getCachedValue()
// The tag is "original" during build and switches to "updated" after revalidation.
const shouldUseCookiesInShell = cachedValue.tag !== 'original'
return (
<main>
<h1>
Partial page that uses cookies in the prefetch at build, but starts
using them in the shell after a revalidation (which makes the shell
become not static)
</h1>
<p>{`Cached value: ${cachedValue.tag}, cookies used in ${shouldUseCookiesInShell ? 'shell' : 'prefetch'}`}</p>
{shouldUseCookiesInShell ? (
<CookieData label="in shell" />
) : (
<p>No cookies used in the shell</p>
)}
<Suspense
fallback={
<div id="prefetch-data-fallback">Loading prefetch data...</div>
}
>
<PrefetchOnly>
<div id="prefetch-data">Prefetch data</div>
{!shouldUseCookiesInShell && <CookieData label="in prefetch" />}
</PrefetchOnly>
</Suspense>
<Suspense
fallback={<div id="dynamic-data-fallback">Loading dynamic data...</div>}
>
<DynamicData />
</Suspense>
</main>
)
}
async function PrefetchOnly({ children }) {
await unstable_prefetch()
return children
}
async function DynamicData() {
await connection()
return <div id="dynamic-data">Dynamic data</div>
}
import { getCachedValue, CookieData } from '../../../cached-value'
import { Suspense } from 'react'
import { connection } from 'next/server'
import { unstable_prefetch } from 'next/cache'
export default async function Page() {
const cachedValue = await getCachedValue()
// The tag is "original" during build and switches to "updated" after revalidation.
const shouldUseCookiesInShell = cachedValue.tag === 'original'
return (
<main>
<h1>
Partial page that uses cookies in the shell at build, but starts using
them only the prefetch after a revalidation (which makes the shell
become static)
</h1>
<p>{`Cached value: ${cachedValue.tag}, cookies used in ${shouldUseCookiesInShell ? 'shell' : 'prefetch'}`}</p>
{shouldUseCookiesInShell ? (
<CookieData label="in shell" />
) : (
<p>No cookies used in the shell</p>
)}
<Suspense
fallback={
<div id="prefetch-data-fallback">Loading prefetch data...</div>
}
>
<PrefetchOnly>
<div id="prefetch-data">Prefetch data</div>
{!shouldUseCookiesInShell && <CookieData label="in prefetch" />}
</PrefetchOnly>
</Suspense>
<Suspense
fallback={<div id="dynamic-data-fallback">Loading dynamic data...</div>}
>
<DynamicData />
</Suspense>
</main>
)
}
async function PrefetchOnly({ children }) {
await unstable_prefetch()
return children
}
async function DynamicData() {
await connection()
return <div id="dynamic-data">Dynamic data</div>
}
......@@ -58,22 +58,26 @@ export async function ConditionalCookies() {
return (
<div>
<p>{`Cached value: ${cachedValue.tag}, cookies used: ${shouldUseCookies}`}</p>
{shouldUseCookies && (
<Suspense
fallback={<div id="cookie-data-fallback">Loading cookie data...</div>}
>
<CookieData />
</Suspense>
)}
{shouldUseCookies && <CookieData />}
</div>
)
}
async function CookieData() {
export async function CookieData({ label }: { label?: string }) {
return (
<Suspense
fallback={<div id="cookie-data-fallback">Loading cookie data...</div>}
>
<CookieDataImpl label={label} />
</Suspense>
)
}
async function CookieDataImpl({ label }: { label?: string }) {
await cookies()
return (
<>
<div id="cookie-data">Cookie data</div>
<div id="cookie-data">{`Cookie data${label ? ' ' + label : ''}`}</div>
<Suspense
fallback={
<div id="cookies-runtime-prefetch-data-fallback">
......
'use client'
import Link from 'next/link'
import Link, { type LinkProps } from 'next/link'
import { useState } from 'react'
export function LinkAccordion({
href,
children,
prefetch,
prefetch = 'auto',
}: {
href: string
children: React.ReactNode
prefetch?: boolean
prefetch?: LinkProps['prefetch']
}) {
const [isVisible, setIsVisible] = useState(false)
const prefetchAttr = prefetch === null ? 'auto' : `${prefetch}`
return (
<>
<input
type="checkbox"
checked={isVisible}
onChange={() => setIsVisible(!isVisible)}
data-prefetch={prefetchAttr}
data-link-accordion={href}
/>
{isVisible ? (
......@@ -25,7 +27,7 @@ export function LinkAccordion({
{children}
</Link>
) : (
`${children} (link is hidden)`
<>{children} (link is hidden)</>
)}
</>
)
......
import { Suspense } from 'react'
import { cacheLife } from 'next/cache'
import { connection } from 'next/server'
import { cookies } from 'next/headers'
type Params = { id: string }
......@@ -14,6 +15,8 @@ export const prefetch = 'partial'
export default function Page({ params }: { params: Promise<Params> }) {
return (
<main>
{/* This page is meant to test a runtime shell, so we add a cookies access. */}
<ForceRuntimeShell />
{/* stale: 5 minutes (the App Shell threshold) — included in the
App Shell. */}
<Suspense
......@@ -39,6 +42,18 @@ export default function Page({ params }: { params: Promise<Params> }) {
)
}
function ForceRuntimeShell() {
return (
<Suspense>
<ForceRuntimeShellInner />
</Suspense>
)
}
async function ForceRuntimeShellInner() {
await cookies()
return null
}
async function LongStaleCached() {
'use cache'
cacheLife('hours') // stale: 5 minutes
......
......@@ -144,7 +144,7 @@ describe('App Shell prefetching', () => {
)
})
it('skips the per-link Speculative prefetch for a route with prefetch = "partial"', async () => {
it('skips the per-link Speculative prefetch for a link without prefetch={true}', async () => {
let page: Playwright.Page
const browser = await next.browser('/', {
beforePageLoad(p: Playwright.Page) {
......@@ -180,7 +180,7 @@ describe('App Shell prefetching', () => {
}, 'no-requests')
})
it('does NOT skip the Speculative prefetch for a prefetch={true} link, even on a partial route', async () => {
it('does NOT skip the Speculative prefetch for a link with prefetch={true}', async () => {
let page: Playwright.Page
const browser = await next.browser('/', {
beforePageLoad(p: Playwright.Page) {
......@@ -277,8 +277,9 @@ describe('App Shell prefetching', () => {
.elementByCss('input[data-link-accordion="/short-stale/1"]')
.click()
}, [
// The route reads request data, so its static-attempt hint is unset
// and the prefetch deopts to a runtime request.
// The route deliberately accesses cookies to force using a runtime shell.
// Note that the short-stale cache is omitted from both static and runtime shells,
// and thus is not counted as a runtime access.
{ includes: 'App shell for short-stale', kind: 'runtime' },
])
......
import { ReactNode } from 'react'
export default function Root({ children }: { children: ReactNode }) {
return (
<html>
<body>{children}</body>
</html>
)
}
import { DebugLinkAccordion } from '../components/link-accordion'
export default function Page() {
const linkForSlug = (slug: string) => `/static-for-some-params/${slug}`
return (
<main>
<h2>Prerendered, did not use cookies</h2>
<ul>
<li>
<DebugLinkAccordion
href={linkForSlug('no-cookies')}
prefetch={true}
/>
</li>
</ul>
<h2>Prerendered, did use cookies</h2>
<ul>
<li>
<DebugLinkAccordion
href={linkForSlug('yes-cookies')}
prefetch="auto"
/>
</li>
<li>
<DebugLinkAccordion
href={linkForSlug('yes-cookies')}
prefetch={true}
/>
</li>
</ul>
<h2>Not prerendered, did not use cookies</h2>
<ul>
<li>
<DebugLinkAccordion
href={linkForSlug('not-prerendered-1-no-cookies-prefetch-auto')}
prefetch="auto"
/>
</li>
<li>
<DebugLinkAccordion
href={linkForSlug('not-prerendered-1-no-cookies-prefetch-true')}
prefetch={true}
/>
</li>
</ul>
<h2>Not prerendered, did use cookies</h2>
<ul>
<li>
<DebugLinkAccordion
href={linkForSlug('not-prerendered-2-yes-cookies-prefetch-auto')}
prefetch="auto"
/>
</li>
<li>
<DebugLinkAccordion
href={linkForSlug('not-prerendered-2-yes-cookies-prefetch-true')}
prefetch={true}
/>
</li>
</ul>
</main>
)
}
import { unstable_navigation } from 'next/cache'
import { cookies } from 'next/headers'
import { Suspense } from 'react'
export async function generateStaticParams() {
return [{ slug: 'no-cookies' }, { slug: 'yes-cookies' }]
}
function shouldUseRuntimeDataForSlug(slug: string) {
return !slug.includes('no-cookies')
}
export default function Page({
params,
}: {
params: Promise<{ slug: string }>
}) {
return (
<main>
<p>This page only accesses runtime data for some param values.</p>
<Suspense fallback={<div>Loading param content...</div>}>
<ParamsDependent params={params} />
</Suspense>
</main>
)
}
async function ParamsDependent({
params,
}: {
params: Promise<{ slug: string }>
}) {
const { slug } = await params
console.log(`rendering page for slug ${slug}`)
if (slug.includes('not-prerendered')) {
// TODO(isr-fallbacks): it seems like in `next start` the initial /_tree request
// already kicks off an ISR prerender. We're trying to test the client router's
// handling of ISR fallbacks, so we need to make sure that the prerender does not finish
// before the page itself is requested (because then we'd serve a concrete
// prerender instead of a fallback)
await cachedDelay(500, slug)
}
return (
<div>
<div id="param-value">{`Slug: ${slug}`}</div>
<ConditionalCookiesUse slug={slug} />
<NavigationContent slug={slug} />
</div>
)
}
async function cachedDelay(durationMs: number, key: string) {
'use cache'
await new Promise((resolve) => setTimeout(resolve, durationMs))
return key
}
async function ConditionalCookiesUse({ slug }: { slug: string }) {
const shouldUseRuntimeData = shouldUseRuntimeDataForSlug(slug)
if (shouldUseRuntimeData) {
await cookies()
}
return (
<div>{`Runtime data accessed on ${slug}: ${shouldUseRuntimeData}'`}</div>
)
}
async function NavigationContent({ slug }: { slug: string }) {
await unstable_navigation()
return <div>{`Navigation content on ${slug}`}</div>
}
'use client'
import Link, { type LinkProps } from 'next/link'
import { useState } from 'react'
export function LinkAccordion({
href,
children,
prefetch,
}: {
href: string
children: React.ReactNode
prefetch?: LinkProps['prefetch']
}) {
const [isVisible, setIsVisible] = useState(false)
const prefetchAttr = prefetch === null ? 'auto' : `${prefetch}`
return (
<>
<input
type="checkbox"
checked={isVisible}
onChange={() => setIsVisible(!isVisible)}
data-link-accordion={href}
data-prefetch={prefetchAttr}
/>
{isVisible ? (
<Link href={href} prefetch={prefetch}>
{children}
</Link>
) : (
<>{children} (link is hidden)</>
)}
</>
)
}
export function DebugLinkAccordion({
href,
prefetch = 'auto',
}: {
href: string
prefetch?: LinkProps['prefetch']
}) {
const prefetchDisplay = prefetch === null ? 'auto' : `${prefetch}`
return (
<LinkAccordion href={href} prefetch={prefetch}>
{href} (prefetch={prefetchDisplay})
</LinkAccordion>
)
}
import type { NextConfig } from 'next'
const nextConfig: NextConfig = {
cacheComponents: true,
partialPrefetching: true,
}
export default nextConfig
......@@ -2,13 +2,11 @@ import type * as Playwright from 'playwright'
import { nextTestSetup } from 'e2e-utils'
import { retry } from 'next-test-utils'
import { createRouterAct } from 'router-act'
// Bit values from PrefetchHint enum (const enum, so we duplicate values here)
const ParentInlinedIntoSelf = 0b100000 // 32
const InlinedIntoChild = 0b1000000 // 64
const HeadInlinedIntoSelf = 0b10000000 // 128
const HeadOutlined = 0b100000000 // 256
const PrefetchDisabled = 0b10000000000 // 1024
import { PrefetchHint } from 'next/src/shared/lib/app-router-types'
import {
type HintsManifest,
getHumanReadablePrefetchHints,
} from '../../../../lib/prefetch-hints'
// The subset of the FlightRouterState tuple the assertions below read (see
// FlightRouterState in shared/lib/app-router-types.ts). The segment is
......@@ -44,7 +42,7 @@ const DYNAMIC_TAG = 'dynamic \u25FB'.padStart(OUTLINED_TAG.length)
function renderInliningTree(tree: FlightRouterStateLike): string {
const lines: string[] = []
const isHeadOutlined = ((tree[4] ?? 0) & HeadOutlined) !== 0
const isHeadOutlined = ((tree[4] ?? 0) & PrefetchHint.HeadOutlined) !== 0
collectNodes(tree, '', !isHeadOutlined, false, lines)
if (isHeadOutlined) {
// Metadata is not inlined into any page — render as a standalone sibling.
......@@ -62,9 +60,9 @@ function collectNodes(
slotKey?: string
): void {
const prefetchHints = node[4] ?? 0
const prefetchDisabled = (prefetchHints & PrefetchDisabled) !== 0
const inlinedIntoChild = (prefetchHints & InlinedIntoChild) !== 0
const headInlined = (prefetchHints & HeadInlinedIntoSelf) !== 0
const prefetchDisabled = (prefetchHints & PrefetchHint.PrefetchDisabled) !== 0
const inlinedIntoChild = (prefetchHints & PrefetchHint.InlinedIntoChild) !== 0
const headInlined = (prefetchHints & PrefetchHint.HeadInlinedIntoSelf) !== 0
const slotPrefix =
slotKey !== undefined && slotKey !== 'children' ? `@${slotKey}/` : ''
......@@ -94,7 +92,7 @@ function collectNodes(
if (keys.length > 0) {
const children = Object.values(slots)
const childrenWithParentInlined = children.filter(
(c) => ((c[4] ?? 0) & ParentInlinedIntoSelf) !== 0
(c) => ((c[4] ?? 0) & PrefetchHint.ParentInlinedIntoSelf) !== 0
)
if (inlinedIntoChild && childrenWithParentInlined.length === 0) {
throw new Error(
......@@ -535,25 +533,34 @@ describe('prefetch inlining', () => {
if (isNextStart) {
it('partially generated dynamic route: build hints use the most specific shell', async () => {
const hints = await next.readJSON('.next/server/prefetch-hints.json')
expect(hints['/test-dynamic-partial/[top]/[bottom]'])
.toMatchInlineSnapshot(`
{
"hints": 64,
"slots": {
"children": {
"hints": 96,
"slots": {
"children": {
"hints": 32,
"slots": {
"children": {
"hints": 64,
"slots": {
"children": {
"hints": 160,
"slots": null,
const hintsManifest: HintsManifest = await next.readJSON(
'.next/server/prefetch-hints.json'
)
// The page awaits fallback params (and no other runtime data), so:
// - the shell can be static (ShouldAttemptStaticShell hint set),
// - the prefetch is runtime (ShouldAttemptStaticPrefetch hint is NOT set).
expect(
getHumanReadablePrefetchHints(
hintsManifest['/test-dynamic-partial/[top]/[bottom]']
)
).toMatchInlineSnapshot(`
{
"hints": "InlinedIntoChild | ShouldAttemptStaticShell",
"slots": {
"children": {
"hints": "ParentInlinedIntoSelf | InlinedIntoChild | ShouldAttemptStaticShell",
"slots": {
"children": {
"hints": "ParentInlinedIntoSelf | ShouldAttemptStaticShell",
"slots": {
"children": {
"hints": "InlinedIntoChild | ShouldAttemptStaticShell",
"slots": {
"children": {
"hints": "ParentInlinedIntoSelf | HeadInlinedIntoSelf | ShouldAttemptStaticShell",
"slots": null,
},
},
},
},
......@@ -561,9 +568,8 @@ describe('prefetch inlining', () => {
},
},
},
},
}
`)
}
`)
})
}
......
......@@ -3,7 +3,6 @@ import { cachedDelay, DebugRenderKind } from '../../../shared'
import { cacheLife } from 'next/cache'
export const instant = true
export const prefetch = 'partial'
export default async function Page() {
return (
......
......@@ -3,7 +3,6 @@ import { cachedDelay, DebugRenderKind } from '../../../shared'
import { cacheLife } from 'next/cache'
export const instant = true
export const prefetch = 'partial'
export default async function Page() {
return (
......
......@@ -3,7 +3,6 @@ import { cachedDelay, DebugRenderKind } from '../../../shared'
import { cacheLife } from 'next/cache'
export const instant = true
export const prefetch = 'partial'
export default async function Page() {
return (
......
......@@ -3,7 +3,6 @@ import { cachedDelay, DebugRenderKind } from '../../../shared'
import { cacheLife } from 'next/cache'
export const instant = true
export const prefetch = 'partial'
export default async function Page() {
return (
......
......@@ -3,7 +3,6 @@ import { cachedDelay, DebugRenderKind } from '../../../shared'
import { cacheLife } from 'next/cache'
export const instant = true
export const prefetch = 'partial'
export default async function Page() {
return (
......
import { Suspense } from 'react'
import { DebugRenderKind } from '../../../../../shared'
import { ForceRuntimeShell } from '../../../../../../components/force-runtime-shell'
type Params = { id: string }
......@@ -14,6 +15,7 @@ export const prefetch = 'partial'
export default async function Page({ params }: { params: Promise<Params> }) {
return (
<main>
<ForceRuntimeShell />
<DebugRenderKind />
<p id="intro">
This page performs sync IO after awaiting params, so we should only see
......
import { Suspense } from 'react'
import { DebugRenderKind } from '../../../../shared'
import { ForceRuntimeShell } from '../../../../../components/force-runtime-shell'
type AnySearchParams = { [key: string]: string | string[] | undefined }
......@@ -18,6 +19,7 @@ export default async function Page({
}) {
return (
<main>
<ForceRuntimeShell />
<DebugRenderKind />
<p id="intro">
This page performs sync IO after awaiting searchParams, so we should
......
......@@ -4,7 +4,6 @@
// In the future, this test can be used to check whether we correctly
// *skip* a runtime prefetch if a page was prerendered as static.
export const instant = true
export const prefetch = 'partial'
export default async function Page() {
return (
......
......@@ -5,7 +5,6 @@ import { cachedDelay, DebugRenderKind } from '../../../shared'
export const instant = {
unstable_samples: [{ cookies: [{ name: 'testCookie', value: 'testValue' }] }],
}
export const prefetch = 'partial'
export default async function Page() {
return (
......
......@@ -6,7 +6,6 @@ import { connection } from 'next/server'
export const instant = {
unstable_samples: [{ cookies: [{ name: 'testCookie', value: 'testValue' }] }],
}
export const prefetch = 'partial'
export default async function Page() {
return (
......
import { Suspense } from 'react'
import { cachedDelay, DebugRenderKind, uncachedIO } from '../../../../shared'
import { connection } from 'next/server'
import { ForceRuntimeShell } from '../../../../../components/force-runtime-shell'
export const instant = {
unstable_samples: [{ params: { id: 'test' } }],
}
export const prefetch = 'partial'
type Params = { id: string }
export default async function Page({ params }: { params: Promise<Params> }) {
return (
<main>
{/* Params are URL data and don't force a runtime shell on their own. */}
<ForceRuntimeShell />
<DebugRenderKind />
<p>
This page uses params and some uncached IO, so parts of it should be
......
......@@ -6,7 +6,6 @@ import { connection } from 'next/server'
export const instant = {
unstable_samples: [{ headers: [['host', 'test-host']] }],
}
export const prefetch = 'partial'
export default async function Page() {
return (
......
import { Suspense } from 'react'
import { cachedDelay, DebugRenderKind, uncachedIO } from '../../../shared'
import { connection } from 'next/server'
import { ForceRuntimeShell } from '../../../../components/force-runtime-shell'
export const instant = {
unstable_samples: [{ searchParams: { searchParam: 'value' } }],
}
export const prefetch = 'partial'
type AnySearchParams = { [key: string]: string | string[] | undefined }
......@@ -16,6 +16,8 @@ export default async function Page({
}) {
return (
<main>
{/* Search params are URL data and don't force a runtime shell on their own. */}
<ForceRuntimeShell />
<DebugRenderKind />
<p>
This page uses search params and some uncached IO, so parts of it should
......
......@@ -5,7 +5,6 @@ import { cachedDelay, DebugRenderKind } from '../../../shared'
export const instant = {
unstable_samples: [{ cookies: [{ name: 'testCookie', value: 'testValue' }] }],
}
export const prefetch = 'partial'
export default async function Page() {
return (
......
......@@ -6,7 +6,6 @@ import { connection } from 'next/server'
export const instant = {
unstable_samples: [{ cookies: [{ name: 'testCookie', value: 'testValue' }] }],
}
export const prefetch = 'partial'
export default async function Page() {
return (
......
import { Suspense } from 'react'
import { cachedDelay, DebugRenderKind, uncachedIO } from '../../../shared'
import { connection } from 'next/server'
import { PrefetchContent } from '../../../../components/prefetch-content'
export const instant = true
export const prefetch = 'partial'
export default async function Page() {
return (
......@@ -25,6 +25,7 @@ async function RuntimePrefetchable() {
return (
<div style={{ border: '1px solid blue', padding: '1em' }}>
<div id="timestamp">{`Timestamp: ${now}`}</div>
<PrefetchContent text={`Timestamp (in prefetch): ${now}`} />
<Suspense fallback={<div style={{ color: 'grey' }}>Loading 2...</div>}>
<Dynamic />
</Suspense>
......
......@@ -5,7 +5,6 @@ import { connection } from 'next/server'
export const instant = {
unstable_samples: [{ params: { id: 'test' } }],
}
export const prefetch = 'partial'
type Params = { id: string }
......
......@@ -6,7 +6,6 @@ import { connection } from 'next/server'
export const instant = {
unstable_samples: [{ headers: [['host', 'test-host']] }],
}
export const prefetch = 'partial'
export default async function Page() {
return (
......
......@@ -5,7 +5,6 @@ import { connection } from 'next/server'
export const instant = {
unstable_samples: [{ searchParams: { searchParam: 'value' } }],
}
export const prefetch = 'partial'
type AnySearchParams = { [key: string]: string | string[] | undefined }
......
......@@ -5,7 +5,6 @@ import { cachedDelay, DebugRenderKind } from '../../../shared'
export const instant = {
unstable_samples: [{ cookies: [{ name: 'testCookie', value: 'testValue' }] }],
}
export const prefetch = 'partial'
export default async function Page() {
return (
......
......@@ -6,7 +6,6 @@ import { connection } from 'next/server'
export const instant = {
unstable_samples: [{ cookies: [{ name: 'testCookie', value: 'testValue' }] }],
}
export const prefetch = 'partial'
export default async function Page() {
return (
......
......@@ -6,7 +6,6 @@ import { cookies } from 'next/headers'
export const instant = {
unstable_samples: [{ params: { id: 'test' } }],
}
export const prefetch = 'partial'
type Params = { id: string }
......
......@@ -6,7 +6,6 @@ import { connection } from 'next/server'
export const instant = {
unstable_samples: [{ headers: [['host', 'test-host']] }],
}
export const prefetch = 'partial'
export default async function Page() {
return (
......
......@@ -6,7 +6,6 @@ import { cookies } from 'next/headers'
export const instant = {
unstable_samples: [{ searchParams: { searchParam: 'value' } }],
}
export const prefetch = 'partial'
type AnySearchParams = { [key: string]: string | string[] | undefined }
......@@ -19,8 +18,9 @@ export default async function Page({
<main>
<DebugRenderKind />
<p>
This page passes search params to a public cache, and uses some uncached
IO, so parts of it should be prefetchable with a runtime prefetch.
This page uses search params (passed to a public cache), and uses some
uncached IO, so parts of it should be prefetchable with a runtime
prefetch.
</p>
<Suspense fallback={<div style={{ color: 'grey' }}>Loading 1...</div>}>
<RuntimePrefetchable searchParams={searchParams} />
......
......@@ -3,15 +3,17 @@ import { cachedDelay, DebugRenderKind, uncachedIO } from '../../../../shared'
import { connection } from 'next/server'
import { lang } from 'next/root-params'
import { cookies } from 'next/headers'
import { ForceRuntimeShell } from '../../../../../components/force-runtime-shell'
import { PrefetchContent } from '../../../../../components/prefetch-content'
export const instant = {
unstable_samples: [{ params: { lang: 'en' } }],
}
export const prefetch = 'partial'
export default async function Page() {
return (
<main>
<ForceRuntimeShell />
<DebugRenderKind />
<p>
This page uses root params and some uncached IO. Root params should
......@@ -33,6 +35,7 @@ async function RuntimePrefetchable() {
return (
<div style={{ border: '1px solid blue', padding: '1em' }}>
<div id="root-param-value">{`Lang: ${currentLang}`}</div>
<PrefetchContent text={`Lang (in prefetch): ${currentLang}`} />
<Suspense fallback={<div style={{ color: 'grey' }}>Loading 2...</div>}>
<Dynamic />
</Suspense>
......
......@@ -2,11 +2,11 @@ import { Suspense } from 'react'
import { cachedDelay, DebugRenderKind, uncachedIO } from '../../../../shared'
import { connection } from 'next/server'
import { lang } from 'next/root-params'
import { PrefetchContent } from '../../../../../components/prefetch-content'
export const instant = {
unstable_samples: [{ params: { lang: 'en' } }],
}
export const prefetch = 'partial'
export default async function Page() {
return (
......@@ -30,6 +30,7 @@ async function RuntimePrefetchable() {
return (
<div style={{ border: '1px solid blue', padding: '1em' }}>
<div id="root-param-value">{`Lang: ${currentLang}`}</div>
<PrefetchContent text={`Lang (in prefetch): ${currentLang}`} />
<Suspense fallback={<div style={{ color: 'grey' }}>Loading 2...</div>}>
<Dynamic />
</Suspense>
......
......@@ -2,15 +2,17 @@ import { Suspense } from 'react'
import { cachedDelay, DebugRenderKind, uncachedIO } from '../../../../shared'
import { connection } from 'next/server'
import { lang } from 'next/root-params'
import { ForceRuntimeShell } from '../../../../../components/force-runtime-shell'
import { PrefetchContent } from '../../../../../components/prefetch-content'
export const instant = {
unstable_samples: [{ params: { lang: 'en' } }],
}
export const prefetch = 'partial'
export default async function Page() {
return (
<main>
<ForceRuntimeShell />
<DebugRenderKind />
<p>
This page uses root params (passed to a private cache) and some uncached
......@@ -30,6 +32,7 @@ async function RuntimePrefetchable() {
return (
<div style={{ border: '1px solid blue', padding: '1em' }}>
<div id="root-param-value">{`Lang: ${currentLang}`}</div>
<PrefetchContent text={`Lang (in prefetch): ${currentLang}`} />
<Suspense fallback={<div style={{ color: 'grey' }}>Loading 2...</div>}>
<Dynamic />
</Suspense>
......
import { unstable_prefetch } from 'next/cache'
import { Suspense } from 'react'
/**
* Only renders `text` when requested with a prefetch (i.e. it's excluded from the app shell)
*
* Exists mostly because of `createRouterAct`'s requirement
* that the text in the `includes` assertion is unique:
*
* ```
* The same expected substring was sent multiple times by the server
* [...]
* Choose a more specific substring to assert on.
* ```
* This component lets us concisely add a second string that will only
* appear in the prefetch:
*
* ```tsx
* <div>{`Foo: ${foo}`}</div>
* <PrefetchContent text={`Foo (in prefetch): ${foo}`}} />
* ```
* Which lets us assert separately on `{ includes: "Foo: ..." }` and
* `{ includes: "Foo (in prefetch): ..." }`
* */
export function PrefetchContent({ text }: { text: string }) {
return (
<Suspense
fallback={
<div id="prefetch-content-fallback">Not included in the shell</div>
}
>
<Inner text={text} />
</Suspense>
)
}
async function Inner({ text }: { text: string }) {
await unstable_prefetch()
return <div id="prefetch-content">{text}</div>
}
......@@ -2,6 +2,7 @@ import type { NextConfig } from 'next'
const nextConfig: NextConfig = {
cacheComponents: true,
partialPrefetching: true,
productionBrowserSourceMaps: true,
experimental: {
// TODO: This test asserts on the pre-`varyParams` cache-keying behavior
......
......@@ -46,6 +46,14 @@ export default function Page() {
Param 1
</LinkAccordion>
</li>
<li>
<LinkAccordion
href="/params-used-after-navigation/1"
prefetch={true}
>
Param 1 (prefetch=true)
</LinkAccordion>
</li>
<li>
<Link href="/params-used-after-navigation/2" prefetch={false}>
Param 2 (unprefetched)
......@@ -61,6 +69,11 @@ export default function Page() {
Param 1
</LinkAccordion>
</li>
<li>
<LinkAccordion href="/params-used-in-icon/1" prefetch={true}>
Param 1 (prefetch=true)
</LinkAccordion>
</li>
<li>
<Link href="/params-used-in-icon/2" prefetch={false}>
Params 2
......@@ -73,9 +86,19 @@ export default function Page() {
Uses search params
</LinkAccordion>
</li>
<li>
<LinkAccordion href="/uses-search-params?q=test" prefetch={true}>
Uses search params (prefetch=true)
</LinkAccordion>
</li>
<li>
<LinkAccordion href="/uses-connection">Uses connection</LinkAccordion>
</li>
<li>
<LinkAccordion href="/uses-connection" prefetch={true}>
Uses connection (prefetch=true)
</LinkAccordion>
</li>
<li>
<LinkAccordion href="/uses-runtime-after-navigation">
Uses runtime APIs after navigation()
......@@ -91,6 +114,11 @@ export default function Page() {
Uses runtime APIs after prefetch()
</LinkAccordion>
</li>
<li>
<LinkAccordion href="/uses-runtime-after-prefetch" prefetch={true}>
Uses runtime APIs after prefetch() (prefetch=true)
</LinkAccordion>
</li>
<li>
<LinkAccordion href="/uses-prefetch-static">
Uses prefetch() on a static page
......
......@@ -25,12 +25,14 @@ async function NavigationOnly(props: Props) {
return (
<>
<div id="navigation-content">Navigation content</div>
<ParamsDependent {...props} />
<Suspense fallback={<p id="param-loading">Loading param content...</p>}>
<ParamsDependent {...props} />
</Suspense>
</>
)
}
async function ParamsDependent(props: Props) {
const { id } = await props.params
return <p id="param-value">Post: {id}</p>
return <p id="param-value">{`Post: ${id}`}</p>
}
import {
PrefetchHint,
type PrefetchHints,
} from 'next/src/shared/lib/app-router-types'
const hintsEnumAsObject = Object.fromEntries(
Object.entries(
// @ts-expect-error - const enum (which jest ignores)
PrefetchHint
)
)
const prefetchHintsByName = Object.fromEntries(
Object.entries(hintsEnumAsObject)
.filter(([, value]) => typeof value === 'number')
.map((entry) => entry as [string, PrefetchHint])
)
export function prefetchHintsValuesToHumanReadableString(
hintsValue: number,
mask?: number
): string {
const names: string[] = []
let hasMaskedValues = false
for (const [name, bit] of Object.entries(prefetchHintsByName)) {
if ((hintsValue & bit) !== 0) {
if (mask !== undefined) {
if (bit & mask) {
names.push(name)
} else {
// if the value is not included in the mask, omit it.
hasMaskedValues = true
}
} else {
names.push(name)
}
}
}
// indicate if we've omitted some values due to a mask.
if (hasMaskedValues) {
names.push('...')
}
return names.join(' | ')
}
export type HintsManifest = { [route: string]: PrefetchHints }
export type HumanReadableHints = {
hints: string
slots: null | Record<string, HumanReadableHints>
}
export function getHumanReadablePrefetchHints(
tree: PrefetchHints,
mask?: number
): HumanReadableHints {
return {
hints: prefetchHintsValuesToHumanReadableString(tree.hints, mask),
slots:
tree.slots === null
? null
: Object.fromEntries(
Object.entries(tree.slots).map(([name, slot]) => [
name,
getHumanReadablePrefetchHints(slot, mask),
])
),
}
}
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