Skip to content

Commit 552b974

Browse files
authored
perf: fix tracing for routes (#56924)
follow up to #56898 where I noticed that we don't apply any filtering to the trace files for the user routes, resulting in files that would need to be filtered like `caniuse` not being filtered out correctly. This fixes that. A lambda in my test project goes from `2.7MB` to `1.4MB` followup: add some snapshot tests before ``` Serverless function size info Serverless Function's pages: _not-found.js, index.js Large Dependencies Uncompressed size Compressed size node_modules/.pnpm/[email protected][email protected][email protected] 4.61 MB 1.35 MB node_modules/.pnpm/[email protected] 909.73 KB 327.14 KB node_modules/.pnpm/[email protected][email protected] 546.21 KB 138.87 KB All dependencies 3.66 MB 2.01 MB Serverless Function's page: favicon.ico.js Large Dependencies Uncompressed size Compressed size node_modules/.pnpm/[email protected][email protected][email protected] 6.71 MB 2.05 MB node_modules/.pnpm/[email protected] 909.73 KB 327.14 KB node_modules/.pnpm/[email protected][email protected] 546.21 KB 138.87 KB All dependencies 5.78 MB 2.71 MB Serverless Function's page: api/hello-world.js Large Dependencies Uncompressed size Compressed size node_modules/.pnpm/[email protected][email protected][email protected] 4.61 MB 1.35 MB node_modules/.pnpm/[email protected] 909.73 KB 327.14 KB node_modules/.pnpm/[email protected][email protected] 546.21 KB 138.87 KB All dependencies 3.65 MB 2.01 MB ``` after ``` Large Dependencies Uncompressed size Compressed size node_modules/.pnpm/file+next-canary+next-13[email protected][email protected] 2.87 MB 844.1 KB All dependencies 341.31 KB 992.45 KB Serverless Function's page: favicon.ico.js Large Dependencies Uncompressed size Compressed size node_modules/.pnpm/file+next-canary+next-13[email protected][email protected] 4.97 MB 1.52 MB All dependencies 2.45 MB 1.67 MB Serverless Function's page: api/hello-world.js Large Dependencies Uncompressed size Compressed size node_modules/.pnpm/file+next-canary+next-13[email protected][email protected] 2.87 MB 844.1 KB All dependencies 328.64 KB 989.23 KB ````
1 parent db21421 commit 552b974

File tree

1 file changed

+52
-31
lines changed

1 file changed

+52
-31
lines changed

packages/next/src/build/collect-build-traces.ts

Lines changed: 52 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -269,11 +269,19 @@ export async function collectBuildTraces({
269269
})
270270
}
271271
}
272-
const serverIgnores = [
272+
273+
const sharedIgnores = [
273274
'**/*.d.ts',
274275
'**/*.map',
275276
'**/next/dist/compiled/next-server/**/*.dev.js',
276277
'**/node_modules/react{,-dom,-dom-server-turbopack}/**/*.development.js',
278+
279+
...additionalIgnores,
280+
...(config.experimental.outputFileTracingIgnores || []),
281+
]
282+
283+
const serverIgnores = [
284+
...sharedIgnores,
277285
isStandalone ? null : '**/next/dist/compiled/jest-worker/**/*',
278286
'**/next/dist/compiled/webpack/(bundle4|bundle5).js',
279287
'**/node_modules/webpack5/**/*',
@@ -294,32 +302,32 @@ export async function collectBuildTraces({
294302
? ['**/next/dist/compiled/@ampproject/toolbox-optimizer/**/*']
295303
: []),
296304

297-
...additionalIgnores,
298-
299305
...(isStandalone ? [] : TRACE_IGNORES),
300-
301-
...(config.experimental.outputFileTracingIgnores || []),
302306
].filter(nonNullable)
303307

304308
const minimalServerIgnores = [
305309
...serverIgnores,
306310
'**/next/dist/compiled/edge-runtime/**/*',
307311
'**/next/dist/server/web/sandbox/**/*',
312+
'**/next/dist/server/post-process.js',
313+
]
314+
315+
const routesIgnores = [
316+
...sharedIgnores,
317+
'**/next/dist/compiled/next-server/**/*',
318+
'**/next/dist/server/optimize-amp.js',
319+
'**/next/dist/server/post-process.js',
308320
]
309321

310-
const serverIgnoreFn = (minimal: boolean) => (pathname: string) => {
322+
const makeIgnoreFn = (ignores: string[]) => (pathname: string) => {
311323
if (path.isAbsolute(pathname) && !pathname.startsWith(root)) {
312324
return true
313325
}
314326

315-
return isMatch(
316-
pathname,
317-
minimal ? minimalServerIgnores : serverIgnores,
318-
{
319-
contains: true,
320-
dot: true,
321-
}
322-
)
327+
return isMatch(pathname, ignores, {
328+
contains: true,
329+
dot: true,
330+
})
323331
}
324332
const traceContext = path.join(nextServerEntry, '..', '..')
325333
const serverTracedFiles = new Set<string>()
@@ -372,9 +380,11 @@ export async function collectBuildTraces({
372380
] as [Set<string>, string[]][]) {
373381
for (const file of files) {
374382
if (
375-
!serverIgnoreFn(set === minimalServerTracedFiles)(
376-
path.join(traceContext, file)
377-
)
383+
!makeIgnoreFn(
384+
set === minimalServerTracedFiles
385+
? minimalServerIgnores
386+
: serverIgnores
387+
)(path.join(traceContext, file))
378388
) {
379389
addToTracedFiles(traceContext, file, set)
380390
}
@@ -433,14 +443,13 @@ export async function collectBuildTraces({
433443
})
434444
const reasons = result.reasons
435445
const fileList = result.fileList
436-
437446
for (const file of result.esmFileList) {
438447
fileList.add(file)
439448
}
440449

441450
const parentFilesMap = getFilesMapFromReasons(fileList, reasons)
442-
const cachedIgnoreFiles = new Map<string, boolean>()
443-
const cachedIgnoreFilesMinimal = new Map<string, boolean>()
451+
const cachedLookupIgnore = new Map<string, boolean>()
452+
const cachedLookupIgnoreMinimal = new Map<string, boolean>()
444453

445454
for (const [entries, tracedFiles] of [
446455
[serverEntries, serverTracedFiles],
@@ -458,11 +467,15 @@ export async function collectBuildTraces({
458467
if (
459468
!shouldIgnore(
460469
curFile,
461-
serverIgnoreFn(tracedFiles === minimalServerTracedFiles),
470+
makeIgnoreFn(
471+
tracedFiles === minimalServerTracedFiles
472+
? minimalServerIgnores
473+
: serverIgnores
474+
),
462475
reasons,
463476
tracedFiles === minimalServerTracedFiles
464-
? cachedIgnoreFilesMinimal
465-
: cachedIgnoreFiles
477+
? cachedLookupIgnoreMinimal
478+
: cachedLookupIgnore
466479
)
467480
) {
468481
tracedFiles.add(
@@ -475,6 +488,8 @@ export async function collectBuildTraces({
475488

476489
const { entryNameFilesMap } = buildTraceContext?.chunksTrace || {}
477490

491+
const cachedLookupIgnoreRoutes = new Map<string, boolean>()
492+
478493
await Promise.all(
479494
[
480495
...(entryNameFilesMap
@@ -514,14 +529,20 @@ export async function collectBuildTraces({
514529
path.relative(outputFileTracingRoot, file)
515530
)
516531
for (const curFile of curFiles || []) {
517-
curTracedFiles.add(
518-
path
519-
.relative(
520-
traceOutputDir,
521-
path.join(outputFileTracingRoot, curFile)
522-
)
532+
if (
533+
!shouldIgnore(
534+
curFile,
535+
makeIgnoreFn(routesIgnores),
536+
reasons,
537+
cachedLookupIgnoreRoutes
538+
)
539+
) {
540+
const filePath = path.join(outputFileTracingRoot, curFile)
541+
const outputFile = path
542+
.relative(traceOutputDir, filePath)
523543
.replace(/\\/g, '/')
524-
)
544+
curTracedFiles.add(outputFile)
545+
}
525546
}
526547
}
527548

@@ -556,7 +577,7 @@ export async function collectBuildTraces({
556577

557578
for (const item of await fs.readdir(contextDir)) {
558579
const itemPath = path.relative(root, path.join(contextDir, item))
559-
if (!serverIgnoreFn(false)(itemPath)) {
580+
if (!makeIgnoreFn(serverIgnores)(itemPath)) {
560581
addToTracedFiles(root, itemPath, serverTracedFiles)
561582
addToTracedFiles(root, itemPath, minimalServerTracedFiles)
562583
}

0 commit comments

Comments
 (0)