Skip to content

Commit d6a1b5d

Browse files
authored
Ensure rewrites are included in build manifest when using Turbopack (#56692)
The build manifest when using webpack includes the rewrites config so that the Pages Router can correctly match the path. The manifest for Turbopack was missing these properties which caused a bunch of the middleware tests to fail. This PR reuses the function to generate rewrites from build-manifest-plugin. <!-- Thanks for opening a PR! Your contribution is much appreciated. To make sure your PR is handled as smoothly as possible we request that you follow the checklist sections below. Choose the right checklist for the change(s) that you're making: ## For Contributors ### Improving Documentation - Run `pnpm prettier-fix` to fix formatting issues before opening the PR. - Read the Docs Contribution Guide to ensure your contribution follows the docs guidelines: https://nextjs.org/docs/community/contribution-guide ### Adding or Updating Examples - The "examples guidelines" are followed from our contributing doc https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md - Make sure the linting passes by running `pnpm build && pnpm lint`. See https://github.com/vercel/next.js/blob/canary/contributing/repository/linting.md ### Fixing a bug - Related issues linked using `fixes #number` - Tests added. See: https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs - Errors have a helpful link attached, see https://github.com/vercel/next.js/blob/canary/contributing.md ### Adding a feature - Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. (A discussion must be opened, see https://github.com/vercel/next.js/discussions/new?category=ideas) - Related issues/discussions are linked using `fixes #number` - e2e tests added (https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs) - Documentation added - Telemetry added. In case of a feature if it's used or not. - Errors have a helpful link attached, see https://github.com/vercel/next.js/blob/canary/contributing.md ## For Maintainers - Minimal description (aim for explaining to someone not on the team to understand the PR) - When linking to a Slack thread, you might want to share details of the conclusion - Link both the Linear (Fixes NEXT-xxx) and the GitHub issues - Add review comments if necessary to explain to the reviewer the logic behind a change ### What? ### Why? ### How? Closes NEXT- Fixes # -->
1 parent e8048b9 commit d6a1b5d

File tree

2 files changed

+43
-28
lines changed

2 files changed

+43
-28
lines changed

packages/next/src/build/webpack/plugins/build-manifest-plugin.ts

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,37 @@ import { spans } from './profiling-plugin'
2020

2121
type DeepMutable<T> = { -readonly [P in keyof T]: DeepMutable<T[P]> }
2222

23-
export type ClientBuildManifest = Record<string, string[]>
23+
export type ClientBuildManifest = {
24+
[key: string]: string[]
25+
}
2426

2527
// Add the runtime ssg manifest file as a lazy-loaded file dependency.
2628
// We also stub this file out for development mode (when it is not
2729
// generated).
2830
export const srcEmptySsgManifest = `self.__SSG_MANIFEST=new Set;self.__SSG_MANIFEST_CB&&self.__SSG_MANIFEST_CB()`
2931

32+
function normalizeRewrite(item: {
33+
source: string
34+
destination: string
35+
has?: any
36+
}): CustomRoutes['rewrites']['beforeFiles'][0] {
37+
return {
38+
has: item.has,
39+
source: item.source,
40+
destination: item.destination,
41+
}
42+
}
43+
44+
export function normalizeRewritesForBuildManifest(
45+
rewrites: CustomRoutes['rewrites']
46+
): CustomRoutes['rewrites'] {
47+
return {
48+
afterFiles: rewrites.afterFiles?.map((item) => normalizeRewrite(item)),
49+
beforeFiles: rewrites.beforeFiles?.map((item) => normalizeRewrite(item)),
50+
fallback: rewrites.fallback?.map((item) => normalizeRewrite(item)),
51+
}
52+
}
53+
3054
// This function takes the asset map generated in BuildManifestPlugin and creates a
3155
// reduced version to send to the client.
3256
function generateClientManifest(
@@ -40,27 +64,9 @@ function generateClientManifest(
4064
'NextJsBuildManifest-generateClientManifest'
4165
)
4266

43-
const normalizeRewrite = (item: {
44-
source: string
45-
destination: string
46-
has?: any
47-
}) => {
48-
return {
49-
has: item.has,
50-
source: item.source,
51-
destination: item.destination,
52-
}
53-
}
54-
5567
return genClientManifestSpan?.traceFn(() => {
5668
const clientManifest: ClientBuildManifest = {
57-
__rewrites: {
58-
afterFiles: rewrites.afterFiles?.map((item) => normalizeRewrite(item)),
59-
beforeFiles: rewrites.beforeFiles?.map((item) =>
60-
normalizeRewrite(item)
61-
),
62-
fallback: rewrites.fallback?.map((item) => normalizeRewrite(item)),
63-
} as any,
69+
__rewrites: normalizeRewritesForBuildManifest(rewrites) as any,
6470
}
6571
const appDependencies = new Set(assetMap.pages['/_app'])
6672
const sortedPageKeys = getSortedRoutes(Object.keys(assetMap.pages))

packages/next/src/server/lib/router-utils/setup-dev-bundler.ts

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,11 @@ import {
9898
} from 'next/dist/compiled/@next/react-dev-overlay/dist/middleware'
9999
import { mkdir, readFile, writeFile, rename, unlink } from 'fs/promises'
100100
import { PageNotFoundError } from '../../../shared/lib/utils'
101-
import { srcEmptySsgManifest } from '../../../build/webpack/plugins/build-manifest-plugin'
101+
import {
102+
type ClientBuildManifest,
103+
normalizeRewritesForBuildManifest,
104+
srcEmptySsgManifest,
105+
} from '../../../build/webpack/plugins/build-manifest-plugin'
102106
import { devPageFiles } from '../../../build/webpack/plugins/next-types-plugin/shared'
103107
import type { LazyRenderServerInstance } from '../router-server'
104108
import { pathToRegexp } from 'next/dist/compiled/path-to-regexp'
@@ -710,16 +714,21 @@ async function startWatcher(opts: SetupOpts) {
710714
}
711715
}
712716

713-
async function writeBuildManifest(): Promise<void> {
717+
async function writeBuildManifest(
718+
rewrites: SetupOpts['fsChecker']['rewrites']
719+
): Promise<void> {
714720
const buildManifest = mergeBuildManifests(buildManifests.values())
715721
const buildManifestPath = path.join(distDir, BUILD_MANIFEST)
716722
deleteCache(buildManifestPath)
717723
await writeFileAtomic(
718724
buildManifestPath,
719725
JSON.stringify(buildManifest, null, 2)
720726
)
721-
const content = {
722-
__rewrites: { afterFiles: [], beforeFiles: [], fallback: [] },
727+
728+
const content: ClientBuildManifest = {
729+
__rewrites: rewrites
730+
? (normalizeRewritesForBuildManifest(rewrites) as any)
731+
: { afterFiles: [], beforeFiles: [], fallback: [] },
723732
...Object.fromEntries(
724733
[...curEntries.keys()].map((pathname) => [
725734
pathname,
@@ -1035,7 +1044,7 @@ async function startWatcher(opts: SetupOpts) {
10351044
)
10361045
)
10371046
await currentEntriesHandling
1038-
await writeBuildManifest()
1047+
await writeBuildManifest(opts.fsChecker.rewrites)
10391048
await writeAppBuildManifest()
10401049
await writeFallbackBuildManifest()
10411050
await writePagesManifest()
@@ -1204,7 +1213,7 @@ async function startWatcher(opts: SetupOpts) {
12041213
await loadBuildManifest('_error')
12051214
await loadPagesManifest('_error')
12061215

1207-
await writeBuildManifest()
1216+
await writeBuildManifest(opts.fsChecker.rewrites)
12081217
await writeFallbackBuildManifest()
12091218
await writePagesManifest()
12101219
await writeMiddlewareManifest()
@@ -1318,7 +1327,7 @@ async function startWatcher(opts: SetupOpts) {
13181327
middlewareManifests.delete(page)
13191328
}
13201329

1321-
await writeBuildManifest()
1330+
await writeBuildManifest(opts.fsChecker.rewrites)
13221331
await writeFallbackBuildManifest()
13231332
await writePagesManifest()
13241333
await writeMiddlewareManifest()
@@ -1379,7 +1388,7 @@ async function startWatcher(opts: SetupOpts) {
13791388
await loadActionManifest(page)
13801389

13811390
await writeAppBuildManifest()
1382-
await writeBuildManifest()
1391+
await writeBuildManifest(opts.fsChecker.rewrites)
13831392
await writeAppPathsManifest()
13841393
await writeMiddlewareManifest()
13851394
await writeActionManifest()

0 commit comments

Comments
 (0)