Skip to content

Commit 7eb04f8

Browse files
committed
Combine necessary file for edge route in size calculation
1 parent 04571f3 commit 7eb04f8

File tree

2 files changed

+52
-5
lines changed

2 files changed

+52
-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+
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 getRequiredFiles(cwd, 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: 33 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,36 @@ 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+
console.log({ manifest })
44+
const manifestFileEntry = path.relative(
45+
path.join(nextAppDir, '.next'),
46+
path.join(nextAppDir, fileName)
47+
)
48+
console.log({ manifestFileEntry })
49+
50+
const manifestEntry = Object.values(manifest).find((entry) => {
51+
return entry.files.includes(manifestFileEntry)
52+
})
53+
54+
if (manifestEntry === undefined) {
55+
throw new Error(
56+
`${manifestFileEntry} is not listed in any manifest files`
57+
)
58+
}
59+
60+
return manifestEntry.files.map((file) => {
61+
return path.join('.next', file)
62+
})
63+
} else {
64+
return [fileName]
65+
}
66+
},
3467
},
3568
{
3669
name: 'Middleware size',

0 commit comments

Comments
 (0)