Unverified Commit 63f14c6c authored by Janka Uryga's avatar Janka Uryga Committed by GitHub

fix: cache miss in App Shell for cached pages with gSP (#95665)

If we're prerendering an App Shell, then url data is excluded (i.e. we
don't advance beyond the `ShellRuntime` stage). however the prospective
prerender was still letting params/searchParams resolve, so if those
ended up being inputs to a page, they wouldn't be hanging inputs, and
we'd get a cache miss for them in the final prerender.

closes NAR-883

---------
Co-authored-by: 's avatarHendrik Liebau <mail@hendrik-liebau.de>
parent e8a2dd17
...@@ -1446,5 +1446,6 @@ ...@@ -1446,5 +1446,6 @@
"1445": "Invariant: Missing `__NEXT_PRIVATE_ORIGIN` environment variable.", "1445": "Invariant: Missing `__NEXT_PRIVATE_ORIGIN` environment variable.",
"1446": "Missing initURL", "1446": "Missing initURL",
"1447": "Could not determine origin for forwarded Server Actions request. This can happen if port or hostname are not configured for this server.", "1447": "Could not determine origin for forwarded Server Actions request. This can happen if port or hostname are not configured for this server.",
"1448": "Error in the \"%s\" app-route HMR subscription" "1448": "Error in the \"%s\" app-route HMR subscription",
"1449": "Accessed `searchParams` during prerendering."
} }
...@@ -1513,6 +1513,7 @@ async function generateRuntimePrefetchResult( ...@@ -1513,6 +1513,7 @@ async function generateRuntimePrefetchResult(
await prospectiveRuntimeServerPrerender( await prospectiveRuntimeServerPrerender(
ctx, ctx,
isShellPrefetch,
generateDynamicRSCPayload.bind(null, ctx), generateDynamicRSCPayload.bind(null, ctx),
prerenderResumeDataCache, prerenderResumeDataCache,
rootParams, rootParams,
...@@ -1568,6 +1569,7 @@ async function generateRuntimePrefetchResult( ...@@ -1568,6 +1569,7 @@ async function generateRuntimePrefetchResult(
async function prospectiveRuntimeServerPrerender( async function prospectiveRuntimeServerPrerender(
ctx: AppRenderContext, ctx: AppRenderContext,
isShellPrefetch: boolean,
getPayload: () => Promise<RSCPayload>, getPayload: () => Promise<RSCPayload>,
resumeDataCache: PrerenderResumeDataCache | null, resumeDataCache: PrerenderResumeDataCache | null,
rootParams: Params, rootParams: Params,
...@@ -1618,6 +1620,7 @@ async function prospectiveRuntimeServerPrerender( ...@@ -1618,6 +1620,7 @@ async function prospectiveRuntimeServerPrerender(
varyParamsAccumulator: null, varyParamsAccumulator: null,
// No stage sequencing needed for prospective renders. // No stage sequencing needed for prospective renders.
stagedRendering: null, stagedRendering: null,
isSessionShell: isShellPrefetch,
// These are not present in regular prerenders, but allowed in a runtime prerender. // These are not present in regular prerenders, but allowed in a runtime prerender.
headers, headers,
cookies, cookies,
...@@ -1792,8 +1795,8 @@ async function finalRuntimeServerPrerender( ...@@ -1792,8 +1795,8 @@ async function finalRuntimeServerPrerender(
resumeDataCache, resumeDataCache,
hmrRefreshHash: undefined, hmrRefreshHash: undefined,
varyParamsAccumulator, varyParamsAccumulator,
// Used to separate the stages in the 5-task pipeline.
stagedRendering: finalStageController, stagedRendering: finalStageController,
isSessionShell: mode.type === 'session-shell-only',
// These are not present in regular prerenders, but allowed in a runtime prerender. // These are not present in regular prerenders, but allowed in a runtime prerender.
headers, headers,
cookies, cookies,
......
...@@ -164,6 +164,7 @@ export interface PrerenderStoreModernRuntime ...@@ -164,6 +164,7 @@ export interface PrerenderStoreModernRuntime
* renders where all stages run without sequencing. * renders where all stages run without sequencing.
*/ */
readonly stagedRendering: StagedRenderingController | null readonly stagedRendering: StagedRenderingController | null
readonly isSessionShell: boolean
readonly headers: RequestStore['headers'] readonly headers: RequestStore['headers']
readonly cookies: RequestStore['cookies'] readonly cookies: RequestStore['cookies']
......
...@@ -244,6 +244,7 @@ export function createServerParamsForServerSegment( ...@@ -244,6 +244,7 @@ export function createServerParamsForServerSegment(
return createRuntimePrerenderParams( return createRuntimePrerenderParams(
underlyingParams, underlyingParams,
optionalCatchAllParamName, optionalCatchAllParamName,
workStore,
workUnitStore, workUnitStore,
varyParamsAccumulator varyParamsAccumulator
) )
...@@ -431,6 +432,7 @@ function createStaticPrerenderParams( ...@@ -431,6 +432,7 @@ function createStaticPrerenderParams(
function createRuntimePrerenderParams( function createRuntimePrerenderParams(
underlyingParams: Params, underlyingParams: Params,
optionalCatchAllParamName: string | null, optionalCatchAllParamName: string | null,
workStore: WorkStore,
workUnitStore: PrerenderStoreModernRuntime, workUnitStore: PrerenderStoreModernRuntime,
varyParamsAccumulator: VaryParamsAccumulator | null varyParamsAccumulator: VaryParamsAccumulator | null
): Promise<Params> { ): Promise<Params> {
...@@ -450,9 +452,14 @@ function createRuntimePrerenderParams( ...@@ -450,9 +452,14 @@ function createRuntimePrerenderParams(
const { stagedRendering } = workUnitStore const { stagedRendering } = workUnitStore
if (!stagedRendering) { if (!stagedRendering) {
// If there's no staging, we're in a prospective runtime prerender, // If there's no staging, we're in a prospective runtime prerender.
// and it doesn't matter when params resolve. if (workUnitStore.isSessionShell) {
return makeUntrackedParams(userspaceParams) // If we're warming up for a session shell, params should be hanging,
// because they'll be a hanging input in the final prerender.
return makeHangingParams(underlyingParams, workStore, workUnitStore)
} else {
return makeUntrackedParams(userspaceParams)
}
} }
// We don't have fallbackParams in runtime prerenders, so we don't know // We don't have fallbackParams in runtime prerenders, so we don't know
...@@ -709,7 +716,7 @@ const fallbackParamsProxyHandler: ProxyHandler<Promise<Params>> = { ...@@ -709,7 +716,7 @@ const fallbackParamsProxyHandler: ProxyHandler<Promise<Params>> = {
function makeHangingParams( function makeHangingParams(
underlyingParams: Params, underlyingParams: Params,
workStore: WorkStore, workStore: WorkStore,
prerenderStore: StaticPrerenderStoreModern prerenderStore: StaticPrerenderStoreModern | PrerenderStoreModernRuntime
): Promise<Params> { ): Promise<Params> {
const cachedParams = CachedParams.get(underlyingParams) const cachedParams = CachedParams.get(underlyingParams)
if (cachedParams) { if (cachedParams) {
......
...@@ -71,7 +71,15 @@ export function createServerPathnameForMetadata( ...@@ -71,7 +71,15 @@ export function createServerPathnameForMetadata(
underlyingPathname underlyingPathname
) )
} else { } else {
return createRenderPathname(underlyingPathname) if (workUnitStore.isSessionShell) {
return makeHangingPromise<string>(
workUnitStore.renderSignal,
workStore.route,
'`pathname`'
)
} else {
return createRenderPathname(underlyingPathname)
}
} }
} }
case 'request': case 'request':
......
...@@ -14,6 +14,7 @@ import { ...@@ -14,6 +14,7 @@ import {
postponeWithTracking, postponeWithTracking,
annotateDynamicAccess, annotateDynamicAccess,
} from '../app-render/dynamic-rendering' } from '../app-render/dynamic-rendering'
import { dynamicAccessAsyncStorage } from '../app-render/dynamic-access-async-storage.external'
import { import {
workUnitAsyncStorage, workUnitAsyncStorage,
...@@ -142,6 +143,7 @@ export function createServerSearchParamsForServerPage( ...@@ -142,6 +143,7 @@ export function createServerSearchParamsForServerPage(
case 'prerender-runtime': case 'prerender-runtime':
return createRuntimePrerenderSearchParams( return createRuntimePrerenderSearchParams(
underlyingSearchParams, underlyingSearchParams,
workStore,
workUnitStore, workUnitStore,
varyParamsAccumulator varyParamsAccumulator
) )
...@@ -237,19 +239,35 @@ function createStaticPrerenderSearchParams( ...@@ -237,19 +239,35 @@ function createStaticPrerenderSearchParams(
function createRuntimePrerenderSearchParams( function createRuntimePrerenderSearchParams(
underlyingSearchParams: SearchParams, underlyingSearchParams: SearchParams,
workStore: WorkStore,
workUnitStore: PrerenderStoreModernRuntime, workUnitStore: PrerenderStoreModernRuntime,
varyParamsAccumulator: VaryParamsAccumulator | null varyParamsAccumulator: VaryParamsAccumulator | null
): Promise<SearchParams> { ): Promise<SearchParams> {
const underlyingSearchParamsWithVarying = const userspaceSearchParams =
varyParamsAccumulator !== null varyParamsAccumulator !== null
? createVaryingSearchParams(varyParamsAccumulator, underlyingSearchParams) ? createVaryingSearchParams(varyParamsAccumulator, underlyingSearchParams)
: underlyingSearchParams : underlyingSearchParams
const result = makeUntrackedSearchParams(underlyingSearchParamsWithVarying) const result = makeUntrackedSearchParams(userspaceSearchParams)
const { stagedRendering } = workUnitStore const { stagedRendering } = workUnitStore
if (!stagedRendering) { if (!stagedRendering) {
// If there's no staging, we're in a prospective runtime prerender.
if (workUnitStore.isSessionShell) {
// If we're warming up for a session shell, search params should hang,
// because they'll be a hanging input in the final prerender.
return makeHangingSearchParams(workStore, workUnitStore)
}
return result return result
} }
// Unlike `createRuntimePrerenderParams`, which uses `delayUntilStage`, we
// resolve with `waitForStage(...).then(...)` here. Switching search params to
// `delayUntilStage` drops the source code frame from the instant-validation
// "URL data outside of Suspense" error when a page awaits `searchParams` at
// the top level (params, read via a nested component, is unaffected). See the
// `missing suspense around search params` cases in the instant-validation
// `suspense-boundaries` tests. The underlying reason in React's async I/O
// await tracking isn't understood yet. TODO: align search params with params
// on `delayUntilStage` once resolved.
const searchParamsStage = RENDER_STAGES_BY_DATA_KIND.runtimeLinkData const searchParamsStage = RENDER_STAGES_BY_DATA_KIND.runtimeLinkData
return stagedRendering.waitForStage(searchParamsStage).then(() => result) return stagedRendering.waitForStage(searchParamsStage).then(() => result)
} }
...@@ -357,7 +375,7 @@ const CachedSearchParamsForUseCache = new WeakMap< ...@@ -357,7 +375,7 @@ const CachedSearchParamsForUseCache = new WeakMap<
function makeHangingSearchParams( function makeHangingSearchParams(
workStore: WorkStore, workStore: WorkStore,
prerenderStore: PrerenderStoreModern prerenderStore: PrerenderStoreModern | PrerenderStoreModernRuntime
): Promise<SearchParams> { ): Promise<SearchParams> {
const cachedSearchParams = CachedSearchParams.get(prerenderStore) const cachedSearchParams = CachedSearchParams.get(prerenderStore)
if (cachedSearchParams) { if (cachedSearchParams) {
...@@ -370,9 +388,9 @@ function makeHangingSearchParams( ...@@ -370,9 +388,9 @@ function makeHangingSearchParams(
'`searchParams`' '`searchParams`'
) )
const proxiedPromise = new Proxy(promise, { const proxyHandler: ProxyHandler<Promise<SearchParams>> = {
get(target, prop, receiver) { get(target, prop, receiver) {
if (Object.hasOwn(promise, prop)) { if (Object.hasOwn(target, prop)) {
// The promise has this property directly. we must return it. // The promise has this property directly. we must return it.
// We know it isn't a dynamic access because it can only be something // We know it isn't a dynamic access because it can only be something
// that was previously written to the promise and thus not an underlying searchParam value // that was previously written to the promise and thus not an underlying searchParam value
...@@ -380,11 +398,32 @@ function makeHangingSearchParams( ...@@ -380,11 +398,32 @@ function makeHangingSearchParams(
} }
switch (prop) { switch (prop) {
case 'then': { case 'then':
const expression = case 'catch':
'`await searchParams`, `searchParams.then`, or similar' case 'finally': {
annotateDynamicAccess(expression, prerenderStore) const originalMethod = ReflectAdapter.get(target, prop, receiver)
return ReflectAdapter.get(target, prop, receiver) return {
[prop]: (...args: unknown[]) => {
const expression =
'`await searchParams`, `searchParams.then`, or similar'
annotateDynamicAccess(expression, prerenderStore)
// Mirror `makeHangingParams`: when this never-resolving promise
// is awaited while a `use cache` key is being encoded
// (dynamicAccessAsyncStorage is set), abort so the surrounding
// cache bails out to a dynamic hole instead of hanging on it.
// Without this, a private cache that reads `searchParams` would
// stall the App Shell cache-warming render. Re-wrapping the
// result propagates the same behavior to promises derived via
// `.then`/`.catch`/`.finally` that are then passed into a cache.
const dynamicAccessStore = dynamicAccessAsyncStorage.getStore()
if (dynamicAccessStore) {
dynamicAccessStore.abortController.abort(
new Error('Accessed `searchParams` during prerendering.')
)
}
return new Proxy(originalMethod.apply(target, args), proxyHandler)
},
}[prop]
} }
case 'status': { case 'status': {
const expression = const expression =
...@@ -398,7 +437,9 @@ function makeHangingSearchParams( ...@@ -398,7 +437,9 @@ function makeHangingSearchParams(
} }
} }
}, },
}) }
const proxiedPromise = new Proxy(promise, proxyHandler)
CachedSearchParams.set(prerenderStore, proxiedPromise) CachedSearchParams.set(prerenderStore, proxiedPromise)
return proxiedPromise return proxiedPromise
......
import { Suspense } from 'react'
import { cookies } from 'next/headers'
// Reading a cookie makes the shell depend on session data, so a prefetch
// renders a runtime App Shell. This is what exercises the prospective/final
// runtime prerender where params are a hanging input in a cached page.
async function SessionData() {
const cookieStore = await cookies()
const value = cookieStore.get('testCookie')?.value ?? 'none'
return <p id="cookie-value">{`Cookie: ${value}`}</p>
}
export default async function Layout({ children }: LayoutProps<'/'>) {
return (
<html>
<body>
<Suspense fallback={<p id="cookie-loading">Loading cookie...</p>}>
<SessionData />
</Suspense>
{children}
</body>
</html>
)
}
import { LinkAccordion } from '../components/link-accordion'
export default function Page() {
return (
<main>
<ul>
<li>
<LinkAccordion href="/slug/prerendered">
prerendered from gSP - /slug/prerendered
</LinkAccordion>
</li>
<li>
<LinkAccordion href="/slug/not-prerendered">
not prerendered - /slug/not-prerendered
</LinkAccordion>
</li>
</ul>
</main>
)
}
export async function generateStaticParams() {
return [{ slug: 'prerendered' }]
}
export default async function Page({
params,
}: {
params: Promise<{ slug: string }>
}) {
'use cache'
const { slug } = await params
return (
<main>
<h1>Cached page with params</h1>
<p id="slug">{`Slug: ${slug}`}</p>
</main>
)
}
'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)
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,
}
export default nextConfig
import { nextTestSetup } from 'e2e-utils'
import type * as Playwright from 'playwright'
import { createRouterAct } from 'router-act'
import { retry } from 'next-test-utils'
const CACHE_MISS_WARNING = 'Unexpected cache miss after cache warming phase'
// The App Shell prefetch (next-router-prefetch: '3') renders the runtime shell
// of the /slug/[slug] route. Because the page is a `'use cache'` page that
// awaits static params, params are a hanging input in the final prerender. If
// the prospective (cache-warming) prerender resolved those params instead of
// leaving them hanging, the cached page's key would differ between the two
// prerenders and the final prerender would log an "Unexpected cache miss"
// warning and degrade the cached segment to a dynamic hole.
describe('App Shell prefetching - cached page with generateStaticParams', () => {
const { next, isNextDev } = nextTestSetup({
files: __dirname,
})
if (isNextDev) {
it('is skipped', () => {})
return
}
it('does not report a cache miss when prefetching the shell of a cached page that reads static params', async () => {
let page: Playwright.Page
const browser = await next.browser('/', {
beforePageLoad(p: Playwright.Page) {
page = p
},
})
const act = createRouterAct(page, { includeAppShellRequests: true })
// Revealing the link prefetches the App Shell for /slug/[slug]. Because the
// shell is param-independent, this single request drives the runtime shell
// prerender of the cached page whose params are a hanging input.
await act(async () => {
await browser
.elementByCss('input[data-link-accordion="/slug/prerendered"]')
.click()
})
expect(next.cliOutput).not.toContain(CACHE_MISS_WARNING)
})
it('renders the cached page content after navigating to a prefetched shell', async () => {
let page: Playwright.Page
const browser = await next.browser('/', {
beforePageLoad(p: Playwright.Page) {
page = p
},
})
const act = createRouterAct(page, { includeAppShellRequests: true })
await act(async () => {
await browser
.elementByCss('input[data-link-accordion="/slug/prerendered"]')
.click()
})
await browser.elementByCss('a[href="/slug/prerendered"]').click()
await retry(async () => {
expect(await browser.elementById('slug').text()).toEqual(
'Slug: prerendered'
)
})
expect(next.cliOutput).not.toContain(CACHE_MISS_WARNING)
})
})
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