Unverified Commit 06136c4a authored by Andrew Clark's avatar Andrew Clark Committed by GitHub

[Partial Prefetching] Don't report vary params in responses that include dynamic data (#99700)

A navigation response that includes dynamic data should not report any
vary params, because (until the Ledgers feature lands in React), we
cannot track params during a streaming render. This updates the server
to send `null`, which represents an unknown set of params.

The previous structure was mostly harmless because the client only uses
the vary params when writing to its local cache, and navigation
responses are not written to the cache (except when they are fully
static). The one exception is when performing a full prefetch when
Partial Prefetching is not enabled — the client had a special case to
ignore the (bogus) vary params sent by the server and treat them as
unknown. The special case is no longer necessary after this fix.

Now the server's vary params can be treated as authoritative in all
cases without special handling on the client.
parent 00f25a86
......@@ -3489,21 +3489,14 @@ function writeSegmentDataIntoCache(
// Decide whether to re-key the entry under a more generic vary path based on
// which params the segment actually depends on.
//
// Skip re-keying for Full prefetches: as of today, `varyParams` tracking only
// works within the static stage portion of a response. A Full prefetch
// response covers all stages, and we can't track params during the dynamic
// stage without dead-locking the Flight stream, so the server-reported set is
// incomplete and can't be trusted for the full response. Re-keying with an
// untrustworthy set could replace concrete params with Fallback and let
// unrelated URLs read each other's content from the cache.
//
// Key the entry by which params the server said this segment depends on
// (judged by the payload's CONTENT: contentFetchStrategy differs from
// fetchStrategy exactly when a shell request's response turned out to
// carry more — the coincident case). Reusing one copy across param values
// is the point of the shell, but it requires knowing the content doesn't
// depend on those params, and the server's report is the direct evidence
// of that.
// of that. This holds for every fetch strategy, Full included: if a report
// is wrong, the fix belongs on the server.
//
// Without that report, assume every param varies — a response without a
// shell/full split is also what a page fully prerendered at concrete
......@@ -3519,11 +3512,7 @@ function writeSegmentDataIntoCache(
// evidence the key derivation below trusts, under the same condition, with
// the same correction; otherwise null, and consumers assume every param.
let recordedVaryParams: VaryParams | null = null
if (
process.env.__NEXT_VARY_PARAMS &&
payloadStrategy !== FetchStrategy.Full &&
segmentVaryParams !== null
) {
if (process.env.__NEXT_VARY_PARAMS && segmentVaryParams !== null) {
// Read the reported set now, when the key is chosen. The payload is fully
// buffered by the time it's written, so the source has settled; a read of
// null means the report is unavailable and every param varies.
......
......@@ -980,7 +980,14 @@ async function generateStagedDynamicFlightRenderResultNode(
// Initialize stale time tracking on the request store.
requestStore.stale = INFINITE_CACHE
requestStore.stagedRendering = stageController
requestStore.varyParamsAccumulator = createResponseVaryParamsAccumulator()
// No vary params tracking. This response includes dynamic data, and param
// tracking can't cover the dynamic stage, so the report would be incomplete.
// Leaving the accumulator unset sends no vary params, which the client
// treats as unknown and keys on every param.
// TODO: We can start tracking params during navigations once Ledger support
// lands in React.
requestStore.asyncApiPromises = createAsyncApiPromises(
stageController,
requestStore.cookies,
......@@ -1099,12 +1106,9 @@ async function generateStagedDynamicFlightRenderResultNode(
() => stageController.advanceStage(RenderStage.Static),
() => {
// This is a separate task that doesn't advance a stage. It forces
// draining the immediate queue so that the stale time iterable and vary
// params accumulators are flushed before we advance to the dynamic stage.
// draining the immediate queue so that the stale time iterable is
// flushed before we advance to the dynamic stage.
staleTimeIterable.close()
if (requestStore.varyParamsAccumulator) {
finishAccumulatingVaryParams(requestStore.varyParamsAccumulator)
}
},
() => stageController.advanceStage(RenderStage.Dynamic)
)
......@@ -3900,8 +3904,10 @@ async function renderToStream(
requestStore.mutableCookies,
requestStore.headers
)
requestStore.varyParamsAccumulator =
createResponseVaryParamsAccumulator()
// No vary params tracking. This response includes dynamic data, and
// param tracking can't cover the dynamic stage, so the report would be
// incomplete. Leaving the accumulator unset sends no vary params,
// which the client treats as unknown and keys on every param.
trackStaleTime(
requestStore as { stale: number },
......@@ -4016,12 +4022,9 @@ async function renderToStream(
() => stageController.advanceStage(RenderStage.Static),
() => {
// This is a separate task that doesn't advance a stage. It forces
// draining the immediate queue so that the stale time iterable and vary
// params accumulators are flushed before we advance to the dynamic stage.
// draining the immediate queue so that the stale time iterable is
// flushed before we advance to the dynamic stage.
staleTimeIterable.close()
if (requestStore.varyParamsAccumulator) {
finishAccumulatingVaryParams(requestStore.varyParamsAccumulator)
}
},
() => stageController.advanceStage(RenderStage.Dynamic)
)
......
......@@ -1454,6 +1454,39 @@ describe('cached navigations', () => {
})
})
it('does not reuse a full prefetch for a different param that is read after connection()', async () => {
let page: Playwright.Page
const browser = await next.browser('/dynamic-param', {
beforePageLoad(p: Playwright.Page) {
page = p
},
})
const act = createRouterAct(page)
// Full prefetch of /dynamic-param/a. The page reads the param only after
// connection(), so the read happens in the dynamic part of the response.
await act(
async () => {
await browser
.elementByCss('input[data-link-accordion="/dynamic-param/a"]')
.click()
},
{ includes: 'Param: a' }
)
// Navigating to /dynamic-param/b must not be served from a's prefetch.
await act(
async () => {
await browser
.elementByCss('input[data-link-accordion="/dynamic-param/b"]')
.click()
await browser.elementByCss('a[href="/dynamic-param/b"]').click()
},
{ includes: 'Param: b' }
)
expect(await browser.elementById('dynamic-param').text()).toBe('Param: b')
})
// A `prefetch` config that enables Partial Prefetching ('partial') also opts
// the route into runtime Cached Navigations, even though this fixture does
// not set the global `partialPrefetching` flag.
......
import { connection } from 'next/server'
import { Suspense } from 'react'
export default function Page({
params,
}: {
params: Promise<{ slug: string }>
}) {
return (
<Suspense fallback={<p id="dynamic-param">Loading...</p>}>
<DynamicParam params={params} />
</Suspense>
)
}
async function DynamicParam({ params }: { params: Promise<{ slug: string }> }) {
// The param is read only after the render reaches the dynamic stage.
await connection()
const { slug } = await params
return <p id="dynamic-param">{`Param: ${slug}`}</p>
}
import { LinkAccordion } from '../../components/link-accordion'
export default function DynamicParamHub() {
return (
<main>
<h1>Dynamic Param Hub</h1>
<ul>
<li>
<LinkAccordion href="/dynamic-param/a" prefetch={true}>
/dynamic-param/a (prefetch=true)
</LinkAccordion>
</li>
<li>
<LinkAccordion href="/dynamic-param/b" prefetch={false}>
/dynamic-param/b
</LinkAccordion>
</li>
</ul>
</main>
)
}
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