UNPKG

next

Version:

The React Framework

132 lines (131 loc) 6.3 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "unstable_rootParams", { enumerable: true, get: function() { return unstable_rootParams; } }); const _invarianterror = require("../../shared/lib/invariant-error"); const _dynamicrendering = require("../app-render/dynamic-rendering"); const _workasyncstorageexternal = require("../app-render/work-async-storage.external"); const _workunitasyncstorageexternal = require("../app-render/work-unit-async-storage.external"); const _dynamicrenderingutils = require("../dynamic-rendering-utils"); const _reflectutils = require("../../shared/lib/utils/reflect-utils"); const CachedParams = new WeakMap(); async function unstable_rootParams() { const workStore = _workasyncstorageexternal.workAsyncStorage.getStore(); if (!workStore) { throw Object.defineProperty(new _invarianterror.InvariantError('Missing workStore in unstable_rootParams'), "__NEXT_ERROR_CODE", { value: "E615", enumerable: false, configurable: true }); } const workUnitStore = _workunitasyncstorageexternal.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 = (0, _dynamicrenderingutils.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 (_reflectutils.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 = (0, _reflectutils.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) (0, _dynamicrendering.postponeWithTracking)(workStore.route, expression, prerenderStore.dynamicTracking); } else { // Legacy Prerender (0, _dynamicrendering.throwToInterruptStaticGeneration)(expression, workStore, prerenderStore); } }, enumerable: true }); } else { ; promise[prop] = underlyingParams[prop]; } } }); return promise; } //# sourceMappingURL=root-params.js.map