Skip to content

Commit

Permalink
fix: Incorrect build size outputs for app dir (#50768)
Browse files Browse the repository at this point in the history
### What?
Fixes #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>
  • Loading branch information
mPaella and ijjk committed Jun 14, 2023
1 parent c76653f commit d492b93
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
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

0 comments on commit d492b93

Please sign in to comment.