{"version":3,"sources":["../../../src/server/request/connection.ts"],"sourcesContent":["import { workAsyncStorage } from '../app-render/work-async-storage.external'\nimport {\n  throwForMissingRequestStore,\n  workUnitAsyncStorage,\n} from '../app-render/work-unit-async-storage.external'\nimport {\n  postponeWithTracking,\n  throwToInterruptStaticGeneration,\n  trackDynamicDataInDynamicRender,\n} from '../app-render/dynamic-rendering'\nimport { StaticGenBailoutError } from '../../client/components/static-generation-bailout'\nimport {\n  makeHangingPromise,\n  makeDevtoolsIOAwarePromise,\n} from '../dynamic-rendering-utils'\nimport { isRequestAPICallableInsideAfter } from './utils'\nimport { RenderStage } from '../app-render/staged-rendering'\nimport { InvariantError } from '../../shared/lib/invariant-error'\n\n/**\n * This function allows you to indicate that you require an actual user Request before continuing.\n *\n * During prerendering it will never resolve and during rendering it resolves immediately.\n */\nexport function connection(): Promise<void> {\n  const callingExpression = 'connection'\n  const workStore = workAsyncStorage.getStore()\n  const workUnitStore = workUnitAsyncStorage.getStore()\n\n  if (workStore) {\n    if (\n      workUnitStore &&\n      workUnitStore.phase === 'after' &&\n      !isRequestAPICallableInsideAfter()\n    ) {\n      throw new Error(\n        `Route ${workStore.route} used \\`connection()\\` inside \\`after()\\`. The \\`connection()\\` function is used to indicate the subsequent code must only run when there is an actual Request, but \\`after()\\` executes after the request, so this function is not allowed in this scope. See more info here: https://nextjs.org/docs/canary/app/api-reference/functions/after`\n      )\n    }\n\n    if (workStore.forceStatic) {\n      // When using forceStatic, we override all other logic and always just\n      // return a resolving promise without tracking.\n      return Promise.resolve(undefined)\n    }\n\n    if (workStore.dynamicShouldError) {\n      throw new StaticGenBailoutError(\n        `Route ${workStore.route} with \\`dynamic = \"error\"\\` couldn't be rendered statically because it used \\`connection()\\`. See more info here: https://nextjs.org/docs/app/building-your-application/rendering/static-and-dynamic#dynamic-rendering`\n      )\n    }\n\n    if (workUnitStore) {\n      switch (workUnitStore.type) {\n        case 'cache': {\n          const error = new Error(\n            `Route ${workStore.route} used \\`connection()\\` inside \"use cache\". The \\`connection()\\` function is used to indicate the subsequent code must only run when there is an actual request, but caches must be able to be produced before a request, so this function is not allowed in this scope. See more info here: https://nextjs.org/docs/messages/next-request-in-use-cache`\n          )\n          Error.captureStackTrace(error, connection)\n          workStore.invalidDynamicUsageError ??= error\n          throw error\n        }\n        case 'private-cache': {\n          // It might not be intuitive to throw for private caches as well, but\n          // we don't consider runtime prefetches as \"actual requests\" (in the\n          // navigation sense), despite allowing them to read cookies.\n          const error = new Error(\n            `Route ${workStore.route} used \\`connection()\\` inside \"use cache: private\". The \\`connection()\\` function is used to indicate the subsequent code must only run when there is an actual navigation request, but caches must be able to be produced before a navigation request, so this function is not allowed in this scope. See more info here: https://nextjs.org/docs/messages/next-request-in-use-cache`\n          )\n          Error.captureStackTrace(error, connection)\n          workStore.invalidDynamicUsageError ??= error\n          throw error\n        }\n        case 'unstable-cache':\n          throw new Error(\n            `Route ${workStore.route} used \\`connection()\\` inside a function cached with \\`unstable_cache()\\`. The \\`connection()\\` function is used to indicate the subsequent code must only run when there is an actual Request, but caches must be able to be produced before a Request so this function is not allowed in this scope. See more info here: https://nextjs.org/docs/app/api-reference/functions/unstable_cache`\n          )\n        case 'generate-static-params':\n          throw new Error(\n            `Route ${workStore.route} used \\`connection()\\` inside \\`generateStaticParams\\`. This is not supported because \\`generateStaticParams\\` runs at build time without an HTTP request. Read more: https://nextjs.org/docs/messages/next-dynamic-api-wrong-context`\n          )\n        case 'prerender':\n        case 'prerender-client':\n        case 'prerender-runtime':\n          // We return a promise that never resolves to allow the prerender to\n          // stall at this point.\n          return makeHangingPromise(\n            workUnitStore.renderSignal,\n            workStore.route,\n            '`connection()`'\n          )\n        case 'validation-client': {\n          // TODO(NAR-789): make this consistent with the actual browser behavior when we change it.\n          // Until then, erroring is fine.\n          const exportName = '`connection`'\n          throw new InvariantError(\n            `${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.`\n          )\n        }\n        case 'prerender-ppr':\n          // We use React's postpone API to interrupt rendering here to create a\n          // dynamic hole\n          return postponeWithTracking(\n            workStore.route,\n            'connection',\n            workUnitStore.dynamicTracking\n          )\n        case 'prerender-legacy':\n          // We throw an error here to interrupt prerendering to mark the route\n          // as dynamic\n          return throwToInterruptStaticGeneration(\n            'connection',\n            workStore,\n            workUnitStore\n          )\n        case 'request':\n          trackDynamicDataInDynamicRender(workUnitStore)\n          if (process.env.NODE_ENV === 'development') {\n            // Semantically we only need the dev tracking when running in `next dev`\n            // but since you would never use next dev with production NODE_ENV we use this\n            // as a proxy so we can statically exclude this code from production builds.\n            if (workUnitStore.asyncApiPromises) {\n              return workUnitStore.asyncApiPromises.connection\n            }\n            return makeDevtoolsIOAwarePromise(\n              undefined,\n              workUnitStore,\n              RenderStage.Dynamic\n            )\n          } else if (workUnitStore.asyncApiPromises) {\n            return workUnitStore.asyncApiPromises.connection\n          } else {\n            return Promise.resolve(undefined)\n          }\n        default:\n          workUnitStore satisfies never\n      }\n    }\n  }\n\n  // If we end up here, there was no work store or work unit store present.\n  // TODO(NAR-789): connection() is not currently statically prevented from being imported in client components,\n  // so we always error about a missing work unit store.\n  throwForMissingRequestStore(callingExpression)\n}\n"],"names":["connection","callingExpression","workStore","workAsyncStorage","getStore","workUnitStore","workUnitAsyncStorage","phase","isRequestAPICallableInsideAfter","Error","route","forceStatic","Promise","resolve","undefined","dynamicShouldError","StaticGenBailoutError","type","error","captureStackTrace","invalidDynamicUsageError","makeHangingPromise","renderSignal","exportName","InvariantError","postponeWithTracking","dynamicTracking","throwToInterruptStaticGeneration","trackDynamicDataInDynamicRender","process","env","NODE_ENV","asyncApiPromises","makeDevtoolsIOAwarePromise","RenderStage","Dynamic","throwForMissingRequestStore"],"mappings":";;;;+BAwBgBA;;;eAAAA;;;0CAxBiB;8CAI1B;kCAKA;yCAC+B;uCAI/B;uBACyC;iCACpB;gCACG;AAOxB,SAASA;IACd,MAAMC,oBAAoB;IAC1B,MAAMC,YAAYC,0CAAgB,CAACC,QAAQ;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