Unverified Commit 4286e5ff authored by Sebastian "Sebbie" Silbermann's avatar Sebastian "Sebbie" Silbermann Committed by GitHub

Ensure unique resolved `headers()` value between render passes (#96085)

A HTTP request can render the same React tree in multiple passes with
different semantics for `connection()`: the prospective and final
prerenders of a runtime prefetch, or a navigation's dynamic render and
the runtime prerender that is spawned from it to refresh the client's
prefetch cache. All of these passes previously resolved `headers()` to
the single sealed headers object created lazily by the request store, so
userland caches keyed on the identity of `await headers()` (any
per-request memoization that treats the headers object as "the request")
leaked promises across passes.

With this change, every render pass resolves `headers()` to a distinct
object over the same underlying data, making "the headers object
identifies the request within one render pass" a real contract.


---------
Co-authored-by: 's avatarClaude Fable 5 <noreply@anthropic.com>
parent 6fc7bf8a
...@@ -131,7 +131,11 @@ import { createFlightRouterStateFromLoaderTree } from './create-flight-router-st ...@@ -131,7 +131,11 @@ import { createFlightRouterStateFromLoaderTree } from './create-flight-router-st
import { handleAction } from './action-handler' import { handleAction } from './action-handler'
import { isBailoutToCSRError } from '../../shared/lib/lazy-dynamic/bailout-to-csr' import { isBailoutToCSRError } from '../../shared/lib/lazy-dynamic/bailout-to-csr'
import { warn, error } from '../../build/output/log' import { warn, error } from '../../build/output/log'
import { appendMutableCookies } from '../web/spec-extension/adapters/request-cookies' import {
appendMutableCookies,
RequestCookiesAdapter,
} from '../web/spec-extension/adapters/request-cookies'
import { HeadersAdapter } from '../web/spec-extension/adapters/headers'
import { createServerInsertedHTML } from './server-inserted-html' import { createServerInsertedHTML } from './server-inserted-html'
import { getRequiredScripts } from './required-scripts' import { getRequiredScripts } from './required-scripts'
import { addPathPrefix } from '../../shared/lib/router/utils/add-path-prefix' import { addPathPrefix } from '../../shared/lib/router/utils/add-path-prefix'
...@@ -1645,9 +1649,12 @@ async function prospectiveRuntimeServerPrerender( ...@@ -1645,9 +1649,12 @@ async function prospectiveRuntimeServerPrerender(
// No stage sequencing needed for prospective renders. // No stage sequencing needed for prospective renders.
stagedRendering: null, stagedRendering: null,
isSessionShell: isShellPrefetch, 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
headers, // prerender.
cookies, // Any cache keyed on headers() or cookies() needs to be invalidated.
// Otherwise some Next.js API semantics leak across render passes.
headers: HeadersAdapter.fresh(headers),
cookies: RequestCookiesAdapter.fresh(cookies),
draftMode, draftMode,
} }
...@@ -1821,9 +1828,10 @@ async function finalRuntimeServerPrerender( ...@@ -1821,9 +1828,10 @@ async function finalRuntimeServerPrerender(
varyParamsAccumulator, varyParamsAccumulator,
stagedRendering: finalStageController, stagedRendering: finalStageController,
isSessionShell: mode.type === 'session-shell-only', 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
headers, // prerender.
cookies, headers: HeadersAdapter.fresh(headers),
cookies: RequestCookiesAdapter.fresh(cookies),
draftMode, draftMode,
} }
......
...@@ -77,8 +77,14 @@ import { ...@@ -77,8 +77,14 @@ import {
import type { CacheReadWriteHandler } from './tiered-cache-handler' import type { CacheReadWriteHandler } from './tiered-cache-handler'
import { cloneCacheEntry } from './clone-cache-entry' import { cloneCacheEntry } from './clone-cache-entry'
import { NEXT_INSTANT_TEST_COOKIE } from '../../client/components/app-router-headers' import { NEXT_INSTANT_TEST_COOKIE } from '../../client/components/app-router-headers'
import type { ReadonlyRequestCookies } from '../web/spec-extension/adapters/request-cookies' import {
import type { ReadonlyHeaders } from '../web/spec-extension/adapters/headers' RequestCookiesAdapter,
type ReadonlyRequestCookies,
} from '../web/spec-extension/adapters/request-cookies'
import {
HeadersAdapter,
type ReadonlyHeaders,
} from '../web/spec-extension/adapters/headers'
import { import {
NestedDynamicUseCacheError, NestedDynamicUseCacheError,
UseCacheDeadlockError, UseCacheDeadlockError,
...@@ -681,8 +687,11 @@ function createUseCacheStore( ...@@ -681,8 +687,11 @@ function createUseCacheStore(
), ),
rootParams: outerWorkUnitStore.rootParams, rootParams: outerWorkUnitStore.rootParams,
readRootParamNames: process.env.__NEXT_DEV_SERVER ? new Set() : undefined, readRootParamNames: process.env.__NEXT_DEV_SERVER ? new Set() : undefined,
headers: outerWorkUnitStore.headers, // Every private cache scope is its own work unit. Any cache keyed on
cookies: outerWorkUnitStore.cookies, // headers() or cookies() needs to be invalidated. Otherwise some
// Next.js API semantics leak across render passes.
headers: HeadersAdapter.fresh(outerWorkUnitStore.headers),
cookies: RequestCookiesAdapter.fresh(outerWorkUnitStore.cookies),
outerOwnerStack: cacheContext.outerOwnerStack, outerOwnerStack: cacheContext.outerOwnerStack,
} }
} else { } else {
......
...@@ -133,6 +133,18 @@ export class HeadersAdapter extends Headers { ...@@ -133,6 +133,18 @@ export class HeadersAdapter extends Headers {
}) })
} }
/**
* @param headers
* @returns A fresh object identity backed by the original value
*/
public static fresh(headers: ReadonlyHeaders): ReadonlyHeaders {
return new Proxy<ReadonlyHeaders>(headers, {
get(target, prop, receiver) {
return ReflectAdapter.get(target, prop, receiver)
},
})
}
/** /**
* Merges a header value into a string. This stores multiple values as an * Merges a header value into a string. This stores multiple values as an
* array, so we need to merge them into a string. * array, so we need to merge them into a string.
......
...@@ -48,6 +48,18 @@ export class RequestCookiesAdapter { ...@@ -48,6 +48,18 @@ export class RequestCookiesAdapter {
}, },
}) })
} }
/**
* @param cookies
* @returns A fresh object identity backed by the original value
*/
public static fresh(cookies: ReadonlyRequestCookies): ReadonlyRequestCookies {
return new Proxy(cookies, {
get(target, prop, receiver) {
return ReflectAdapter.get(target, prop, receiver)
},
})
}
} }
const SYMBOL_MODIFY_COOKIE_VALUES = Symbol.for('next.mutated.cookies') const SYMBOL_MODIFY_COOKIE_VALUES = Symbol.for('next.mutated.cookies')
......
'use client'
export default function DynamicError() {
return <div id="dynamic-error">Failed to render dynamic content</div>
}
import { Suspense } from 'react'
import { headers } from 'next/headers'
import { connection } from 'next/server'
export const instant = {
unstable_samples: [{ headers: [['host', 'test-host']] }],
}
export const prefetch = 'partial'
export default function Page() {
return (
<main>
<p id="intro">This page gates its dynamic content on connection().</p>
<Suspense fallback={<div style={{ color: 'grey' }}>Loading 1...</div>}>
<RuntimePrefetchable />
</Suspense>
</main>
)
}
async function RuntimePrefetchable() {
const headersStore = await headers()
const headerValue = headersStore.get('host') === null ? 'missing' : 'present'
return (
<div>
<div id="header-value">{`Header: ${headerValue}`}</div>
<Suspense fallback={<div style={{ color: 'grey' }}>Loading 2...</div>}>
<Dynamic />
</Suspense>
</div>
)
}
// A module-level cache keyed on the identity of the headers object (like
// `dedupe()` from the Flags SDK, or any per-request memoization that treats
// the headers object as "the request"), gating its data on `connection()` so
// it only produces data during actual navigations, never during (runtime)
// prefetches.
//
// A request can be rendered by multiple passes with different semantics for
// `connection()`: the prospective and final prerenders of a runtime prefetch,
// or a navigation's dynamic render and the runtime prerender that is spawned
// from it to refresh the client's prefetch cache. In prerenders the
// connection() promise hangs and is rejected when the pass is aborted; during
// navigations it resolves. Each render pass resolves `await headers()` to a
// distinct object, which scopes identity-keyed memoization like this cache to
// a single pass: a promise created under one pass's semantics is never
// consumed by another pass.
const requestDataCache = new WeakMap<object, Promise<string>>()
async function getRequestData(): Promise<string> {
const headersStore = await headers()
let dataPromise = requestDataCache.get(headersStore)
if (dataPromise === undefined) {
dataPromise = (async () => {
await connection()
return 'request data'
})()
requestDataCache.set(headersStore, dataPromise)
}
return dataPromise
}
async function Dynamic() {
const data = await getRequestData()
return <div id="dynamic-content">Dynamic content: {data}</div>
}
import { ReactNode } from 'react'
export default function RootLayout({ children }: { children: ReactNode }) {
return (
<html>
<body style={{ fontFamily: 'monospace' }}>{children}</body>
</html>
)
}
import { LinkAccordion } from '../components/link-accordion'
export default function Page() {
return (
<main>
<h1>Index</h1>
<ul>
<li>
<LinkAccordion href="/dynamic">Dynamic page</LinkAccordion>
</li>
</ul>
</main>
)
}
'use client'
import Link from 'next/link'
import { useState } from 'react'
export function LinkAccordion({
href,
children,
}: {
href: string
children: React.ReactNode
}) {
const [isVisible, setIsVisible] = useState(false)
return (
<>
<input
type="checkbox"
checked={isVisible}
onChange={() => setIsVisible(!isVisible)}
data-link-accordion={href}
/>
{isVisible ? (
<Link href={href} prefetch>
{children}
</Link>
) : (
<>{children} (link is hidden)</>
)}
</>
)
}
import { nextTestSetup } from 'e2e-utils'
import { retry } from 'next-test-utils'
import type * as Playwright from 'playwright'
import { createRouterAct } from 'router-act'
describe('module-level caches keyed on the headers object', () => {
const { next, isNextDev } = nextTestSetup({
files: __dirname,
})
if (isNextDev) {
// Runtime prefetching only happens in production builds.
it('is skipped in dev', () => {})
return
}
it('renders dynamic content on navigation even when the spawned runtime prerender populated the cache first', async () => {
const cliOutputStart = next.cliOutput.length
let page: Playwright.Page
const browser = await next.browser('/', {
beforePageLoad(p: Playwright.Page) {
page = p
},
})
const act = createRouterAct(page)
// Reveal the link, triggering the runtime prefetch.
await act(
async () => {
const linkToggle = await browser.elementByCss(
'input[data-link-accordion="/dynamic"]'
)
await linkToggle.click()
},
{ includes: 'Header:' }
)
// Navigate. The navigation request also spawns a runtime prerender to
// refresh the client's prefetch cache, which reaches the module-level
// cache before the stage-gated dynamic render of the navigation does.
// Because each render pass resolves `headers()` to a distinct object, the
// hanging connection() promise it memoizes is keyed to the prerender pass
// only: the navigation's dynamic render misses the cache, creates its own
// promise, and connection() resolves, so the dynamic content renders.
await browser.elementByCss('a[href="/dynamic"]').click()
await retry(async () => {
expect(await browser.elementById('dynamic-content').text()).toBe(
'Dynamic content: request data'
)
})
expect(await browser.hasElementByCssSelector('#dynamic-error')).toBe(false)
// The rejection of the prerender pass's hanging promise stays within the
// pass that created it, so nothing is reported to onRequestError either.
const cliOutput = next.cliOutput.slice(cliOutputStart)
expect(cliOutput).not.toContain('[instrumentation] onRequestError:')
})
})
import { type Instrumentation } from 'next'
export const onRequestError: Instrumentation.onRequestError = (err) => {
console.log(`[instrumentation] onRequestError:${(err as Error).message}`)
}
import type { NextConfig } from 'next'
const nextConfig: NextConfig = {
cacheComponents: true,
experimental: {
cachedNavigations: 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