Unverified Commit 662918f9 authored by Hendrik Liebau's avatar Hendrik Liebau Committed by GitHub

[Cache Components] Disallow sync access of cookies & headers at runtime (#82564)

In #81162 we removed the support for accessing request data
synchronously when `experimental.cacheComponents` is enabled. However,
we missed updating the `cookies()` and `headers()` promises that are
created at runtime to not be "exotic" promises, i.e. promises that also
have the cookies/headers methods defined on them. Those two cases are
now updated as well with this PR. (Params, search params, and draft mode
were already handled correctly in the original PR.)

In the next major version we'll remove the support for the legacy
synchronous access independent of the `cacheComponents` flag.
parent b6d2c365
...@@ -121,9 +121,13 @@ export function cookies(): Promise<ReadonlyRequestCookies> { ...@@ -121,9 +121,13 @@ export function cookies(): Promise<ReadonlyRequestCookies> {
case 'prerender-runtime': case 'prerender-runtime':
return delayUntilRuntimeStage( return delayUntilRuntimeStage(
workUnitStore, workUnitStore,
makeUntrackedExoticCookies(workUnitStore.cookies) makeUntrackedCookies(workUnitStore.cookies)
) )
case 'private-cache': case 'private-cache':
if (process.env.__NEXT_CACHE_COMPONENTS) {
return makeUntrackedCookies(workUnitStore.cookies)
}
return makeUntrackedExoticCookies(workUnitStore.cookies) return makeUntrackedExoticCookies(workUnitStore.cookies)
case 'request': case 'request':
trackDynamicDataInDynamicRender(workUnitStore) trackDynamicDataInDynamicRender(workUnitStore)
...@@ -155,6 +159,10 @@ export function cookies(): Promise<ReadonlyRequestCookies> { ...@@ -155,6 +159,10 @@ export function cookies(): Promise<ReadonlyRequestCookies> {
workStore?.route workStore?.route
) )
} else { } else {
if (process.env.__NEXT_CACHE_COMPONENTS) {
return makeUntrackedCookies(underlyingCookies)
}
return makeUntrackedExoticCookies(underlyingCookies) return makeUntrackedExoticCookies(underlyingCookies)
} }
default: default:
...@@ -196,6 +204,20 @@ function makeHangingCookies( ...@@ -196,6 +204,20 @@ function makeHangingCookies(
return promise return promise
} }
function makeUntrackedCookies(
underlyingCookies: ReadonlyRequestCookies
): Promise<ReadonlyRequestCookies> {
const cachedCookies = CachedCookies.get(underlyingCookies)
if (cachedCookies) {
return cachedCookies
}
const promise = Promise.resolve(underlyingCookies)
CachedCookies.set(underlyingCookies, promise)
return promise
}
function makeUntrackedExoticCookies( function makeUntrackedExoticCookies(
underlyingCookies: ReadonlyRequestCookies underlyingCookies: ReadonlyRequestCookies
): Promise<ReadonlyRequestCookies> { ): Promise<ReadonlyRequestCookies> {
......
...@@ -169,6 +169,10 @@ export function headers(): Promise<ReadonlyHeaders> { ...@@ -169,6 +169,10 @@ export function headers(): Promise<ReadonlyHeaders> {
workStore?.route workStore?.route
) )
} else { } else {
if (process.env.__NEXT_CACHE_COMPONENTS) {
return makeUntrackedHeaders(workUnitStore.headers)
}
return makeUntrackedExoticHeaders(workUnitStore.headers) return makeUntrackedExoticHeaders(workUnitStore.headers)
} }
break break
...@@ -204,6 +208,20 @@ function makeHangingHeaders( ...@@ -204,6 +208,20 @@ function makeHangingHeaders(
return promise return promise
} }
function makeUntrackedHeaders(
underlyingHeaders: ReadonlyHeaders
): Promise<ReadonlyHeaders> {
const cachedHeaders = CachedHeaders.get(underlyingHeaders)
if (cachedHeaders) {
return cachedHeaders
}
const promise = Promise.resolve(underlyingHeaders)
CachedHeaders.set(underlyingHeaders, promise)
return promise
}
function makeUntrackedExoticHeaders( function makeUntrackedExoticHeaders(
underlyingHeaders: ReadonlyHeaders underlyingHeaders: ReadonlyHeaders
): Promise<ReadonlyHeaders> { ): Promise<ReadonlyHeaders> {
......
export default function Root({ children }: { children: React.ReactNode }) {
return (
<html>
<body>
<main>{children}</main>
</body>
</html>
)
}
import { cookies, type UnsafeUnwrappedCookies } from 'next/headers'
import { connection } from 'next/server'
import { Suspense } from 'react'
export default async function Page() {
return (
<>
<p>
This page accesses cookies synchronously at runtime. This triggers a
type error. In dev mode, we also log an explicit error that `cookies()`
should be awaited.
</p>
<Suspense>
<CookiesReadingComponent />
</Suspense>
</>
)
}
async function CookiesReadingComponent() {
// Await a connection to test the subsequent sync cookies access at runtime.
await connection()
const token = (cookies() as unknown as UnsafeUnwrappedCookies).get('token')
return (
<div>
this component reads the `token` cookie synchronously: {token?.value}
</div>
)
}
...@@ -14,6 +14,11 @@ export default async function Page() { ...@@ -14,6 +14,11 @@ export default async function Page() {
} }
async function CookiesReadingComponent() { async function CookiesReadingComponent() {
const _token = (cookies() as unknown as UnsafeUnwrappedCookies).get('token') const token = (cookies() as unknown as UnsafeUnwrappedCookies).get('token')
return <div>this component reads the `token` cookie synchronously</div>
return (
<div>
this component reads the `token` cookie synchronously: {token?.value}
</div>
)
} }
export default function Root({ children }: { children: React.ReactNode }) {
return (
<html>
<body>
<main>{children}</main>
</body>
</html>
)
}
import { headers, type UnsafeUnwrappedHeaders } from 'next/headers'
import { connection } from 'next/server'
import { Suspense } from 'react'
export default async function Page() {
return (
<>
<p>
This page accesses headers synchronously at runtime. This triggers a
type error. In dev mode, we also log an explicit error that `headers()`
should be awaited.
</p>
<Suspense>
<HeadersReadingComponent />
</Suspense>
</>
)
}
async function HeadersReadingComponent() {
// Await a connection to test the subsequent sync headers access at runtime.
await connection()
const userAgent = (headers() as unknown as UnsafeUnwrappedHeaders).get(
'user-agent'
)
return (
<div>
this component reads the `user-agent` header synchronously: {userAgent}
</div>
)
}
...@@ -3,6 +3,20 @@ const abc = 'abcdefghijklmnopqrstuvwxyz' ...@@ -3,6 +3,20 @@ const abc = 'abcdefghijklmnopqrstuvwxyz'
const hostElementsUsedInFixtures = ['html', 'body', 'main', 'div'] const hostElementsUsedInFixtures = ['html', 'body', 'main', 'div']
const ignoredLines = ['Generating static pages', 'Inlining static env'] const ignoredLines = ['Generating static pages', 'Inlining static env']
/**
* Converts a module function sequence expression, e.g.:
* - (0 , __TURBOPACK__imported__module__1836__.cookies)(...)
* - (0 , c.cookies)(...)
* - (0 , cookies.U)(...)
* - (0 , e.U)(...)
* to a deterministic, bundler-agnostic representation.
*/
export function convertModuleFunctionSequenceExpression(
output: string
): string {
return output.replace(/\(0 , \w+\.(\w+)\)\(\.\.\.\)/, '<module-function>()')
}
export function getPrerenderOutput( export function getPrerenderOutput(
cliOutput: string, cliOutput: string,
{ isMinified }: { isMinified: boolean } { isMinified }: { isMinified: boolean }
...@@ -61,17 +75,11 @@ export function getPrerenderOutput( ...@@ -61,17 +75,11 @@ export function getPrerenderOutput(
) )
.replace(/at \d+ \(/, replaceNumericModuleId) .replace(/at \d+ \(/, replaceNumericModuleId)
.replace(/digest: '\d+'/, "digest: '<error-digest>'") .replace(/digest: '\d+'/, "digest: '<error-digest>'")
// Convert a module function sequence expression, e.g.:
// - (0 , __TURBOPACK__imported__module__1836__.cookies)(...)
// - (0 , c.cookies)(...)
// - (0 , cookies.U)(...)
// - (0 , e.U)(...)
.replace(/\(0 , \w+\.(\w+)\)\(\.\.\.\)/, '<module-function>()')
// TODO(veil): Bundler protocols should not appear in stack frames. // TODO(veil): Bundler protocols should not appear in stack frames.
.replace('webpack:///', 'bundler:///') .replace('webpack:///', 'bundler:///')
.replace('turbopack:///[project]/', 'bundler:///') .replace('turbopack:///[project]/', 'bundler:///')
lines.push(line) lines.push(convertModuleFunctionSequenceExpression(line))
} }
} }
......
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