Skip to content

Commit 8eb701c

Browse files
authored
Combine necessary file for edge route in size calculation (#65053)
Closes NEXT-3228
1 parent 4e84f69 commit 8eb701c

File tree

2 files changed

+53
-5
lines changed

2 files changed

+53
-5
lines changed

.github/actions/next-stats-action/src/run/collect-stats.js

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ const { parse: urlParse } = require('url')
1010
const benchmarkUrl = require('./benchmark-url')
1111
const { statsAppDir, diffingDir, benchTitle } = require('../constants')
1212

13+
async function defaultGetRequiredFiles(nextAppDir, fileName) {
14+
return [fileName]
15+
}
16+
1317
module.exports = async function collectStats(
1418
runConfig = {},
1519
statsConfig = {},
@@ -136,7 +140,11 @@ module.exports = async function collectStats(
136140
}
137141

138142
for (const fileGroup of runConfig.filesToTrack) {
139-
const { name, globs } = fileGroup
143+
const {
144+
getRequiredFiles = defaultGetRequiredFiles,
145+
name,
146+
globs,
147+
} = fileGroup
140148
const groupStats = {}
141149
const curFiles = new Set()
142150

@@ -147,11 +155,17 @@ module.exports = async function collectStats(
147155

148156
for (const file of curFiles) {
149157
const fileKey = path.basename(file)
150-
const absPath = path.join(curDir, file)
151158
try {
152-
const fileInfo = await fs.stat(absPath)
153-
groupStats[fileKey] = fileInfo.size
154-
groupStats[`${fileKey} gzip`] = await gzipSize.file(absPath)
159+
let parsedSizeSum = 0
160+
let gzipSizeSum = 0
161+
for (const requiredFile of await getRequiredFiles(curDir, file)) {
162+
const absPath = path.join(curDir, requiredFile)
163+
const fileInfo = await fs.stat(absPath)
164+
parsedSizeSum += fileInfo.size
165+
gzipSizeSum += await gzipSize.file(absPath)
166+
}
167+
groupStats[fileKey] = parsedSizeSum
168+
groupStats[`${fileKey} gzip`] = gzipSizeSum
155169
} catch (err) {
156170
logger.error('Failed to get file stats', err)
157171
}

test/.stats-app/stats-config.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
const fs = require('fs/promises')
2+
const path = require('path')
3+
14
const clientGlobs = [
25
{
36
name: 'Client Bundles (main, webpack)',
@@ -31,6 +34,37 @@ const clientGlobs = [
3134
'.next/server/pages/edge-ssr.js',
3235
'.next/server/app/app-edge-ssr/page.js',
3336
],
37+
getRequiredFiles: async (nextAppDir, fileName) => {
38+
if (fileName.startsWith('.next/server/app')) {
39+
const manifestJson = await fs.readFile(
40+
path.join(nextAppDir, '.next/server/middleware-manifest.json')
41+
)
42+
const manifest = JSON.parse(manifestJson)
43+
const manifestFileEntry = path.relative(
44+
path.join(nextAppDir, '.next'),
45+
path.join(nextAppDir, fileName)
46+
)
47+
48+
const functionEntry = Object.values(manifest.functions).find(
49+
(entry) => {
50+
return entry.files.includes(manifestFileEntry)
51+
}
52+
)
53+
54+
if (functionEntry === undefined) {
55+
throw new Error(
56+
`${manifestFileEntry} is not listed in the files files of any functions in the manifest:\n` +
57+
JSON.stringify(manifest, null, 2)
58+
)
59+
}
60+
61+
return functionEntry.files.map((file) => {
62+
return path.join('.next', file)
63+
})
64+
} else {
65+
return [fileName]
66+
}
67+
},
3468
},
3569
{
3670
name: 'Middleware size',

0 commit comments

Comments
 (0)