fix: Incorrect build size outputs for app dir (#50768)

### What?
Fixes https://github.com/vercel/next.js/issues/50129

### Why?
The current denormalization of app dir paths doesnt account for the
normalization done in `normalizeAppPath`, causing build log outputs for
certain paths such as anything under a route groups to be incorrect

### How?
There's 2 ways this could be fixed:

1.) Denormalize the app dir paths by reading the
`app-path-routes-manifest.json` and mapping back to the original file
path
2.) (what I chose to do) Normalize the keys of `appBuildManifest`

### Result

App dir paths, including route groups, now have their correct output
size

<img width="494" alt="image"
src="https://github.com/vercel/next.js/assets/93682696/0eee79b8-7d60-4c88-b07a-dfb750aa9592">

---------

Co-authored-by: JJ Kasper <jj@jjsweb.site>
This commit is contained in:
mPaella 2023-06-14 17:24:57 -04:00 committed by GitHub
parent c76653f2f7
commit d492b937e2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 12 deletions

View file

@ -64,6 +64,7 @@ import { IncrementalCache } from '../server/lib/incremental-cache'
import { patchFetch } from '../server/lib/patch-fetch'
import { nodeFs } from '../server/lib/node-fs-methods'
import * as ciEnvironment from '../telemetry/ci-info'
import { normalizeAppPath } from '../shared/lib/router/utils/app-paths'
export type ROUTER_TYPE = 'pages' | 'app'
@ -110,14 +111,6 @@ function sum(a: ReadonlyArray<number>): number {
return a.reduce((size, stat) => size + stat, 0)
}
function denormalizeAppPagePath(page: string): string {
// `/` is normalized to `/index` and `/index` is normalized to `/index/index`
if (page.endsWith('/index')) {
page = page.replace(/\/index$/, '')
}
return page + '/page'
}
type ComputeFilesGroup = {
files: ReadonlyArray<string>
size: {
@ -773,6 +766,18 @@ export async function getJsPageSizeInKb(
throw new Error('expected appBuildManifest with an "app" pageType')
}
// Normalize appBuildManifest keys
if (routerType === 'app') {
pageManifest.pages = Object.entries(pageManifest.pages).reduce(
(acc: Record<string, string[]>, [key, value]) => {
const newKey = normalizeAppPath(key)
acc[newKey] = value as string[]
return acc
},
{}
)
}
// If stats was not provided, then compute it again.
const stats =
cachedStats ??
@ -788,10 +793,11 @@ export async function getJsPageSizeInKb(
throw new Error('expected "app" manifest data with an "app" pageType')
}
const pagePath =
routerType === 'pages'
? denormalizePagePath(page)
: denormalizeAppPagePath(page)
// Denormalization is not needed for "app" dir, as we normalize the keys of appBuildManifest instead
let pagePath = page
if (routerType === 'pages') {
pagePath = denormalizePagePath(page)
}
const fnFilterJs = (entry: string) => entry.endsWith('.js')

View file

@ -11,6 +11,12 @@ createNextDescribe(
},
({ next, isNextDev: isDev, isNextStart, isNextDeploy }) => {
if (isNextStart) {
it('should have correct size in build output', async () => {
expect(next.cliOutput).toMatch(
/\/dashboard\/another.*? [^0]{1,} [\w]{1,}B/
)
})
it('should have correct preferredRegion values in manifest', async () => {
const middlewareManifest = JSON.parse(
await next.readFile('.next/server/middleware-manifest.json')