| | import type { OutgoingHttpHeaders } from 'node:http' |
| | import type { ExportRouteResult } from '../types' |
| | import type { RenderOpts } from '../../server/app-render/types' |
| | import type { NextParsedUrlQuery } from '../../server/request-meta' |
| | import type { RouteMetadata } from './types' |
| |
|
| | import type { |
| | MockedRequest, |
| | MockedResponse, |
| | } from '../../server/lib/mock-request' |
| | import { isDynamicUsageError } from '../helpers/is-dynamic-usage-error' |
| | import { |
| | NEXT_CACHE_TAGS_HEADER, |
| | NEXT_META_SUFFIX, |
| | RSC_PREFETCH_SUFFIX, |
| | RSC_SUFFIX, |
| | RSC_SEGMENTS_DIR_SUFFIX, |
| | RSC_SEGMENT_SUFFIX, |
| | } from '../../lib/constants' |
| | import { hasNextSupport } from '../../server/ci-info' |
| | import { lazyRenderAppPage } from '../../server/route-modules/app-page/module.render' |
| | import { isBailoutToCSRError } from '../../shared/lib/lazy-dynamic/bailout-to-csr' |
| | import { NodeNextRequest, NodeNextResponse } from '../../server/base-http/node' |
| | import { NEXT_IS_PRERENDER_HEADER } from '../../client/components/app-router-headers' |
| | import type { FetchMetrics } from '../../server/base-http' |
| | import type { WorkStore } from '../../server/app-render/work-async-storage.external' |
| | import type { FallbackRouteParams } from '../../server/request/fallback-params' |
| | import { AfterRunner } from '../../server/after/run-with-after' |
| | import type { RequestLifecycleOpts } from '../../server/base-server' |
| | import type { AppSharedContext } from '../../server/app-render/app-render' |
| | import type { MultiFileWriter } from '../../lib/multi-file-writer' |
| | import { stringifyResumeDataCache } from '../../server/resume-data-cache/resume-data-cache' |
| |
|
| | |
| | |
| | |
| | export async function exportAppPage( |
| | req: MockedRequest, |
| | res: MockedResponse, |
| | page: string, |
| | path: string, |
| | pathname: string, |
| | query: NextParsedUrlQuery, |
| | fallbackRouteParams: FallbackRouteParams | null, |
| | partialRenderOpts: Omit<RenderOpts, keyof RequestLifecycleOpts>, |
| | htmlFilepath: string, |
| | debugOutput: boolean, |
| | isDynamicError: boolean, |
| | fileWriter: MultiFileWriter, |
| | sharedContext: AppSharedContext |
| | ): Promise<ExportRouteResult> { |
| | const afterRunner = new AfterRunner() |
| |
|
| | const renderOpts: RenderOpts = { |
| | ...partialRenderOpts, |
| | waitUntil: afterRunner.context.waitUntil, |
| | onClose: afterRunner.context.onClose, |
| | onAfterTaskError: afterRunner.context.onTaskError, |
| | } |
| |
|
| | let isDefaultNotFound = false |
| | |
| | |
| | if (page === '/_not-found/page') { |
| | isDefaultNotFound = true |
| | pathname = '/404' |
| | } |
| |
|
| | try { |
| | const result = await lazyRenderAppPage( |
| | new NodeNextRequest(req), |
| | new NodeNextResponse(res), |
| | pathname, |
| | query, |
| | fallbackRouteParams, |
| | renderOpts, |
| | undefined, |
| | false, |
| | sharedContext |
| | ) |
| |
|
| | const html = result.toUnchunkedString() |
| |
|
| | |
| | |
| | await afterRunner.executeAfter() |
| |
|
| | const { metadata } = result |
| | const { |
| | flightData, |
| | cacheControl = { revalidate: false, expire: undefined }, |
| | postponed, |
| | fetchTags, |
| | fetchMetrics, |
| | segmentData, |
| | renderResumeDataCache, |
| | } = metadata |
| |
|
| | |
| | if (postponed && !renderOpts.experimental.isRoutePPREnabled) { |
| | throw new Error('Invariant: page postponed without PPR being enabled') |
| | } |
| |
|
| | if (cacheControl.revalidate === 0) { |
| | if (isDynamicError) { |
| | throw new Error( |
| | `Page with dynamic = "error" encountered dynamic data method on ${path}.` |
| | ) |
| | } |
| | const { staticBailoutInfo = {} } = metadata |
| |
|
| | if (debugOutput && staticBailoutInfo?.description) { |
| | logDynamicUsageWarning({ |
| | path, |
| | description: staticBailoutInfo.description, |
| | stack: staticBailoutInfo.stack, |
| | }) |
| | } |
| |
|
| | return { cacheControl, fetchMetrics } |
| | } |
| |
|
| | |
| | |
| | |
| | if ( |
| | !flightData && |
| | (!fallbackRouteParams || fallbackRouteParams.size === 0) |
| | ) { |
| | throw new Error(`Invariant: failed to get page data for ${path}`) |
| | } |
| |
|
| | if (flightData) { |
| | |
| | |
| | |
| | |
| | if (renderOpts.experimental.isRoutePPREnabled) { |
| | |
| | |
| | |
| | |
| | fileWriter.append( |
| | htmlFilepath.replace(/\.html$/, RSC_PREFETCH_SUFFIX), |
| | flightData |
| | ) |
| | } else { |
| | |
| | fileWriter.append( |
| | htmlFilepath.replace(/\.html$/, RSC_SUFFIX), |
| | flightData |
| | ) |
| | } |
| | } |
| |
|
| | let segmentPaths |
| | if (segmentData) { |
| | |
| | |
| | |
| | segmentPaths = [] |
| | const segmentsDir = htmlFilepath.replace( |
| | /\.html$/, |
| | RSC_SEGMENTS_DIR_SUFFIX |
| | ) |
| |
|
| | for (const [segmentPath, buffer] of segmentData) { |
| | segmentPaths.push(segmentPath) |
| | const segmentDataFilePath = |
| | segmentsDir + segmentPath + RSC_SEGMENT_SUFFIX |
| | fileWriter.append(segmentDataFilePath, buffer) |
| | } |
| | } |
| |
|
| | const headers: OutgoingHttpHeaders = { ...metadata.headers } |
| |
|
| | |
| | headers[NEXT_IS_PRERENDER_HEADER] = '1' |
| |
|
| | if (fetchTags) { |
| | headers[NEXT_CACHE_TAGS_HEADER] = fetchTags |
| | } |
| |
|
| | |
| | fileWriter.append(htmlFilepath, html) |
| |
|
| | const isParallelRoute = /\/@\w+/.test(page) |
| | const isNonSuccessfulStatusCode = res.statusCode > 300 |
| |
|
| | |
| | |
| | |
| | let status: number | undefined = renderOpts.experimental.isRoutePPREnabled |
| | ? res.statusCode |
| | : undefined |
| |
|
| | if (isDefaultNotFound) { |
| | |
| | status = 404 |
| | } else if (isNonSuccessfulStatusCode && !isParallelRoute) { |
| | |
| | status = res.statusCode |
| | } |
| |
|
| | |
| | const meta: RouteMetadata = { |
| | status, |
| | headers, |
| | postponed, |
| | segmentPaths, |
| | } |
| |
|
| | fileWriter.append( |
| | htmlFilepath.replace(/\.html$/, NEXT_META_SUFFIX), |
| | JSON.stringify(meta, null, 2) |
| | ) |
| |
|
| | return { |
| | |
| | metadata: hasNextSupport |
| | ? meta |
| | : { |
| | segmentPaths: meta.segmentPaths, |
| | }, |
| | hasEmptyStaticShell: Boolean(postponed) && html === '', |
| | hasPostponed: Boolean(postponed), |
| | cacheControl, |
| | fetchMetrics, |
| | renderResumeDataCache: renderResumeDataCache |
| | ? await stringifyResumeDataCache(renderResumeDataCache) |
| | : undefined, |
| | } |
| | } catch (err) { |
| | if (!isDynamicUsageError(err)) { |
| | throw err |
| | } |
| |
|
| | |
| | |
| | if (isBailoutToCSRError(err)) { |
| | throw err |
| | } |
| |
|
| | let fetchMetrics: FetchMetrics | undefined |
| |
|
| | if (debugOutput) { |
| | const store = (renderOpts as any).store as WorkStore |
| | const { dynamicUsageDescription, dynamicUsageStack } = store |
| | fetchMetrics = store.fetchMetrics |
| |
|
| | logDynamicUsageWarning({ |
| | path, |
| | description: dynamicUsageDescription ?? '', |
| | stack: dynamicUsageStack, |
| | }) |
| | } |
| |
|
| | return { cacheControl: { revalidate: 0, expire: undefined }, fetchMetrics } |
| | } |
| | } |
| |
|
| | function logDynamicUsageWarning({ |
| | path, |
| | description, |
| | stack, |
| | }: { |
| | path: string |
| | description: string |
| | stack?: string |
| | }) { |
| | const errMessage = new Error( |
| | `Static generation failed due to dynamic usage on ${path}, reason: ${description}` |
| | ) |
| |
|
| | if (stack) { |
| | errMessage.stack = errMessage.message + stack.substring(stack.indexOf('\n')) |
| | } |
| |
|
| | console.warn(errMessage) |
| | } |
| |
|