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

fix: Incorrect build size outputs for app dir #50768

Merged
merged 4 commits into from
Jun 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
30 changes: 18 additions & 12 deletions packages/next/src/build/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -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: {
Expand Down Expand Up @@ -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 ??
Expand All @@ -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')

Expand Down
6 changes: 6 additions & 0 deletions test/e2e/app-dir/app/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down