From 8eb701c19ae01ae19543b3d9af7f96def0b1300f Mon Sep 17 00:00:00 2001 From: Sebastian Silbermann Date: Fri, 26 Apr 2024 19:57:22 +0200 Subject: [PATCH] Combine necessary file for edge route in size calculation (#65053) Closes NEXT-3228 --- .../src/run/collect-stats.js | 24 ++++++++++--- test/.stats-app/stats-config.js | 34 +++++++++++++++++++ 2 files changed, 53 insertions(+), 5 deletions(-) diff --git a/.github/actions/next-stats-action/src/run/collect-stats.js b/.github/actions/next-stats-action/src/run/collect-stats.js index 8c54fca65e..9f6f627c42 100644 --- a/.github/actions/next-stats-action/src/run/collect-stats.js +++ b/.github/actions/next-stats-action/src/run/collect-stats.js @@ -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 = {}, @@ -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() @@ -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) } diff --git a/test/.stats-app/stats-config.js b/test/.stats-app/stats-config.js index e2a8202f12..6e280627bf 100644 --- a/test/.stats-app/stats-config.js +++ b/test/.stats-app/stats-config.js @@ -1,3 +1,6 @@ +const fs = require('fs/promises') +const path = require('path') + const clientGlobs = [ { name: 'Client Bundles (main, webpack)', @@ -31,6 +34,37 @@ 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) + const manifestFileEntry = path.relative( + path.join(nextAppDir, '.next'), + path.join(nextAppDir, fileName) + ) + + 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',