{"version":3,"sources":["../../../../src/export/routes/app-route.ts"],"sourcesContent":["import type { ExportRouteResult } from '../types'\nimport type AppRouteRouteModule from '../../server/route-modules/app-route/module'\nimport type { AppRouteRouteHandlerContext } from '../../server/route-modules/app-route/module'\nimport type { IncrementalCache } from '../../server/lib/incremental-cache'\n\nimport {\n  INFINITE_CACHE,\n  NEXT_BODY_SUFFIX,\n  NEXT_CACHE_TAGS_HEADER,\n  NEXT_META_SUFFIX,\n} from '../../lib/constants'\nimport { NodeNextRequest } from '../../server/base-http/node'\nimport {\n  NextRequestAdapter,\n  signalFromNodeResponse,\n} from '../../server/web/spec-extension/adapters/next-request'\nimport { toNodeOutgoingHttpHeaders } from '../../server/web/utils'\nimport type {\n  MockedRequest,\n  MockedResponse,\n} from '../../server/lib/mock-request'\nimport { isDynamicUsageError } from '../helpers/is-dynamic-usage-error'\nimport { isStaticGenEnabled } from '../../server/route-modules/app-route/helpers/is-static-gen-enabled'\nimport type { ExperimentalConfig } from '../../server/config-shared'\nimport { isMetadataRoute } from '../../lib/metadata/is-metadata-route'\nimport { normalizeAppPath } from '../../shared/lib/router/utils/app-paths'\nimport type { Params } from '../../server/request/params'\nimport { AfterRunner } from '../../server/after/run-with-after'\nimport type { MultiFileWriter } from '../../lib/multi-file-writer'\n\nexport const enum ExportedAppRouteFiles {\n  BODY = 'BODY',\n  META = 'META',\n}\n\nexport async function exportAppRoute(\n  req: MockedRequest,\n  res: MockedResponse,\n  params: Params | undefined,\n  page: string,\n  module: AppRouteRouteModule,\n  incrementalCache: IncrementalCache | undefined,\n  cacheLifeProfiles:\n    | undefined\n    | {\n        [profile: string]: import('../../server/use-cache/cache-life').CacheLife\n      },\n  htmlFilepath: string,\n  fileWriter: MultiFileWriter,\n  cacheComponents: boolean,\n  experimental: Required<Pick<ExperimentalConfig, 'authInterrupts'>>,\n  buildId: string,\n  deploymentId: string\n): Promise<ExportRouteResult> {\n  // Ensure that the URL is absolute.\n  req.url = `http://localhost:3000${req.url}`\n\n  // Adapt the request and response to the Next.js request and response.\n  const request = NextRequestAdapter.fromNodeNextRequest(\n    new NodeNextRequest(req),\n    signalFromNodeResponse(res)\n  )\n\n  const afterRunner = new AfterRunner()\n\n  // Create the context for the handler. This contains the params from\n  // the route and the context for the request.\n  const context: AppRouteRouteHandlerContext = {\n    params,\n    previewProps: {\n      previewModeEncryptionKey: '',\n      previewModeId: '',\n      previewModeSigningKey: '',\n    },\n    renderOpts: {\n      cacheComponents,\n      experimental,\n      isBuildTimePrerendering: true,\n      supportsDynamicResponse: false,\n      incrementalCache,\n      waitUntil: afterRunner.context.waitUntil,\n      onClose: afterRunner.context.onClose,\n      onAfterTaskError: afterRunner.context.onTaskError,\n      cacheLifeProfiles,\n    },\n    sharedContext: {\n      buildId,\n      deploymentId,\n    },\n  }\n\n  try {\n    const userland = module.userland\n    // we don't bail from the static optimization for\n    // metadata routes, since it's app-route we can always append /route suffix.\n    const routePath = normalizeAppPath(page) + '/route'\n    const isPageMetadataRoute = isMetadataRoute(routePath)\n\n    if (\n      !isStaticGenEnabled(userland) &&\n      !isPageMetadataRoute &&\n      // We don't disable static gen when cacheComponents is enabled because we\n      // expect that anything dynamic in the GET handler will make it dynamic\n      // and thus avoid the cache surprises that led to us removing static gen\n      // unless specifically opted into\n      cacheComponents !== true\n    ) {\n      return { cacheControl: { revalidate: 0, expire: undefined } }\n    }\n\n    const response = await module.handle(request, context)\n\n    const isValidStatus = response.status < 400 || response.status === 404\n    if (!isValidStatus) {\n      return { cacheControl: { revalidate: 0, expire: undefined } }\n    }\n\n    const blob = await response.blob()\n\n    // TODO(after): if we abort a prerender because of an error in an after-callback\n    // we should probably communicate that better (and not log the error twice)\n    await afterRunner.executeAfter()\n\n    const revalidate =\n      typeof context.renderOpts.collectedRevalidate === 'undefined' ||\n      context.renderOpts.collectedRevalidate >= INFINITE_CACHE\n        ? false\n        : context.renderOpts.collectedRevalidate\n\n    const expire =\n      typeof context.renderOpts.collectedExpire === 'undefined' ||\n      context.renderOpts.collectedExpire >= INFINITE_CACHE\n        ? undefined\n        : context.renderOpts.collectedExpire\n\n    const headers = toNodeOutgoingHttpHeaders(response.headers)\n    const cacheTags = context.renderOpts.collectedTags\n\n    if (cacheTags) {\n      headers[NEXT_CACHE_TAGS_HEADER] = cacheTags\n    }\n\n    if (!headers['content-type'] && blob.type) {\n      headers['content-type'] = blob.type\n    }\n\n    // Writing response body to a file.\n    const body = Buffer.from(await blob.arrayBuffer())\n    fileWriter.append(htmlFilepath.replace(/\\.html$/, NEXT_BODY_SUFFIX), body)\n\n    // Write the request metadata to a file.\n    const meta = { status: response.status, headers }\n    fileWriter.append(\n      htmlFilepath.replace(/\\.html$/, NEXT_META_SUFFIX),\n      JSON.stringify(meta)\n    )\n\n    return {\n      cacheControl: { revalidate, expire },\n      metadata: meta,\n    }\n  } catch (err) {\n    if (!isDynamicUsageError(err)) {\n      throw err\n    }\n\n    return { cacheControl: { revalidate: 0, expire: undefined } }\n  }\n}\n"],"names":["INFINITE_CACHE","NEXT_BODY_SUFFIX","NEXT_CACHE_TAGS_HEADER","NEXT_META_SUFFIX","NodeNextRequest","NextRequestAdapter","signalFromNodeResponse","toNodeOutgoingHttpHeaders","isDynamicUsageError","isStaticGenEnabled","isMetadataRoute","normalizeAppPath","AfterRunner","ExportedAppRouteFiles","exportAppRoute","req","res","params","page","module","incrementalCache","cacheLifeProfiles","htmlFilepath","fileWriter","cacheComponents","experimental","buildId","deploymentId","url","request","fromNodeNextRequest","afterRunner","context","previewProps","previewModeEncryptionKey","previewModeId","previewModeSigningKey","renderOpts","isBuildTimePrerendering","supportsDynamicResponse","waitUntil","onClose","onAfterTaskError","onTaskError","sharedContext","userland","routePath","isPageMetadataRoute","cacheControl","revalidate","expire","undefined","response","handle","isValidStatus","status","blob","executeAfter","collectedRevalidate","collectedExpire","headers","cacheTags","collectedTags","type","body","Buffer","from","arrayBuffer","append","replace","meta","JSON","stringify","metadata","err"],"mappings":"AAKA,SACEA,cAAc,EACdC,gBAAgB,EAChBC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