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

[PPF] Fix missing runtime-follow up if hints are stale (#98278)

if a page didn't use runtime data during build but started doing so
after the revalidation (i.e. the hints say its statically-prefetchable
but now it's not), we're supposed to be doing a follow-up runtime
request based on the `needsRuntimeRequest` promise (introduced in
#96095), but this was not actually happening.

the bug goes as follows:
1. if we get an entry from a static prefetch, but `needsRuntimeRequest`
is true, we save it as `PPR` (or `StaticShell` if it's a shell rewound
from the static response). otherwise, we'd have saved it as `PPRRuntime`
(or `RuntimeShell` for shells) to indicate that it satisfies those
requests.
2. `wouldRuntimeRequestProvideMore` compares the runtime strategy for
the current static walk to the entry's strategy to check if it should do
a runtime request. this boils down to a comparison of `RuntimeShell >
PPR` for a shell request
3. however, in the `FetchStrategy` enum, `RuntimeShell = 2` and `PPR =
3`, so `RuntimeShell > PPR` is false, which makes
`wouldRuntimeRequestProvideMore` return `false`. so, if we have a `PPR`
entry, **we'll never do a follow-up runtime shell request**.

This is fixed by swapping the order of the two values in the enum.

Now, if we end up with a `PPR` response where `needsRuntimeRequest` is
true due to runtime data, that indicates that we *should've done a
runtime request in the first place*, we just didn't know we should do so
because the prefetch hint was stale, so the new ordering achieves the
correct behavior, i.e. `pingSegmentBundle` decides that it needs a
runtime shell request, which makes it match the intended PPF prefetching
behavior.

The enum ordering is a bit of a lie, because there is no single correct
ordering -- a `PPR` entry *might actually provide more content* than a
`ShellRuntime` entry if the route uses static params. In practice this
is resolved by the vary path (shell responses are keyed with fallback
params, and a PPR entry with static params would have the actual param
values, and the param-ful entry would end up being preferred) so the
strategy order wouldn't matter.

Now that `RuntimeShell` beats `PPR`, i had to disable the strategy
upgrading (`StaticShell -> RuntimeShell` and `PPR -> PPRRuntime`) on
routes that don't use Partial Prefetching -- otherwise, a
rewound`ShellRuntime` entry from a different param was winning over
`PPR` in `pingSegmentBundle`, so it was preventing issuing new static
requests even if we needed to do them. However, without PPF, the runtime
tiers are not meaningful anyway, so this should be fine.

### Other bugs

This fix also revealed a bug where `FetchStrategy.Full` requests were
writing a shell as `RuntimeShell` even though the byte offset only marks
a *static* shell. I've refactored the logic for picking the strategies
to fix this and make it more explicit.

### Edge cases

there is one (somewhat involved) edge case to this that i know of.
if the post-revalidation static response has:

1. a shell that didn't access runtime data (i.e. `needsRuntimeRequest =
false` in the shell stage)
2. a prefetch that did access runtime data (i.e. `needsRuntimeRequest =
true` in the static stage)

then we'll:

1. write the shell as `ShellRuntime` (because `needsRuntimeRequest =
false`, and a runtime shell would not in fact provide more data)
2. write the prefetch as `PPR` (and not `PPRRuntime`, because
`needsRuntimeRequest = true`, and a runtime prefetch *would* provide
more data)

with the new ordering, this means we'll end up preferring the
`ShellRuntime` entry over the `PPR` entry even though the shell is a
strict subset of it and would be missing static params and content gated
behind `prefetch()` or `navigation()`. (note that this only happens if
they share the same vary path)

this can be observed as follows:
1. when a `<Link prefetch={true}>` to such a page is revealed, we won't
request a runtime shell (due to already having a `ShellRuntime` entry),
but we will request a runtime prefetch (because `PPR` is not enough). so
far this is correct
2. if the navigation happens **before** this runtime prefetch completes,
we will display what we already have. but we'll prefer the rewound
`ShellRuntime` shell entry over than the `PPR` entry it originated from
because of the enum ordering. so we may end up not showing content
despite having it in the cache

i'm not sure how to resolve this yet, but i think the current
`recordedFetchStrategy` trick can't handle this, and we need a more
involved mechanism that can detect when the `PPR` entry is actually
better. i don't consider this a blocker for this fix though
parent f4449e8f
...@@ -2259,35 +2259,48 @@ function pingSegmentBundle( ...@@ -2259,35 +2259,48 @@ function pingSegmentBundle(
// entry itself — nothing ever pings a Rejected entry. // entry itself — nothing ever pings a Rejected entry.
break break
case EntryStatus.Fulfilled: { case EntryStatus.Fulfilled: {
const runtimeWouldProvideMore = wouldRuntimeRequestProvideMore( let willBeSupersededByRuntimeRequest = false
nodeEntry, if (walkRequiresRuntimeCompleteness(fetchStrategy, route)) {
fetchStrategy const runtimeWouldProvideMore = wouldRuntimeRequestProvideMore(
)
// An eligible shell-tier entry takes the static attempt path below
// (the revalidation) instead of deopting straight to a runtime
// request; see isShellEntryEligibleForStaticAttempt. The attempt
// can't recur: its response records at least the concrete static
// tier, which fails the shell-tier check on the re-run pass — and an
// attempt that already settled without healing the entry reads as
// ineligible, so the deopt proceeds after all.
const shellEntryEligibleForStaticAttempt =
isShellEntryEligibleForStaticAttempt(
now,
task.segmentCacheMap,
nodeEntry, nodeEntry,
nodeTree,
fetchStrategy fetchStrategy
) )
if (runtimeWouldProvideMore && !shellEntryEligibleForStaticAttempt) { // An eligible shell-tier entry takes the static attempt path below
// A runtime request would return more content for this segment // (the revalidation) instead of deopting straight to a runtime
// than the entry contains. Surface it via the return value, so the // request; see isShellEntryEligibleForStaticAttempt. The attempt
// caller can deopt this subtree to a runtime prefetch. (An // can't recur: its response records at least the concrete static
// eligible shell-tier entry withholds the signal for this pass: // tier, which fails the shell-tier check on the re-run pass — and an
// the static attempt spawned below blocks the task, and the re-run // attempt that already settled without healing the entry reads as
// pass reads the attempt's result instead.) // ineligible, so the deopt proceeds after all.
needsRuntimeRequest = true const shellEntryEligibleForStaticAttempt =
isShellEntryEligibleForStaticAttempt(
now,
task.segmentCacheMap,
nodeEntry,
nodeTree,
fetchStrategy
)
if (runtimeWouldProvideMore && !shellEntryEligibleForStaticAttempt) {
// A runtime request would return more content for this segment
// than the entry contains. Surface it via the return value, so the
// caller can deopt this subtree to a runtime prefetch. (An
// eligible shell-tier entry withholds the signal for this pass:
// the static attempt spawned below blocks the task, and the re-run
// pass reads the attempt's result instead.)
needsRuntimeRequest = true
// If a runtime request would return more content skip the static path
// entirely — unless the entry is an eligible shell-tier entry (see above),
// whose static attempt IS this upgrade.
// Otherwise the runtime request covers this segment and supersedes
// anything a static fetch could add, so a static upgrade would at best
// duplicate it — delivering the same content twice — and at worst replace
// runtime content with static content.
willBeSupersededByRuntimeRequest = true
}
} }
// For entries below this phase's tier, upgrade during the phase // For entries below this phase's tier, upgrade during the phase
...@@ -2295,23 +2308,8 @@ function pingSegmentBundle( ...@@ -2295,23 +2308,8 @@ function pingSegmentBundle(
// Speculative phase is to bring the cache up to the // Speculative phase is to bring the cache up to the
// per-link-concrete tier. `isPartial` ensures a complete entry isn't // per-link-concrete tier. `isPartial` ensures a complete entry isn't
// re-fetched. // re-fetched.
// // If we can use runtime requests and a runtime request would provide more
// Exception: when a runtime request would return more AND this walk // data, we also skip the upgrade (see `willBeSupersededByRuntimeRequest`)
// permits one, skip the static path entirely — unless the entry is
// an eligible shell-tier entry (see above), whose static attempt IS
// this upgrade. Otherwise the runtime request covers this segment
// and supersedes anything a static fetch could add, so a static
// upgrade would at best duplicate it — delivering the same content
// twice — and at worst replace runtime content with static content.
//
// When no runtime request is permitted, the signal is irrelevant:
// nothing can act on it, so it must not suppress the static upgrade.
// That's what keeps a link prefetching static content on top of a
// cached shell.
const willBeSupersededByRuntimeRequest =
runtimeWouldProvideMore &&
!shellEntryEligibleForStaticAttempt &&
walkRequiresRuntimeCompleteness(fetchStrategy, route)
// Check if we should attempt to upgrade a fallback ISR response to // Check if we should attempt to upgrade a fallback ISR response to
// a concrete version. // a concrete version.
......
...@@ -59,8 +59,8 @@ export const enum FetchStrategy { ...@@ -59,8 +59,8 @@ export const enum FetchStrategy {
// session data (cookies, headers) — and less complete than PPR at concrete // session data (cookies, headers) — and less complete than PPR at concrete
// paths, which includes prerendered param-dependent content. // paths, which includes prerendered param-dependent content.
StaticShell = 1, StaticShell = 1,
RuntimeShell = 2, PPR = 2,
PPR = 3, RuntimeShell = 3,
PPRRuntime = 4, PPRRuntime = 4,
Full = 5, Full = 5,
} }
......
export default async function Layout({ children }: LayoutProps<'/'>) {
return (
<html>
<body>{children}</body>
</html>
)
}
import { LinkAccordion } from '../components/link-accordion'
export default async function Page() {
return (
<main>
<h2>Conditional cookies in the shell of a static page</h2>
<ul>
<li>
<LinkAccordion href="/static-conditional-cookies-in-shell/static-shell-equal-to-prefetch">
/static-conditional-cookies-in-shell/static-shell-equal-to-prefetch
</LinkAccordion>
</li>
<li>
<LinkAccordion href="/static-conditional-cookies-in-shell/static-shell-smaller-than-prefetch">
/static-conditional-cookies-in-shell/static-shell-smaller-than-prefetch
</LinkAccordion>
</li>
</ul>
<h2>Conditional cookies in the shell of a partial page</h2>
<ul>
<li>
<LinkAccordion href="/partial-conditional-cookies-in-shell/static-shell-equal-to-prefetch">
/partial-conditional-cookies-in-shell/static-shell-equal-to-prefetch
</LinkAccordion>
</li>
<li>
<LinkAccordion href="/partial-conditional-cookies-in-shell/static-shell-smaller-than-prefetch">
/partial-conditional-cookies-in-shell/static-shell-smaller-than-prefetch
</LinkAccordion>
</li>
</ul>
<h2>Conditional cookies in the prefetch of a partial page</h2>
<ul>
<li data-prefetch="auto">
<LinkAccordion href="/partial-conditional-cookies-in-prefetch">
/partial-conditional-cookies-in-prefetch (auto prefetch)
</LinkAccordion>
</li>
<li data-prefetch="true">
<LinkAccordion
href="/partial-conditional-cookies-in-prefetch"
prefetch={true}
>
/partial-conditional-cookies-in-prefetch (prefetch=true)
</LinkAccordion>
</li>
</ul>
</main>
)
}
import { ConditionalCookies } from '../../cached-value'
import { Suspense } from 'react'
import { connection } from 'next/server'
import { unstable_prefetch } from 'next/cache'
export default async function Page() {
return (
<main>
<h1>Partial page that conditionally uses cookies in the prefetch</h1>
<Suspense
fallback={
<div id="prefetch-data-fallback">Loading prefetch data...</div>
}
>
<PrefetchOnly>
<div id="prefetch-data">Prefetch data</div>
<ConditionalCookies />
</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 { ConditionalCookies } from '../../../cached-value'
import { Suspense } from 'react'
import { connection } from 'next/server'
export default async function Page() {
return (
<main>
<h1>Partial page that conditionally uses cookies in the shell</h1>
<ConditionalCookies />
<Suspense
fallback={<div id="dynamic-data-fallback">Loading dynamic data...</div>}
>
<DynamicData />
</Suspense>
</main>
)
}
async function DynamicData() {
await connection()
return <div id="dynamic-data">Dynamic data</div>
}
import { ConditionalCookies } from '../../../cached-value'
import { Suspense } from 'react'
import { connection } from 'next/server'
import { unstable_prefetch } from 'next/cache'
export default async function Page() {
return (
<main>
<h1>
Partial page that conditionally uses cookies in the shell, but also has
prefetch-only content
</h1>
<ConditionalCookies />
<Suspense
fallback={
<div id="prefetch-data-fallback">Loading prefetch data...</div>
}
>
<PrefetchData />
</Suspense>
<Suspense
fallback={<div id="dynamic-data-fallback">Loading dynamic data...</div>}
>
<DynamicData />
</Suspense>
</main>
)
}
async function DynamicData() {
await connection()
return <div id="dynamic-data">Dynamic data</div>
}
async function PrefetchData() {
await unstable_prefetch()
return <div id="prefetch-data">Prefetch data</div>
}
import { resetCachedValue } from '../../cached-value'
export async function POST() {
const newValue = await resetCachedValue()
return Response.json(newValue)
}
import { ConditionalCookies } from '../../../cached-value'
export default async function Page() {
return (
<main>
<h1>Static page that conditionally uses cookies in the shell</h1>
<ConditionalCookies />
</main>
)
}
import { ConditionalCookies } from '../../../cached-value'
import { Suspense } from 'react'
import { unstable_prefetch } from 'next/cache'
export default async function Page() {
return (
<main>
<h1>
Static page that conditionally uses cookies in the shell, but also has
prefetch-only content
</h1>
<ConditionalCookies />
<Suspense
fallback={
<div id="prefetch-data-fallback">Loading prefetch data...</div>
}
>
<PrefetchData />
</Suspense>
</main>
)
}
async function PrefetchData() {
await unstable_prefetch()
return <div id="prefetch-data">Prefetch data</div>
}
import { updateCachedValue } from '../../cached-value'
export async function POST() {
const newValue = await updateCachedValue()
return Response.json(newValue)
}
import { cacheTag, revalidateTag, unstable_prefetch } from 'next/cache'
import * as fs from 'node:fs'
import { Suspense } from 'react'
import { cookies } from 'next/headers'
// value.json at the root of the app
let CACHE_FILE: string
if (process.env.CACHE_FILE) {
// allow overriding, because that can be useful when running the app directly
CACHE_FILE = process.env.CACHE_FILE
} else {
// resolving a relative path seems strangely difficult to do consistently
// in a way that works both when running the app directly and when running
// it in tests, so we rely on the app root being at CWD instead
CACHE_FILE = process.cwd() + '/value.json'
if (!fs.existsSync(/*turbopackIgnore: true*/ CACHE_FILE)) {
throw new Error(
`Cache file not found at ${CACHE_FILE}. This app must run with its root as CWD or provide a path to a cache file via the CACHE_FILE environment variable`
)
}
}
const CACHE_TAG = 'cache-value'
export type Value = { tag: 'original' | 'updated'; timestamp: number }
export async function updateCachedValue(): Promise<Value> {
const newValue: Value = { tag: 'updated', timestamp: Date.now() }
writeNewValue(newValue)
return newValue
}
export async function resetCachedValue(): Promise<Value> {
const newValue: Value = { tag: 'original', timestamp: -1 }
writeNewValue(newValue)
return newValue
}
function writeNewValue(value: Value) {
fs.writeFileSync(CACHE_FILE, JSON.stringify(value))
revalidateTag(CACHE_TAG, { expire: 0 })
}
export async function getCachedValue(): Promise<Value> {
'use cache'
cacheTag(CACHE_TAG)
const value: Value = JSON.parse(
fs.readFileSync(/*turbopackIgnore: true*/ CACHE_FILE, 'utf8')
)
return value
}
export async function ConditionalCookies() {
const cachedValue = await getCachedValue()
// The tag is "original" during build and switches to "updated" after revalidation.
const shouldUseCookies = cachedValue.tag !== 'original'
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>
)}
</div>
)
}
async function CookieData() {
await cookies()
return (
<>
<div id="cookie-data">Cookie data</div>
<Suspense
fallback={
<div id="cookies-runtime-prefetch-data-fallback">
Loading runtime prefetch data... (behind cookies)
</div>
}
>
<RuntimePrefetchData />
</Suspense>
</>
)
}
async function RuntimePrefetchData() {
await unstable_prefetch()
return (
<div id="cookies-runtime-prefetch-data">
Runtime prefetch data (behind cookies)
</div>
)
}
'use client'
import Link from 'next/link'
import { useState } from 'react'
export function LinkAccordion({
href,
children,
prefetch,
}: {
href: string
children: React.ReactNode
prefetch?: boolean
}) {
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)`
)}
</>
)
}
import type { NextConfig } from 'next'
const nextConfig: NextConfig = {
cacheComponents: true,
partialPrefetching: true,
experimental: {
// reactDebugChannel: true
},
typescript: {
ignoreBuildErrors: true,
},
productionBrowserSourceMaps: true,
}
export default nextConfig
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