{"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>,\n buildId: string\n): Promise {\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 prerenderManifest: {\n version: 4,\n routes: {},\n dynamicRoutes: {},\n preview: {\n previewModeEncryptionKey: '',\n previewModeId: '',\n previewModeSigningKey: '',\n },\n notFoundRoutes: [],\n },\n renderOpts: {\n cacheComponents,\n experimental,\n nextExport: 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 },\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":["ExportedAppRouteFiles","exportAppRoute","req","res","params","page","module","incrementalCache","cacheLifeProfiles","htmlFilepath","fileWriter","cacheComponents","experimental","buildId","url","request","NextRequestAdapter","fromNodeNextRequest","NodeNextRequest","signalFromNodeResponse","afterRunner","AfterRunner","context","prerenderManifest","version","routes","dynamicRoutes","preview","previewModeEncryptionKey","previewModeId","previewModeSigningKey","notFoundRoutes","renderOpts","nextExport","supportsDynamicResponse","waitUntil","onClose","onAfterTaskError","onTaskError","sharedContext","userland","routePath","normalizeAppPath","isPageMetadataRoute","isMetadataRoute","isStaticGenEnabled","cacheControl","revalidate","expire","undefined","response","handle","isValidStatus","status","blob","executeAfter","collectedRevalidate","INFINITE_CACHE","collectedExpire","headers","toNodeOutgoingHttpHeaders","cacheTags","collectedTags","NEXT_CACHE_TAGS_HEADER","type","body","Buffer","from","arrayBuffer","append","replace","NEXT_BODY_SUFFIX","meta","NEXT_META_SUFFIX","JSON","stringify","metadata","err","isDynamicUsageError"],"mappings":";;;;;;;;;;;;;;;IA8BkBA,qBAAqB;eAArBA;;IAKIC,cAAc;eAAdA;;;2BAzBf;sBACyB;6BAIzB;uBACmC;qCAKN;oCACD;iCAEH;0BACC;8BAEL;AAGrB,IAAA,AAAWD,+CAAAA;;;WAAAA;;AAKX,e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