Skip to content

Commit

Permalink
perf: fix tracing for routes (#56924)
Browse files Browse the repository at this point in the history
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/next@13.5.6-canary.1_react-dom@18.2.0_react@18.2.0            4.61 MB          1.35 MB
node_modules/.pnpm/caniuse-lite@1.0.30001517                                   909.73 KB        327.14 KB
node_modules/.pnpm/react-dom@18.2.0_react@18.2.0                               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/next@13.5.6-canary.1_react-dom@18.2.0_react@18.2.0            6.71 MB          2.05 MB
node_modules/.pnpm/caniuse-lite@1.0.30001517                                   909.73 KB        327.14 KB
node_modules/.pnpm/react-dom@18.2.0_react@18.2.0                               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/next@13.5.6-canary.1_react-dom@18.2.0_react@18.2.0            4.61 MB          1.35 MB
node_modules/.pnpm/caniuse-lite@1.0.30001517                                   909.73 KB        327.14 KB
node_modules/.pnpm/react-dom@18.2.0_react@18.2.0                               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.5.6-canary.1.tgz_react-dom@18.2.0_react@18.2.0            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.5.6-canary.1.tgz_react-dom@18.2.0_react@18.2.0            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.5.6-canary.1.tgz_react-dom@18.2.0_react@18.2.0            2.87 MB         844.1 KB

All dependencies                                                                                    328.64 KB        989.23 KB
````
  • Loading branch information
feedthejim authored Oct 17, 2023
1 parent db21421 commit 552b974
Showing 1 changed file with 52 additions and 31 deletions.
83 changes: 52 additions & 31 deletions packages/next/src/build/collect-build-traces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,11 +269,19 @@ export async function collectBuildTraces({
})
}
}
const serverIgnores = [

const sharedIgnores = [
'**/*.d.ts',
'**/*.map',
'**/next/dist/compiled/next-server/**/*.dev.js',
'**/node_modules/react{,-dom,-dom-server-turbopack}/**/*.development.js',

...additionalIgnores,
...(config.experimental.outputFileTracingIgnores || []),
]

const serverIgnores = [
...sharedIgnores,
isStandalone ? null : '**/next/dist/compiled/jest-worker/**/*',
'**/next/dist/compiled/webpack/(bundle4|bundle5).js',
'**/node_modules/webpack5/**/*',
Expand All @@ -294,32 +302,32 @@ export async function collectBuildTraces({
? ['**/next/dist/compiled/@ampproject/toolbox-optimizer/**/*']
: []),

...additionalIgnores,

...(isStandalone ? [] : TRACE_IGNORES),

...(config.experimental.outputFileTracingIgnores || []),
].filter(nonNullable)

const minimalServerIgnores = [
...serverIgnores,
'**/next/dist/compiled/edge-runtime/**/*',
'**/next/dist/server/web/sandbox/**/*',
'**/next/dist/server/post-process.js',
]

const routesIgnores = [
...sharedIgnores,
'**/next/dist/compiled/next-server/**/*',
'**/next/dist/server/optimize-amp.js',
'**/next/dist/server/post-process.js',
]

const serverIgnoreFn = (minimal: boolean) => (pathname: string) => {
const makeIgnoreFn = (ignores: string[]) => (pathname: string) => {
if (path.isAbsolute(pathname) && !pathname.startsWith(root)) {
return true
}

return isMatch(
pathname,
minimal ? minimalServerIgnores : serverIgnores,
{
contains: true,
dot: true,
}
)
return isMatch(pathname, ignores, {
contains: true,
dot: true,
})
}
const traceContext = path.join(nextServerEntry, '..', '..')
const serverTracedFiles = new Set<string>()
Expand Down Expand Up @@ -372,9 +380,11 @@ export async function collectBuildTraces({
] as [Set<string>, string[]][]) {
for (const file of files) {
if (
!serverIgnoreFn(set === minimalServerTracedFiles)(
path.join(traceContext, file)
)
!makeIgnoreFn(
set === minimalServerTracedFiles
? minimalServerIgnores
: serverIgnores
)(path.join(traceContext, file))
) {
addToTracedFiles(traceContext, file, set)
}
Expand Down Expand Up @@ -433,14 +443,13 @@ export async function collectBuildTraces({
})
const reasons = result.reasons
const fileList = result.fileList

for (const file of result.esmFileList) {
fileList.add(file)
}

const parentFilesMap = getFilesMapFromReasons(fileList, reasons)
const cachedIgnoreFiles = new Map<string, boolean>()
const cachedIgnoreFilesMinimal = new Map<string, boolean>()
const cachedLookupIgnore = new Map<string, boolean>()
const cachedLookupIgnoreMinimal = new Map<string, boolean>()

for (const [entries, tracedFiles] of [
[serverEntries, serverTracedFiles],
Expand All @@ -458,11 +467,15 @@ export async function collectBuildTraces({
if (
!shouldIgnore(
curFile,
serverIgnoreFn(tracedFiles === minimalServerTracedFiles),
makeIgnoreFn(
tracedFiles === minimalServerTracedFiles
? minimalServerIgnores
: serverIgnores
),
reasons,
tracedFiles === minimalServerTracedFiles
? cachedIgnoreFilesMinimal
: cachedIgnoreFiles
? cachedLookupIgnoreMinimal
: cachedLookupIgnore
)
) {
tracedFiles.add(
Expand All @@ -475,6 +488,8 @@ export async function collectBuildTraces({

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

const cachedLookupIgnoreRoutes = new Map<string, boolean>()

await Promise.all(
[
...(entryNameFilesMap
Expand Down Expand Up @@ -514,14 +529,20 @@ export async function collectBuildTraces({
path.relative(outputFileTracingRoot, file)
)
for (const curFile of curFiles || []) {
curTracedFiles.add(
path
.relative(
traceOutputDir,
path.join(outputFileTracingRoot, curFile)
)
if (
!shouldIgnore(
curFile,
makeIgnoreFn(routesIgnores),
reasons,
cachedLookupIgnoreRoutes
)
) {
const filePath = path.join(outputFileTracingRoot, curFile)
const outputFile = path
.relative(traceOutputDir, filePath)
.replace(/\\/g, '/')
)
curTracedFiles.add(outputFile)
}
}
}

Expand Down Expand Up @@ -556,7 +577,7 @@ export async function collectBuildTraces({

for (const item of await fs.readdir(contextDir)) {
const itemPath = path.relative(root, path.join(contextDir, item))
if (!serverIgnoreFn(false)(itemPath)) {
if (!makeIgnoreFn(serverIgnores)(itemPath)) {
addToTracedFiles(root, itemPath, serverTracedFiles)
addToTracedFiles(root, itemPath, minimalServerTracedFiles)
}
Expand Down

0 comments on commit 552b974

Please sign in to comment.