From 4d72befc6cba86617ae63778772e6984ce2bd64d Mon Sep 17 00:00:00 2001 From: Tim Neutkens Date: Fri, 15 Dec 2023 16:42:23 +0100 Subject: [PATCH 1/4] Reduce amount of data passed to collectBuildTraces --- .../next/src/build/collect-build-traces.ts | 14 +++----------- packages/next/src/build/index.ts | 6 +++--- packages/next/src/build/utils.ts | 19 ++++++++++++------- 3 files changed, 18 insertions(+), 21 deletions(-) diff --git a/packages/next/src/build/collect-build-traces.ts b/packages/next/src/build/collect-build-traces.ts index 87ca40ece1752..d2c789841ee8a 100644 --- a/packages/next/src/build/collect-build-traces.ts +++ b/packages/next/src/build/collect-build-traces.ts @@ -14,11 +14,6 @@ import { import path from 'path' import fs from 'fs/promises' -import { - deserializePageInfos, - type PageInfos, - type SerializedPageInfos, -} from './utils' import { loadBindings } from './swc' import { nonNullable } from '../lib/non-nullable' import * as ciEnvironment from '../telemetry/ci-info' @@ -71,7 +66,7 @@ export async function collectBuildTraces({ dir, config, distDir, - pageInfos, + edgeRuntimeRoutes, staticPages, nextBuildSpan = new Span({ name: 'build' }), hasSsrAmpPages, @@ -84,7 +79,7 @@ export async function collectBuildTraces({ hasSsrAmpPages: boolean outputFileTracingRoot: string // pageInfos is serialized when this function runs in a worker. - pageInfos: PageInfos | SerializedPageInfos + edgeRuntimeRoutes: string[] nextBuildSpan?: Span config: NextConfigComplete buildTraceContext?: BuildTraceContext @@ -628,8 +623,6 @@ export async function collectBuildTraces({ } const { entryNameFilesMap } = buildTraceContext?.chunksTrace || {} - const infos = - pageInfos instanceof Map ? pageInfos : deserializePageInfos(pageInfos) await Promise.all( [ @@ -650,8 +643,7 @@ export async function collectBuildTraces({ } // edge routes have no trace files - const pageInfo = infos.get(route) - if (pageInfo?.runtime === 'edge') { + if (edgeRuntimeRoutes.includes(route)) { return } diff --git a/packages/next/src/build/index.ts b/packages/next/src/build/index.ts index 45da66f31ee95..c6a44d7359a38 100644 --- a/packages/next/src/build/index.ts +++ b/packages/next/src/build/index.ts @@ -114,7 +114,7 @@ import { copyTracedFiles, isReservedPage, isAppBuiltinNotFoundPage, - serializePageInfos, + collectRoutesUsingEdgeRuntime, } from './utils' import type { PageInfo, PageInfos, AppConfig } from './utils' import { writeBuildId } from './write-build-id' @@ -1134,7 +1134,7 @@ export default async function build( config, distDir, // Serialize Map as this is sent to the worker. - pageInfos: serializePageInfos(new Map()), + edgeRuntimeRoutes: collectRoutesUsingEdgeRuntime(new Map()), staticPages: [], hasSsrAmpPages: false, buildTraceContext, @@ -1948,7 +1948,7 @@ export default async function build( dir, config, distDir, - pageInfos, + edgeRuntimeRoutes: collectRoutesUsingEdgeRuntime(pageInfos), staticPages: [...staticPages], nextBuildSpan, hasSsrAmpPages, diff --git a/packages/next/src/build/utils.ts b/packages/next/src/build/utils.ts index 8a7d94b35cc63..889e8f5819e3d 100644 --- a/packages/next/src/build/utils.ts +++ b/packages/next/src/build/utils.ts @@ -345,14 +345,19 @@ export interface PageInfo { export type PageInfos = Map -export type SerializedPageInfos = [string, PageInfo][] - -export function serializePageInfos(input: PageInfos): SerializedPageInfos { - return Array.from(input.entries()) -} +export type RoutesUsingEdgeRuntime = string[] + +export function collectRoutesUsingEdgeRuntime( + input: PageInfos +): RoutesUsingEdgeRuntime { + const routesUsingEdgeRuntime = [] + for (const [route, info] of input.entries()) { + if (isEdgeRuntime(info.runtime)) { + routesUsingEdgeRuntime.push(route) + } + } -export function deserializePageInfos(input: SerializedPageInfos): PageInfos { - return new Map(input) + return routesUsingEdgeRuntime } export async function printTreeView( From dd7cd63b12e3a339f75bafe34da10bc1e0bef501 Mon Sep 17 00:00:00 2001 From: Tim Neutkens Date: Fri, 15 Dec 2023 16:45:42 +0100 Subject: [PATCH 2/4] Fix type --- packages/next/src/build/collect-build-traces.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/next/src/build/collect-build-traces.ts b/packages/next/src/build/collect-build-traces.ts index d2c789841ee8a..2c9290b0c3239 100644 --- a/packages/next/src/build/collect-build-traces.ts +++ b/packages/next/src/build/collect-build-traces.ts @@ -25,6 +25,7 @@ import { normalizePagePath } from '../shared/lib/page-path/normalize-page-path' import { normalizeAppPath } from '../shared/lib/router/utils/app-paths' import isError from '../lib/is-error' import type { NodeFileTraceReasons } from '@vercel/nft' +import type { RoutesUsingEdgeRuntime } from './worker' const debug = debugOriginal('next:build:build-traces') @@ -79,7 +80,7 @@ export async function collectBuildTraces({ hasSsrAmpPages: boolean outputFileTracingRoot: string // pageInfos is serialized when this function runs in a worker. - edgeRuntimeRoutes: string[] + edgeRuntimeRoutes: RoutesUsingEdgeRuntime nextBuildSpan?: Span config: NextConfigComplete buildTraceContext?: BuildTraceContext From 4b9a368683471e3fd83316ed18d540e849dfc0a7 Mon Sep 17 00:00:00 2001 From: Tim Neutkens Date: Mon, 1 Jan 2024 17:04:59 +0100 Subject: [PATCH 3/4] Apply suggestion --- packages/next/src/build/collect-build-traces.ts | 2 +- packages/next/src/build/utils.ts | 8 +++++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/packages/next/src/build/collect-build-traces.ts b/packages/next/src/build/collect-build-traces.ts index 2c9290b0c3239..115c3661a5f34 100644 --- a/packages/next/src/build/collect-build-traces.ts +++ b/packages/next/src/build/collect-build-traces.ts @@ -644,7 +644,7 @@ export async function collectBuildTraces({ } // edge routes have no trace files - if (edgeRuntimeRoutes.includes(route)) { + if (edgeRuntimeRoutes.hasOwnProperty(route)) { return } diff --git a/packages/next/src/build/utils.ts b/packages/next/src/build/utils.ts index 889e8f5819e3d..6f7fc154a9d7c 100644 --- a/packages/next/src/build/utils.ts +++ b/packages/next/src/build/utils.ts @@ -345,15 +345,17 @@ export interface PageInfo { export type PageInfos = Map -export type RoutesUsingEdgeRuntime = string[] +export interface RoutesUsingEdgeRuntime { + [route: string]: 0 +} export function collectRoutesUsingEdgeRuntime( input: PageInfos ): RoutesUsingEdgeRuntime { - const routesUsingEdgeRuntime = [] + const routesUsingEdgeRuntime: RoutesUsingEdgeRuntime = {} for (const [route, info] of input.entries()) { if (isEdgeRuntime(info.runtime)) { - routesUsingEdgeRuntime.push(route) + routesUsingEdgeRuntime[route] = 0 } } From b8a49d1b31d3e1d68107a43b9e2ea127d803ab14 Mon Sep 17 00:00:00 2001 From: Tim Neutkens Date: Mon, 15 Apr 2024 10:21:59 +0200 Subject: [PATCH 4/4] Fix type import --- packages/next/src/build/collect-build-traces.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/next/src/build/collect-build-traces.ts b/packages/next/src/build/collect-build-traces.ts index 2a86b9477b0ab..86cdcc49336b4 100644 --- a/packages/next/src/build/collect-build-traces.ts +++ b/packages/next/src/build/collect-build-traces.ts @@ -25,7 +25,7 @@ import { normalizePagePath } from '../shared/lib/page-path/normalize-page-path' import { normalizeAppPath } from '../shared/lib/router/utils/app-paths' import isError from '../lib/is-error' import type { NodeFileTraceReasons } from '@vercel/nft' -import type { RoutesUsingEdgeRuntime } from './worker' +import type { RoutesUsingEdgeRuntime } from './utils' const debug = debugOriginal('next:build:build-traces')