Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Combine necessary file for edge route in size calculation #65053

Merged
merged 2 commits into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions .github/actions/next-stats-action/src/run/collect-stats.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ const { parse: urlParse } = require('url')
const benchmarkUrl = require('./benchmark-url')
const { statsAppDir, diffingDir, benchTitle } = require('../constants')

async function defaultGetRequiredFiles(nextAppDir, fileName) {
return [fileName]
}

module.exports = async function collectStats(
runConfig = {},
statsConfig = {},
Expand Down Expand Up @@ -136,7 +140,11 @@ module.exports = async function collectStats(
}

for (const fileGroup of runConfig.filesToTrack) {
const { name, globs } = fileGroup
const {
getRequiredFiles = defaultGetRequiredFiles,
name,
globs,
} = fileGroup
const groupStats = {}
const curFiles = new Set()

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

for (const file of curFiles) {
const fileKey = path.basename(file)
const absPath = path.join(curDir, file)
try {
const fileInfo = await fs.stat(absPath)
groupStats[fileKey] = fileInfo.size
groupStats[`${fileKey} gzip`] = await gzipSize.file(absPath)
let parsedSizeSum = 0
let gzipSizeSum = 0
for (const requiredFile of await getRequiredFiles(curDir, file)) {
const absPath = path.join(curDir, requiredFile)
const fileInfo = await fs.stat(absPath)
parsedSizeSum += fileInfo.size
gzipSizeSum += await gzipSize.file(absPath)
}
groupStats[fileKey] = parsedSizeSum
groupStats[`${fileKey} gzip`] = gzipSizeSum
} catch (err) {
logger.error('Failed to get file stats', err)
}
Expand Down
36 changes: 36 additions & 0 deletions test/.stats-app/stats-config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
const fs = require('fs/promises')
const path = require('path')

const clientGlobs = [
{
name: 'Client Bundles (main, webpack)',
Expand Down Expand Up @@ -31,6 +34,39 @@ const clientGlobs = [
'.next/server/pages/edge-ssr.js',
'.next/server/app/app-edge-ssr/page.js',
],
getRequiredFiles: async (nextAppDir, fileName) => {
if (fileName.startsWith('.next/server/app')) {
const manifestJson = await fs.readFile(
path.join(nextAppDir, '.next/server/middleware-manifest.json')
)
const manifest = JSON.parse(manifestJson)
console.log({ manifest, functions: manifest.functions })
const manifestFileEntry = path.relative(
path.join(nextAppDir, '.next'),
path.join(nextAppDir, fileName)
)
console.log({ manifestFileEntry })
huozhi marked this conversation as resolved.
Show resolved Hide resolved

const functionEntry = Object.values(manifest.functions).find(
(entry) => {
return entry.files.includes(manifestFileEntry)
}
)

if (functionEntry === undefined) {
throw new Error(
`${manifestFileEntry} is not listed in the files files of any functions in the manifest:\n` +
JSON.stringify(manifest, null, 2)
)
}

return functionEntry.files.map((file) => {
return path.join('.next', file)
})
} else {
return [fileName]
}
},
},
{
name: 'Middleware size',
Expand Down
Loading