From d492b937e216f0f193bf4d9c233424e808e049d1 Mon Sep 17 00:00:00 2001
From: mPaella <93682696+mPaella@users.noreply.github.com>
Date: Wed, 14 Jun 2023 17:24:57 -0400
Subject: [PATCH] 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
---------
Co-authored-by: JJ Kasper
---
packages/next/src/build/utils.ts | 30 ++++++++++++++++++------------
test/e2e/app-dir/app/index.test.ts | 6 ++++++
2 files changed, 24 insertions(+), 12 deletions(-)
diff --git a/packages/next/src/build/utils.ts b/packages/next/src/build/utils.ts
index a47a8c83cfb86..83ae9c6f8e222 100644
--- a/packages/next/src/build/utils.ts
+++ b/packages/next/src/build/utils.ts
@@ -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 {
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
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, [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')
diff --git a/test/e2e/app-dir/app/index.test.ts b/test/e2e/app-dir/app/index.test.ts
index 271c9b654e72b..cea3d44a7433a 100644
--- a/test/e2e/app-dir/app/index.test.ts
+++ b/test/e2e/app-dir/app/index.test.ts
@@ -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')