diff --git a/packages/next/src/build/webpack/plugins/build-manifest-plugin.ts b/packages/next/src/build/webpack/plugins/build-manifest-plugin.ts index 015dd08426bfd..9b9c3006d9a56 100644 --- a/packages/next/src/build/webpack/plugins/build-manifest-plugin.ts +++ b/packages/next/src/build/webpack/plugins/build-manifest-plugin.ts @@ -20,13 +20,37 @@ import { spans } from './profiling-plugin' type DeepMutable = { -readonly [P in keyof T]: DeepMutable } -export type ClientBuildManifest = Record +export type ClientBuildManifest = { + [key: string]: string[] +} // Add the runtime ssg manifest file as a lazy-loaded file dependency. // We also stub this file out for development mode (when it is not // generated). export const srcEmptySsgManifest = `self.__SSG_MANIFEST=new Set;self.__SSG_MANIFEST_CB&&self.__SSG_MANIFEST_CB()` +function normalizeRewrite(item: { + source: string + destination: string + has?: any +}): CustomRoutes['rewrites']['beforeFiles'][0] { + return { + has: item.has, + source: item.source, + destination: item.destination, + } +} + +export function normalizeRewritesForBuildManifest( + rewrites: CustomRoutes['rewrites'] +): CustomRoutes['rewrites'] { + return { + afterFiles: rewrites.afterFiles?.map((item) => normalizeRewrite(item)), + beforeFiles: rewrites.beforeFiles?.map((item) => normalizeRewrite(item)), + fallback: rewrites.fallback?.map((item) => normalizeRewrite(item)), + } +} + // This function takes the asset map generated in BuildManifestPlugin and creates a // reduced version to send to the client. function generateClientManifest( @@ -40,27 +64,9 @@ function generateClientManifest( 'NextJsBuildManifest-generateClientManifest' ) - const normalizeRewrite = (item: { - source: string - destination: string - has?: any - }) => { - return { - has: item.has, - source: item.source, - destination: item.destination, - } - } - return genClientManifestSpan?.traceFn(() => { const clientManifest: ClientBuildManifest = { - __rewrites: { - afterFiles: rewrites.afterFiles?.map((item) => normalizeRewrite(item)), - beforeFiles: rewrites.beforeFiles?.map((item) => - normalizeRewrite(item) - ), - fallback: rewrites.fallback?.map((item) => normalizeRewrite(item)), - } as any, + __rewrites: normalizeRewritesForBuildManifest(rewrites) as any, } const appDependencies = new Set(assetMap.pages['/_app']) const sortedPageKeys = getSortedRoutes(Object.keys(assetMap.pages)) diff --git a/packages/next/src/server/lib/router-utils/setup-dev-bundler.ts b/packages/next/src/server/lib/router-utils/setup-dev-bundler.ts index 36d4700f1716c..0aace1745121b 100644 --- a/packages/next/src/server/lib/router-utils/setup-dev-bundler.ts +++ b/packages/next/src/server/lib/router-utils/setup-dev-bundler.ts @@ -98,7 +98,11 @@ import { } from 'next/dist/compiled/@next/react-dev-overlay/dist/middleware' import { mkdir, readFile, writeFile, rename, unlink } from 'fs/promises' import { PageNotFoundError } from '../../../shared/lib/utils' -import { srcEmptySsgManifest } from '../../../build/webpack/plugins/build-manifest-plugin' +import { + type ClientBuildManifest, + normalizeRewritesForBuildManifest, + srcEmptySsgManifest, +} from '../../../build/webpack/plugins/build-manifest-plugin' import { devPageFiles } from '../../../build/webpack/plugins/next-types-plugin/shared' import type { LazyRenderServerInstance } from '../router-server' import { pathToRegexp } from 'next/dist/compiled/path-to-regexp' @@ -710,7 +714,9 @@ async function startWatcher(opts: SetupOpts) { } } - async function writeBuildManifest(): Promise { + async function writeBuildManifest( + rewrites: SetupOpts['fsChecker']['rewrites'] + ): Promise { const buildManifest = mergeBuildManifests(buildManifests.values()) const buildManifestPath = path.join(distDir, BUILD_MANIFEST) deleteCache(buildManifestPath) @@ -718,8 +724,11 @@ async function startWatcher(opts: SetupOpts) { buildManifestPath, JSON.stringify(buildManifest, null, 2) ) - const content = { - __rewrites: { afterFiles: [], beforeFiles: [], fallback: [] }, + + const content: ClientBuildManifest = { + __rewrites: rewrites + ? (normalizeRewritesForBuildManifest(rewrites) as any) + : { afterFiles: [], beforeFiles: [], fallback: [] }, ...Object.fromEntries( [...curEntries.keys()].map((pathname) => [ pathname, @@ -1035,7 +1044,7 @@ async function startWatcher(opts: SetupOpts) { ) ) await currentEntriesHandling - await writeBuildManifest() + await writeBuildManifest(opts.fsChecker.rewrites) await writeAppBuildManifest() await writeFallbackBuildManifest() await writePagesManifest() @@ -1204,7 +1213,7 @@ async function startWatcher(opts: SetupOpts) { await loadBuildManifest('_error') await loadPagesManifest('_error') - await writeBuildManifest() + await writeBuildManifest(opts.fsChecker.rewrites) await writeFallbackBuildManifest() await writePagesManifest() await writeMiddlewareManifest() @@ -1318,7 +1327,7 @@ async function startWatcher(opts: SetupOpts) { middlewareManifests.delete(page) } - await writeBuildManifest() + await writeBuildManifest(opts.fsChecker.rewrites) await writeFallbackBuildManifest() await writePagesManifest() await writeMiddlewareManifest() @@ -1379,7 +1388,7 @@ async function startWatcher(opts: SetupOpts) { await loadActionManifest(page) await writeAppBuildManifest() - await writeBuildManifest() + await writeBuildManifest(opts.fsChecker.rewrites) await writeAppPathsManifest() await writeMiddlewareManifest() await writeActionManifest()