Spaces:
Sleeping
Sleeping
| ; | |
| Object.defineProperty(exports, "__esModule", { | |
| value: true | |
| }); | |
| Object.defineProperty(exports, "headers", { | |
| enumerable: true, | |
| get: function() { | |
| return headers; | |
| } | |
| }); | |
| const _headers = require("../web/spec-extension/adapters/headers"); | |
| const _workasyncstorageexternal = require("../app-render/work-async-storage.external"); | |
| const _workunitasyncstorageexternal = require("../app-render/work-unit-async-storage.external"); | |
| const _dynamicrendering = require("../app-render/dynamic-rendering"); | |
| const _staticgenerationbailout = require("../../client/components/static-generation-bailout"); | |
| const _dynamicrenderingutils = require("../dynamic-rendering-utils"); | |
| const _creatededupedbycallsiteservererrorlogger = require("../create-deduped-by-callsite-server-error-logger"); | |
| const _utils = require("./utils"); | |
| const _invarianterror = require("../../shared/lib/invariant-error"); | |
| const _reflect = require("../web/spec-extension/adapters/reflect"); | |
| function headers() { | |
| const callingExpression = 'headers'; | |
| const workStore = _workasyncstorageexternal.workAsyncStorage.getStore(); | |
| const workUnitStore = _workunitasyncstorageexternal.workUnitAsyncStorage.getStore(); | |
| if (workStore) { | |
| if (workUnitStore && workUnitStore.phase === 'after' && !(0, _utils.isRequestAPICallableInsideAfter)()) { | |
| throw Object.defineProperty(new Error(`Route ${workStore.route} used "headers" inside "after(...)". This is not supported. If you need this data inside an "after" callback, use "headers" outside of the callback. See more info here: https://nextjs.org/docs/canary/app/api-reference/functions/after`), "__NEXT_ERROR_CODE", { | |
| value: "E367", | |
| enumerable: false, | |
| configurable: true | |
| }); | |
| } | |
| if (workStore.forceStatic) { | |
| // When using forceStatic we override all other logic and always just return an empty | |
| // headers object without tracking | |
| const underlyingHeaders = _headers.HeadersAdapter.seal(new Headers({})); | |
| return makeUntrackedExoticHeaders(underlyingHeaders); | |
| } | |
| if (workUnitStore) { | |
| switch(workUnitStore.type){ | |
| case 'cache': | |
| { | |
| const error = Object.defineProperty(new Error(`Route ${workStore.route} used "headers" inside "use cache". Accessing Dynamic data sources inside a cache scope is not supported. If you need this data inside a cached function use "headers" 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`), "__NEXT_ERROR_CODE", { | |
| value: "E304", | |
| enumerable: false, | |
| configurable: true | |
| }); | |
| Error.captureStackTrace(error, headers); | |
| workStore.invalidDynamicUsageError ??= error; | |
| throw error; | |
| } | |
| case 'private-cache': | |
| { | |
| const error = Object.defineProperty(new Error(`Route ${workStore.route} used "headers" inside "use cache: private". Accessing "headers" inside a private cache scope is not supported. If you need this data inside a cached function use "headers" 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`), "__NEXT_ERROR_CODE", { | |
| value: "E742", | |
| enumerable: false, | |
| configurable: true | |
| }); | |
| Error.captureStackTrace(error, headers); | |
| workStore.invalidDynamicUsageError ??= error; | |
| throw error; | |
| } | |
| case 'unstable-cache': | |
| throw Object.defineProperty(new Error(`Route ${workStore.route} used "headers" 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 "headers" 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`), "__NEXT_ERROR_CODE", { | |
| value: "E127", | |
| enumerable: false, | |
| configurable: true | |
| }); | |
| case 'prerender': | |
| case 'prerender-client': | |
| case 'prerender-runtime': | |
| case 'prerender-ppr': | |
| case 'prerender-legacy': | |
| case 'request': | |
| break; | |
| default: | |
| workUnitStore; | |
| } | |
| } | |
| if (workStore.dynamicShouldError) { | |
| throw Object.defineProperty(new _staticgenerationbailout.StaticGenBailoutError(`Route ${workStore.route} with \`dynamic = "error"\` couldn't be rendered statically because it used \`headers\`. See more info here: https://nextjs.org/docs/app/building-your-application/rendering/static-and-dynamic#dynamic-rendering`), "__NEXT_ERROR_CODE", { | |
| value: "E525", | |
| enumerable: false, | |
| configurable: true | |
| }); | |
| } | |
| if (workUnitStore) { | |
| switch(workUnitStore.type){ | |
| case 'prerender': | |
| case 'prerender-runtime': | |
| return makeHangingHeaders(workStore, workUnitStore); | |
| case 'prerender-client': | |
| const exportName = '`headers`'; | |
| throw Object.defineProperty(new _invarianterror.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.`), "__NEXT_ERROR_CODE", { | |
| value: "E693", | |
| enumerable: false, | |
| configurable: true | |
| }); | |
| case 'prerender-ppr': | |
| // PPR Prerender (no cacheComponents) | |
| // We are prerendering with PPR. We need track dynamic access here eagerly | |
| // to keep continuity with how headers has worked in PPR without cacheComponents. | |
| // TODO consider switching the semantic to throw on property access instead | |
| return (0, _dynamicrendering.postponeWithTracking)(workStore.route, callingExpression, workUnitStore.dynamicTracking); | |
| case 'prerender-legacy': | |
| // Legacy Prerender | |
| // We are in a legacy static generation mode while prerendering | |
| // We track dynamic access here so we don't need to wrap the headers in | |
| // individual property access tracking. | |
| return (0, _dynamicrendering.throwToInterruptStaticGeneration)(callingExpression, workStore, workUnitStore); | |
| case 'request': | |
| (0, _dynamicrendering.trackDynamicDataInDynamicRender)(workUnitStore); | |
| if (process.env.NODE_ENV === 'development') { | |
| // Semantically we only need the dev tracking when running in `next dev` | |
| // but since you would never use next dev with production NODE_ENV we use this | |
| // as a proxy so we can statically exclude this code from production builds. | |
| if (process.env.__NEXT_CACHE_COMPONENTS) { | |
| return makeUntrackedHeadersWithDevWarnings(workUnitStore.headers, workStore == null ? void 0 : workStore.route); | |
| } | |
| return makeUntrackedExoticHeadersWithDevWarnings(workUnitStore.headers, workStore == null ? void 0 : workStore.route); | |
| } else { | |
| if (process.env.__NEXT_CACHE_COMPONENTS) { | |
| return makeUntrackedHeaders(workUnitStore.headers); | |
| } | |
| return makeUntrackedExoticHeaders(workUnitStore.headers); | |
| } | |
| break; | |
| default: | |
| workUnitStore; | |
| } | |
| } | |
| } | |
| // If we end up here, there was no work store or work unit store present. | |
| (0, _workunitasyncstorageexternal.throwForMissingRequestStore)(callingExpression); | |
| } | |
| const CachedHeaders = new WeakMap(); | |
| function makeHangingHeaders(workStore, prerenderStore) { | |
| const cachedHeaders = CachedHeaders.get(prerenderStore); | |
| if (cachedHeaders) { | |
| return cachedHeaders; | |
| } | |
| const promise = (0, _dynamicrenderingutils.makeHangingPromise)(prerenderStore.renderSignal, workStore.route, '`headers()`'); | |
| CachedHeaders.set(prerenderStore, promise); | |
| return promise; | |
| } | |
| function makeUntrackedHeaders(underlyingHeaders) { | |
| const cachedHeaders = CachedHeaders.get(underlyingHeaders); | |
| if (cachedHeaders) { | |
| return cachedHeaders; | |
| } | |
| const promise = Promise.resolve(underlyingHeaders); | |
| CachedHeaders.set(underlyingHeaders, promise); | |
| return promise; | |
| } | |
| function makeUntrackedExoticHeaders(underlyingHeaders) { | |
| const cachedHeaders = CachedHeaders.get(underlyingHeaders); | |
| if (cachedHeaders) { | |
| return cachedHeaders; | |
| } | |
| const promise = Promise.resolve(underlyingHeaders); | |
| CachedHeaders.set(underlyingHeaders, promise); | |
| Object.defineProperties(promise, { | |
| append: { | |
| value: underlyingHeaders.append.bind(underlyingHeaders) | |
| }, | |
| delete: { | |
| value: underlyingHeaders.delete.bind(underlyingHeaders) | |
| }, | |
| get: { | |
| value: underlyingHeaders.get.bind(underlyingHeaders) | |
| }, | |
| has: { | |
| value: underlyingHeaders.has.bind(underlyingHeaders) | |
| }, | |
| set: { | |
| value: underlyingHeaders.set.bind(underlyingHeaders) | |
| }, | |
| getSetCookie: { | |
| value: underlyingHeaders.getSetCookie.bind(underlyingHeaders) | |
| }, | |
| forEach: { | |
| value: underlyingHeaders.forEach.bind(underlyingHeaders) | |
| }, | |
| keys: { | |
| value: underlyingHeaders.keys.bind(underlyingHeaders) | |
| }, | |
| values: { | |
| value: underlyingHeaders.values.bind(underlyingHeaders) | |
| }, | |
| entries: { | |
| value: underlyingHeaders.entries.bind(underlyingHeaders) | |
| }, | |
| [Symbol.iterator]: { | |
| value: underlyingHeaders[Symbol.iterator].bind(underlyingHeaders) | |
| } | |
| }); | |
| return promise; | |
| } | |
| function makeUntrackedExoticHeadersWithDevWarnings(underlyingHeaders, route) { | |
| const cachedHeaders = CachedHeaders.get(underlyingHeaders); | |
| if (cachedHeaders) { | |
| return cachedHeaders; | |
| } | |
| const promise = (0, _dynamicrenderingutils.makeDevtoolsIOAwarePromise)(underlyingHeaders); | |
| CachedHeaders.set(underlyingHeaders, promise); | |
| Object.defineProperties(promise, { | |
| append: { | |
| value: function append() { | |
| const expression = `\`headers().append(${describeNameArg(arguments[0])}, ...)\``; | |
| syncIODev(route, expression); | |
| return underlyingHeaders.append.apply(underlyingHeaders, arguments); | |
| } | |
| }, | |
| delete: { | |
| value: function _delete() { | |
| const expression = `\`headers().delete(${describeNameArg(arguments[0])})\``; | |
| syncIODev(route, expression); | |
| return underlyingHeaders.delete.apply(underlyingHeaders, arguments); | |
| } | |
| }, | |
| get: { | |
| value: function get() { | |
| const expression = `\`headers().get(${describeNameArg(arguments[0])})\``; | |
| syncIODev(route, expression); | |
| return underlyingHeaders.get.apply(underlyingHeaders, arguments); | |
| } | |
| }, | |
| has: { | |
| value: function has() { | |
| const expression = `\`headers().has(${describeNameArg(arguments[0])})\``; | |
| syncIODev(route, expression); | |
| return underlyingHeaders.has.apply(underlyingHeaders, arguments); | |
| } | |
| }, | |
| set: { | |
| value: function set() { | |
| const expression = `\`headers().set(${describeNameArg(arguments[0])}, ...)\``; | |
| syncIODev(route, expression); | |
| return underlyingHeaders.set.apply(underlyingHeaders, arguments); | |
| } | |
| }, | |
| getSetCookie: { | |
| value: function getSetCookie() { | |
| const expression = '`headers().getSetCookie()`'; | |
| syncIODev(route, expression); | |
| return underlyingHeaders.getSetCookie.apply(underlyingHeaders, arguments); | |
| } | |
| }, | |
| forEach: { | |
| value: function forEach() { | |
| const expression = '`headers().forEach(...)`'; | |
| syncIODev(route, expression); | |
| return underlyingHeaders.forEach.apply(underlyingHeaders, arguments); | |
| } | |
| }, | |
| keys: { | |
| value: function keys() { | |
| const expression = '`headers().keys()`'; | |
| syncIODev(route, expression); | |
| return underlyingHeaders.keys.apply(underlyingHeaders, arguments); | |
| } | |
| }, | |
| values: { | |
| value: function values() { | |
| const expression = '`headers().values()`'; | |
| syncIODev(route, expression); | |
| return underlyingHeaders.values.apply(underlyingHeaders, arguments); | |
| } | |
| }, | |
| entries: { | |
| value: function entries() { | |
| const expression = '`headers().entries()`'; | |
| syncIODev(route, expression); | |
| return underlyingHeaders.entries.apply(underlyingHeaders, arguments); | |
| } | |
| }, | |
| [Symbol.iterator]: { | |
| value: function() { | |
| const expression = '`...headers()` or similar iteration'; | |
| syncIODev(route, expression); | |
| return underlyingHeaders[Symbol.iterator].apply(underlyingHeaders, arguments); | |
| } | |
| } | |
| }); | |
| return promise; | |
| } | |
| // Similar to `makeUntrackedExoticHeadersWithDevWarnings`, but just logging the | |
| // sync access without actually defining the headers properties on the promise. | |
| function makeUntrackedHeadersWithDevWarnings(underlyingHeaders, route) { | |
| const cachedHeaders = CachedHeaders.get(underlyingHeaders); | |
| if (cachedHeaders) { | |
| return cachedHeaders; | |
| } | |
| const promise = (0, _dynamicrenderingutils.makeDevtoolsIOAwarePromise)(underlyingHeaders); | |
| const proxiedPromise = new Proxy(promise, { | |
| get (target, prop, receiver) { | |
| switch(prop){ | |
| case Symbol.iterator: | |
| { | |
| warnForSyncAccess(route, '`...headers()` or similar iteration'); | |
| break; | |
| } | |
| case 'append': | |
| case 'delete': | |
| case 'get': | |
| case 'has': | |
| case 'set': | |
| case 'getSetCookie': | |
| case 'forEach': | |
| case 'keys': | |
| case 'values': | |
| case 'entries': | |
| { | |
| warnForSyncAccess(route, `\`headers().${prop}\``); | |
| break; | |
| } | |
| default: | |
| { | |
| // We only warn for well-defined properties of the headers object. | |
| } | |
| } | |
| return _reflect.ReflectAdapter.get(target, prop, receiver); | |
| } | |
| }); | |
| CachedHeaders.set(underlyingHeaders, proxiedPromise); | |
| return proxiedPromise; | |
| } | |
| function describeNameArg(arg) { | |
| return typeof arg === 'string' ? `'${arg}'` : '...'; | |
| } | |
| function syncIODev(route, expression) { | |
| const workUnitStore = _workunitasyncstorageexternal.workUnitAsyncStorage.getStore(); | |
| if (workUnitStore) { | |
| switch(workUnitStore.type){ | |
| case 'request': | |
| if (workUnitStore.prerenderPhase === true) { | |
| // When we're rendering dynamically in dev, we need to advance out of | |
| // the Prerender environment when we read Request data synchronously. | |
| (0, _dynamicrendering.trackSynchronousRequestDataAccessInDev)(workUnitStore); | |
| } | |
| break; | |
| case 'prerender': | |
| case 'prerender-client': | |
| case 'prerender-runtime': | |
| case 'prerender-ppr': | |
| case 'prerender-legacy': | |
| case 'cache': | |
| case 'private-cache': | |
| case 'unstable-cache': | |
| break; | |
| default: | |
| workUnitStore; | |
| } | |
| } | |
| // In all cases we warn normally | |
| warnForSyncAccess(route, expression); | |
| } | |
| const warnForSyncAccess = (0, _creatededupedbycallsiteservererrorlogger.createDedupedByCallsiteServerErrorLoggerDev)(createHeadersAccessError); | |
| function createHeadersAccessError(route, expression) { | |
| const prefix = route ? `Route "${route}" ` : 'This route '; | |
| return Object.defineProperty(new Error(`${prefix}used ${expression}. ` + `\`headers()\` should be awaited before using its value. ` + `Learn more: https://nextjs.org/docs/messages/sync-dynamic-apis`), "__NEXT_ERROR_CODE", { | |
| value: "E277", | |
| enumerable: false, | |
| configurable: true | |
| }); | |
| } | |
| //# sourceMappingURL=headers.js.map |