|
|
import { |
|
|
type ReadonlyRequestCookies, |
|
|
type ResponseCookies, |
|
|
areCookiesMutableInCurrentPhase, |
|
|
RequestCookiesAdapter, |
|
|
} from '../web/spec-extension/adapters/request-cookies' |
|
|
import { RequestCookies } from '../web/spec-extension/cookies' |
|
|
import { workAsyncStorage } from '../app-render/work-async-storage.external' |
|
|
import { |
|
|
throwForMissingRequestStore, |
|
|
workUnitAsyncStorage, |
|
|
type PrerenderStoreModern, |
|
|
} from '../app-render/work-unit-async-storage.external' |
|
|
import { |
|
|
postponeWithTracking, |
|
|
throwToInterruptStaticGeneration, |
|
|
trackDynamicDataInDynamicRender, |
|
|
trackSynchronousRequestDataAccessInDev, |
|
|
} from '../app-render/dynamic-rendering' |
|
|
import { StaticGenBailoutError } from '../../client/components/static-generation-bailout' |
|
|
import { makeHangingPromise } from '../dynamic-rendering-utils' |
|
|
import { createDedupedByCallsiteServerErrorLoggerDev } from '../create-deduped-by-callsite-server-error-logger' |
|
|
import { scheduleImmediate } from '../../lib/scheduler' |
|
|
import { isRequestAPICallableInsideAfter } from './utils' |
|
|
import { InvariantError } from '../../shared/lib/invariant-error' |
|
|
import { ReflectAdapter } from '../web/spec-extension/adapters/reflect' |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export type UnsafeUnwrappedCookies = ReadonlyRequestCookies |
|
|
|
|
|
export function cookies(): Promise<ReadonlyRequestCookies> { |
|
|
const callingExpression = 'cookies' |
|
|
const workStore = workAsyncStorage.getStore() |
|
|
const workUnitStore = workUnitAsyncStorage.getStore() |
|
|
|
|
|
if (workStore) { |
|
|
if ( |
|
|
workUnitStore && |
|
|
workUnitStore.phase === 'after' && |
|
|
!isRequestAPICallableInsideAfter() |
|
|
) { |
|
|
throw new Error( |
|
|
|
|
|
`Route ${workStore.route} used "cookies" inside "after(...)". This is not supported. If you need this data inside an "after" callback, use "cookies" outside of the callback. See more info here: https://nextjs.org/docs/canary/app/api-reference/functions/after` |
|
|
) |
|
|
} |
|
|
|
|
|
if (workStore.forceStatic) { |
|
|
|
|
|
|
|
|
const underlyingCookies = createEmptyCookies() |
|
|
return makeUntrackedExoticCookies(underlyingCookies) |
|
|
} |
|
|
|
|
|
if (workStore.dynamicShouldError) { |
|
|
throw new StaticGenBailoutError( |
|
|
`Route ${workStore.route} with \`dynamic = "error"\` couldn't be rendered statically because it used \`cookies\`. See more info here: https://nextjs.org/docs/app/building-your-application/rendering/static-and-dynamic#dynamic-rendering` |
|
|
) |
|
|
} |
|
|
|
|
|
if (workUnitStore) { |
|
|
switch (workUnitStore.type) { |
|
|
case 'cache': |
|
|
const error = new Error( |
|
|
`Route ${workStore.route} used "cookies" inside "use cache". Accessing Dynamic data sources inside a cache scope is not supported. If you need this data inside a cached function use "cookies" outside of the cached function and pass the required dynamic data in as an argument. See more info here: https://nextjs.org/docs/messages/next-request-in-use-cache` |
|
|
) |
|
|
Error.captureStackTrace(error, cookies) |
|
|
workStore.invalidDynamicUsageError ??= error |
|
|
throw error |
|
|
case 'unstable-cache': |
|
|
throw new Error( |
|
|
`Route ${workStore.route} used "cookies" inside a function cached with "unstable_cache(...)". Accessing Dynamic data sources inside a cache scope is not supported. If you need this data inside a cached function use "cookies" outside of the cached function and pass the required dynamic data in as an argument. See more info here: https://nextjs.org/docs/app/api-reference/functions/unstable_cache` |
|
|
) |
|
|
case 'prerender': |
|
|
return makeHangingCookies(workUnitStore) |
|
|
case 'prerender-client': |
|
|
const exportName = '`cookies`' |
|
|
throw new InvariantError( |
|
|
`${exportName} must not be used within a client component. Next.js should be preventing ${exportName} from being included in client components statically, but did not in this case.` |
|
|
) |
|
|
case 'prerender-ppr': |
|
|
|
|
|
|
|
|
return postponeWithTracking( |
|
|
workStore.route, |
|
|
callingExpression, |
|
|
workUnitStore.dynamicTracking |
|
|
) |
|
|
case 'prerender-legacy': |
|
|
|
|
|
|
|
|
return throwToInterruptStaticGeneration( |
|
|
callingExpression, |
|
|
workStore, |
|
|
workUnitStore |
|
|
) |
|
|
case 'private-cache': |
|
|
return makeUntrackedExoticCookies(workUnitStore.cookies) |
|
|
case 'request': |
|
|
trackDynamicDataInDynamicRender(workUnitStore) |
|
|
|
|
|
let underlyingCookies: ReadonlyRequestCookies |
|
|
|
|
|
if (areCookiesMutableInCurrentPhase(workUnitStore)) { |
|
|
|
|
|
|
|
|
underlyingCookies = |
|
|
workUnitStore.userspaceMutableCookies as unknown as ReadonlyRequestCookies |
|
|
} else { |
|
|
underlyingCookies = workUnitStore.cookies |
|
|
} |
|
|
|
|
|
if ( |
|
|
process.env.NODE_ENV === 'development' && |
|
|
!workStore?.isPrefetchRequest |
|
|
) { |
|
|
if (process.env.__NEXT_CACHE_COMPONENTS) { |
|
|
return makeUntrackedCookiesWithDevWarnings( |
|
|
underlyingCookies, |
|
|
workStore?.route |
|
|
) |
|
|
} |
|
|
|
|
|
return makeUntrackedExoticCookiesWithDevWarnings( |
|
|
underlyingCookies, |
|
|
workStore?.route |
|
|
) |
|
|
} else { |
|
|
return makeUntrackedExoticCookies(underlyingCookies) |
|
|
} |
|
|
default: |
|
|
workUnitStore satisfies never |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
throwForMissingRequestStore(callingExpression) |
|
|
} |
|
|
|
|
|
function createEmptyCookies(): ReadonlyRequestCookies { |
|
|
return RequestCookiesAdapter.seal(new RequestCookies(new Headers({}))) |
|
|
} |
|
|
|
|
|
interface CacheLifetime {} |
|
|
const CachedCookies = new WeakMap< |
|
|
CacheLifetime, |
|
|
Promise<ReadonlyRequestCookies> |
|
|
>() |
|
|
|
|
|
function makeHangingCookies( |
|
|
prerenderStore: PrerenderStoreModern |
|
|
): Promise<ReadonlyRequestCookies> { |
|
|
const cachedPromise = CachedCookies.get(prerenderStore) |
|
|
if (cachedPromise) { |
|
|
return cachedPromise |
|
|
} |
|
|
|
|
|
const promise = makeHangingPromise<ReadonlyRequestCookies>( |
|
|
prerenderStore.renderSignal, |
|
|
'`cookies()`' |
|
|
) |
|
|
CachedCookies.set(prerenderStore, promise) |
|
|
|
|
|
return promise |
|
|
} |
|
|
|
|
|
function makeUntrackedExoticCookies( |
|
|
underlyingCookies: ReadonlyRequestCookies |
|
|
): Promise<ReadonlyRequestCookies> { |
|
|
const cachedCookies = CachedCookies.get(underlyingCookies) |
|
|
if (cachedCookies) { |
|
|
return cachedCookies |
|
|
} |
|
|
|
|
|
const promise = Promise.resolve(underlyingCookies) |
|
|
CachedCookies.set(underlyingCookies, promise) |
|
|
|
|
|
Object.defineProperties(promise, { |
|
|
[Symbol.iterator]: { |
|
|
value: underlyingCookies[Symbol.iterator] |
|
|
? underlyingCookies[Symbol.iterator].bind(underlyingCookies) |
|
|
: |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
polyfilledResponseCookiesIterator.bind(underlyingCookies), |
|
|
}, |
|
|
size: { |
|
|
get(): number { |
|
|
return underlyingCookies.size |
|
|
}, |
|
|
}, |
|
|
get: { |
|
|
value: underlyingCookies.get.bind(underlyingCookies), |
|
|
}, |
|
|
getAll: { |
|
|
value: underlyingCookies.getAll.bind(underlyingCookies), |
|
|
}, |
|
|
has: { |
|
|
value: underlyingCookies.has.bind(underlyingCookies), |
|
|
}, |
|
|
set: { |
|
|
value: underlyingCookies.set.bind(underlyingCookies), |
|
|
}, |
|
|
delete: { |
|
|
value: underlyingCookies.delete.bind(underlyingCookies), |
|
|
}, |
|
|
clear: { |
|
|
value: |
|
|
|
|
|
typeof underlyingCookies.clear === 'function' |
|
|
? |
|
|
underlyingCookies.clear.bind(underlyingCookies) |
|
|
: |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
polyfilledResponseCookiesClear.bind(underlyingCookies, promise), |
|
|
}, |
|
|
toString: { |
|
|
value: underlyingCookies.toString.bind(underlyingCookies), |
|
|
}, |
|
|
} satisfies CookieExtensions) |
|
|
|
|
|
return promise |
|
|
} |
|
|
|
|
|
function makeUntrackedExoticCookiesWithDevWarnings( |
|
|
underlyingCookies: ReadonlyRequestCookies, |
|
|
route?: string |
|
|
): Promise<ReadonlyRequestCookies> { |
|
|
const cachedCookies = CachedCookies.get(underlyingCookies) |
|
|
if (cachedCookies) { |
|
|
return cachedCookies |
|
|
} |
|
|
|
|
|
const promise = new Promise<ReadonlyRequestCookies>((resolve) => |
|
|
scheduleImmediate(() => resolve(underlyingCookies)) |
|
|
) |
|
|
CachedCookies.set(underlyingCookies, promise) |
|
|
|
|
|
Object.defineProperties(promise, { |
|
|
[Symbol.iterator]: { |
|
|
value: function () { |
|
|
const expression = '`...cookies()` or similar iteration' |
|
|
syncIODev(route, expression) |
|
|
return underlyingCookies[Symbol.iterator] |
|
|
? underlyingCookies[Symbol.iterator].apply( |
|
|
underlyingCookies, |
|
|
arguments as any |
|
|
) |
|
|
: |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
polyfilledResponseCookiesIterator.call(underlyingCookies) |
|
|
}, |
|
|
writable: false, |
|
|
}, |
|
|
size: { |
|
|
get(): number { |
|
|
const expression = '`cookies().size`' |
|
|
syncIODev(route, expression) |
|
|
return underlyingCookies.size |
|
|
}, |
|
|
}, |
|
|
get: { |
|
|
value: function get() { |
|
|
let expression: string |
|
|
if (arguments.length === 0) { |
|
|
expression = '`cookies().get()`' |
|
|
} else { |
|
|
expression = `\`cookies().get(${describeNameArg(arguments[0])})\`` |
|
|
} |
|
|
syncIODev(route, expression) |
|
|
return underlyingCookies.get.apply(underlyingCookies, arguments as any) |
|
|
}, |
|
|
writable: false, |
|
|
}, |
|
|
getAll: { |
|
|
value: function getAll() { |
|
|
let expression: string |
|
|
if (arguments.length === 0) { |
|
|
expression = '`cookies().getAll()`' |
|
|
} else { |
|
|
expression = `\`cookies().getAll(${describeNameArg(arguments[0])})\`` |
|
|
} |
|
|
syncIODev(route, expression) |
|
|
return underlyingCookies.getAll.apply( |
|
|
underlyingCookies, |
|
|
arguments as any |
|
|
) |
|
|
}, |
|
|
writable: false, |
|
|
}, |
|
|
has: { |
|
|
value: function get() { |
|
|
let expression: string |
|
|
if (arguments.length === 0) { |
|
|
expression = '`cookies().has()`' |
|
|
} else { |
|
|
expression = `\`cookies().has(${describeNameArg(arguments[0])})\`` |
|
|
} |
|
|
syncIODev(route, expression) |
|
|
return underlyingCookies.has.apply(underlyingCookies, arguments as any) |
|
|
}, |
|
|
writable: false, |
|
|
}, |
|
|
set: { |
|
|
value: function set() { |
|
|
let expression: string |
|
|
if (arguments.length === 0) { |
|
|
expression = '`cookies().set()`' |
|
|
} else { |
|
|
const arg = arguments[0] |
|
|
if (arg) { |
|
|
expression = `\`cookies().set(${describeNameArg(arg)}, ...)\`` |
|
|
} else { |
|
|
expression = '`cookies().set(...)`' |
|
|
} |
|
|
} |
|
|
syncIODev(route, expression) |
|
|
return underlyingCookies.set.apply(underlyingCookies, arguments as any) |
|
|
}, |
|
|
writable: false, |
|
|
}, |
|
|
delete: { |
|
|
value: function () { |
|
|
let expression: string |
|
|
if (arguments.length === 0) { |
|
|
expression = '`cookies().delete()`' |
|
|
} else if (arguments.length === 1) { |
|
|
expression = `\`cookies().delete(${describeNameArg(arguments[0])})\`` |
|
|
} else { |
|
|
expression = `\`cookies().delete(${describeNameArg(arguments[0])}, ...)\`` |
|
|
} |
|
|
syncIODev(route, expression) |
|
|
return underlyingCookies.delete.apply( |
|
|
underlyingCookies, |
|
|
arguments as any |
|
|
) |
|
|
}, |
|
|
writable: false, |
|
|
}, |
|
|
clear: { |
|
|
value: function clear() { |
|
|
const expression = '`cookies().clear()`' |
|
|
syncIODev(route, expression) |
|
|
|
|
|
return typeof underlyingCookies.clear === 'function' |
|
|
? |
|
|
underlyingCookies.clear.apply(underlyingCookies, arguments) |
|
|
: |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
polyfilledResponseCookiesClear.call(underlyingCookies, promise) |
|
|
}, |
|
|
writable: false, |
|
|
}, |
|
|
toString: { |
|
|
value: function toString() { |
|
|
const expression = '`cookies().toString()` or implicit casting' |
|
|
syncIODev(route, expression) |
|
|
return underlyingCookies.toString.apply( |
|
|
underlyingCookies, |
|
|
arguments as any |
|
|
) |
|
|
}, |
|
|
writable: false, |
|
|
}, |
|
|
} satisfies CookieExtensions) |
|
|
|
|
|
return promise |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
function makeUntrackedCookiesWithDevWarnings( |
|
|
underlyingCookies: ReadonlyRequestCookies, |
|
|
route?: string |
|
|
): Promise<ReadonlyRequestCookies> { |
|
|
const cachedCookies = CachedCookies.get(underlyingCookies) |
|
|
if (cachedCookies) { |
|
|
return cachedCookies |
|
|
} |
|
|
|
|
|
const promise = new Promise<ReadonlyRequestCookies>((resolve) => |
|
|
scheduleImmediate(() => resolve(underlyingCookies)) |
|
|
) |
|
|
|
|
|
const proxiedPromise = new Proxy(promise, { |
|
|
get(target, prop, receiver) { |
|
|
switch (prop) { |
|
|
case Symbol.iterator: { |
|
|
warnForSyncAccess(route, '`...cookies()` or similar iteration') |
|
|
break |
|
|
} |
|
|
case 'size': |
|
|
case 'get': |
|
|
case 'getAll': |
|
|
case 'has': |
|
|
case 'set': |
|
|
case 'delete': |
|
|
case 'clear': |
|
|
case 'toString': { |
|
|
warnForSyncAccess(route, `\`cookies().${prop}\``) |
|
|
break |
|
|
} |
|
|
default: { |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
return ReflectAdapter.get(target, prop, receiver) |
|
|
}, |
|
|
}) |
|
|
|
|
|
CachedCookies.set(underlyingCookies, proxiedPromise) |
|
|
|
|
|
return proxiedPromise |
|
|
} |
|
|
|
|
|
function describeNameArg(arg: unknown) { |
|
|
return typeof arg === 'object' && |
|
|
arg !== null && |
|
|
typeof (arg as any).name === 'string' |
|
|
? `'${(arg as any).name}'` |
|
|
: typeof arg === 'string' |
|
|
? `'${arg}'` |
|
|
: '...' |
|
|
} |
|
|
|
|
|
function syncIODev(route: string | undefined, expression: string) { |
|
|
const workUnitStore = workUnitAsyncStorage.getStore() |
|
|
|
|
|
if (workUnitStore) { |
|
|
switch (workUnitStore.type) { |
|
|
case 'request': |
|
|
if (workUnitStore.prerenderPhase === true) { |
|
|
|
|
|
|
|
|
trackSynchronousRequestDataAccessInDev(workUnitStore) |
|
|
} |
|
|
break |
|
|
case 'prerender': |
|
|
case 'prerender-client': |
|
|
case 'prerender-ppr': |
|
|
case 'prerender-legacy': |
|
|
case 'cache': |
|
|
case 'private-cache': |
|
|
case 'unstable-cache': |
|
|
break |
|
|
default: |
|
|
workUnitStore satisfies never |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
warnForSyncAccess(route, expression) |
|
|
} |
|
|
|
|
|
const warnForSyncAccess = createDedupedByCallsiteServerErrorLoggerDev( |
|
|
createCookiesAccessError |
|
|
) |
|
|
|
|
|
function createCookiesAccessError( |
|
|
route: string | undefined, |
|
|
expression: string |
|
|
) { |
|
|
const prefix = route ? `Route "${route}" ` : 'This route ' |
|
|
return new Error( |
|
|
`${prefix}used ${expression}. ` + |
|
|
`\`cookies()\` should be awaited before using its value. ` + |
|
|
`Learn more: https://nextjs.org/docs/messages/sync-dynamic-apis` |
|
|
) |
|
|
} |
|
|
|
|
|
function polyfilledResponseCookiesIterator( |
|
|
this: ResponseCookies |
|
|
): ReturnType<ReadonlyRequestCookies[typeof Symbol.iterator]> { |
|
|
return this.getAll() |
|
|
.map((c) => [c.name, c] as [string, any]) |
|
|
.values() |
|
|
} |
|
|
|
|
|
function polyfilledResponseCookiesClear( |
|
|
this: ResponseCookies, |
|
|
returnable: Promise<ReadonlyRequestCookies> |
|
|
): typeof returnable { |
|
|
for (const cookie of this.getAll()) { |
|
|
this.delete(cookie.name) |
|
|
} |
|
|
return returnable |
|
|
} |
|
|
|
|
|
type CookieExtensions = { |
|
|
[K in keyof ReadonlyRequestCookies | 'clear']: unknown |
|
|
} |
|
|
|