UNPKG

next

Version:

The React Framework

122 lines (121 loc) 5.95 kB
import { InvariantError } from '../../shared/lib/invariant-error'; import { postponeWithTracking, throwToInterruptStaticGeneration } from '../app-render/dynamic-rendering'; import { workAsyncStorage } from '../app-render/work-async-storage.external'; import { workUnitAsyncStorage } from '../app-render/work-unit-async-storage.external'; import { makeHangingPromise } from '../dynamic-rendering-utils'; import { describeStringPropertyAccess, wellKnownProperties } from '../../shared/lib/utils/reflect-utils'; const CachedParams = new WeakMap(); export async function unstable_rootParams() { const workStore = workAsyncStorage.getStore(); if (!workStore) { throw Object.defineProperty(new InvariantError('Missing workStore in unstable_rootParams'), "__NEXT_ERROR_CODE", { value: "E615", enumerable: false, configurable: true }); } const workUnitStore = workUnitAsyncStorage.getStore(); if (!workUnitStore) { throw Object.defineProperty(new Error(`Route ${workStore.route} used \`unstable_rootParams()\` in Pages Router. This API is only available within App Router.`), "__NEXT_ERROR_CODE", { value: "E641", enumerable: false, configurable: true }); } switch(workUnitStore.type){ case 'unstable-cache': case 'cache': { throw Object.defineProperty(new Error(`Route ${workStore.route} used \`unstable_rootParams()\` inside \`"use cache"\` or \`unstable_cache\`. Support for this API inside cache scopes is planned for a future version of Next.js.`), "__NEXT_ERROR_CODE", { value: "E642", enumerable: false, configurable: true }); } case 'prerender': case 'prerender-ppr': case 'prerender-legacy': return createPrerenderRootParams(workUnitStore.rootParams, workStore, workUnitStore); default: return Promise.resolve(workUnitStore.rootParams); } } function createPrerenderRootParams(underlyingParams, workStore, prerenderStore) { const fallbackParams = workStore.fallbackRouteParams; if (fallbackParams) { let hasSomeFallbackParams = false; for(const key in underlyingParams){ if (fallbackParams.has(key)) { hasSomeFallbackParams = true; break; } } if (hasSomeFallbackParams) { // params need to be treated as dynamic because we have at least one fallback param if (prerenderStore.type === 'prerender') { // We are in a dynamicIO (PPR or otherwise) prerender const cachedParams = CachedParams.get(underlyingParams); if (cachedParams) { return cachedParams; } const promise = makeHangingPromise(prerenderStore.renderSignal, '`unstable_rootParams`'); CachedParams.set(underlyingParams, promise); return promise; } // remaining cases are prerender-ppr and prerender-legacy // We aren't in a dynamicIO prerender but we do have fallback params at this // level so we need to make an erroring params object which will postpone // if you access the fallback params return makeErroringRootParams(underlyingParams, fallbackParams, workStore, prerenderStore); } } // We don't have any fallback params so we have an entirely static safe params object return Promise.resolve(underlyingParams); } function makeErroringRootParams(underlyingParams, fallbackParams, workStore, prerenderStore) { const cachedParams = CachedParams.get(underlyingParams); if (cachedParams) { return cachedParams; } const augmentedUnderlying = { ...underlyingParams }; // We don't use makeResolvedReactPromise here because params // supports copying with spread and we don't want to unnecessarily // instrument the promise with spreadable properties of ReactPromise. const promise = Promise.resolve(augmentedUnderlying); CachedParams.set(underlyingParams, promise); Object.keys(underlyingParams).forEach((prop)=>{ if (wellKnownProperties.has(prop)) { // These properties cannot be shadowed because they need to be the // true underlying value for Promises to work correctly at runtime } else { if (fallbackParams.has(prop)) { Object.defineProperty(augmentedUnderlying, prop, { get () { const expression = describeStringPropertyAccess('unstable_rootParams', prop); // In most dynamic APIs we also throw if `dynamic = "error"` however // for params is only dynamic when we're generating a fallback shell // and even when `dynamic = "error"` we still support generating dynamic // fallback shells // TODO remove this comment when dynamicIO is the default since there // will be no `dynamic = "error"` if (prerenderStore.type === 'prerender-ppr') { // PPR Prerender (no dynamicIO) postponeWithTracking(workStore.route, expression, prerenderStore.dynamicTracking); } else { // Legacy Prerender throwToInterruptStaticGeneration(expression, workStore, prerenderStore); } }, enumerable: true }); } else { ; promise[prop] = underlyingParams[prop]; } } }); return promise; } //# sourceMappingURL=root-params.js.map