|
|
import type { AppBuildManifest } from './webpack/plugins/app-build-manifest-plugin' |
|
|
import type { PagesManifest } from './webpack/plugins/pages-manifest-plugin' |
|
|
import type { ExportPathMap, NextConfigComplete } from '../server/config-shared' |
|
|
import type { MiddlewareManifest } from './webpack/plugins/middleware-plugin' |
|
|
import type { ActionManifest } from './webpack/plugins/flight-client-entry-plugin' |
|
|
import type { CacheControl, Revalidate } from '../server/lib/cache-control' |
|
|
|
|
|
import '../lib/setup-exception-listeners' |
|
|
|
|
|
import { loadEnvConfig, type LoadedEnvFiles } from '@next/env' |
|
|
import { bold, yellow } from '../lib/picocolors' |
|
|
import { makeRe } from 'next/dist/compiled/picomatch' |
|
|
import { existsSync, promises as fs } from 'fs' |
|
|
import os from 'os' |
|
|
import { Worker } from '../lib/worker' |
|
|
import { defaultConfig } from '../server/config-shared' |
|
|
import devalue from 'next/dist/compiled/devalue' |
|
|
import findUp from 'next/dist/compiled/find-up' |
|
|
import { nanoid } from 'next/dist/compiled/nanoid/index.cjs' |
|
|
import path from 'path' |
|
|
import { |
|
|
STATIC_STATUS_PAGE_GET_INITIAL_PROPS_ERROR, |
|
|
PUBLIC_DIR_MIDDLEWARE_CONFLICT, |
|
|
MIDDLEWARE_FILENAME, |
|
|
PAGES_DIR_ALIAS, |
|
|
INSTRUMENTATION_HOOK_FILENAME, |
|
|
RSC_PREFETCH_SUFFIX, |
|
|
RSC_SUFFIX, |
|
|
NEXT_RESUME_HEADER, |
|
|
PRERENDER_REVALIDATE_HEADER, |
|
|
PRERENDER_REVALIDATE_ONLY_GENERATED_HEADER, |
|
|
NEXT_CACHE_REVALIDATE_TAG_TOKEN_HEADER, |
|
|
NEXT_CACHE_REVALIDATED_TAGS_HEADER, |
|
|
MATCHED_PATH_HEADER, |
|
|
RSC_SEGMENTS_DIR_SUFFIX, |
|
|
RSC_SEGMENT_SUFFIX, |
|
|
} from '../lib/constants' |
|
|
import { FileType, fileExists } from '../lib/file-exists' |
|
|
import { findPagesDir } from '../lib/find-pages-dir' |
|
|
import loadCustomRoutes, { |
|
|
normalizeRouteRegex, |
|
|
} from '../lib/load-custom-routes' |
|
|
import type { |
|
|
CustomRoutes, |
|
|
Header, |
|
|
Redirect, |
|
|
Rewrite, |
|
|
RouteHas, |
|
|
} from '../lib/load-custom-routes' |
|
|
import { nonNullable } from '../lib/non-nullable' |
|
|
import { recursiveDelete } from '../lib/recursive-delete' |
|
|
import { verifyPartytownSetup } from '../lib/verify-partytown-setup' |
|
|
import { |
|
|
BUILD_ID_FILE, |
|
|
BUILD_MANIFEST, |
|
|
CLIENT_STATIC_FILES_PATH, |
|
|
EXPORT_DETAIL, |
|
|
EXPORT_MARKER, |
|
|
IMAGES_MANIFEST, |
|
|
PAGES_MANIFEST, |
|
|
PHASE_PRODUCTION_BUILD, |
|
|
PRERENDER_MANIFEST, |
|
|
REACT_LOADABLE_MANIFEST, |
|
|
ROUTES_MANIFEST, |
|
|
SERVER_DIRECTORY, |
|
|
SERVER_FILES_MANIFEST, |
|
|
STATIC_STATUS_PAGES, |
|
|
MIDDLEWARE_MANIFEST, |
|
|
APP_PATHS_MANIFEST, |
|
|
APP_PATH_ROUTES_MANIFEST, |
|
|
APP_BUILD_MANIFEST, |
|
|
RSC_MODULE_TYPES, |
|
|
NEXT_FONT_MANIFEST, |
|
|
SUBRESOURCE_INTEGRITY_MANIFEST, |
|
|
MIDDLEWARE_BUILD_MANIFEST, |
|
|
MIDDLEWARE_REACT_LOADABLE_MANIFEST, |
|
|
SERVER_REFERENCE_MANIFEST, |
|
|
FUNCTIONS_CONFIG_MANIFEST, |
|
|
UNDERSCORE_NOT_FOUND_ROUTE_ENTRY, |
|
|
UNDERSCORE_NOT_FOUND_ROUTE, |
|
|
DYNAMIC_CSS_MANIFEST, |
|
|
TURBOPACK_CLIENT_MIDDLEWARE_MANIFEST, |
|
|
} from '../shared/lib/constants' |
|
|
import { isDynamicRoute } from '../shared/lib/router/utils' |
|
|
import type { __ApiPreviewProps } from '../server/api-utils' |
|
|
import loadConfig from '../server/config' |
|
|
import type { BuildManifest } from '../server/get-page-files' |
|
|
import { normalizePagePath } from '../shared/lib/page-path/normalize-page-path' |
|
|
import { getPagePath } from '../server/require' |
|
|
import * as ciEnvironment from '../server/ci-info' |
|
|
import { |
|
|
turborepoTraceAccess, |
|
|
TurborepoAccessTraceResult, |
|
|
writeTurborepoAccessTraceResult, |
|
|
} from './turborepo-access-trace' |
|
|
|
|
|
import { |
|
|
eventBuildOptimize, |
|
|
eventCliSession, |
|
|
eventBuildFeatureUsage, |
|
|
eventNextPlugins, |
|
|
EVENT_BUILD_FEATURE_USAGE, |
|
|
eventPackageUsedInGetServerSideProps, |
|
|
eventBuildCompleted, |
|
|
eventBuildFailed, |
|
|
} from '../telemetry/events' |
|
|
import type { EventBuildFeatureUsage } from '../telemetry/events' |
|
|
import { Telemetry } from '../telemetry/storage' |
|
|
import { |
|
|
createPagesMapping, |
|
|
getStaticInfoIncludingLayouts, |
|
|
sortByPageExts, |
|
|
} from './entries' |
|
|
import { PAGE_TYPES } from '../lib/page-types' |
|
|
import { generateBuildId } from './generate-build-id' |
|
|
import { isWriteable } from './is-writeable' |
|
|
import * as Log from './output/log' |
|
|
import createSpinner from './spinner' |
|
|
import { trace, flushAllTraces, setGlobal, type Span } from '../trace' |
|
|
import { |
|
|
detectConflictingPaths, |
|
|
computeFromManifest, |
|
|
getJsPageSizeInKb, |
|
|
printCustomRoutes, |
|
|
printTreeView, |
|
|
copyTracedFiles, |
|
|
isReservedPage, |
|
|
isAppBuiltinNotFoundPage, |
|
|
collectRoutesUsingEdgeRuntime, |
|
|
collectMeta, |
|
|
} from './utils' |
|
|
import type { PageInfo, PageInfos } from './utils' |
|
|
import type { PrerenderedRoute } from './static-paths/types' |
|
|
import type { AppSegmentConfig } from './segment-config/app/app-segment-config' |
|
|
import { writeBuildId } from './write-build-id' |
|
|
import { normalizeLocalePath } from '../shared/lib/i18n/normalize-locale-path' |
|
|
import isError from '../lib/is-error' |
|
|
import type { NextError } from '../lib/is-error' |
|
|
import { isEdgeRuntime } from '../lib/is-edge-runtime' |
|
|
import { recursiveCopy } from '../lib/recursive-copy' |
|
|
import { recursiveReadDir } from '../lib/recursive-readdir' |
|
|
import { lockfilePatchPromise, teardownTraceSubscriber } from './swc' |
|
|
import { getNamedRouteRegex } from '../shared/lib/router/utils/route-regex' |
|
|
import { getFilesInDir } from '../lib/get-files-in-dir' |
|
|
import { eventSwcPlugins } from '../telemetry/events/swc-plugins' |
|
|
import { normalizeAppPath } from '../shared/lib/router/utils/app-paths' |
|
|
import { |
|
|
ACTION_HEADER, |
|
|
NEXT_ROUTER_PREFETCH_HEADER, |
|
|
RSC_HEADER, |
|
|
RSC_CONTENT_TYPE_HEADER, |
|
|
NEXT_ROUTER_STATE_TREE_HEADER, |
|
|
NEXT_DID_POSTPONE_HEADER, |
|
|
NEXT_ROUTER_SEGMENT_PREFETCH_HEADER, |
|
|
NEXT_REWRITTEN_PATH_HEADER, |
|
|
NEXT_REWRITTEN_QUERY_HEADER, |
|
|
} from '../client/components/app-router-headers' |
|
|
import { webpackBuild } from './webpack-build' |
|
|
import { NextBuildContext, type MappedPages } from './build-context' |
|
|
import { normalizePathSep } from '../shared/lib/page-path/normalize-path-sep' |
|
|
import { isAppRouteRoute } from '../lib/is-app-route-route' |
|
|
import { createClientRouterFilter } from '../lib/create-client-router-filter' |
|
|
import { createValidFileMatcher } from '../server/lib/find-page-file' |
|
|
import { startTypeChecking } from './type-check' |
|
|
import { generateInterceptionRoutesRewrites } from '../lib/generate-interception-routes-rewrites' |
|
|
|
|
|
import { buildDataRoute } from '../server/lib/router-utils/build-data-route' |
|
|
import { collectBuildTraces } from './collect-build-traces' |
|
|
import type { BuildTraceContext } from './webpack/plugins/next-trace-entrypoints-plugin' |
|
|
import { formatManifest } from './manifests/formatter/format-manifest' |
|
|
import { |
|
|
recordFrameworkVersion, |
|
|
updateBuildDiagnostics, |
|
|
recordFetchMetrics, |
|
|
} from '../diagnostics/build-diagnostics' |
|
|
import { getStartServerInfo, logStartInfo } from '../server/lib/app-info-log' |
|
|
import type { NextEnabledDirectories } from '../server/base-server' |
|
|
import { hasCustomExportOutput } from '../export/utils' |
|
|
import { buildCustomRoute } from '../lib/build-custom-route' |
|
|
import { traceMemoryUsage } from '../lib/memory/trace' |
|
|
import { generateEncryptionKeyBase64 } from '../server/app-render/encryption-utils-server' |
|
|
import type { DeepReadonly } from '../shared/lib/deep-readonly' |
|
|
import uploadTrace from '../trace/upload-trace' |
|
|
import { |
|
|
checkIsAppPPREnabled, |
|
|
checkIsRoutePPREnabled, |
|
|
} from '../server/lib/experimental/ppr' |
|
|
import { FallbackMode, fallbackModeToFallbackField } from '../lib/fallback' |
|
|
import { RenderingMode } from './rendering-mode' |
|
|
import { InvariantError } from '../shared/lib/invariant-error' |
|
|
import { HTML_LIMITED_BOT_UA_RE_STRING } from '../shared/lib/router/utils/is-bot' |
|
|
import type { UseCacheTrackerKey } from './webpack/plugins/telemetry-plugin/use-cache-tracker-utils' |
|
|
import { |
|
|
buildPrefetchSegmentDataRoute, |
|
|
type PrefetchSegmentDataRoute, |
|
|
} from '../server/lib/router-utils/build-prefetch-segment-data-route' |
|
|
|
|
|
import { turbopackBuild } from './turbopack-build' |
|
|
import { isPersistentCachingEnabled } from '../shared/lib/turbopack/utils' |
|
|
import { inlineStaticEnv } from '../lib/inline-static-env' |
|
|
import { populateStaticEnv } from '../lib/static-env' |
|
|
import { durationToString } from './duration-to-string' |
|
|
import { traceGlobals } from '../trace/shared' |
|
|
import { extractNextErrorCode } from '../lib/error-telemetry-utils' |
|
|
import { runAfterProductionCompile } from './after-production-compile' |
|
|
import { generatePreviewKeys } from './preview-key-utils' |
|
|
import { handleBuildComplete } from './adapter/build-complete' |
|
|
import { |
|
|
sortPageObjects, |
|
|
sortPages, |
|
|
sortSortableRouteObjects, |
|
|
} from '../shared/lib/router/utils/sortable-routes' |
|
|
|
|
|
type Fallback = null | boolean | string |
|
|
|
|
|
export interface PrerenderManifestRoute { |
|
|
dataRoute: string | null |
|
|
experimentalBypassFor?: RouteHas[] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
initialHeaders?: Record<string, string> |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
initialStatus?: number |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
initialRevalidateSeconds: Revalidate |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
initialExpireSeconds: number | undefined |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
prefetchDataRoute: string | null | undefined |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
srcRoute: string | null |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
experimentalPPR: boolean | undefined |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
renderingMode: RenderingMode | undefined |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
allowHeader: string[] |
|
|
} |
|
|
|
|
|
export interface DynamicPrerenderManifestRoute { |
|
|
dataRoute: string | null |
|
|
dataRouteRegex: string | null |
|
|
experimentalBypassFor?: RouteHas[] |
|
|
fallback: Fallback |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fallbackRevalidate: Revalidate | undefined |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fallbackExpire: number | undefined |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fallbackHeaders?: Record<string, string> |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fallbackStatus?: number |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fallbackRootParams: readonly string[] | undefined |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fallbackSourceRoute: string | undefined |
|
|
|
|
|
prefetchDataRoute: string | null | undefined |
|
|
prefetchDataRouteRegex: string | null | undefined |
|
|
routeRegex: string |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
experimentalPPR: boolean | undefined |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
renderingMode: RenderingMode | undefined |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
allowHeader: string[] |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const ALLOWED_HEADERS: string[] = [ |
|
|
'host', |
|
|
MATCHED_PATH_HEADER, |
|
|
PRERENDER_REVALIDATE_HEADER, |
|
|
PRERENDER_REVALIDATE_ONLY_GENERATED_HEADER, |
|
|
NEXT_CACHE_REVALIDATED_TAGS_HEADER, |
|
|
NEXT_CACHE_REVALIDATE_TAG_TOKEN_HEADER, |
|
|
] |
|
|
|
|
|
export type PrerenderManifest = { |
|
|
version: 4 |
|
|
routes: { [route: string]: PrerenderManifestRoute } |
|
|
dynamicRoutes: { [route: string]: DynamicPrerenderManifestRoute } |
|
|
notFoundRoutes: string[] |
|
|
preview: __ApiPreviewProps |
|
|
} |
|
|
|
|
|
type ManifestBuiltRoute = { |
|
|
|
|
|
|
|
|
|
|
|
regex: string |
|
|
} |
|
|
|
|
|
export enum RouteType { |
|
|
|
|
|
|
|
|
|
|
|
PAGES = 'PAGES', |
|
|
|
|
|
|
|
|
|
|
|
PAGES_API = 'PAGES_API', |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
APP_PAGE = 'APP_PAGE', |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
APP_ROUTE = 'APP_ROUTE', |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
STATIC_FILE = 'STATIC_FILE', |
|
|
|
|
|
MIDDLEWARE = 'MIDDLEWARE', |
|
|
} |
|
|
|
|
|
export type ManifestRewriteRoute = ManifestBuiltRoute & Rewrite |
|
|
export type ManifestRedirectRoute = ManifestBuiltRoute & Redirect |
|
|
export type ManifestHeaderRoute = ManifestBuiltRoute & Header |
|
|
|
|
|
export type ManifestRoute = ManifestBuiltRoute & { |
|
|
page: string |
|
|
namedRegex?: string |
|
|
routeKeys?: { [key: string]: string } |
|
|
prefetchSegmentDataRoutes?: PrefetchSegmentDataRoute[] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
skipInternalRouting?: boolean |
|
|
} |
|
|
|
|
|
type DynamicManifestRoute = ManifestRoute & { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sourcePage: string | undefined |
|
|
} |
|
|
|
|
|
type ManifestDataRoute = { |
|
|
page: string |
|
|
routeKeys?: { [key: string]: string } |
|
|
dataRouteRegex: string |
|
|
namedDataRouteRegex?: string |
|
|
} |
|
|
|
|
|
export type RoutesManifest = { |
|
|
version: number |
|
|
pages404: boolean |
|
|
basePath: string |
|
|
redirects: Array<ManifestRedirectRoute> |
|
|
rewrites: { |
|
|
beforeFiles: Array<ManifestRewriteRoute> |
|
|
afterFiles: Array<ManifestRewriteRoute> |
|
|
fallback: Array<ManifestRewriteRoute> |
|
|
} |
|
|
headers: Array<ManifestHeaderRoute> |
|
|
staticRoutes: Array<ManifestRoute> |
|
|
dynamicRoutes: ReadonlyArray<DynamicManifestRoute> |
|
|
dataRoutes: Array<ManifestDataRoute> |
|
|
i18n?: { |
|
|
domains?: ReadonlyArray<{ |
|
|
http?: true |
|
|
domain: string |
|
|
locales?: readonly string[] |
|
|
defaultLocale: string |
|
|
}> |
|
|
locales: readonly string[] |
|
|
defaultLocale: string |
|
|
localeDetection?: false |
|
|
} |
|
|
rsc: { |
|
|
header: typeof RSC_HEADER |
|
|
didPostponeHeader: typeof NEXT_DID_POSTPONE_HEADER |
|
|
contentTypeHeader: typeof RSC_CONTENT_TYPE_HEADER |
|
|
varyHeader: string |
|
|
prefetchHeader: typeof NEXT_ROUTER_PREFETCH_HEADER |
|
|
suffix: typeof RSC_SUFFIX |
|
|
prefetchSuffix: typeof RSC_PREFETCH_SUFFIX |
|
|
prefetchSegmentHeader: typeof NEXT_ROUTER_SEGMENT_PREFETCH_HEADER |
|
|
prefetchSegmentDirSuffix: typeof RSC_SEGMENTS_DIR_SUFFIX |
|
|
prefetchSegmentSuffix: typeof RSC_SEGMENT_SUFFIX |
|
|
} |
|
|
rewriteHeaders: { |
|
|
pathHeader: typeof NEXT_REWRITTEN_PATH_HEADER |
|
|
queryHeader: typeof NEXT_REWRITTEN_QUERY_HEADER |
|
|
} |
|
|
skipMiddlewareUrlNormalize?: boolean |
|
|
caseSensitive?: boolean |
|
|
|
|
|
|
|
|
|
|
|
ppr?: { |
|
|
|
|
|
|
|
|
|
|
|
chain: { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
headers: Record<string, string> |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function pageToRoute(page: string): ManifestRoute |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function pageToRoute( |
|
|
page: string, |
|
|
sourcePage: string | undefined |
|
|
): DynamicManifestRoute |
|
|
function pageToRoute( |
|
|
page: string, |
|
|
sourcePage?: string |
|
|
): DynamicManifestRoute | ManifestRoute { |
|
|
const routeRegex = getNamedRouteRegex(page, { |
|
|
prefixRouteKeys: true, |
|
|
}) |
|
|
return { |
|
|
sourcePage, |
|
|
page, |
|
|
regex: normalizeRouteRegex(routeRegex.re.source), |
|
|
routeKeys: routeRegex.routeKeys, |
|
|
namedRegex: routeRegex.namedRegex, |
|
|
} |
|
|
} |
|
|
|
|
|
function getCacheDir(distDir: string): string { |
|
|
const cacheDir = path.join(distDir, 'cache') |
|
|
if (ciEnvironment.isCI && !ciEnvironment.hasNextSupport) { |
|
|
const hasCache = existsSync(cacheDir) |
|
|
|
|
|
if (!hasCache) { |
|
|
|
|
|
|
|
|
console.log( |
|
|
`${Log.prefixes.warn} No build cache found. Please configure build caching for faster rebuilds. Read more: https://nextjs.org/docs/messages/no-cache` |
|
|
) |
|
|
} |
|
|
} |
|
|
return cacheDir |
|
|
} |
|
|
|
|
|
async function writeFileUtf8(filePath: string, content: string): Promise<void> { |
|
|
await fs.writeFile(filePath, content, 'utf-8') |
|
|
} |
|
|
|
|
|
function readFileUtf8(filePath: string): Promise<string> { |
|
|
return fs.readFile(filePath, 'utf8') |
|
|
} |
|
|
|
|
|
async function writeManifest<T extends object>( |
|
|
filePath: string, |
|
|
manifest: T |
|
|
): Promise<void> { |
|
|
await writeFileUtf8(filePath, formatManifest(manifest)) |
|
|
} |
|
|
|
|
|
async function readManifest<T extends object>(filePath: string): Promise<T> { |
|
|
return JSON.parse(await readFileUtf8(filePath)) |
|
|
} |
|
|
|
|
|
async function writePrerenderManifest( |
|
|
distDir: string, |
|
|
manifest: DeepReadonly<PrerenderManifest> |
|
|
): Promise<void> { |
|
|
await writeManifest(path.join(distDir, PRERENDER_MANIFEST), manifest) |
|
|
} |
|
|
|
|
|
async function writeClientSsgManifest( |
|
|
prerenderManifest: DeepReadonly<PrerenderManifest>, |
|
|
{ |
|
|
buildId, |
|
|
distDir, |
|
|
locales, |
|
|
}: { |
|
|
buildId: string |
|
|
distDir: string |
|
|
locales: readonly string[] | undefined |
|
|
} |
|
|
) { |
|
|
const ssgPages = new Set<string>( |
|
|
[ |
|
|
...Object.entries(prerenderManifest.routes) |
|
|
|
|
|
.filter(([, { srcRoute }]) => srcRoute == null) |
|
|
.map(([route]) => normalizeLocalePath(route, locales).pathname), |
|
|
...Object.keys(prerenderManifest.dynamicRoutes), |
|
|
].sort() |
|
|
) |
|
|
|
|
|
const clientSsgManifestContent = `self.__SSG_MANIFEST=${devalue( |
|
|
ssgPages |
|
|
)};self.__SSG_MANIFEST_CB&&self.__SSG_MANIFEST_CB()` |
|
|
|
|
|
await writeFileUtf8( |
|
|
path.join(distDir, CLIENT_STATIC_FILES_PATH, buildId, '_ssgManifest.js'), |
|
|
clientSsgManifestContent |
|
|
) |
|
|
} |
|
|
|
|
|
export interface FunctionsConfigManifest { |
|
|
version: number |
|
|
functions: Record< |
|
|
string, |
|
|
{ |
|
|
maxDuration?: number | undefined |
|
|
runtime?: 'nodejs' |
|
|
regions?: string[] | string |
|
|
matchers?: Array<{ |
|
|
regexp: string |
|
|
originalSource: string |
|
|
has?: Rewrite['has'] |
|
|
missing?: Rewrite['has'] |
|
|
}> |
|
|
} |
|
|
> |
|
|
} |
|
|
|
|
|
async function writeFunctionsConfigManifest( |
|
|
distDir: string, |
|
|
manifest: FunctionsConfigManifest |
|
|
): Promise<void> { |
|
|
await writeManifest( |
|
|
path.join(distDir, SERVER_DIRECTORY, FUNCTIONS_CONFIG_MANIFEST), |
|
|
manifest |
|
|
) |
|
|
} |
|
|
|
|
|
export interface RequiredServerFilesManifest { |
|
|
version: number |
|
|
config: NextConfigComplete |
|
|
appDir: string |
|
|
relativeAppDir: string |
|
|
files: string[] |
|
|
ignore: string[] |
|
|
} |
|
|
|
|
|
async function writeRequiredServerFilesManifest( |
|
|
distDir: string, |
|
|
requiredServerFiles: RequiredServerFilesManifest |
|
|
) { |
|
|
await writeManifest( |
|
|
path.join(distDir, SERVER_FILES_MANIFEST), |
|
|
requiredServerFiles |
|
|
) |
|
|
} |
|
|
|
|
|
async function writeImagesManifest( |
|
|
distDir: string, |
|
|
config: NextConfigComplete |
|
|
): Promise<void> { |
|
|
const images = { ...config.images } |
|
|
const { deviceSizes, imageSizes } = images |
|
|
;(images as any).sizes = [...deviceSizes, ...imageSizes] |
|
|
|
|
|
|
|
|
images.remotePatterns = (config?.images?.remotePatterns || []).map((p) => ({ |
|
|
|
|
|
protocol: p.protocol?.replace(/:$/, '') as 'http' | 'https' | undefined, |
|
|
hostname: makeRe(p.hostname).source, |
|
|
port: p.port, |
|
|
pathname: makeRe(p.pathname ?? '**', { dot: true }).source, |
|
|
search: p.search, |
|
|
})) |
|
|
|
|
|
|
|
|
if (config?.images?.localPatterns) { |
|
|
images.localPatterns = config.images.localPatterns.map((p) => ({ |
|
|
|
|
|
pathname: makeRe(p.pathname ?? '**', { dot: true }).source, |
|
|
search: p.search, |
|
|
})) |
|
|
} |
|
|
|
|
|
await writeManifest(path.join(distDir, IMAGES_MANIFEST), { |
|
|
version: 1, |
|
|
images, |
|
|
}) |
|
|
} |
|
|
|
|
|
const STANDALONE_DIRECTORY = 'standalone' as const |
|
|
async function writeStandaloneDirectory( |
|
|
nextBuildSpan: Span, |
|
|
distDir: string, |
|
|
pageKeys: { pages: string[]; app: string[] | undefined }, |
|
|
denormalizedAppPages: string[] | undefined, |
|
|
outputFileTracingRoot: string, |
|
|
requiredServerFiles: RequiredServerFilesManifest, |
|
|
middlewareManifest: MiddlewareManifest, |
|
|
hasNodeMiddleware: boolean, |
|
|
hasInstrumentationHook: boolean, |
|
|
staticPages: Set<string>, |
|
|
loadedEnvFiles: LoadedEnvFiles, |
|
|
appDir: string | undefined |
|
|
) { |
|
|
await nextBuildSpan |
|
|
.traceChild('write-standalone-directory') |
|
|
.traceAsyncFn(async () => { |
|
|
await copyTracedFiles( |
|
|
|
|
|
requiredServerFiles.appDir, |
|
|
distDir, |
|
|
pageKeys.pages, |
|
|
denormalizedAppPages, |
|
|
outputFileTracingRoot, |
|
|
requiredServerFiles.config, |
|
|
middlewareManifest, |
|
|
hasNodeMiddleware, |
|
|
hasInstrumentationHook, |
|
|
staticPages |
|
|
) |
|
|
|
|
|
for (const file of [ |
|
|
...requiredServerFiles.files, |
|
|
path.join(requiredServerFiles.config.distDir, SERVER_FILES_MANIFEST), |
|
|
...loadedEnvFiles.reduce<string[]>((acc, envFile) => { |
|
|
if (['.env', '.env.production'].includes(envFile.path)) { |
|
|
acc.push(envFile.path) |
|
|
} |
|
|
return acc |
|
|
}, []), |
|
|
]) { |
|
|
|
|
|
const filePath = path.join(requiredServerFiles.appDir, file) |
|
|
const outputPath = path.join( |
|
|
distDir, |
|
|
STANDALONE_DIRECTORY, |
|
|
path.relative(outputFileTracingRoot, filePath) |
|
|
) |
|
|
await fs.mkdir(path.dirname(outputPath), { |
|
|
recursive: true, |
|
|
}) |
|
|
await fs.copyFile(filePath, outputPath) |
|
|
} |
|
|
|
|
|
if (hasNodeMiddleware) { |
|
|
const middlewareOutput = path.join( |
|
|
distDir, |
|
|
STANDALONE_DIRECTORY, |
|
|
path.relative(outputFileTracingRoot, distDir), |
|
|
SERVER_DIRECTORY, |
|
|
'middleware.js' |
|
|
) |
|
|
|
|
|
await fs.mkdir(path.dirname(middlewareOutput), { recursive: true }) |
|
|
await fs.copyFile( |
|
|
path.join(distDir, SERVER_DIRECTORY, 'middleware.js'), |
|
|
middlewareOutput |
|
|
) |
|
|
} |
|
|
|
|
|
await recursiveCopy( |
|
|
path.join(distDir, SERVER_DIRECTORY, 'pages'), |
|
|
path.join( |
|
|
distDir, |
|
|
STANDALONE_DIRECTORY, |
|
|
path.relative(outputFileTracingRoot, distDir), |
|
|
SERVER_DIRECTORY, |
|
|
'pages' |
|
|
), |
|
|
{ overwrite: true } |
|
|
) |
|
|
if (appDir) { |
|
|
const originalServerApp = path.join(distDir, SERVER_DIRECTORY, 'app') |
|
|
if (existsSync(originalServerApp)) { |
|
|
await recursiveCopy( |
|
|
originalServerApp, |
|
|
path.join( |
|
|
distDir, |
|
|
STANDALONE_DIRECTORY, |
|
|
path.relative(outputFileTracingRoot, distDir), |
|
|
SERVER_DIRECTORY, |
|
|
'app' |
|
|
), |
|
|
{ overwrite: true } |
|
|
) |
|
|
} |
|
|
} |
|
|
}) |
|
|
} |
|
|
|
|
|
function getNumberOfWorkers(config: NextConfigComplete) { |
|
|
if ( |
|
|
config.experimental.cpus && |
|
|
config.experimental.cpus !== defaultConfig.experimental!.cpus |
|
|
) { |
|
|
return config.experimental.cpus |
|
|
} |
|
|
|
|
|
if (config.experimental.memoryBasedWorkersCount) { |
|
|
return Math.max( |
|
|
Math.min(config.experimental.cpus || 1, Math.floor(os.freemem() / 1e9)), |
|
|
|
|
|
4 |
|
|
) |
|
|
} |
|
|
|
|
|
if (config.experimental.cpus) { |
|
|
return config.experimental.cpus |
|
|
} |
|
|
|
|
|
|
|
|
return 4 |
|
|
} |
|
|
|
|
|
const staticWorkerPath = require.resolve('./worker') |
|
|
const staticWorkerExposedMethods = [ |
|
|
'hasCustomGetInitialProps', |
|
|
'isPageStatic', |
|
|
'getDefinedNamedExports', |
|
|
'exportPages', |
|
|
] as const |
|
|
export type StaticWorker = typeof import('./worker') & Worker |
|
|
export function createStaticWorker( |
|
|
config: NextConfigComplete, |
|
|
options: { |
|
|
debuggerPortOffset: number |
|
|
progress?: { |
|
|
run: () => void |
|
|
clear: () => void |
|
|
} |
|
|
} |
|
|
): StaticWorker { |
|
|
const { debuggerPortOffset, progress } = options |
|
|
return new Worker(staticWorkerPath, { |
|
|
logger: Log, |
|
|
numWorkers: getNumberOfWorkers(config), |
|
|
onActivity: () => { |
|
|
progress?.run() |
|
|
}, |
|
|
onActivityAbort: () => { |
|
|
progress?.clear() |
|
|
}, |
|
|
debuggerPortOffset, |
|
|
enableSourceMaps: config.experimental.enablePrerenderSourceMaps, |
|
|
|
|
|
isolatedMemory: true, |
|
|
enableWorkerThreads: config.experimental.workerThreads, |
|
|
exposedMethods: staticWorkerExposedMethods, |
|
|
}) as StaticWorker |
|
|
} |
|
|
|
|
|
async function writeFullyStaticExport( |
|
|
config: NextConfigComplete, |
|
|
dir: string, |
|
|
enabledDirectories: NextEnabledDirectories, |
|
|
configOutDir: string, |
|
|
nextBuildSpan: Span |
|
|
): Promise<void> { |
|
|
const exportApp = (require('../export') as typeof import('../export')) |
|
|
.default as typeof import('../export').default |
|
|
|
|
|
await exportApp( |
|
|
dir, |
|
|
{ |
|
|
buildExport: false, |
|
|
nextConfig: config, |
|
|
enabledDirectories, |
|
|
silent: true, |
|
|
outdir: path.join(dir, configOutDir), |
|
|
numWorkers: getNumberOfWorkers(config), |
|
|
}, |
|
|
nextBuildSpan |
|
|
) |
|
|
} |
|
|
|
|
|
async function getBuildId( |
|
|
isGenerateMode: boolean, |
|
|
distDir: string, |
|
|
nextBuildSpan: Span, |
|
|
config: NextConfigComplete |
|
|
) { |
|
|
if (isGenerateMode) { |
|
|
return await fs.readFile(path.join(distDir, 'BUILD_ID'), 'utf8') |
|
|
} |
|
|
return await nextBuildSpan |
|
|
.traceChild('generate-buildid') |
|
|
.traceAsyncFn(() => generateBuildId(config.generateBuildId, nanoid)) |
|
|
} |
|
|
|
|
|
export default async function build( |
|
|
dir: string, |
|
|
reactProductionProfiling = false, |
|
|
debugOutput = false, |
|
|
debugPrerender = false, |
|
|
runLint = true, |
|
|
noMangling = false, |
|
|
appDirOnly = false, |
|
|
isTurbopack = false, |
|
|
experimentalBuildMode: 'default' | 'compile' | 'generate' | 'generate-env', |
|
|
traceUploadUrl: string | undefined |
|
|
): Promise<void> { |
|
|
const isCompileMode = experimentalBuildMode === 'compile' |
|
|
const isGenerateMode = experimentalBuildMode === 'generate' |
|
|
NextBuildContext.isCompileMode = isCompileMode |
|
|
const buildStartTime = Date.now() |
|
|
|
|
|
let loadedConfig: NextConfigComplete | undefined |
|
|
try { |
|
|
const nextBuildSpan = trace('next-build', undefined, { |
|
|
buildMode: experimentalBuildMode, |
|
|
isTurboBuild: String(isTurbopack), |
|
|
version: process.env.__NEXT_VERSION as string, |
|
|
}) |
|
|
|
|
|
NextBuildContext.nextBuildSpan = nextBuildSpan |
|
|
NextBuildContext.dir = dir |
|
|
NextBuildContext.appDirOnly = appDirOnly |
|
|
NextBuildContext.reactProductionProfiling = reactProductionProfiling |
|
|
NextBuildContext.noMangling = noMangling |
|
|
NextBuildContext.debugPrerender = debugPrerender |
|
|
|
|
|
await nextBuildSpan.traceAsyncFn(async () => { |
|
|
|
|
|
const { loadedEnvFiles } = nextBuildSpan |
|
|
.traceChild('load-dotenv') |
|
|
.traceFn(() => loadEnvConfig(dir, false, Log)) |
|
|
NextBuildContext.loadedEnvFiles = loadedEnvFiles |
|
|
|
|
|
const turborepoAccessTraceResult = new TurborepoAccessTraceResult() |
|
|
const config: NextConfigComplete = await nextBuildSpan |
|
|
.traceChild('load-next-config') |
|
|
.traceAsyncFn(() => |
|
|
turborepoTraceAccess( |
|
|
() => |
|
|
loadConfig(PHASE_PRODUCTION_BUILD, dir, { |
|
|
|
|
|
silent: false, |
|
|
reactProductionProfiling, |
|
|
debugPrerender, |
|
|
}), |
|
|
turborepoAccessTraceResult |
|
|
) |
|
|
) |
|
|
loadedConfig = config |
|
|
|
|
|
process.env.NEXT_DEPLOYMENT_ID = config.deploymentId || '' |
|
|
NextBuildContext.config = config |
|
|
|
|
|
let configOutDir = 'out' |
|
|
if (hasCustomExportOutput(config)) { |
|
|
configOutDir = config.distDir |
|
|
config.distDir = '.next' |
|
|
} |
|
|
const distDir = path.join(dir, config.distDir) |
|
|
NextBuildContext.distDir = distDir |
|
|
setGlobal('phase', PHASE_PRODUCTION_BUILD) |
|
|
setGlobal('distDir', distDir) |
|
|
|
|
|
const buildId = await getBuildId( |
|
|
isGenerateMode, |
|
|
distDir, |
|
|
nextBuildSpan, |
|
|
config |
|
|
) |
|
|
NextBuildContext.buildId = buildId |
|
|
|
|
|
if (experimentalBuildMode === 'generate-env') { |
|
|
if (isTurbopack) { |
|
|
Log.warn('generate-env is not needed with turbopack') |
|
|
process.exit(0) |
|
|
} |
|
|
Log.info('Inlining static env ...') |
|
|
await nextBuildSpan |
|
|
.traceChild('inline-static-env') |
|
|
.traceAsyncFn(async () => { |
|
|
await inlineStaticEnv({ |
|
|
distDir, |
|
|
config, |
|
|
}) |
|
|
}) |
|
|
|
|
|
Log.info('Complete') |
|
|
await flushAllTraces() |
|
|
teardownTraceSubscriber() |
|
|
process.exit(0) |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (isCompileMode || isGenerateMode) { |
|
|
populateStaticEnv(config) |
|
|
} |
|
|
|
|
|
const customRoutes: CustomRoutes = await nextBuildSpan |
|
|
.traceChild('load-custom-routes') |
|
|
.traceAsyncFn(() => loadCustomRoutes(config)) |
|
|
|
|
|
const { headers, rewrites, redirects } = customRoutes |
|
|
const combinedRewrites: Rewrite[] = [ |
|
|
...rewrites.beforeFiles, |
|
|
...rewrites.afterFiles, |
|
|
...rewrites.fallback, |
|
|
] |
|
|
const hasRewrites = combinedRewrites.length > 0 |
|
|
NextBuildContext.hasRewrites = hasRewrites |
|
|
NextBuildContext.originalRewrites = config._originalRewrites |
|
|
NextBuildContext.originalRedirects = config._originalRedirects |
|
|
|
|
|
const cacheDir = getCacheDir(distDir) |
|
|
|
|
|
const telemetry = new Telemetry({ distDir }) |
|
|
|
|
|
setGlobal('telemetry', telemetry) |
|
|
|
|
|
const publicDir = path.join(dir, 'public') |
|
|
const { pagesDir, appDir } = findPagesDir(dir) |
|
|
NextBuildContext.pagesDir = pagesDir |
|
|
NextBuildContext.appDir = appDir |
|
|
|
|
|
const enabledDirectories: NextEnabledDirectories = { |
|
|
app: typeof appDir === 'string', |
|
|
pages: typeof pagesDir === 'string', |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
const encryptionKey = await generateEncryptionKeyBase64({ |
|
|
isBuild: true, |
|
|
distDir, |
|
|
}) |
|
|
NextBuildContext.encryptionKey = encryptionKey |
|
|
|
|
|
const isSrcDir = path |
|
|
.relative(dir, pagesDir || appDir || '') |
|
|
.startsWith('src') |
|
|
const hasPublicDir = existsSync(publicDir) |
|
|
|
|
|
telemetry.record( |
|
|
eventCliSession(dir, config, { |
|
|
webpackVersion: 5, |
|
|
cliCommand: 'build', |
|
|
isSrcDir, |
|
|
hasNowJson: !!(await findUp('now.json', { cwd: dir })), |
|
|
isCustomServer: null, |
|
|
turboFlag: false, |
|
|
pagesDir: !!pagesDir, |
|
|
appDir: !!appDir, |
|
|
}) |
|
|
) |
|
|
|
|
|
eventNextPlugins(path.resolve(dir)).then((events) => |
|
|
telemetry.record(events) |
|
|
) |
|
|
|
|
|
eventSwcPlugins(path.resolve(dir), config).then((events) => |
|
|
telemetry.record(events) |
|
|
) |
|
|
|
|
|
|
|
|
const { envInfo, experimentalFeatures } = await getStartServerInfo({ |
|
|
dir, |
|
|
dev: false, |
|
|
debugPrerender, |
|
|
}) |
|
|
|
|
|
logStartInfo({ |
|
|
networkUrl: null, |
|
|
appUrl: null, |
|
|
envInfo, |
|
|
experimentalFeatures, |
|
|
}) |
|
|
|
|
|
const ignoreESLint = Boolean(config.eslint.ignoreDuringBuilds) |
|
|
const shouldLint = !ignoreESLint && runLint |
|
|
|
|
|
const typeCheckingOptions: Parameters<typeof startTypeChecking>[0] = { |
|
|
dir, |
|
|
appDir, |
|
|
pagesDir, |
|
|
runLint, |
|
|
shouldLint, |
|
|
ignoreESLint, |
|
|
telemetry, |
|
|
nextBuildSpan, |
|
|
config, |
|
|
cacheDir, |
|
|
} |
|
|
|
|
|
const distDirCreated = await nextBuildSpan |
|
|
.traceChild('create-dist-dir') |
|
|
.traceAsyncFn(async () => { |
|
|
try { |
|
|
await fs.mkdir(distDir, { recursive: true }) |
|
|
return true |
|
|
} catch (err) { |
|
|
if (isError(err) && err.code === 'EPERM') { |
|
|
return false |
|
|
} |
|
|
throw err |
|
|
} |
|
|
}) |
|
|
|
|
|
if (!distDirCreated || !(await isWriteable(distDir))) { |
|
|
throw new Error( |
|
|
'> Build directory is not writeable. https://nextjs.org/docs/messages/build-dir-not-writeable' |
|
|
) |
|
|
} |
|
|
|
|
|
if (config.cleanDistDir && !isGenerateMode) { |
|
|
await recursiveDelete(distDir, /^cache/) |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!appDir && !isCompileMode) |
|
|
await startTypeChecking(typeCheckingOptions) |
|
|
|
|
|
if (appDir && 'exportPathMap' in config) { |
|
|
Log.error( |
|
|
'The "exportPathMap" configuration cannot be used with the "app" directory. Please use generateStaticParams() instead.' |
|
|
) |
|
|
await telemetry.flush() |
|
|
process.exit(1) |
|
|
} |
|
|
|
|
|
const buildLintEvent: EventBuildFeatureUsage = { |
|
|
featureName: 'build-lint', |
|
|
invocationCount: shouldLint ? 1 : 0, |
|
|
} |
|
|
telemetry.record({ |
|
|
eventName: EVENT_BUILD_FEATURE_USAGE, |
|
|
payload: buildLintEvent, |
|
|
}) |
|
|
|
|
|
const validFileMatcher = createValidFileMatcher( |
|
|
config.pageExtensions, |
|
|
appDir |
|
|
) |
|
|
|
|
|
const providedPagePaths: string[] = JSON.parse( |
|
|
process.env.NEXT_PRIVATE_PAGE_PATHS || '[]' |
|
|
) |
|
|
|
|
|
let pagesPaths = Boolean(process.env.NEXT_PRIVATE_PAGE_PATHS) |
|
|
? providedPagePaths |
|
|
: !appDirOnly && pagesDir |
|
|
? await nextBuildSpan.traceChild('collect-pages').traceAsyncFn(() => |
|
|
recursiveReadDir(pagesDir, { |
|
|
pathnameFilter: validFileMatcher.isPageFile, |
|
|
}) |
|
|
) |
|
|
: [] |
|
|
|
|
|
const middlewareDetectionRegExp = new RegExp( |
|
|
`^${MIDDLEWARE_FILENAME}\\.(?:${config.pageExtensions.join('|')})$` |
|
|
) |
|
|
|
|
|
const instrumentationHookDetectionRegExp = new RegExp( |
|
|
`^${INSTRUMENTATION_HOOK_FILENAME}\\.(?:${config.pageExtensions.join( |
|
|
'|' |
|
|
)})$` |
|
|
) |
|
|
|
|
|
const rootDir = path.join((pagesDir || appDir)!, '..') |
|
|
const includes = [ |
|
|
middlewareDetectionRegExp, |
|
|
instrumentationHookDetectionRegExp, |
|
|
] |
|
|
|
|
|
const rootPaths = Array.from(await getFilesInDir(rootDir)) |
|
|
.filter((file) => includes.some((include) => include.test(file))) |
|
|
.sort(sortByPageExts(config.pageExtensions)) |
|
|
.map((file) => path.join(rootDir, file).replace(dir, '')) |
|
|
|
|
|
const hasInstrumentationHook = rootPaths.some((p) => |
|
|
p.includes(INSTRUMENTATION_HOOK_FILENAME) |
|
|
) |
|
|
const hasMiddlewareFile = rootPaths.some((p) => |
|
|
p.includes(MIDDLEWARE_FILENAME) |
|
|
) |
|
|
|
|
|
NextBuildContext.hasInstrumentationHook = hasInstrumentationHook |
|
|
|
|
|
const previewProps: __ApiPreviewProps = await generatePreviewKeys({ |
|
|
isBuild: true, |
|
|
distDir, |
|
|
}) |
|
|
NextBuildContext.previewProps = previewProps |
|
|
|
|
|
const mappedPages = await nextBuildSpan |
|
|
.traceChild('create-pages-mapping') |
|
|
.traceAsyncFn(() => |
|
|
createPagesMapping({ |
|
|
isDev: false, |
|
|
pageExtensions: config.pageExtensions, |
|
|
pagesType: PAGE_TYPES.PAGES, |
|
|
pagePaths: pagesPaths, |
|
|
pagesDir, |
|
|
appDir, |
|
|
}) |
|
|
) |
|
|
NextBuildContext.mappedPages = mappedPages |
|
|
|
|
|
let mappedAppPages: MappedPages | undefined |
|
|
let denormalizedAppPages: string[] | undefined |
|
|
|
|
|
if (appDir) { |
|
|
const providedAppPaths: string[] = JSON.parse( |
|
|
process.env.NEXT_PRIVATE_APP_PATHS || '[]' |
|
|
) |
|
|
|
|
|
let appPaths = Boolean(process.env.NEXT_PRIVATE_APP_PATHS) |
|
|
? providedAppPaths |
|
|
: await nextBuildSpan |
|
|
.traceChild('collect-app-paths') |
|
|
.traceAsyncFn(() => |
|
|
recursiveReadDir(appDir, { |
|
|
pathnameFilter: (absolutePath) => |
|
|
validFileMatcher.isAppRouterPage(absolutePath) || |
|
|
|
|
|
|
|
|
validFileMatcher.isRootNotFound(absolutePath), |
|
|
ignorePartFilter: (part) => part.startsWith('_'), |
|
|
}) |
|
|
) |
|
|
|
|
|
mappedAppPages = await nextBuildSpan |
|
|
.traceChild('create-app-mapping') |
|
|
.traceAsyncFn(() => |
|
|
createPagesMapping({ |
|
|
pagePaths: appPaths, |
|
|
isDev: false, |
|
|
pagesType: PAGE_TYPES.APP, |
|
|
pageExtensions: config.pageExtensions, |
|
|
pagesDir, |
|
|
appDir, |
|
|
}) |
|
|
) |
|
|
|
|
|
NextBuildContext.mappedAppPages = mappedAppPages |
|
|
} |
|
|
|
|
|
const mappedRootPaths = await createPagesMapping({ |
|
|
isDev: false, |
|
|
pageExtensions: config.pageExtensions, |
|
|
pagePaths: rootPaths, |
|
|
pagesType: PAGE_TYPES.ROOT, |
|
|
pagesDir: pagesDir, |
|
|
appDir, |
|
|
}) |
|
|
NextBuildContext.mappedRootPaths = mappedRootPaths |
|
|
|
|
|
const pagesPageKeys = Object.keys(mappedPages) |
|
|
|
|
|
const conflictingAppPagePaths: [pagePath: string, appPath: string][] = [] |
|
|
const appPageKeys = new Set<string>() |
|
|
if (mappedAppPages) { |
|
|
denormalizedAppPages = Object.keys(mappedAppPages) |
|
|
for (const appKey of denormalizedAppPages) { |
|
|
const normalizedAppPageKey = normalizeAppPath(appKey) |
|
|
const pagePath = mappedPages[normalizedAppPageKey] |
|
|
if (pagePath) { |
|
|
const appPath = mappedAppPages[appKey] |
|
|
conflictingAppPagePaths.push([ |
|
|
pagePath.replace(/^private-next-pages/, 'pages'), |
|
|
appPath.replace(/^private-next-app-dir/, 'app'), |
|
|
]) |
|
|
} |
|
|
appPageKeys.add(normalizedAppPageKey) |
|
|
} |
|
|
} |
|
|
|
|
|
const appPaths = Array.from(appPageKeys) |
|
|
|
|
|
rewrites.beforeFiles.push( |
|
|
...generateInterceptionRoutesRewrites(appPaths, config.basePath) |
|
|
) |
|
|
|
|
|
NextBuildContext.rewrites = rewrites |
|
|
|
|
|
const totalAppPagesCount = appPaths.length |
|
|
|
|
|
const pageKeys = { |
|
|
pages: pagesPageKeys, |
|
|
app: appPaths.length > 0 ? appPaths : undefined, |
|
|
} |
|
|
|
|
|
|
|
|
if (!isTurbopack) { |
|
|
const numConflictingAppPaths = conflictingAppPagePaths.length |
|
|
if (mappedAppPages && numConflictingAppPaths > 0) { |
|
|
Log.error( |
|
|
`Conflicting app and page file${ |
|
|
numConflictingAppPaths === 1 ? ' was' : 's were' |
|
|
} found, please remove the conflicting files to continue:` |
|
|
) |
|
|
for (const [pagePath, appPath] of conflictingAppPagePaths) { |
|
|
Log.error(` "${pagePath}" - "${appPath}"`) |
|
|
} |
|
|
await telemetry.flush() |
|
|
process.exit(1) |
|
|
} |
|
|
} |
|
|
|
|
|
const conflictingPublicFiles: string[] = [] |
|
|
const hasPages404 = mappedPages['/404']?.startsWith(PAGES_DIR_ALIAS) |
|
|
const hasApp404 = !!mappedAppPages?.[UNDERSCORE_NOT_FOUND_ROUTE_ENTRY] |
|
|
const hasCustomErrorPage = |
|
|
mappedPages['/_error'].startsWith(PAGES_DIR_ALIAS) |
|
|
|
|
|
if (hasPublicDir) { |
|
|
const hasPublicUnderScoreNextDir = existsSync( |
|
|
path.join(publicDir, '_next') |
|
|
) |
|
|
if (hasPublicUnderScoreNextDir) { |
|
|
throw new Error(PUBLIC_DIR_MIDDLEWARE_CONFLICT) |
|
|
} |
|
|
} |
|
|
|
|
|
await nextBuildSpan |
|
|
.traceChild('public-dir-conflict-check') |
|
|
.traceAsyncFn(async () => { |
|
|
|
|
|
|
|
|
for (const page in mappedPages) { |
|
|
const hasPublicPageFile = await fileExists( |
|
|
path.join(publicDir, page === '/' ? '/index' : page), |
|
|
FileType.File |
|
|
) |
|
|
if (hasPublicPageFile) { |
|
|
conflictingPublicFiles.push(page) |
|
|
} |
|
|
} |
|
|
|
|
|
const numConflicting = conflictingPublicFiles.length |
|
|
|
|
|
if (numConflicting) { |
|
|
throw new Error( |
|
|
`Conflicting public and page file${ |
|
|
numConflicting === 1 ? ' was' : 's were' |
|
|
} found. https://nextjs.org/docs/messages/conflicting-public-file-page\n${conflictingPublicFiles.join( |
|
|
'\n' |
|
|
)}` |
|
|
) |
|
|
} |
|
|
}) |
|
|
|
|
|
const nestedReservedPages = pageKeys.pages.filter((page) => { |
|
|
return ( |
|
|
page.match(/\/(_app|_document|_error)$/) && path.dirname(page) !== '/' |
|
|
) |
|
|
}) |
|
|
|
|
|
if (nestedReservedPages.length) { |
|
|
Log.warn( |
|
|
`The following reserved Next.js pages were detected not directly under the pages directory:\n` + |
|
|
nestedReservedPages.join('\n') + |
|
|
`\nSee more info here: https://nextjs.org/docs/messages/nested-reserved-page\n` |
|
|
) |
|
|
} |
|
|
|
|
|
const restrictedRedirectPaths = ['/_next'].map((p) => |
|
|
config.basePath ? `${config.basePath}${p}` : p |
|
|
) |
|
|
|
|
|
const isAppCacheComponentsEnabled = Boolean( |
|
|
config.experimental.cacheComponents |
|
|
) |
|
|
const isAuthInterruptsEnabled = Boolean( |
|
|
config.experimental.authInterrupts |
|
|
) |
|
|
const isAppPPREnabled = checkIsAppPPREnabled(config.experimental.ppr) |
|
|
|
|
|
const routesManifestPath = path.join(distDir, ROUTES_MANIFEST) |
|
|
const dynamicRoutes: Array<DynamicManifestRoute> = [] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const sourcePages = new Map<string, string>() |
|
|
const routesManifest: RoutesManifest = nextBuildSpan |
|
|
.traceChild('generate-routes-manifest') |
|
|
.traceFn(() => { |
|
|
const sortedRoutes = sortPages([ |
|
|
...pageKeys.pages, |
|
|
...(pageKeys.app ?? []), |
|
|
]) |
|
|
const staticRoutes: Array<ManifestRoute> = [] |
|
|
|
|
|
for (const route of sortedRoutes) { |
|
|
if (isDynamicRoute(route)) { |
|
|
dynamicRoutes.push( |
|
|
pageToRoute( |
|
|
route, |
|
|
|
|
|
undefined |
|
|
) |
|
|
) |
|
|
} else if (!isReservedPage(route)) { |
|
|
staticRoutes.push(pageToRoute(route)) |
|
|
} |
|
|
} |
|
|
|
|
|
return { |
|
|
version: 3, |
|
|
pages404: true, |
|
|
caseSensitive: !!config.experimental.caseSensitiveRoutes, |
|
|
basePath: config.basePath, |
|
|
redirects: redirects.map((r) => |
|
|
buildCustomRoute('redirect', r, restrictedRedirectPaths) |
|
|
), |
|
|
headers: headers.map((r) => buildCustomRoute('header', r)), |
|
|
rewrites: { |
|
|
beforeFiles: rewrites.beforeFiles.map((r) => |
|
|
buildCustomRoute('rewrite', r) |
|
|
), |
|
|
afterFiles: rewrites.afterFiles.map((r) => |
|
|
buildCustomRoute('rewrite', r) |
|
|
), |
|
|
fallback: rewrites.fallback.map((r) => |
|
|
buildCustomRoute('rewrite', r) |
|
|
), |
|
|
}, |
|
|
dynamicRoutes, |
|
|
staticRoutes, |
|
|
dataRoutes: [], |
|
|
i18n: config.i18n || undefined, |
|
|
rsc: { |
|
|
header: RSC_HEADER, |
|
|
|
|
|
|
|
|
varyHeader: `${RSC_HEADER}, ${NEXT_ROUTER_STATE_TREE_HEADER}, ${NEXT_ROUTER_PREFETCH_HEADER}, ${NEXT_ROUTER_SEGMENT_PREFETCH_HEADER}`, |
|
|
prefetchHeader: NEXT_ROUTER_PREFETCH_HEADER, |
|
|
didPostponeHeader: NEXT_DID_POSTPONE_HEADER, |
|
|
contentTypeHeader: RSC_CONTENT_TYPE_HEADER, |
|
|
suffix: RSC_SUFFIX, |
|
|
prefetchSuffix: RSC_PREFETCH_SUFFIX, |
|
|
prefetchSegmentHeader: NEXT_ROUTER_SEGMENT_PREFETCH_HEADER, |
|
|
prefetchSegmentSuffix: RSC_SEGMENT_SUFFIX, |
|
|
prefetchSegmentDirSuffix: RSC_SEGMENTS_DIR_SUFFIX, |
|
|
}, |
|
|
rewriteHeaders: { |
|
|
pathHeader: NEXT_REWRITTEN_PATH_HEADER, |
|
|
queryHeader: NEXT_REWRITTEN_QUERY_HEADER, |
|
|
}, |
|
|
skipMiddlewareUrlNormalize: config.skipMiddlewareUrlNormalize, |
|
|
ppr: isAppPPREnabled |
|
|
? { |
|
|
chain: { |
|
|
headers: { |
|
|
[NEXT_RESUME_HEADER]: '1', |
|
|
}, |
|
|
}, |
|
|
} |
|
|
: undefined, |
|
|
} satisfies RoutesManifest |
|
|
}) |
|
|
|
|
|
let clientRouterFilters: |
|
|
| undefined |
|
|
| ReturnType<typeof createClientRouterFilter> |
|
|
|
|
|
if (config.experimental.clientRouterFilter) { |
|
|
const nonInternalRedirects = (config._originalRedirects || []).filter( |
|
|
(r: any) => !r.internal |
|
|
) |
|
|
clientRouterFilters = createClientRouterFilter( |
|
|
[...appPaths], |
|
|
config.experimental.clientRouterFilterRedirects |
|
|
? nonInternalRedirects |
|
|
: [], |
|
|
config.experimental.clientRouterFilterAllowedRate |
|
|
) |
|
|
NextBuildContext.clientRouterFilters = clientRouterFilters |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
await writeFileUtf8( |
|
|
path.join(distDir, 'package.json'), |
|
|
'{"type": "commonjs"}' |
|
|
) |
|
|
|
|
|
|
|
|
await recordFrameworkVersion(process.env.__NEXT_VERSION as string) |
|
|
await updateBuildDiagnostics({ |
|
|
buildStage: 'start', |
|
|
}) |
|
|
|
|
|
const outputFileTracingRoot = config.outputFileTracingRoot || dir |
|
|
|
|
|
const pagesManifestPath = path.join( |
|
|
distDir, |
|
|
SERVER_DIRECTORY, |
|
|
PAGES_MANIFEST |
|
|
) |
|
|
|
|
|
let buildTraceContext: undefined | BuildTraceContext |
|
|
let buildTracesPromise: Promise<any> | undefined = undefined |
|
|
|
|
|
|
|
|
|
|
|
const useBuildWorker = |
|
|
config.experimental.webpackBuildWorker || |
|
|
(config.experimental.webpackBuildWorker === undefined && |
|
|
!config.webpack) |
|
|
const runServerAndEdgeInParallel = |
|
|
config.experimental.parallelServerCompiles |
|
|
const collectServerBuildTracesInParallel = |
|
|
config.experimental.parallelServerBuildTraces || |
|
|
(config.experimental.parallelServerBuildTraces === undefined && |
|
|
isCompileMode) |
|
|
|
|
|
nextBuildSpan.setAttribute( |
|
|
'has-custom-webpack-config', |
|
|
String(!!config.webpack) |
|
|
) |
|
|
nextBuildSpan.setAttribute('use-build-worker', String(useBuildWorker)) |
|
|
|
|
|
if ( |
|
|
!useBuildWorker && |
|
|
(runServerAndEdgeInParallel || collectServerBuildTracesInParallel) |
|
|
) { |
|
|
throw new Error( |
|
|
'The "parallelServerBuildTraces" and "parallelServerCompiles" options may only be used when build workers can be used. Read more: https://nextjs.org/docs/messages/parallel-build-without-worker' |
|
|
) |
|
|
} |
|
|
|
|
|
Log.info('Creating an optimized production build ...') |
|
|
traceMemoryUsage('Starting build', nextBuildSpan) |
|
|
|
|
|
await updateBuildDiagnostics({ |
|
|
buildStage: 'compile', |
|
|
buildOptions: { |
|
|
useBuildWorker: String(useBuildWorker), |
|
|
}, |
|
|
}) |
|
|
|
|
|
let shutdownPromise = Promise.resolve() |
|
|
if (!isGenerateMode) { |
|
|
if (isTurbopack) { |
|
|
const { |
|
|
duration: compilerDuration, |
|
|
shutdownPromise: p, |
|
|
...rest |
|
|
} = await turbopackBuild( |
|
|
process.env.NEXT_TURBOPACK_USE_WORKER === undefined || |
|
|
process.env.NEXT_TURBOPACK_USE_WORKER !== '0' |
|
|
) |
|
|
shutdownPromise = p |
|
|
traceMemoryUsage('Finished build', nextBuildSpan) |
|
|
|
|
|
buildTraceContext = rest.buildTraceContext |
|
|
|
|
|
const durationString = durationToString(compilerDuration) |
|
|
Log.event(`Compiled successfully in ${durationString}`) |
|
|
|
|
|
telemetry.record( |
|
|
eventBuildCompleted(pagesPaths, { |
|
|
bundler: 'turbopack', |
|
|
durationInSeconds: Math.round(compilerDuration), |
|
|
totalAppPagesCount, |
|
|
}) |
|
|
) |
|
|
} else { |
|
|
if ( |
|
|
runServerAndEdgeInParallel || |
|
|
collectServerBuildTracesInParallel |
|
|
) { |
|
|
let durationInSeconds = 0 |
|
|
|
|
|
await updateBuildDiagnostics({ |
|
|
buildStage: 'compile-server', |
|
|
}) |
|
|
|
|
|
const serverBuildPromise = webpackBuild(useBuildWorker, [ |
|
|
'server', |
|
|
]).then((res) => { |
|
|
traceMemoryUsage('Finished server compilation', nextBuildSpan) |
|
|
buildTraceContext = res.buildTraceContext |
|
|
durationInSeconds += res.duration |
|
|
|
|
|
if (collectServerBuildTracesInParallel) { |
|
|
const buildTraceWorker = new Worker( |
|
|
require.resolve('./collect-build-traces'), |
|
|
{ |
|
|
debuggerPortOffset: -1, |
|
|
isolatedMemory: false, |
|
|
numWorkers: 1, |
|
|
exposedMethods: ['collectBuildTraces'], |
|
|
} |
|
|
) as Worker & typeof import('./collect-build-traces') |
|
|
|
|
|
buildTracesPromise = buildTraceWorker |
|
|
.collectBuildTraces({ |
|
|
dir, |
|
|
config, |
|
|
distDir, |
|
|
|
|
|
edgeRuntimeRoutes: collectRoutesUsingEdgeRuntime(new Map()), |
|
|
staticPages: [], |
|
|
hasSsrAmpPages: false, |
|
|
buildTraceContext, |
|
|
outputFileTracingRoot, |
|
|
isTurbopack: false, |
|
|
}) |
|
|
.catch((err) => { |
|
|
console.error(err) |
|
|
process.exit(1) |
|
|
}) |
|
|
} |
|
|
}) |
|
|
if (!runServerAndEdgeInParallel) { |
|
|
await serverBuildPromise |
|
|
await updateBuildDiagnostics({ |
|
|
buildStage: 'webpack-compile-edge-server', |
|
|
}) |
|
|
} |
|
|
|
|
|
const edgeBuildPromise = webpackBuild(useBuildWorker, [ |
|
|
'edge-server', |
|
|
]).then((res) => { |
|
|
durationInSeconds += res.duration |
|
|
traceMemoryUsage( |
|
|
'Finished edge-server compilation', |
|
|
nextBuildSpan |
|
|
) |
|
|
}) |
|
|
if (runServerAndEdgeInParallel) { |
|
|
await serverBuildPromise |
|
|
await updateBuildDiagnostics({ |
|
|
buildStage: 'webpack-compile-edge-server', |
|
|
}) |
|
|
} |
|
|
await edgeBuildPromise |
|
|
|
|
|
await updateBuildDiagnostics({ |
|
|
buildStage: 'webpack-compile-client', |
|
|
}) |
|
|
|
|
|
await webpackBuild(useBuildWorker, ['client']).then((res) => { |
|
|
durationInSeconds += res.duration |
|
|
traceMemoryUsage('Finished client compilation', nextBuildSpan) |
|
|
}) |
|
|
|
|
|
const durationString = durationToString(durationInSeconds) |
|
|
Log.event(`Compiled successfully in ${durationString}`) |
|
|
|
|
|
telemetry.record( |
|
|
eventBuildCompleted(pagesPaths, { |
|
|
bundler: getBundlerForTelemetry(isTurbopack), |
|
|
durationInSeconds, |
|
|
totalAppPagesCount, |
|
|
}) |
|
|
) |
|
|
} else { |
|
|
const { duration: compilerDuration, ...rest } = await webpackBuild( |
|
|
useBuildWorker, |
|
|
null |
|
|
) |
|
|
traceMemoryUsage('Finished build', nextBuildSpan) |
|
|
|
|
|
buildTraceContext = rest.buildTraceContext |
|
|
|
|
|
telemetry.record( |
|
|
eventBuildCompleted(pagesPaths, { |
|
|
bundler: getBundlerForTelemetry(isTurbopack), |
|
|
durationInSeconds: compilerDuration, |
|
|
totalAppPagesCount, |
|
|
}) |
|
|
) |
|
|
} |
|
|
} |
|
|
await runAfterProductionCompile({ |
|
|
config, |
|
|
buildSpan: nextBuildSpan, |
|
|
telemetry, |
|
|
metadata: { |
|
|
projectDir: dir, |
|
|
distDir, |
|
|
}, |
|
|
}) |
|
|
} |
|
|
|
|
|
|
|
|
if (appDir && !isCompileMode && !isGenerateMode) { |
|
|
await updateBuildDiagnostics({ |
|
|
buildStage: 'type-checking', |
|
|
}) |
|
|
await startTypeChecking(typeCheckingOptions) |
|
|
traceMemoryUsage('Finished type checking', nextBuildSpan) |
|
|
} |
|
|
|
|
|
const postCompileSpinner = createSpinner('Collecting page data') |
|
|
|
|
|
const buildManifestPath = path.join(distDir, BUILD_MANIFEST) |
|
|
const appBuildManifestPath = path.join(distDir, APP_BUILD_MANIFEST) |
|
|
|
|
|
let staticAppPagesCount = 0 |
|
|
let serverAppPagesCount = 0 |
|
|
let edgeRuntimeAppCount = 0 |
|
|
let edgeRuntimePagesCount = 0 |
|
|
const ssgPages = new Set<string>() |
|
|
const ssgStaticFallbackPages = new Set<string>() |
|
|
const ssgBlockingFallbackPages = new Set<string>() |
|
|
const staticPages = new Set<string>() |
|
|
const invalidPages = new Set<string>() |
|
|
const hybridAmpPages = new Set<string>() |
|
|
const serverPropsPages = new Set<string>() |
|
|
const additionalPaths = new Map<string, PrerenderedRoute[]>() |
|
|
const staticPaths = new Map<string, PrerenderedRoute[]>() |
|
|
const appNormalizedPaths = new Map<string, string>() |
|
|
const fallbackModes = new Map<string, FallbackMode>() |
|
|
const appDefaultConfigs = new Map<string, AppSegmentConfig>() |
|
|
const pageInfos: PageInfos = new Map<string, PageInfo>() |
|
|
let pagesManifest = await readManifest<PagesManifest>(pagesManifestPath) |
|
|
const buildManifest = await readManifest<BuildManifest>(buildManifestPath) |
|
|
const appBuildManifest = appDir |
|
|
? await readManifest<AppBuildManifest>(appBuildManifestPath) |
|
|
: undefined |
|
|
|
|
|
const appPathRoutes: Record<string, string> = {} |
|
|
|
|
|
if (appDir) { |
|
|
const appPathsManifest = await readManifest<Record<string, string>>( |
|
|
path.join(distDir, SERVER_DIRECTORY, APP_PATHS_MANIFEST) |
|
|
) |
|
|
|
|
|
for (const key in appPathsManifest) { |
|
|
appPathRoutes[key] = normalizeAppPath(key) |
|
|
} |
|
|
|
|
|
await writeManifest( |
|
|
path.join(distDir, APP_PATH_ROUTES_MANIFEST), |
|
|
appPathRoutes |
|
|
) |
|
|
} |
|
|
|
|
|
process.env.NEXT_PHASE = PHASE_PRODUCTION_BUILD |
|
|
|
|
|
const worker = createStaticWorker(config, { debuggerPortOffset: -1 }) |
|
|
|
|
|
const analysisBegin = process.hrtime() |
|
|
const staticCheckSpan = nextBuildSpan.traceChild('static-check') |
|
|
|
|
|
const functionsConfigManifest: FunctionsConfigManifest = { |
|
|
version: 1, |
|
|
functions: {}, |
|
|
} |
|
|
|
|
|
const { |
|
|
customAppGetInitialProps, |
|
|
namedExports, |
|
|
isNextImageImported, |
|
|
hasSsrAmpPages, |
|
|
hasNonStaticErrorPage, |
|
|
} = await staticCheckSpan.traceAsyncFn(async () => { |
|
|
if (isCompileMode) { |
|
|
return { |
|
|
customAppGetInitialProps: false, |
|
|
namedExports: [], |
|
|
isNextImageImported: true, |
|
|
hasSsrAmpPages: !!pagesDir, |
|
|
hasNonStaticErrorPage: true, |
|
|
} |
|
|
} |
|
|
|
|
|
const { configFileName, publicRuntimeConfig, serverRuntimeConfig } = |
|
|
config |
|
|
const runtimeEnvConfig = { publicRuntimeConfig, serverRuntimeConfig } |
|
|
const sriEnabled = Boolean(config.experimental.sri?.algorithm) |
|
|
|
|
|
const nonStaticErrorPageSpan = staticCheckSpan.traceChild( |
|
|
'check-static-error-page' |
|
|
) |
|
|
const errorPageHasCustomGetInitialProps = |
|
|
nonStaticErrorPageSpan.traceAsyncFn( |
|
|
async () => |
|
|
hasCustomErrorPage && |
|
|
(await worker.hasCustomGetInitialProps({ |
|
|
page: '/_error', |
|
|
distDir, |
|
|
runtimeEnvConfig, |
|
|
checkingApp: false, |
|
|
sriEnabled, |
|
|
})) |
|
|
) |
|
|
|
|
|
const errorPageStaticResult = nonStaticErrorPageSpan.traceAsyncFn( |
|
|
async () => |
|
|
hasCustomErrorPage && |
|
|
worker.isPageStatic({ |
|
|
dir, |
|
|
page: '/_error', |
|
|
distDir, |
|
|
configFileName, |
|
|
runtimeEnvConfig, |
|
|
cacheComponents: isAppCacheComponentsEnabled, |
|
|
authInterrupts: isAuthInterruptsEnabled, |
|
|
httpAgentOptions: config.httpAgentOptions, |
|
|
locales: config.i18n?.locales, |
|
|
defaultLocale: config.i18n?.defaultLocale, |
|
|
nextConfigOutput: config.output, |
|
|
pprConfig: config.experimental.ppr, |
|
|
cacheLifeProfiles: config.experimental.cacheLife, |
|
|
buildId, |
|
|
sriEnabled, |
|
|
}) |
|
|
) |
|
|
|
|
|
const appPageToCheck = '/_app' |
|
|
|
|
|
const customAppGetInitialPropsPromise = worker.hasCustomGetInitialProps( |
|
|
{ |
|
|
page: appPageToCheck, |
|
|
distDir, |
|
|
runtimeEnvConfig, |
|
|
checkingApp: true, |
|
|
sriEnabled, |
|
|
} |
|
|
) |
|
|
|
|
|
const namedExportsPromise = worker.getDefinedNamedExports({ |
|
|
page: appPageToCheck, |
|
|
distDir, |
|
|
runtimeEnvConfig, |
|
|
sriEnabled, |
|
|
}) |
|
|
|
|
|
|
|
|
let isNextImageImported: boolean | undefined |
|
|
|
|
|
let hasSsrAmpPages = false |
|
|
|
|
|
const computedManifestData = await computeFromManifest( |
|
|
{ build: buildManifest, app: appBuildManifest }, |
|
|
distDir, |
|
|
config.experimental.gzipSize |
|
|
) |
|
|
|
|
|
const middlewareManifest: MiddlewareManifest = require( |
|
|
path.join(distDir, SERVER_DIRECTORY, MIDDLEWARE_MANIFEST) |
|
|
) |
|
|
|
|
|
const actionManifest = appDir |
|
|
? (require( |
|
|
path.join( |
|
|
distDir, |
|
|
SERVER_DIRECTORY, |
|
|
SERVER_REFERENCE_MANIFEST + '.json' |
|
|
) |
|
|
) as ActionManifest) |
|
|
: null |
|
|
const entriesWithAction = actionManifest ? new Set() : null |
|
|
if (actionManifest && entriesWithAction) { |
|
|
for (const id in actionManifest.node) { |
|
|
for (const entry in actionManifest.node[id].workers) { |
|
|
entriesWithAction.add(entry) |
|
|
} |
|
|
} |
|
|
for (const id in actionManifest.edge) { |
|
|
for (const entry in actionManifest.edge[id].workers) { |
|
|
entriesWithAction.add(entry) |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
for (const key of Object.keys(middlewareManifest?.functions)) { |
|
|
if (key.startsWith('/api')) { |
|
|
edgeRuntimePagesCount++ |
|
|
} |
|
|
} |
|
|
|
|
|
await Promise.all( |
|
|
Object.entries(pageKeys) |
|
|
.reduce<Array<{ pageType: keyof typeof pageKeys; page: string }>>( |
|
|
(acc, [key, files]) => { |
|
|
if (!files) { |
|
|
return acc |
|
|
} |
|
|
|
|
|
const pageType = key as keyof typeof pageKeys |
|
|
|
|
|
for (const page of files) { |
|
|
acc.push({ pageType, page }) |
|
|
} |
|
|
|
|
|
return acc |
|
|
}, |
|
|
[] |
|
|
) |
|
|
.map(({ pageType, page }) => { |
|
|
const checkPageSpan = staticCheckSpan.traceChild('check-page', { |
|
|
page, |
|
|
}) |
|
|
return checkPageSpan.traceAsyncFn(async () => { |
|
|
const actualPage = normalizePagePath(page) |
|
|
const [size, totalSize] = await getJsPageSizeInKb( |
|
|
pageType, |
|
|
actualPage, |
|
|
distDir, |
|
|
buildManifest, |
|
|
appBuildManifest, |
|
|
config.experimental.gzipSize, |
|
|
computedManifestData |
|
|
) |
|
|
|
|
|
let isRoutePPREnabled = false |
|
|
let isSSG = false |
|
|
let isStatic = false |
|
|
let isServerComponent = false |
|
|
let isHybridAmp = false |
|
|
let ssgPageRoutes: string[] | null = null |
|
|
let pagePath = '' |
|
|
|
|
|
if (pageType === 'pages') { |
|
|
pagePath = |
|
|
pagesPaths.find((p) => { |
|
|
p = normalizePathSep(p) |
|
|
return ( |
|
|
p.startsWith(actualPage + '.') || |
|
|
p.startsWith(actualPage + '/index.') |
|
|
) |
|
|
}) || '' |
|
|
} |
|
|
let originalAppPath: string | undefined |
|
|
|
|
|
if (pageType === 'app' && mappedAppPages) { |
|
|
for (const [originalPath, normalizedPath] of Object.entries( |
|
|
appPathRoutes |
|
|
)) { |
|
|
if (normalizedPath === page) { |
|
|
pagePath = mappedAppPages[originalPath].replace( |
|
|
/^private-next-app-dir/, |
|
|
'' |
|
|
) |
|
|
originalAppPath = originalPath |
|
|
break |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
const pageFilePath = isAppBuiltinNotFoundPage(pagePath) |
|
|
? require.resolve( |
|
|
'next/dist/client/components/builtin/not-found' |
|
|
) |
|
|
: path.join( |
|
|
(pageType === 'pages' ? pagesDir : appDir) || '', |
|
|
pagePath |
|
|
) |
|
|
|
|
|
const isInsideAppDir = pageType === 'app' |
|
|
const staticInfo = pagePath |
|
|
? await getStaticInfoIncludingLayouts({ |
|
|
isInsideAppDir, |
|
|
pageFilePath, |
|
|
pageExtensions: config.pageExtensions, |
|
|
appDir, |
|
|
config, |
|
|
isDev: false, |
|
|
|
|
|
|
|
|
|
|
|
page: isInsideAppDir ? originalAppPath! : page, |
|
|
}) |
|
|
: undefined |
|
|
|
|
|
if (staticInfo?.hadUnsupportedValue) { |
|
|
errorFromUnsupportedSegmentConfig() |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if ( |
|
|
typeof staticInfo?.runtime !== 'undefined' || |
|
|
typeof staticInfo?.maxDuration !== 'undefined' || |
|
|
typeof staticInfo?.preferredRegion !== 'undefined' |
|
|
) { |
|
|
const regions = staticInfo?.preferredRegion |
|
|
? typeof staticInfo.preferredRegion === 'string' |
|
|
? [staticInfo.preferredRegion] |
|
|
: staticInfo.preferredRegion |
|
|
: undefined |
|
|
|
|
|
functionsConfigManifest.functions[page] = { |
|
|
maxDuration: staticInfo?.maxDuration, |
|
|
...(regions && { regions }), |
|
|
} |
|
|
} |
|
|
|
|
|
const pageRuntime = middlewareManifest.functions[ |
|
|
originalAppPath || page |
|
|
] |
|
|
? 'edge' |
|
|
: staticInfo?.runtime |
|
|
|
|
|
if (!isCompileMode) { |
|
|
isServerComponent = |
|
|
pageType === 'app' && |
|
|
staticInfo?.rsc !== RSC_MODULE_TYPES.client |
|
|
|
|
|
if (pageType === 'app' || !isReservedPage(page)) { |
|
|
try { |
|
|
let edgeInfo: any |
|
|
|
|
|
if (isEdgeRuntime(pageRuntime)) { |
|
|
if (pageType === 'app') { |
|
|
edgeRuntimeAppCount++ |
|
|
} else { |
|
|
edgeRuntimePagesCount++ |
|
|
} |
|
|
|
|
|
const manifestKey = |
|
|
pageType === 'pages' ? page : originalAppPath || '' |
|
|
|
|
|
edgeInfo = middlewareManifest.functions[manifestKey] |
|
|
} |
|
|
|
|
|
let isPageStaticSpan = |
|
|
checkPageSpan.traceChild('is-page-static') |
|
|
let workerResult = await isPageStaticSpan.traceAsyncFn( |
|
|
() => { |
|
|
return worker.isPageStatic({ |
|
|
dir, |
|
|
page, |
|
|
originalAppPath, |
|
|
distDir, |
|
|
configFileName, |
|
|
runtimeEnvConfig, |
|
|
httpAgentOptions: config.httpAgentOptions, |
|
|
locales: config.i18n?.locales, |
|
|
defaultLocale: config.i18n?.defaultLocale, |
|
|
parentId: isPageStaticSpan.getId(), |
|
|
pageRuntime, |
|
|
edgeInfo, |
|
|
pageType, |
|
|
cacheComponents: isAppCacheComponentsEnabled, |
|
|
authInterrupts: isAuthInterruptsEnabled, |
|
|
cacheHandler: config.cacheHandler, |
|
|
cacheHandlers: config.experimental.cacheHandlers, |
|
|
isrFlushToDisk: ciEnvironment.hasNextSupport |
|
|
? false |
|
|
: config.experimental.isrFlushToDisk, |
|
|
maxMemoryCacheSize: config.cacheMaxMemorySize, |
|
|
nextConfigOutput: config.output, |
|
|
pprConfig: config.experimental.ppr, |
|
|
cacheLifeProfiles: config.experimental.cacheLife, |
|
|
buildId, |
|
|
sriEnabled, |
|
|
}) |
|
|
} |
|
|
) |
|
|
|
|
|
if (pageType === 'app' && originalAppPath) { |
|
|
appNormalizedPaths.set(originalAppPath, page) |
|
|
|
|
|
if (isEdgeRuntime(pageRuntime)) { |
|
|
isStatic = false |
|
|
isSSG = false |
|
|
|
|
|
Log.warnOnce( |
|
|
`Using edge runtime on a page currently disables static generation for that page` |
|
|
) |
|
|
} else { |
|
|
const isDynamic = isDynamicRoute(page) |
|
|
|
|
|
if ( |
|
|
typeof workerResult.isRoutePPREnabled === 'boolean' |
|
|
) { |
|
|
isRoutePPREnabled = workerResult.isRoutePPREnabled |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (workerResult.isRoutePPREnabled) { |
|
|
isSSG = true |
|
|
isStatic = true |
|
|
|
|
|
staticPaths.set(originalAppPath, []) |
|
|
} |
|
|
|
|
|
if (workerResult.prerenderedRoutes) { |
|
|
staticPaths.set( |
|
|
originalAppPath, |
|
|
workerResult.prerenderedRoutes |
|
|
) |
|
|
ssgPageRoutes = workerResult.prerenderedRoutes.map( |
|
|
(route) => route.pathname |
|
|
) |
|
|
isSSG = true |
|
|
} |
|
|
|
|
|
const appConfig = workerResult.appConfig || {} |
|
|
if (appConfig.revalidate !== 0) { |
|
|
const hasGenerateStaticParams = |
|
|
workerResult.prerenderedRoutes && |
|
|
workerResult.prerenderedRoutes.length > 0 |
|
|
|
|
|
if ( |
|
|
config.output === 'export' && |
|
|
isDynamic && |
|
|
!hasGenerateStaticParams |
|
|
) { |
|
|
throw new Error( |
|
|
`Page "${page}" is missing "generateStaticParams()" so it cannot be used with "output: export" config.` |
|
|
) |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!isDynamic) { |
|
|
staticPaths.set(originalAppPath, [ |
|
|
{ |
|
|
params: {}, |
|
|
pathname: page, |
|
|
encodedPathname: page, |
|
|
fallbackRouteParams: [], |
|
|
fallbackMode: |
|
|
workerResult.prerenderFallbackMode, |
|
|
fallbackRootParams: [], |
|
|
throwOnEmptyStaticShell: true, |
|
|
}, |
|
|
]) |
|
|
isStatic = true |
|
|
} else if ( |
|
|
!hasGenerateStaticParams && |
|
|
(appConfig.dynamic === 'error' || |
|
|
appConfig.dynamic === 'force-static') |
|
|
) { |
|
|
staticPaths.set(originalAppPath, []) |
|
|
isStatic = true |
|
|
isRoutePPREnabled = false |
|
|
} |
|
|
} |
|
|
|
|
|
if (workerResult.prerenderFallbackMode) { |
|
|
fallbackModes.set( |
|
|
originalAppPath, |
|
|
workerResult.prerenderFallbackMode |
|
|
) |
|
|
} |
|
|
|
|
|
appDefaultConfigs.set(originalAppPath, appConfig) |
|
|
} |
|
|
} else { |
|
|
if (isEdgeRuntime(pageRuntime)) { |
|
|
if (workerResult.hasStaticProps) { |
|
|
console.warn( |
|
|
`"getStaticProps" is not yet supported fully with "experimental-edge", detected on ${page}` |
|
|
) |
|
|
} |
|
|
workerResult.isStatic = false |
|
|
workerResult.hasStaticProps = false |
|
|
} |
|
|
|
|
|
if ( |
|
|
workerResult.isStatic === false && |
|
|
(workerResult.isHybridAmp || workerResult.isAmpOnly) |
|
|
) { |
|
|
hasSsrAmpPages = true |
|
|
} |
|
|
|
|
|
if (workerResult.isHybridAmp) { |
|
|
isHybridAmp = true |
|
|
hybridAmpPages.add(page) |
|
|
} |
|
|
|
|
|
if (workerResult.isNextImageImported) { |
|
|
isNextImageImported = true |
|
|
} |
|
|
|
|
|
if (workerResult.hasStaticProps) { |
|
|
ssgPages.add(page) |
|
|
isSSG = true |
|
|
|
|
|
if ( |
|
|
workerResult.prerenderedRoutes && |
|
|
workerResult.prerenderedRoutes.length > 0 |
|
|
) { |
|
|
additionalPaths.set( |
|
|
page, |
|
|
workerResult.prerenderedRoutes |
|
|
) |
|
|
ssgPageRoutes = workerResult.prerenderedRoutes.map( |
|
|
(route) => route.pathname |
|
|
) |
|
|
} |
|
|
|
|
|
if ( |
|
|
workerResult.prerenderFallbackMode === |
|
|
FallbackMode.BLOCKING_STATIC_RENDER |
|
|
) { |
|
|
ssgBlockingFallbackPages.add(page) |
|
|
} else if ( |
|
|
workerResult.prerenderFallbackMode === |
|
|
FallbackMode.PRERENDER |
|
|
) { |
|
|
ssgStaticFallbackPages.add(page) |
|
|
} |
|
|
} else if (workerResult.hasServerProps) { |
|
|
serverPropsPages.add(page) |
|
|
} else if ( |
|
|
workerResult.isStatic && |
|
|
!isServerComponent && |
|
|
(await customAppGetInitialPropsPromise) === false |
|
|
) { |
|
|
staticPages.add(page) |
|
|
isStatic = true |
|
|
} else if (isServerComponent) { |
|
|
|
|
|
|
|
|
ssgPages.add(page) |
|
|
isSSG = true |
|
|
} |
|
|
|
|
|
if (hasPages404 && page === '/404') { |
|
|
if ( |
|
|
!workerResult.isStatic && |
|
|
!workerResult.hasStaticProps |
|
|
) { |
|
|
throw new Error( |
|
|
`\`pages/404\` ${STATIC_STATUS_PAGE_GET_INITIAL_PROPS_ERROR}` |
|
|
) |
|
|
} |
|
|
|
|
|
|
|
|
if ( |
|
|
(await customAppGetInitialPropsPromise) && |
|
|
!workerResult.hasStaticProps |
|
|
) { |
|
|
staticPages.delete(page) |
|
|
} |
|
|
} |
|
|
|
|
|
if ( |
|
|
STATIC_STATUS_PAGES.includes(page) && |
|
|
!workerResult.isStatic && |
|
|
!workerResult.hasStaticProps |
|
|
) { |
|
|
throw new Error( |
|
|
`\`pages${page}\` ${STATIC_STATUS_PAGE_GET_INITIAL_PROPS_ERROR}` |
|
|
) |
|
|
} |
|
|
} |
|
|
} catch (err) { |
|
|
if ( |
|
|
!isError(err) || |
|
|
err.message !== 'INVALID_DEFAULT_EXPORT' |
|
|
) |
|
|
throw err |
|
|
invalidPages.add(page) |
|
|
} |
|
|
} |
|
|
|
|
|
if (pageType === 'app') { |
|
|
if (isSSG || isStatic) { |
|
|
staticAppPagesCount++ |
|
|
} else { |
|
|
serverAppPagesCount++ |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
pageInfos.set(page, { |
|
|
originalAppPath, |
|
|
size, |
|
|
totalSize, |
|
|
isStatic, |
|
|
isSSG, |
|
|
isRoutePPREnabled, |
|
|
isHybridAmp, |
|
|
ssgPageRoutes, |
|
|
initialCacheControl: undefined, |
|
|
runtime: pageRuntime, |
|
|
pageDuration: undefined, |
|
|
ssgPageDurations: undefined, |
|
|
hasEmptyStaticShell: undefined, |
|
|
}) |
|
|
}) |
|
|
}) |
|
|
) |
|
|
|
|
|
const errorPageResult = await errorPageStaticResult |
|
|
const nonStaticErrorPage = |
|
|
(await errorPageHasCustomGetInitialProps) || |
|
|
(errorPageResult && errorPageResult.hasServerProps) |
|
|
|
|
|
const returnValue = { |
|
|
customAppGetInitialProps: await customAppGetInitialPropsPromise, |
|
|
namedExports: await namedExportsPromise, |
|
|
isNextImageImported, |
|
|
hasSsrAmpPages, |
|
|
hasNonStaticErrorPage: nonStaticErrorPage, |
|
|
} |
|
|
|
|
|
return returnValue |
|
|
}) |
|
|
|
|
|
if (postCompileSpinner) postCompileSpinner.stopAndPersist() |
|
|
traceMemoryUsage('Finished collecting page data', nextBuildSpan) |
|
|
|
|
|
if (customAppGetInitialProps) { |
|
|
console.warn( |
|
|
bold(yellow(`Warning: `)) + |
|
|
yellow( |
|
|
`You have opted-out of Automatic Static Optimization due to \`getInitialProps\` in \`pages/_app\`. This does not opt-out pages with \`getStaticProps\`` |
|
|
) |
|
|
) |
|
|
console.warn( |
|
|
'Read more: https://nextjs.org/docs/messages/opt-out-auto-static-optimization\n' |
|
|
) |
|
|
} |
|
|
|
|
|
const { cacheHandler } = config |
|
|
|
|
|
const instrumentationHookEntryFiles: string[] = [] |
|
|
if (hasInstrumentationHook) { |
|
|
instrumentationHookEntryFiles.push( |
|
|
path.join(SERVER_DIRECTORY, `${INSTRUMENTATION_HOOK_FILENAME}.js`) |
|
|
) |
|
|
|
|
|
|
|
|
if (!isTurbopack && (edgeRuntimeAppCount || edgeRuntimePagesCount)) { |
|
|
instrumentationHookEntryFiles.push( |
|
|
path.join( |
|
|
SERVER_DIRECTORY, |
|
|
`edge-${INSTRUMENTATION_HOOK_FILENAME}.js` |
|
|
) |
|
|
) |
|
|
} |
|
|
} |
|
|
|
|
|
const requiredServerFilesManifest = nextBuildSpan |
|
|
.traceChild('generate-required-server-files') |
|
|
.traceFn(() => { |
|
|
const normalizedCacheHandlers: Record<string, string> = {} |
|
|
|
|
|
for (const [key, value] of Object.entries( |
|
|
config.experimental.cacheHandlers || {} |
|
|
)) { |
|
|
if (key && value) { |
|
|
normalizedCacheHandlers[key] = path.relative(distDir, value) |
|
|
} |
|
|
} |
|
|
|
|
|
const serverFilesManifest: RequiredServerFilesManifest = { |
|
|
version: 1, |
|
|
config: { |
|
|
...config, |
|
|
configFile: undefined, |
|
|
...(ciEnvironment.hasNextSupport |
|
|
? { |
|
|
compress: false, |
|
|
} |
|
|
: {}), |
|
|
cacheHandler: cacheHandler |
|
|
? path.relative(distDir, cacheHandler) |
|
|
: config.cacheHandler, |
|
|
experimental: { |
|
|
...config.experimental, |
|
|
cacheHandlers: normalizedCacheHandlers, |
|
|
trustHostHeader: ciEnvironment.hasNextSupport, |
|
|
isExperimentalCompile: isCompileMode, |
|
|
}, |
|
|
}, |
|
|
appDir: dir, |
|
|
relativeAppDir: path.relative(outputFileTracingRoot, dir), |
|
|
files: [ |
|
|
ROUTES_MANIFEST, |
|
|
path.relative(distDir, pagesManifestPath), |
|
|
BUILD_MANIFEST, |
|
|
PRERENDER_MANIFEST, |
|
|
path.join(SERVER_DIRECTORY, FUNCTIONS_CONFIG_MANIFEST), |
|
|
path.join(SERVER_DIRECTORY, MIDDLEWARE_MANIFEST), |
|
|
path.join(SERVER_DIRECTORY, MIDDLEWARE_BUILD_MANIFEST + '.js'), |
|
|
...(!isTurbopack |
|
|
? [ |
|
|
path.join( |
|
|
SERVER_DIRECTORY, |
|
|
MIDDLEWARE_REACT_LOADABLE_MANIFEST + '.js' |
|
|
), |
|
|
REACT_LOADABLE_MANIFEST, |
|
|
] |
|
|
: []), |
|
|
...(appDir |
|
|
? [ |
|
|
...(config.experimental.sri |
|
|
? [ |
|
|
path.join( |
|
|
SERVER_DIRECTORY, |
|
|
SUBRESOURCE_INTEGRITY_MANIFEST + '.js' |
|
|
), |
|
|
path.join( |
|
|
SERVER_DIRECTORY, |
|
|
SUBRESOURCE_INTEGRITY_MANIFEST + '.json' |
|
|
), |
|
|
] |
|
|
: []), |
|
|
path.join(SERVER_DIRECTORY, APP_PATHS_MANIFEST), |
|
|
path.join(APP_PATH_ROUTES_MANIFEST), |
|
|
APP_BUILD_MANIFEST, |
|
|
path.join( |
|
|
SERVER_DIRECTORY, |
|
|
SERVER_REFERENCE_MANIFEST + '.js' |
|
|
), |
|
|
path.join( |
|
|
SERVER_DIRECTORY, |
|
|
SERVER_REFERENCE_MANIFEST + '.json' |
|
|
), |
|
|
] |
|
|
: []), |
|
|
...(pagesDir && !isTurbopack |
|
|
? [ |
|
|
DYNAMIC_CSS_MANIFEST + '.json', |
|
|
path.join(SERVER_DIRECTORY, DYNAMIC_CSS_MANIFEST + '.js'), |
|
|
] |
|
|
: []), |
|
|
BUILD_ID_FILE, |
|
|
path.join(SERVER_DIRECTORY, NEXT_FONT_MANIFEST + '.js'), |
|
|
path.join(SERVER_DIRECTORY, NEXT_FONT_MANIFEST + '.json'), |
|
|
SERVER_FILES_MANIFEST, |
|
|
...instrumentationHookEntryFiles, |
|
|
] |
|
|
.filter(nonNullable) |
|
|
.map((file) => path.join(config.distDir, file)), |
|
|
ignore: [] as string[], |
|
|
} |
|
|
|
|
|
return serverFilesManifest |
|
|
}) |
|
|
|
|
|
if (!hasSsrAmpPages) { |
|
|
requiredServerFilesManifest.ignore.push( |
|
|
path.relative( |
|
|
dir, |
|
|
path.join( |
|
|
path.dirname( |
|
|
require.resolve( |
|
|
'next/dist/compiled/@ampproject/toolbox-optimizer' |
|
|
) |
|
|
), |
|
|
'**/*' |
|
|
) |
|
|
) |
|
|
) |
|
|
} |
|
|
|
|
|
const middlewareFile = rootPaths.find((p) => |
|
|
p.includes(MIDDLEWARE_FILENAME) |
|
|
) |
|
|
let hasNodeMiddleware = false |
|
|
|
|
|
if (middlewareFile) { |
|
|
const staticInfo = await getStaticInfoIncludingLayouts({ |
|
|
isInsideAppDir: false, |
|
|
pageFilePath: path.join(dir, middlewareFile), |
|
|
config, |
|
|
appDir, |
|
|
pageExtensions: config.pageExtensions, |
|
|
isDev: false, |
|
|
page: 'middleware', |
|
|
}) |
|
|
|
|
|
if (staticInfo.hadUnsupportedValue) { |
|
|
errorFromUnsupportedSegmentConfig() |
|
|
} |
|
|
|
|
|
if (staticInfo.runtime === 'nodejs') { |
|
|
hasNodeMiddleware = true |
|
|
functionsConfigManifest.functions['/_middleware'] = { |
|
|
runtime: staticInfo.runtime, |
|
|
matchers: staticInfo.middleware?.matchers ?? [ |
|
|
{ |
|
|
regexp: '^.*$', |
|
|
originalSource: '/:path*', |
|
|
}, |
|
|
], |
|
|
} |
|
|
|
|
|
if (isTurbopack) { |
|
|
await writeManifest( |
|
|
path.join( |
|
|
distDir, |
|
|
'static', |
|
|
buildId, |
|
|
TURBOPACK_CLIENT_MIDDLEWARE_MANIFEST |
|
|
), |
|
|
functionsConfigManifest.functions['/_middleware'].matchers || [] |
|
|
) |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
await writeFunctionsConfigManifest(distDir, functionsConfigManifest) |
|
|
|
|
|
if (!isGenerateMode && !buildTracesPromise) { |
|
|
buildTracesPromise = collectBuildTraces({ |
|
|
dir, |
|
|
config, |
|
|
distDir, |
|
|
edgeRuntimeRoutes: collectRoutesUsingEdgeRuntime(pageInfos), |
|
|
staticPages: [...staticPages], |
|
|
nextBuildSpan, |
|
|
hasSsrAmpPages, |
|
|
buildTraceContext, |
|
|
outputFileTracingRoot, |
|
|
isTurbopack: true, |
|
|
}).catch((err) => { |
|
|
console.error(err) |
|
|
process.exit(1) |
|
|
}) |
|
|
} |
|
|
|
|
|
if (serverPropsPages.size > 0 || ssgPages.size > 0) { |
|
|
|
|
|
|
|
|
routesManifest.dataRoutes = sortPages([ |
|
|
...serverPropsPages, |
|
|
...ssgPages, |
|
|
]).map((page) => { |
|
|
return buildDataRoute(page, buildId) |
|
|
}) |
|
|
} |
|
|
|
|
|
|
|
|
await nextBuildSpan |
|
|
.traceChild('write-routes-manifest') |
|
|
.traceAsyncFn(() => writeManifest(routesManifestPath, routesManifest)) |
|
|
|
|
|
|
|
|
|
|
|
const useStaticPages404 = |
|
|
!customAppGetInitialProps && (!hasNonStaticErrorPage || hasPages404) |
|
|
|
|
|
if (invalidPages.size > 0) { |
|
|
const err = new Error( |
|
|
`Build optimization failed: found page${ |
|
|
invalidPages.size === 1 ? '' : 's' |
|
|
} without a React Component as default export in \n${[...invalidPages] |
|
|
.map((pg) => `pages${pg}`) |
|
|
.join( |
|
|
'\n' |
|
|
)}\n\nSee https://nextjs.org/docs/messages/page-without-valid-component for more info.\n` |
|
|
) as NextError |
|
|
err.code = 'BUILD_OPTIMIZATION_FAILED' |
|
|
throw err |
|
|
} |
|
|
|
|
|
await writeBuildId(distDir, buildId) |
|
|
|
|
|
if (config.experimental.optimizeCss) { |
|
|
const globOrig = |
|
|
require('next/dist/compiled/glob') as typeof import('next/dist/compiled/glob') |
|
|
|
|
|
const cssFilePaths = await new Promise<string[]>((resolve, reject) => { |
|
|
globOrig( |
|
|
'**/*.css', |
|
|
{ cwd: path.join(distDir, 'static') }, |
|
|
(err, files) => { |
|
|
if (err) { |
|
|
return reject(err) |
|
|
} |
|
|
resolve(files) |
|
|
} |
|
|
) |
|
|
}) |
|
|
|
|
|
requiredServerFilesManifest.files.push( |
|
|
...cssFilePaths.map((filePath) => |
|
|
path.join(config.distDir, 'static', filePath) |
|
|
) |
|
|
) |
|
|
} |
|
|
|
|
|
const features: EventBuildFeatureUsage[] = [ |
|
|
{ |
|
|
featureName: 'experimental/cacheComponents', |
|
|
invocationCount: config.experimental.cacheComponents ? 1 : 0, |
|
|
}, |
|
|
{ |
|
|
featureName: 'experimental/optimizeCss', |
|
|
invocationCount: config.experimental.optimizeCss ? 1 : 0, |
|
|
}, |
|
|
{ |
|
|
featureName: 'experimental/nextScriptWorkers', |
|
|
invocationCount: config.experimental.nextScriptWorkers ? 1 : 0, |
|
|
}, |
|
|
{ |
|
|
featureName: 'experimental/ppr', |
|
|
invocationCount: config.experimental.ppr ? 1 : 0, |
|
|
}, |
|
|
{ |
|
|
featureName: 'turbopackPersistentCaching', |
|
|
invocationCount: isPersistentCachingEnabled(config) ? 1 : 0, |
|
|
}, |
|
|
] |
|
|
telemetry.record( |
|
|
features.map((feature) => { |
|
|
return { |
|
|
eventName: EVENT_BUILD_FEATURE_USAGE, |
|
|
payload: feature, |
|
|
} |
|
|
}) |
|
|
) |
|
|
|
|
|
await writeRequiredServerFilesManifest( |
|
|
distDir, |
|
|
requiredServerFilesManifest |
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
if (isGenerateMode && !isTurbopack) { |
|
|
Log.info('Inlining static env ...') |
|
|
|
|
|
await nextBuildSpan |
|
|
.traceChild('inline-static-env') |
|
|
.traceAsyncFn(async () => { |
|
|
await inlineStaticEnv({ |
|
|
distDir, |
|
|
config, |
|
|
}) |
|
|
}) |
|
|
} |
|
|
|
|
|
const middlewareManifest: MiddlewareManifest = await readManifest( |
|
|
path.join(distDir, SERVER_DIRECTORY, MIDDLEWARE_MANIFEST) |
|
|
) |
|
|
|
|
|
const prerenderManifest: PrerenderManifest = { |
|
|
version: 4, |
|
|
routes: {}, |
|
|
dynamicRoutes: {}, |
|
|
notFoundRoutes: [], |
|
|
preview: previewProps, |
|
|
} |
|
|
|
|
|
const tbdPrerenderRoutes: string[] = [] |
|
|
|
|
|
const { i18n } = config |
|
|
|
|
|
const usedStaticStatusPages = STATIC_STATUS_PAGES.filter( |
|
|
(page) => |
|
|
mappedPages[page] && |
|
|
mappedPages[page].startsWith('private-next-pages') |
|
|
) |
|
|
usedStaticStatusPages.forEach((page) => { |
|
|
if (!ssgPages.has(page) && !customAppGetInitialProps) { |
|
|
staticPages.add(page) |
|
|
} |
|
|
}) |
|
|
|
|
|
const hasPages500 = usedStaticStatusPages.includes('/500') |
|
|
const useDefaultStatic500 = |
|
|
!hasPages500 && !hasNonStaticErrorPage && !customAppGetInitialProps |
|
|
|
|
|
const combinedPages = [...staticPages, ...ssgPages] |
|
|
const isApp404Static = staticPaths.has(UNDERSCORE_NOT_FOUND_ROUTE_ENTRY) |
|
|
const hasStaticApp404 = hasApp404 && isApp404Static |
|
|
|
|
|
await updateBuildDiagnostics({ |
|
|
buildStage: 'static-generation', |
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ( |
|
|
!isCompileMode && |
|
|
(combinedPages.length > 0 || |
|
|
useStaticPages404 || |
|
|
useDefaultStatic500 || |
|
|
appDir) |
|
|
) { |
|
|
const staticGenerationSpan = |
|
|
nextBuildSpan.traceChild('static-generation') |
|
|
await staticGenerationSpan.traceAsyncFn(async () => { |
|
|
detectConflictingPaths( |
|
|
[ |
|
|
...combinedPages, |
|
|
...pageKeys.pages.filter((page) => !combinedPages.includes(page)), |
|
|
], |
|
|
ssgPages, |
|
|
new Map( |
|
|
Array.from(additionalPaths.entries()).map( |
|
|
([page, routes]): [string, string[]] => { |
|
|
return [page, routes.map((route) => route.pathname)] |
|
|
} |
|
|
) |
|
|
) |
|
|
) |
|
|
|
|
|
const exportApp = (require('../export') as typeof import('../export')) |
|
|
.default as typeof import('../export').default |
|
|
|
|
|
const exportConfig: NextConfigComplete = { |
|
|
...config, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
exportPathMap: (defaultMap: ExportPathMap) => { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ssgPages.forEach((page) => { |
|
|
if (isDynamicRoute(page)) { |
|
|
tbdPrerenderRoutes.push(page) |
|
|
|
|
|
if (ssgStaticFallbackPages.has(page)) { |
|
|
|
|
|
|
|
|
if (i18n) { |
|
|
defaultMap[`/${i18n.defaultLocale}${page}`] = { |
|
|
page, |
|
|
_pagesFallback: true, |
|
|
} |
|
|
} else { |
|
|
defaultMap[page] = { |
|
|
page, |
|
|
_pagesFallback: true, |
|
|
} |
|
|
} |
|
|
} else { |
|
|
|
|
|
|
|
|
delete defaultMap[page] |
|
|
} |
|
|
} |
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
additionalPaths.forEach((routes, page) => { |
|
|
routes.forEach((route) => { |
|
|
defaultMap[route.pathname] = { |
|
|
page, |
|
|
_ssgPath: route.encodedPathname, |
|
|
} |
|
|
}) |
|
|
}) |
|
|
|
|
|
if (useStaticPages404) { |
|
|
defaultMap['/404'] = { |
|
|
page: hasPages404 ? '/404' : '/_error', |
|
|
} |
|
|
} |
|
|
|
|
|
if (useDefaultStatic500) { |
|
|
defaultMap['/500'] = { |
|
|
page: '/_error', |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
staticPaths.forEach((routes, originalAppPath) => { |
|
|
const appConfig = appDefaultConfigs.get(originalAppPath) |
|
|
const isDynamicError = appConfig?.dynamic === 'error' |
|
|
|
|
|
const isRoutePPREnabled: boolean = appConfig |
|
|
? checkIsRoutePPREnabled(config.experimental.ppr, appConfig) |
|
|
: false |
|
|
|
|
|
routes.forEach((route) => { |
|
|
|
|
|
|
|
|
|
|
|
if ( |
|
|
route.fallbackRootParams && |
|
|
route.fallbackRootParams.length > 0 |
|
|
) { |
|
|
return |
|
|
} |
|
|
|
|
|
defaultMap[route.pathname] = { |
|
|
page: originalAppPath, |
|
|
_ssgPath: route.encodedPathname, |
|
|
_fallbackRouteParams: route.fallbackRouteParams, |
|
|
_isDynamicError: isDynamicError, |
|
|
_isAppDir: true, |
|
|
_isRoutePPREnabled: isRoutePPREnabled, |
|
|
_allowEmptyStaticShell: !route.throwOnEmptyStaticShell, |
|
|
} |
|
|
}) |
|
|
}) |
|
|
|
|
|
if (i18n) { |
|
|
for (const page of [ |
|
|
...staticPages, |
|
|
...ssgPages, |
|
|
...(useStaticPages404 ? ['/404'] : []), |
|
|
...(useDefaultStatic500 ? ['/500'] : []), |
|
|
]) { |
|
|
const isSsg = ssgPages.has(page) |
|
|
const isDynamic = isDynamicRoute(page) |
|
|
const isFallback = isSsg && ssgStaticFallbackPages.has(page) |
|
|
|
|
|
for (const locale of i18n.locales) { |
|
|
|
|
|
if (isSsg && isDynamic && !isFallback) continue |
|
|
const outputPath = `/${locale}${page === '/' ? '' : page}` |
|
|
|
|
|
defaultMap[outputPath] = { |
|
|
page: defaultMap[page]?.page || page, |
|
|
_locale: locale, |
|
|
_pagesFallback: isFallback, |
|
|
} |
|
|
} |
|
|
|
|
|
if (isSsg) { |
|
|
|
|
|
delete defaultMap[page] |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
return defaultMap |
|
|
}, |
|
|
} |
|
|
|
|
|
const outdir = path.join(distDir, 'export') |
|
|
const exportResult = await exportApp( |
|
|
dir, |
|
|
{ |
|
|
nextConfig: exportConfig, |
|
|
enabledDirectories, |
|
|
silent: true, |
|
|
buildExport: true, |
|
|
debugOutput, |
|
|
debugPrerender, |
|
|
pages: combinedPages, |
|
|
outdir, |
|
|
statusMessage: 'Generating static pages', |
|
|
numWorkers: getNumberOfWorkers(exportConfig), |
|
|
}, |
|
|
nextBuildSpan |
|
|
) |
|
|
|
|
|
|
|
|
if (!exportResult) return |
|
|
|
|
|
const getFallbackMode = (route: PrerenderedRoute) => { |
|
|
const hasEmptyStaticShell = exportResult.byPath.get( |
|
|
route.pathname |
|
|
)?.hasEmptyStaticShell |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ( |
|
|
hasEmptyStaticShell && |
|
|
!route.throwOnEmptyStaticShell && |
|
|
route.fallbackMode === FallbackMode.PRERENDER |
|
|
) { |
|
|
return FallbackMode.BLOCKING_STATIC_RENDER |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (!route.fallbackMode) { |
|
|
return FallbackMode.NOT_FOUND |
|
|
} |
|
|
|
|
|
return route.fallbackMode |
|
|
} |
|
|
|
|
|
const getCacheControl = ( |
|
|
exportPath: string, |
|
|
defaultRevalidate: Revalidate = false |
|
|
): CacheControl => { |
|
|
const cacheControl = |
|
|
exportResult.byPath.get(exportPath)?.cacheControl |
|
|
|
|
|
if (!cacheControl) { |
|
|
return { revalidate: defaultRevalidate, expire: undefined } |
|
|
} |
|
|
|
|
|
if ( |
|
|
cacheControl.revalidate !== false && |
|
|
cacheControl.revalidate > 0 && |
|
|
cacheControl.expire === undefined |
|
|
) { |
|
|
return { |
|
|
revalidate: cacheControl.revalidate, |
|
|
expire: config.expireTime, |
|
|
} |
|
|
} |
|
|
|
|
|
return cacheControl |
|
|
} |
|
|
|
|
|
if (debugOutput || process.env.NEXT_SSG_FETCH_METRICS === '1') { |
|
|
recordFetchMetrics(exportResult) |
|
|
} |
|
|
|
|
|
writeTurborepoAccessTraceResult({ |
|
|
distDir: config.distDir, |
|
|
traces: [ |
|
|
turborepoAccessTraceResult, |
|
|
...exportResult.turborepoAccessTraceResults.values(), |
|
|
], |
|
|
}) |
|
|
|
|
|
prerenderManifest.notFoundRoutes = Array.from( |
|
|
exportResult.ssgNotFoundPaths |
|
|
) |
|
|
|
|
|
|
|
|
for (const page of staticPages) { |
|
|
const serverBundle = getPagePath(page, distDir, undefined, false) |
|
|
await fs.unlink(serverBundle) |
|
|
} |
|
|
|
|
|
staticPaths.forEach((prerenderedRoutes, originalAppPath) => { |
|
|
const page = appNormalizedPaths.get(originalAppPath) |
|
|
if (!page) throw new InvariantError('Page not found') |
|
|
|
|
|
const appConfig = appDefaultConfigs.get(originalAppPath) |
|
|
if (!appConfig) throw new InvariantError('App config not found') |
|
|
|
|
|
let hasRevalidateZero = |
|
|
appConfig.revalidate === 0 || |
|
|
getCacheControl(page).revalidate === 0 |
|
|
|
|
|
if (hasRevalidateZero && pageInfos.get(page)?.isStatic) { |
|
|
|
|
|
|
|
|
pageInfos.set(page, { |
|
|
...(pageInfos.get(page) as PageInfo), |
|
|
isStatic: false, |
|
|
isSSG: false, |
|
|
}) |
|
|
} |
|
|
|
|
|
const isAppRouteHandler = isAppRouteRoute(originalAppPath) |
|
|
|
|
|
|
|
|
|
|
|
const isRoutePPREnabled: true | undefined = |
|
|
!isAppRouteHandler && |
|
|
checkIsRoutePPREnabled(config.experimental.ppr, appConfig) |
|
|
? true |
|
|
: undefined |
|
|
|
|
|
const htmlBotsRegexString = |
|
|
|
|
|
config.htmlLimitedBots || HTML_LIMITED_BOT_UA_RE_STRING |
|
|
|
|
|
|
|
|
|
|
|
const bypassFor: RouteHas[] = [ |
|
|
{ type: 'header', key: ACTION_HEADER }, |
|
|
{ |
|
|
type: 'header', |
|
|
key: 'content-type', |
|
|
value: 'multipart/form-data;.*', |
|
|
}, |
|
|
|
|
|
|
|
|
...(isRoutePPREnabled |
|
|
? [ |
|
|
{ |
|
|
type: 'header', |
|
|
key: 'user-agent', |
|
|
value: htmlBotsRegexString, |
|
|
}, |
|
|
] |
|
|
: []), |
|
|
] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const staticPrerenderedRoutes: PrerenderedRoute[] = [] |
|
|
const dynamicPrerenderedRoutes: PrerenderedRoute[] = [] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const unsortedUnknownPrerenderRoutes: PrerenderedRoute[] = [] |
|
|
const unsortedKnownPrerenderRoutes: PrerenderedRoute[] = [] |
|
|
for (const prerenderedRoute of prerenderedRoutes) { |
|
|
if ( |
|
|
prerenderedRoute.fallbackRouteParams && |
|
|
prerenderedRoute.fallbackRouteParams.length > 0 |
|
|
) { |
|
|
unsortedUnknownPrerenderRoutes.push(prerenderedRoute) |
|
|
} else { |
|
|
unsortedKnownPrerenderRoutes.push(prerenderedRoute) |
|
|
} |
|
|
} |
|
|
|
|
|
const sortedUnknownPrerenderRoutes = sortPageObjects( |
|
|
unsortedUnknownPrerenderRoutes, |
|
|
(prerenderedRoute) => prerenderedRoute.pathname |
|
|
) |
|
|
const sortedKnownPrerenderRoutes = sortPageObjects( |
|
|
unsortedKnownPrerenderRoutes, |
|
|
(prerenderedRoute) => prerenderedRoute.pathname |
|
|
) |
|
|
|
|
|
prerenderedRoutes = [ |
|
|
...sortedKnownPrerenderRoutes, |
|
|
...sortedUnknownPrerenderRoutes, |
|
|
] |
|
|
|
|
|
for (const prerenderedRoute of prerenderedRoutes) { |
|
|
|
|
|
|
|
|
if (prerenderedRoute.pathname === UNDERSCORE_NOT_FOUND_ROUTE) { |
|
|
continue |
|
|
} |
|
|
|
|
|
if ( |
|
|
isRoutePPREnabled && |
|
|
prerenderedRoute.fallbackRouteParams && |
|
|
prerenderedRoute.fallbackRouteParams.length > 0 |
|
|
) { |
|
|
|
|
|
|
|
|
dynamicPrerenderedRoutes.push(prerenderedRoute) |
|
|
} else { |
|
|
|
|
|
|
|
|
staticPrerenderedRoutes.push(prerenderedRoute) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
for (const route of staticPrerenderedRoutes) { |
|
|
if (isDynamicRoute(page) && route.pathname === page) continue |
|
|
if (route.pathname === UNDERSCORE_NOT_FOUND_ROUTE) continue |
|
|
|
|
|
const { |
|
|
metadata = {}, |
|
|
hasEmptyStaticShell, |
|
|
hasPostponed, |
|
|
} = exportResult.byPath.get(route.pathname) ?? {} |
|
|
|
|
|
const cacheControl = getCacheControl( |
|
|
route.pathname, |
|
|
appConfig.revalidate |
|
|
) |
|
|
|
|
|
pageInfos.set(route.pathname, { |
|
|
...(pageInfos.get(route.pathname) as PageInfo), |
|
|
hasPostponed, |
|
|
hasEmptyStaticShell, |
|
|
initialCacheControl: cacheControl, |
|
|
}) |
|
|
|
|
|
|
|
|
pageInfos.set(page, { |
|
|
...(pageInfos.get(page) as PageInfo), |
|
|
hasPostponed, |
|
|
hasEmptyStaticShell, |
|
|
initialCacheControl: cacheControl, |
|
|
}) |
|
|
|
|
|
if (cacheControl.revalidate !== 0) { |
|
|
const normalizedRoute = normalizePagePath(route.pathname) |
|
|
|
|
|
let dataRoute: string | null |
|
|
if (isAppRouteHandler) { |
|
|
dataRoute = null |
|
|
} else { |
|
|
dataRoute = path.posix.join(`${normalizedRoute}${RSC_SUFFIX}`) |
|
|
} |
|
|
|
|
|
let prefetchDataRoute: string | null | undefined |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!isAppRouteHandler && isAppPPREnabled) { |
|
|
prefetchDataRoute = path.posix.join( |
|
|
`${normalizedRoute}${RSC_PREFETCH_SUFFIX}` |
|
|
) |
|
|
} |
|
|
|
|
|
const meta = collectMeta(metadata) |
|
|
|
|
|
prerenderManifest.routes[route.pathname] = { |
|
|
initialStatus: meta.status, |
|
|
initialHeaders: meta.headers, |
|
|
renderingMode: isAppPPREnabled |
|
|
? isRoutePPREnabled |
|
|
? RenderingMode.PARTIALLY_STATIC |
|
|
: RenderingMode.STATIC |
|
|
: undefined, |
|
|
experimentalPPR: isRoutePPREnabled, |
|
|
experimentalBypassFor: bypassFor, |
|
|
initialRevalidateSeconds: cacheControl.revalidate, |
|
|
initialExpireSeconds: cacheControl.expire, |
|
|
srcRoute: page, |
|
|
dataRoute, |
|
|
prefetchDataRoute, |
|
|
allowHeader: ALLOWED_HEADERS, |
|
|
} |
|
|
} else { |
|
|
hasRevalidateZero = true |
|
|
|
|
|
|
|
|
pageInfos.set(route.pathname, { |
|
|
...(pageInfos.get(route.pathname) as PageInfo), |
|
|
isSSG: false, |
|
|
isStatic: false, |
|
|
}) |
|
|
} |
|
|
} |
|
|
|
|
|
if (!hasRevalidateZero && isDynamicRoute(page)) { |
|
|
|
|
|
|
|
|
|
|
|
if (!isRoutePPREnabled) { |
|
|
dynamicPrerenderedRoutes.push({ |
|
|
params: {}, |
|
|
pathname: page, |
|
|
encodedPathname: page, |
|
|
fallbackRouteParams: [], |
|
|
fallbackMode: |
|
|
fallbackModes.get(originalAppPath) ?? |
|
|
FallbackMode.NOT_FOUND, |
|
|
fallbackRootParams: [], |
|
|
throwOnEmptyStaticShell: true, |
|
|
}) |
|
|
} |
|
|
|
|
|
for (const route of dynamicPrerenderedRoutes) { |
|
|
const normalizedRoute = normalizePagePath(route.pathname) |
|
|
|
|
|
const metadata = exportResult.byPath.get( |
|
|
route.pathname |
|
|
)?.metadata |
|
|
|
|
|
const cacheControl = getCacheControl(route.pathname) |
|
|
|
|
|
let dataRoute: string | null = null |
|
|
if (!isAppRouteHandler) { |
|
|
dataRoute = path.posix.join(`${normalizedRoute}${RSC_SUFFIX}`) |
|
|
} |
|
|
|
|
|
let prefetchDataRoute: string | undefined |
|
|
let dynamicRoute = routesManifest.dynamicRoutes.find( |
|
|
(r) => r.page === route.pathname |
|
|
) |
|
|
if (!isAppRouteHandler && isAppPPREnabled) { |
|
|
prefetchDataRoute = path.posix.join( |
|
|
`${normalizedRoute}${RSC_PREFETCH_SUFFIX}` |
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!dynamicRoute) { |
|
|
dynamicRoute = pageToRoute(route.pathname, page) |
|
|
sourcePages.set(route.pathname, page) |
|
|
|
|
|
|
|
|
|
|
|
dynamicRoute.skipInternalRouting = true |
|
|
|
|
|
|
|
|
|
|
|
dynamicRoutes.push(dynamicRoute) |
|
|
} |
|
|
} |
|
|
|
|
|
if (!isAppRouteHandler && metadata?.segmentPaths) { |
|
|
|
|
|
|
|
|
|
|
|
if (!dynamicRoute) { |
|
|
dynamicRoute = dynamicRoutes.find((r) => r.page === page) |
|
|
|
|
|
|
|
|
if (!dynamicRoute) { |
|
|
throw new InvariantError('Dynamic route not found') |
|
|
} |
|
|
} |
|
|
|
|
|
if (metadata.segmentPaths) { |
|
|
const pageSegmentPath = metadata.segmentPaths.find((item) => |
|
|
item.endsWith('__PAGE__') |
|
|
) |
|
|
if (!pageSegmentPath) { |
|
|
throw new Error(`Invariant: missing __PAGE__ segmentPath`) |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const builtSegmentDataRoute = buildPrefetchSegmentDataRoute( |
|
|
route.pathname, |
|
|
pageSegmentPath |
|
|
) |
|
|
|
|
|
builtSegmentDataRoute.source = |
|
|
builtSegmentDataRoute.source.replace( |
|
|
'/__PAGE__\\.segment\\.rsc$', |
|
|
`(?<segment>/__PAGE__\\.segment\\.rsc|\\.segment\\.rsc)(?:/)?$` |
|
|
) |
|
|
builtSegmentDataRoute.destination = |
|
|
builtSegmentDataRoute.destination.replace( |
|
|
'/__PAGE__.segment.rsc', |
|
|
'$segment' |
|
|
) |
|
|
dynamicRoute.prefetchSegmentDataRoutes ??= [] |
|
|
dynamicRoute.prefetchSegmentDataRoutes.push( |
|
|
builtSegmentDataRoute |
|
|
) |
|
|
} |
|
|
} |
|
|
|
|
|
pageInfos.set(route.pathname, { |
|
|
...(pageInfos.get(route.pathname) as PageInfo), |
|
|
isDynamicAppRoute: true, |
|
|
|
|
|
|
|
|
hasPostponed: isRoutePPREnabled, |
|
|
}) |
|
|
|
|
|
const fallbackMode = getFallbackMode(route) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const fallbackCacheControl = |
|
|
isRoutePPREnabled && fallbackMode === FallbackMode.PRERENDER |
|
|
? cacheControl |
|
|
: undefined |
|
|
|
|
|
const fallback: Fallback = fallbackModeToFallbackField( |
|
|
fallbackMode, |
|
|
route.pathname |
|
|
) |
|
|
|
|
|
const meta = |
|
|
metadata && |
|
|
isRoutePPREnabled && |
|
|
fallbackMode === FallbackMode.PRERENDER |
|
|
? collectMeta(metadata) |
|
|
: {} |
|
|
|
|
|
prerenderManifest.dynamicRoutes[route.pathname] = { |
|
|
experimentalPPR: isRoutePPREnabled, |
|
|
renderingMode: isAppPPREnabled |
|
|
? isRoutePPREnabled |
|
|
? RenderingMode.PARTIALLY_STATIC |
|
|
: RenderingMode.STATIC |
|
|
: undefined, |
|
|
experimentalBypassFor: bypassFor, |
|
|
routeRegex: normalizeRouteRegex( |
|
|
getNamedRouteRegex(route.pathname, { |
|
|
prefixRouteKeys: false, |
|
|
}).re.source |
|
|
), |
|
|
dataRoute, |
|
|
fallback, |
|
|
fallbackRevalidate: fallbackCacheControl?.revalidate, |
|
|
fallbackExpire: fallbackCacheControl?.expire, |
|
|
fallbackStatus: meta.status, |
|
|
fallbackHeaders: meta.headers, |
|
|
fallbackRootParams: fallback |
|
|
? route.fallbackRootParams |
|
|
: undefined, |
|
|
fallbackSourceRoute: route.fallbackRouteParams?.length |
|
|
? page |
|
|
: undefined, |
|
|
dataRouteRegex: !dataRoute |
|
|
? null |
|
|
: normalizeRouteRegex( |
|
|
getNamedRouteRegex(dataRoute, { |
|
|
prefixRouteKeys: false, |
|
|
includeSuffix: true, |
|
|
excludeOptionalTrailingSlash: true, |
|
|
}).re.source |
|
|
), |
|
|
prefetchDataRoute, |
|
|
prefetchDataRouteRegex: !prefetchDataRoute |
|
|
? undefined |
|
|
: normalizeRouteRegex( |
|
|
getNamedRouteRegex(prefetchDataRoute, { |
|
|
prefixRouteKeys: false, |
|
|
includeSuffix: true, |
|
|
excludeOptionalTrailingSlash: true, |
|
|
}).re.source |
|
|
), |
|
|
allowHeader: ALLOWED_HEADERS, |
|
|
} |
|
|
} |
|
|
} |
|
|
}) |
|
|
|
|
|
const moveExportedPage = async ( |
|
|
originPage: string, |
|
|
page: string, |
|
|
file: string, |
|
|
isSsg: boolean, |
|
|
ext: 'html' | 'json', |
|
|
additionalSsgFile = false |
|
|
) => { |
|
|
return staticGenerationSpan |
|
|
.traceChild('move-exported-page') |
|
|
.traceAsyncFn(async () => { |
|
|
file = `${file}.${ext}` |
|
|
const orig = path.join(outdir, file) |
|
|
const pagePath = getPagePath( |
|
|
originPage, |
|
|
distDir, |
|
|
undefined, |
|
|
false |
|
|
) |
|
|
|
|
|
const relativeDest = path |
|
|
.relative( |
|
|
path.join(distDir, SERVER_DIRECTORY), |
|
|
path.join( |
|
|
path.join( |
|
|
pagePath, |
|
|
|
|
|
|
|
|
originPage |
|
|
.slice(1) |
|
|
.split('/') |
|
|
.map(() => '..') |
|
|
.join('/') |
|
|
), |
|
|
file |
|
|
) |
|
|
) |
|
|
.replace(/\\/g, '/') |
|
|
|
|
|
if ( |
|
|
!isSsg && |
|
|
!( |
|
|
|
|
|
|
|
|
( |
|
|
STATIC_STATUS_PAGES.includes(page) && |
|
|
!usedStaticStatusPages.includes(page) |
|
|
) |
|
|
) |
|
|
) { |
|
|
pagesManifest[page] = relativeDest |
|
|
} |
|
|
|
|
|
const dest = path.join(distDir, SERVER_DIRECTORY, relativeDest) |
|
|
const isNotFound = |
|
|
prerenderManifest.notFoundRoutes.includes(page) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ((!i18n || additionalSsgFile) && !isNotFound) { |
|
|
await fs.mkdir(path.dirname(dest), { recursive: true }) |
|
|
await fs.rename(orig, dest) |
|
|
} else if (i18n && !isSsg) { |
|
|
|
|
|
|
|
|
delete pagesManifest[page] |
|
|
} |
|
|
|
|
|
if (i18n) { |
|
|
if (additionalSsgFile) return |
|
|
|
|
|
const localeExt = page === '/' ? path.extname(file) : '' |
|
|
const relativeDestNoPages = relativeDest.slice( |
|
|
'pages/'.length |
|
|
) |
|
|
|
|
|
for (const locale of i18n.locales) { |
|
|
const curPath = `/${locale}${page === '/' ? '' : page}` |
|
|
|
|
|
if ( |
|
|
isSsg && |
|
|
prerenderManifest.notFoundRoutes.includes(curPath) |
|
|
) { |
|
|
continue |
|
|
} |
|
|
|
|
|
const updatedRelativeDest = path |
|
|
.join( |
|
|
'pages', |
|
|
locale + localeExt, |
|
|
|
|
|
|
|
|
page === '/' ? '' : relativeDestNoPages |
|
|
) |
|
|
.replace(/\\/g, '/') |
|
|
|
|
|
const updatedOrig = path.join( |
|
|
outdir, |
|
|
locale + localeExt, |
|
|
page === '/' ? '' : file |
|
|
) |
|
|
const updatedDest = path.join( |
|
|
distDir, |
|
|
SERVER_DIRECTORY, |
|
|
updatedRelativeDest |
|
|
) |
|
|
|
|
|
if (!isSsg) { |
|
|
pagesManifest[curPath] = updatedRelativeDest |
|
|
} |
|
|
await fs.mkdir(path.dirname(updatedDest), { |
|
|
recursive: true, |
|
|
}) |
|
|
await fs.rename(updatedOrig, updatedDest) |
|
|
} |
|
|
} |
|
|
}) |
|
|
} |
|
|
|
|
|
async function moveExportedAppNotFoundTo404() { |
|
|
return staticGenerationSpan |
|
|
.traceChild('move-exported-app-not-found-') |
|
|
.traceAsyncFn(async () => { |
|
|
const orig = path.join( |
|
|
distDir, |
|
|
'server', |
|
|
'app', |
|
|
'_not-found.html' |
|
|
) |
|
|
const updatedRelativeDest = path |
|
|
.join('pages', '404.html') |
|
|
.replace(/\\/g, '/') |
|
|
|
|
|
if (existsSync(orig)) { |
|
|
await fs.copyFile( |
|
|
orig, |
|
|
path.join(distDir, 'server', updatedRelativeDest) |
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
if (i18n) { |
|
|
for (const locale of i18n.locales) { |
|
|
const curPath = `/${locale}/404` |
|
|
pagesManifest[curPath] = updatedRelativeDest |
|
|
} |
|
|
} |
|
|
|
|
|
pagesManifest['/404'] = updatedRelativeDest |
|
|
} |
|
|
}) |
|
|
} |
|
|
|
|
|
|
|
|
if (hasStaticApp404) { |
|
|
await moveExportedAppNotFoundTo404() |
|
|
} else { |
|
|
|
|
|
if (!hasPages404 && !hasApp404 && useStaticPages404) { |
|
|
await moveExportedPage('/_error', '/404', '/404', false, 'html') |
|
|
} |
|
|
} |
|
|
|
|
|
if (useDefaultStatic500) { |
|
|
await moveExportedPage('/_error', '/500', '/500', false, 'html') |
|
|
} |
|
|
|
|
|
for (const page of combinedPages) { |
|
|
const isSsg = ssgPages.has(page) |
|
|
const isStaticSsgFallback = ssgStaticFallbackPages.has(page) |
|
|
const isDynamic = isDynamicRoute(page) |
|
|
const hasAmp = hybridAmpPages.has(page) |
|
|
const file = normalizePagePath(page) |
|
|
|
|
|
const pageInfo = pageInfos.get(page) |
|
|
const durationInfo = exportResult.byPage.get(page) |
|
|
if (pageInfo && durationInfo) { |
|
|
|
|
|
if (pageInfo.ssgPageRoutes) { |
|
|
pageInfo.ssgPageDurations = pageInfo.ssgPageRoutes.map( |
|
|
(pagePath) => { |
|
|
const duration = durationInfo.durationsByPath.get(pagePath) |
|
|
if (typeof duration === 'undefined') { |
|
|
throw new Error("Invariant: page wasn't built") |
|
|
} |
|
|
|
|
|
return duration |
|
|
} |
|
|
) |
|
|
} |
|
|
pageInfo.pageDuration = durationInfo.durationsByPath.get(page) |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const hasHtmlOutput = !(isSsg && isDynamic && !isStaticSsgFallback) |
|
|
|
|
|
if (hasHtmlOutput) { |
|
|
await moveExportedPage(page, page, file, isSsg, 'html') |
|
|
} |
|
|
|
|
|
if (hasAmp && (!isSsg || (isSsg && !isDynamic))) { |
|
|
const ampPage = `${file}.amp` |
|
|
await moveExportedPage(page, ampPage, ampPage, isSsg, 'html') |
|
|
|
|
|
if (isSsg) { |
|
|
await moveExportedPage(page, ampPage, ampPage, isSsg, 'json') |
|
|
} |
|
|
} |
|
|
|
|
|
if (isSsg) { |
|
|
|
|
|
|
|
|
if (!isDynamic) { |
|
|
await moveExportedPage(page, page, file, isSsg, 'json') |
|
|
|
|
|
if (i18n) { |
|
|
|
|
|
for (const locale of i18n.locales) { |
|
|
const localePage = `/${locale}${page === '/' ? '' : page}` |
|
|
|
|
|
const cacheControl = getCacheControl(localePage) |
|
|
|
|
|
prerenderManifest.routes[localePage] = { |
|
|
initialRevalidateSeconds: cacheControl.revalidate, |
|
|
initialExpireSeconds: cacheControl.expire, |
|
|
experimentalPPR: undefined, |
|
|
renderingMode: undefined, |
|
|
srcRoute: null, |
|
|
dataRoute: path.posix.join( |
|
|
'/_next/data', |
|
|
buildId, |
|
|
`${file}.json` |
|
|
), |
|
|
prefetchDataRoute: undefined, |
|
|
allowHeader: ALLOWED_HEADERS, |
|
|
} |
|
|
} |
|
|
} else { |
|
|
const cacheControl = getCacheControl(page) |
|
|
|
|
|
prerenderManifest.routes[page] = { |
|
|
initialRevalidateSeconds: cacheControl.revalidate, |
|
|
initialExpireSeconds: cacheControl.expire, |
|
|
experimentalPPR: undefined, |
|
|
renderingMode: undefined, |
|
|
srcRoute: null, |
|
|
dataRoute: path.posix.join( |
|
|
'/_next/data', |
|
|
buildId, |
|
|
`${file}.json` |
|
|
), |
|
|
|
|
|
prefetchDataRoute: undefined, |
|
|
allowHeader: ALLOWED_HEADERS, |
|
|
} |
|
|
} |
|
|
if (pageInfo) { |
|
|
pageInfo.initialCacheControl = getCacheControl(page) |
|
|
} |
|
|
} else { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (const route of additionalPaths.get(page) ?? []) { |
|
|
const pageFile = normalizePagePath(route.pathname) |
|
|
await moveExportedPage( |
|
|
page, |
|
|
route.pathname, |
|
|
pageFile, |
|
|
isSsg, |
|
|
'html', |
|
|
true |
|
|
) |
|
|
await moveExportedPage( |
|
|
page, |
|
|
route.pathname, |
|
|
pageFile, |
|
|
isSsg, |
|
|
'json', |
|
|
true |
|
|
) |
|
|
|
|
|
if (hasAmp) { |
|
|
const ampPage = `${pageFile}.amp` |
|
|
await moveExportedPage( |
|
|
page, |
|
|
ampPage, |
|
|
ampPage, |
|
|
isSsg, |
|
|
'html', |
|
|
true |
|
|
) |
|
|
await moveExportedPage( |
|
|
page, |
|
|
ampPage, |
|
|
ampPage, |
|
|
isSsg, |
|
|
'json', |
|
|
true |
|
|
) |
|
|
} |
|
|
|
|
|
const cacheControl = getCacheControl(route.pathname) |
|
|
|
|
|
prerenderManifest.routes[route.pathname] = { |
|
|
initialRevalidateSeconds: cacheControl.revalidate, |
|
|
initialExpireSeconds: cacheControl.expire, |
|
|
experimentalPPR: undefined, |
|
|
renderingMode: undefined, |
|
|
srcRoute: page, |
|
|
dataRoute: path.posix.join( |
|
|
'/_next/data', |
|
|
buildId, |
|
|
`${normalizePagePath(route.pathname)}.json` |
|
|
), |
|
|
|
|
|
prefetchDataRoute: undefined, |
|
|
allowHeader: ALLOWED_HEADERS, |
|
|
} |
|
|
|
|
|
if (pageInfo) { |
|
|
pageInfo.initialCacheControl = cacheControl |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
await fs.rm(outdir, { recursive: true, force: true }) |
|
|
await writeManifest(pagesManifestPath, pagesManifest) |
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
routesManifest.dynamicRoutes = sortSortableRouteObjects( |
|
|
dynamicRoutes, |
|
|
(route) => ({ |
|
|
|
|
|
|
|
|
sourcePage: sourcePages.get(route.page) ?? route.page, |
|
|
page: route.page, |
|
|
}) |
|
|
) |
|
|
|
|
|
|
|
|
await nextBuildSpan |
|
|
.traceChild('write-routes-manifest') |
|
|
.traceAsyncFn(() => writeManifest(routesManifestPath, routesManifest)) |
|
|
} |
|
|
|
|
|
const postBuildSpinner = createSpinner('Finalizing page optimization') |
|
|
let buildTracesSpinner = createSpinner(`Collecting build traces`) |
|
|
|
|
|
|
|
|
worker.end() |
|
|
|
|
|
const analysisEnd = process.hrtime(analysisBegin) |
|
|
telemetry.record( |
|
|
eventBuildOptimize(pagesPaths, { |
|
|
durationInSeconds: analysisEnd[0], |
|
|
staticPageCount: staticPages.size, |
|
|
staticPropsPageCount: ssgPages.size, |
|
|
serverPropsPageCount: serverPropsPages.size, |
|
|
ssrPageCount: |
|
|
pagesPaths.length - |
|
|
(staticPages.size + ssgPages.size + serverPropsPages.size), |
|
|
hasStatic404: useStaticPages404, |
|
|
hasReportWebVitals: |
|
|
namedExports?.includes('reportWebVitals') ?? false, |
|
|
rewritesCount: combinedRewrites.length, |
|
|
headersCount: headers.length, |
|
|
redirectsCount: redirects.length - 1, |
|
|
headersWithHasCount: headers.filter((r: any) => !!r.has).length, |
|
|
rewritesWithHasCount: combinedRewrites.filter((r: any) => !!r.has) |
|
|
.length, |
|
|
redirectsWithHasCount: redirects.filter((r: any) => !!r.has).length, |
|
|
middlewareCount: hasMiddlewareFile ? 1 : 0, |
|
|
totalAppPagesCount, |
|
|
staticAppPagesCount, |
|
|
serverAppPagesCount, |
|
|
edgeRuntimeAppCount, |
|
|
edgeRuntimePagesCount, |
|
|
}) |
|
|
) |
|
|
|
|
|
if (NextBuildContext.telemetryState) { |
|
|
const events = eventBuildFeatureUsage( |
|
|
NextBuildContext.telemetryState.usages |
|
|
) |
|
|
telemetry.record(events) |
|
|
telemetry.record( |
|
|
eventPackageUsedInGetServerSideProps( |
|
|
NextBuildContext.telemetryState.packagesUsedInServerSideProps |
|
|
) |
|
|
) |
|
|
const useCacheTracker = NextBuildContext.telemetryState.useCacheTracker |
|
|
|
|
|
for (const [key, value] of Object.entries(useCacheTracker)) { |
|
|
telemetry.record( |
|
|
eventBuildFeatureUsage([ |
|
|
{ |
|
|
featureName: key as UseCacheTrackerKey, |
|
|
invocationCount: value, |
|
|
}, |
|
|
]) |
|
|
) |
|
|
} |
|
|
} |
|
|
|
|
|
if (ssgPages.size > 0 || appDir) { |
|
|
tbdPrerenderRoutes.forEach((tbdRoute) => { |
|
|
const normalizedRoute = normalizePagePath(tbdRoute) |
|
|
const dataRoute = path.posix.join( |
|
|
'/_next/data', |
|
|
buildId, |
|
|
`${normalizedRoute}.json` |
|
|
) |
|
|
|
|
|
prerenderManifest.dynamicRoutes[tbdRoute] = { |
|
|
routeRegex: normalizeRouteRegex( |
|
|
getNamedRouteRegex(tbdRoute, { |
|
|
prefixRouteKeys: false, |
|
|
}).re.source |
|
|
), |
|
|
experimentalPPR: undefined, |
|
|
renderingMode: undefined, |
|
|
dataRoute, |
|
|
fallback: ssgBlockingFallbackPages.has(tbdRoute) |
|
|
? null |
|
|
: ssgStaticFallbackPages.has(tbdRoute) |
|
|
? `${normalizedRoute}.html` |
|
|
: false, |
|
|
fallbackRevalidate: undefined, |
|
|
fallbackExpire: undefined, |
|
|
fallbackSourceRoute: undefined, |
|
|
fallbackRootParams: undefined, |
|
|
dataRouteRegex: normalizeRouteRegex( |
|
|
getNamedRouteRegex(dataRoute, { |
|
|
prefixRouteKeys: true, |
|
|
includeSuffix: true, |
|
|
excludeOptionalTrailingSlash: true, |
|
|
}).re.source |
|
|
), |
|
|
|
|
|
prefetchDataRoute: undefined, |
|
|
prefetchDataRouteRegex: undefined, |
|
|
allowHeader: ALLOWED_HEADERS, |
|
|
} |
|
|
}) |
|
|
|
|
|
NextBuildContext.previewModeId = previewProps.previewModeId |
|
|
NextBuildContext.fetchCacheKeyPrefix = |
|
|
config.experimental.fetchCacheKeyPrefix |
|
|
NextBuildContext.allowedRevalidateHeaderKeys = |
|
|
config.experimental.allowedRevalidateHeaderKeys |
|
|
|
|
|
await writePrerenderManifest(distDir, prerenderManifest) |
|
|
await writeClientSsgManifest(prerenderManifest, { |
|
|
distDir, |
|
|
buildId, |
|
|
locales: config.i18n?.locales, |
|
|
}) |
|
|
} else { |
|
|
await writePrerenderManifest(distDir, { |
|
|
version: 4, |
|
|
routes: {}, |
|
|
dynamicRoutes: {}, |
|
|
preview: previewProps, |
|
|
notFoundRoutes: [], |
|
|
}) |
|
|
} |
|
|
|
|
|
await writeImagesManifest(distDir, config) |
|
|
await writeManifest(path.join(distDir, EXPORT_MARKER), { |
|
|
version: 1, |
|
|
hasExportPathMap: typeof config.exportPathMap === 'function', |
|
|
exportTrailingSlash: config.trailingSlash === true, |
|
|
isNextImageImported: isNextImageImported === true, |
|
|
}) |
|
|
await fs.unlink(path.join(distDir, EXPORT_DETAIL)).catch((err) => { |
|
|
if (err.code === 'ENOENT') { |
|
|
return Promise.resolve() |
|
|
} |
|
|
return Promise.reject(err) |
|
|
}) |
|
|
|
|
|
if (Boolean(config.experimental.nextScriptWorkers)) { |
|
|
await nextBuildSpan |
|
|
.traceChild('verify-partytown-setup') |
|
|
.traceAsyncFn(async () => { |
|
|
await verifyPartytownSetup( |
|
|
dir, |
|
|
path.join(distDir, CLIENT_STATIC_FILES_PATH) |
|
|
) |
|
|
}) |
|
|
} |
|
|
|
|
|
await buildTracesPromise |
|
|
|
|
|
if (buildTracesSpinner) { |
|
|
buildTracesSpinner.stopAndPersist() |
|
|
buildTracesSpinner = undefined |
|
|
} |
|
|
|
|
|
if (isCompileMode) { |
|
|
Log.info( |
|
|
`Build ran with "compile" mode, to finalize the build run either "generate" or "generate-env" mode as well` |
|
|
) |
|
|
} |
|
|
|
|
|
if (config.output === 'export') { |
|
|
await writeFullyStaticExport( |
|
|
config, |
|
|
dir, |
|
|
enabledDirectories, |
|
|
configOutDir, |
|
|
nextBuildSpan |
|
|
) |
|
|
} |
|
|
|
|
|
if (config.experimental.adapterPath) { |
|
|
await handleBuildComplete({ |
|
|
dir, |
|
|
distDir, |
|
|
tracingRoot: outputFileTracingRoot, |
|
|
hasNodeMiddleware, |
|
|
hasInstrumentationHook, |
|
|
adapterPath: config.experimental.adapterPath, |
|
|
pageKeys: pageKeys.pages, |
|
|
appPageKeys: denormalizedAppPages, |
|
|
routesManifest, |
|
|
prerenderManifest, |
|
|
middlewareManifest, |
|
|
functionsConfigManifest, |
|
|
requiredServerFiles: requiredServerFilesManifest.files, |
|
|
}) |
|
|
} |
|
|
|
|
|
if (config.output === 'standalone') { |
|
|
await writeStandaloneDirectory( |
|
|
nextBuildSpan, |
|
|
distDir, |
|
|
pageKeys, |
|
|
denormalizedAppPages, |
|
|
outputFileTracingRoot, |
|
|
requiredServerFilesManifest, |
|
|
middlewareManifest, |
|
|
hasNodeMiddleware, |
|
|
hasInstrumentationHook, |
|
|
staticPages, |
|
|
loadedEnvFiles, |
|
|
appDir |
|
|
) |
|
|
} |
|
|
|
|
|
if (postBuildSpinner) postBuildSpinner.stopAndPersist() |
|
|
console.log() |
|
|
|
|
|
if (debugOutput) { |
|
|
nextBuildSpan |
|
|
.traceChild('print-custom-routes') |
|
|
.traceFn(() => printCustomRoutes({ redirects, rewrites, headers })) |
|
|
} |
|
|
|
|
|
await nextBuildSpan.traceChild('print-tree-view').traceAsyncFn(() => |
|
|
printTreeView(pageKeys, pageInfos, { |
|
|
distPath: distDir, |
|
|
buildId: buildId, |
|
|
pagesDir, |
|
|
useStaticPages404, |
|
|
pageExtensions: config.pageExtensions, |
|
|
appBuildManifest, |
|
|
buildManifest, |
|
|
middlewareManifest, |
|
|
gzipSize: config.experimental.gzipSize, |
|
|
}) |
|
|
) |
|
|
|
|
|
await nextBuildSpan |
|
|
.traceChild('telemetry-flush') |
|
|
.traceAsyncFn(() => telemetry.flush()) |
|
|
|
|
|
await shutdownPromise |
|
|
}) |
|
|
} catch (e) { |
|
|
const telemetry: Telemetry | undefined = traceGlobals.get('telemetry') |
|
|
if (telemetry) { |
|
|
telemetry.record( |
|
|
eventBuildFailed({ |
|
|
bundler: getBundlerForTelemetry(isTurbopack), |
|
|
errorCode: getErrorCodeForTelemetry(e), |
|
|
durationInSeconds: Math.floor((Date.now() - buildStartTime) / 1000), |
|
|
}) |
|
|
) |
|
|
} |
|
|
throw e |
|
|
} finally { |
|
|
|
|
|
await lockfilePatchPromise.cur |
|
|
|
|
|
if (isTurbopack && !process.env.__NEXT_TEST_MODE) { |
|
|
warnAboutTurbopackBuilds(loadedConfig) |
|
|
} |
|
|
|
|
|
|
|
|
await flushAllTraces() |
|
|
teardownTraceSubscriber() |
|
|
|
|
|
if (traceUploadUrl && loadedConfig) { |
|
|
uploadTrace({ |
|
|
traceUploadUrl, |
|
|
mode: 'build', |
|
|
projectDir: dir, |
|
|
distDir: loadedConfig.distDir, |
|
|
isTurboSession: isTurbopack, |
|
|
sync: true, |
|
|
}) |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
function errorFromUnsupportedSegmentConfig(): never { |
|
|
Log.error( |
|
|
`Invalid segment configuration export detected. This can cause unexpected behavior from the configs not being applied. You should see the relevant failures in the logs above. Please fix them to continue.` |
|
|
) |
|
|
process.exit(1) |
|
|
} |
|
|
|
|
|
function warnAboutTurbopackBuilds(config?: NextConfigComplete) { |
|
|
let warningStr = |
|
|
`Support for Turbopack builds is experimental. ` + |
|
|
bold( |
|
|
`We don't recommend deploying mission-critical applications to production.` |
|
|
) |
|
|
warningStr += |
|
|
'\n\n- ' + |
|
|
bold( |
|
|
'Turbopack currently always builds production source maps for the browser. This will include project source code if deployed to production.' |
|
|
) |
|
|
warningStr += |
|
|
'\n- It is expected that your bundle size might be different from `next build` with webpack. This will be improved as we work towards stability.' |
|
|
|
|
|
if (!config?.experimental.turbopackPersistentCaching) { |
|
|
warningStr += |
|
|
'\n- This build is without disk caching; subsequent builds will become faster when disk caching becomes available.' |
|
|
} |
|
|
|
|
|
warningStr += |
|
|
'\n- When comparing output to webpack builds, make sure to first clear the Next.js cache by deleting the `.next` directory.' |
|
|
warningStr += |
|
|
'\n\nProvide feedback for Turbopack builds at https://github.com/vercel/next.js/discussions/77721' |
|
|
|
|
|
Log.warn(warningStr) |
|
|
} |
|
|
|
|
|
function getBundlerForTelemetry(isTurbopack: boolean) { |
|
|
if (isTurbopack) { |
|
|
return 'turbopack' |
|
|
} |
|
|
|
|
|
if (process.env.NEXT_RSPACK) { |
|
|
return 'rspack' |
|
|
} |
|
|
|
|
|
return 'webpack' |
|
|
} |
|
|
|
|
|
function getErrorCodeForTelemetry(err: unknown) { |
|
|
const code = extractNextErrorCode(err) |
|
|
if (code != null) { |
|
|
return code |
|
|
} |
|
|
|
|
|
if (err instanceof Error && 'code' in err && typeof err.code === 'string') { |
|
|
return err.code |
|
|
} |
|
|
|
|
|
if (err instanceof Error) { |
|
|
return err.name |
|
|
} |
|
|
|
|
|
return 'Unknown' |
|
|
} |
|
|
|