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

Ensure rewrites are included in build manifest when using Turbopack #56692

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
46 changes: 26 additions & 20 deletions packages/next/src/build/webpack/plugins/build-manifest-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,37 @@ import { spans } from './profiling-plugin'

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

export type ClientBuildManifest = Record<string, string[]>
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(
Expand All @@ -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))
Expand Down
25 changes: 17 additions & 8 deletions packages/next/src/server/lib/router-utils/setup-dev-bundler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -710,16 +714,21 @@ async function startWatcher(opts: SetupOpts) {
}
}

async function writeBuildManifest(): Promise<void> {
async function writeBuildManifest(
rewrites: SetupOpts['fsChecker']['rewrites']
): Promise<void> {
const buildManifest = mergeBuildManifests(buildManifests.values())
const buildManifestPath = path.join(distDir, BUILD_MANIFEST)
deleteCache(buildManifestPath)
await writeFileAtomic(
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,
Expand Down Expand Up @@ -1035,7 +1044,7 @@ async function startWatcher(opts: SetupOpts) {
)
)
await currentEntriesHandling
await writeBuildManifest()
await writeBuildManifest(opts.fsChecker.rewrites)
await writeAppBuildManifest()
await writeFallbackBuildManifest()
await writePagesManifest()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down
Loading