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')