| |
| |
| |
| |
| @@ -131,7 +131,11 @@ import { createFlightRouterStateFromLoaderTree } from './create-flight-router-st |
| import { handleAction } from './action-handler' |
| import { isBailoutToCSRError } from '../../shared/lib/lazy-dynamic/bailout-to-csr' |
| 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 { getRequiredScripts } from './required-scripts' |
| import { addPathPrefix } from '../../shared/lib/router/utils/add-path-prefix' |
| @@ -1645,9 +1649,12 @@ async function prospectiveRuntimeServerPrerender( |
| // No stage sequencing needed for prospective renders. |
| stagedRendering: null, |
| isSessionShell: isShellPrefetch, |
| - // These are not present in regular prerenders, but allowed in a runtime prerender. |
| - headers, |
| - cookies, |
| + // These are not present in regular prerenders, but allowed in a runtime |
| + // prerender. |
| + // 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, |
| } |
| |
| @@ -1821,9 +1828,10 @@ async function finalRuntimeServerPrerender( |
| varyParamsAccumulator, |
| stagedRendering: finalStageController, |
| isSessionShell: mode.type |
| - // These are not present in regular prerenders, but allowed in a runtime prerender. |
| - headers, |
| - cookies, |
| + // These are not present in regular prerenders, but allowed in a runtime |
| + // prerender. |
| + headers: HeadersAdapter.fresh(headers), |
| + cookies: RequestCookiesAdapter.fresh(cookies), |
| draftMode, |
| } |
| |
| |
| |
| |
| |
| @@ -77,8 +77,14 @@ import { |
| import type { CacheReadWriteHandler } from './tiered-cache-handler' |
| import { cloneCacheEntry } from './clone-cache-entry' |
| import { NEXT_INSTANT_TEST_COOKIE } from '../../client/components/app-router-headers' |
| -import type { ReadonlyRequestCookies } from '../web/spec-extension/adapters/request-cookies' |
| -import type { ReadonlyHeaders } from '../web/spec-extension/adapters/headers' |
| +import { |
| + RequestCookiesAdapter, |
| + type ReadonlyRequestCookies, |
| +} from '../web/spec-extension/adapters/request-cookies' |
| +import { |
| + HeadersAdapter, |
| + type ReadonlyHeaders, |
| +} from '../web/spec-extension/adapters/headers' |
| import { |
| NestedDynamicUseCacheError, |
| UseCacheDeadlockError, |
| @@ -681,8 +687,11 @@ function createUseCacheStore( |
| ), |
| rootParams: outerWorkUnitStore.rootParams, |
| readRootParamNames: process.env.__NEXT_DEV_SERVER ? new Set() : undefined, |
| - headers: outerWorkUnitStore.headers, |
| - cookies: outerWorkUnitStore.cookies, |
| + // Every private cache scope is its own work unit. Any cache keyed on |
| + // 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, |
| } |
| } else { |
| |
| |
| |
| |
| @@ -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 |
| * array, so we need to merge them into a string. |
| |
| |
| |
| |
| @@ -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') |
|
|