{"version":3,"sources":["../../../../src/server/request/root-params.ts"],"sourcesContent":["import { InvariantError } from '../../shared/lib/invariant-error'\nimport {\n  postponeWithTracking,\n  throwToInterruptStaticGeneration,\n} from '../app-render/dynamic-rendering'\nimport {\n  workAsyncStorage,\n  type WorkStore,\n} from '../app-render/work-async-storage.external'\nimport {\n  workUnitAsyncStorage,\n  type PrerenderStoreLegacy,\n  type PrerenderStoreModernServer,\n  type PrerenderStorePPR,\n} from '../app-render/work-unit-async-storage.external'\nimport { makeHangingPromise } from '../dynamic-rendering-utils'\nimport type { ParamValue } from './params'\nimport { describeStringPropertyAccess } from '../../shared/lib/utils/reflect-utils'\nimport { actionAsyncStorage } from '../app-render/action-async-storage.external'\nimport { accumulateRootVaryParam } from '../app-render/vary-params'\n\n/**\n * Used for the compiler-generated `next/root-params` module.\n * @internal\n */\nexport function getRootParam(paramName: string): Promise<ParamValue> {\n  const apiName = `\\`import('next/root-params').${paramName}()\\``\n\n  const workStore = workAsyncStorage.getStore()\n  if (!workStore) {\n    throw new InvariantError(`Missing workStore in ${apiName}`)\n  }\n\n  const workUnitStore = workUnitAsyncStorage.getStore()\n  if (!workUnitStore) {\n    throw new Error(\n      `Route ${workStore.route} used ${apiName} outside of a Server Component. This is not allowed.`\n    )\n  }\n\n  const actionStore = actionAsyncStorage.getStore()\n  if (actionStore) {\n    if (actionStore.isAppRoute) {\n      // TODO(root-params): add support for route handlers\n      throw new Error(\n        `Route ${workStore.route} used ${apiName} inside a Route Handler. Support for this API in Route Handlers is planned for a future version of Next.js.`\n      )\n    }\n    if (actionStore.isAction && workUnitStore.phase === 'action') {\n      // Actions are not fundamentally tied to a route (even if they're always submitted from some page),\n      // so root params would be inconsistent if an action is called from multiple roots.\n      // Make sure we check if the phase is \"action\" - we should not error in the rerender\n      // after an action revalidates or updates cookies (which will still have `actionStore.isAction === true`)\n      throw new Error(\n        `${apiName} was used inside a Server Action. This is not supported. Functions from 'next/root-params' can only be called in the context of a route.`\n      )\n    }\n  }\n\n  switch (workUnitStore.type) {\n    case 'unstable-cache': {\n      throw new Error(\n        `Route ${workStore.route} used ${apiName} inside \\`unstable_cache\\`. This is not supported. Use \\`\"use cache\"\\` instead.`\n      )\n    }\n    case 'cache': {\n      if (!workUnitStore.rootParams) {\n        throw new Error(\n          `Route ${workStore.route} used ${apiName} inside \\`\"use cache\"\\` nested within \\`unstable_cache\\`. Root params are not available in this context.`\n        )\n      }\n      workUnitStore.readRootParamNames.add(paramName)\n      return Promise.resolve(workUnitStore.rootParams[paramName])\n    }\n    case 'prerender':\n    case 'prerender-ppr':\n    case 'prerender-legacy': {\n      return createPrerenderRootParamPromise(\n        paramName,\n        workStore,\n        workUnitStore,\n        apiName\n      )\n    }\n    case 'validation-client':\n    case 'prerender-client': {\n      throw new InvariantError(\n        `${apiName} must not be used within a client component. Next.js should be preventing ${apiName} from being included in client components statically, but did not in this case.`\n      )\n    }\n    case 'request': {\n      if (\n        process.env.__NEXT_CACHE_COMPONENTS &&\n        workUnitStore.validationSamples\n      ) {\n        const { assertRootParamInSamples } =\n          require('../app-render/instant-validation/instant-samples') as typeof import('../app-render/instant-validation/instant-samples')\n        // If we error, make sure we return a rejected promise instead of erroring synchronously.\n        try {\n          assertRootParamInSamples(\n            workStore,\n            workUnitStore.validationSamples.params,\n            paramName\n          )\n        } catch (err) {\n          return Promise.reject(err)\n        }\n      }\n      break\n    }\n    case 'private-cache':\n    case 'prerender-runtime': {\n      break\n    }\n    case 'generate-static-params': {\n      if (!(paramName in workUnitStore.rootParams)) {\n        throw new Error(\n          `Route ${workStore.route} used ${apiName} inside \\`generateStaticParams\\`, but the \\`${paramName}\\` parameter was not provided by a parent \\`generateStaticParams\\`. In \\`generateStaticParams\\`, root params are only available for segments nested below the segment that provides them.`\n        )\n      }\n      break\n    }\n    default: {\n      workUnitStore satisfies never\n    }\n  }\n\n  accumulateRootVaryParam(paramName)\n  return Promise.resolve(workUnitStore.rootParams[paramName])\n}\n\nfunction createPrerenderRootParamPromise(\n  paramName: string,\n  workStore: WorkStore,\n  prerenderStore:\n    | PrerenderStorePPR\n    | PrerenderStoreLegacy\n    | PrerenderStoreModernServer,\n  apiName: string\n): Promise<ParamValue> {\n  switch (prerenderStore.type) {\n    case 'prerender':\n    case 'prerender-legacy':\n    case 'prerender-ppr':\n    default:\n  }\n\n  const underlyingParams = prerenderStore.rootParams\n\n  switch (prerenderStore.type) {\n    case 'prerender': {\n      // We are in a cacheComponents prerender.\n      // The param is a fallback, so it should be treated as dynamic.\n      if (\n        prerenderStore.fallbackRouteParams &&\n        prerenderStore.fallbackRouteParams.has(paramName)\n      ) {\n        return makeHangingPromise<ParamValue>(\n          prerenderStore.renderSignal,\n          workStore.route,\n          apiName\n        )\n      }\n      break\n    }\n    case 'prerender-ppr': {\n      // We aren't in a cacheComponents prerender, but the param is a fallback,\n      // so we need to make an erroring params object which will postpone/error if you access it\n      if (\n        prerenderStore.fallbackRouteParams &&\n        prerenderStore.fallbackRouteParams.has(paramName)\n      ) {\n        return makeErroringRootParamPromise(\n          paramName,\n          workStore,\n          prerenderStore,\n          apiName\n        )\n      }\n      break\n    }\n    case 'prerender-legacy': {\n      // legacy prerenders can't have fallback params\n      break\n    }\n    default: {\n      prerenderStore satisfies never\n    }\n  }\n\n  // If the param is not a fallback param, we just return the statically available value.\n  accumulateRootVaryParam(paramName)\n  return Promise.resolve(underlyingParams[paramName])\n}\n\n/** Deliberately async -- we want to create a rejected promise, not error synchronously. */\nasync function makeErroringRootParamPromise(\n  paramName: string,\n  workStore: WorkStore,\n  prerenderStore: PrerenderStorePPR | PrerenderStoreLegacy,\n  apiName: string\n): Promise<ParamValue> {\n  const expression = describeStringPropertyAccess(apiName, paramName)\n  // In most dynamic APIs, we also throw if `dynamic = \"error\"`.\n  // However, root params are only dynamic when we're generating a fallback shell,\n  // and even with `dynamic = \"error\"` we still support generating dynamic fallback shells.\n  // TODO: remove this comment when cacheComponents is the default since there will be no `dynamic = \"error\"`\n  switch (prerenderStore.type) {\n    case 'prerender-ppr': {\n      return postponeWithTracking(\n        workStore.route,\n        expression,\n        prerenderStore.dynamicTracking\n      )\n    }\n    case 'prerender-legacy': {\n      return throwToInterruptStaticGeneration(\n        expression,\n        workStore,\n        prerenderStore\n      )\n    }\n    default: {\n      prerenderStore satisfies never\n    }\n  }\n}\n"],"names":["InvariantError","postponeWithTracking","throwToInterruptStaticGeneration","workAsyncStorage","workUnitAsyncStorage","makeHangingPromise","describeStringPropertyAccess","actionAsyncStorage","accumulateRootVaryParam","getRootParam","paramName","apiName","workStore","getStore","workUnitStore","Error","route","actionStore","isAppRoute","isAction","phase","type","rootParams","readRootParamNames","add","Promise","resolve","createPrerenderRootParamPromise","process","env","__NEXT_CACHE_COMPONENTS","validationSamples","assertRootParamInSamples","require","params","err","reject","prerenderStore","underlyingParams","fallbackRouteParams","has","renderSignal","makeErroringRootParamPromise","expression","dynamicTracking"],"mappings":"AAAA,SAASA,cAAc,QAAQ,mCAAkC;AACjE,SACEC,oBAAoB,EACpBC,gCAAgC,QAC3B,kCAAiC;AACxC,SACEC,gBAAgB,QAEX,4CAA2C;AAClD,SACEC,oBAAoB,QAIf,iDAAgD;AACvD,SAASC,kBAAkB,QAAQ,6BAA4B;AAE/D,SAASC,4BAA4B,QAAQ,uCAAsC;AACnF,SAASC,kBAAkB,QAAQ,8CAA6C;AAChF,SAASC,uBAAuB,QAAQ,4BAA2B;AAEnE;;;CAGC,GACD,OAAO,SAASC,aAAaC,SAAiB;IAC5C,MAAMC,UAAU,CAAC,6BAA6B,EAAED,UAAU,IAAI,CAAC;IAE/D,MAAME,YAAYT,iBAAiBU,QAAQ;IAC3C,IAAI,CAACD,WAAW;QACd,MAAM,qBAAqD,CAArD,IAAIZ,eAAe,CAAC,qBAAqB,EAAEW,SAAS,GAApD,qBAAA;mBAAA;wBAAA;0BAAA;QAAoD;IAC5D;IAEA,MAAMG,gBAAgBV,qBAAqBS,QAAQ;IACnD,IAAI,CAACC,eAAe;QAClB,MAAM,qBAEL,CAFK,IAAIC,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