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

Add writePrerenderManifest #60158

Merged
merged 8 commits into from
Jan 4, 2024
107 changes: 50 additions & 57 deletions packages/next/src/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,34 +315,6 @@ export function buildCustomRoute(
}
}

async function generateClientSsgManifest(
prerenderManifest: PrerenderManifest,
{
buildId,
distDir,
locales,
}: { buildId: string; distDir: string; locales: string[] }
) {
const ssgPages = new Set<string>(
[
...Object.entries(prerenderManifest.routes)
// Filter out dynamic routes
.filter(([, { srcRoute }]) => srcRoute == null)
.map(([route]) => normalizeLocalePath(route, locales).pathname),
...Object.keys(prerenderManifest.dynamicRoutes),
].sort()
)

const clientSsgManifestContent = `self.__SSG_MANIFEST=${devalue(
ssgPages
)};self.__SSG_MANIFEST_CB&&self.__SSG_MANIFEST_CB()`

await writeFileUtf8(
path.join(distDir, CLIENT_STATIC_FILES_PATH, buildId, '_ssgManifest.js'),
clientSsgManifestContent
)
}

function pageToRoute(page: string) {
const routeRegex = getNamedRouteRegex(page, true)
return {
Expand Down Expand Up @@ -412,6 +384,45 @@ async function readManifest<T extends object>(filePath: string): Promise<T> {
return JSON.parse(await readFileUtf8(filePath))
}

async function writePrerenderManifest(
distDir: string,
manifest: Readonly<PrerenderManifest>
): Promise<void> {
await writeManifest(path.join(distDir, PRERENDER_MANIFEST), manifest)
await writeFileUtf8(
path.join(distDir, PRERENDER_MANIFEST).replace(/\.json$/, '.js'),
`self.__PRERENDER_MANIFEST=${JSON.stringify(JSON.stringify(manifest))}`
)
}

async function writeClientSsgManifest(
prerenderManifest: PrerenderManifest,
{
buildId,
distDir,
locales,
}: { buildId: string; distDir: string; locales: string[] }
) {
const ssgPages = new Set<string>(
[
...Object.entries(prerenderManifest.routes)
// Filter out dynamic routes
.filter(([, { srcRoute }]) => srcRoute == null)
.map(([route]) => normalizeLocalePath(route, locales).pathname),
...Object.keys(prerenderManifest.dynamicRoutes),
].sort()
)

const clientSsgManifestContent = `self.__SSG_MANIFEST=${devalue(
ssgPages
)};self.__SSG_MANIFEST_CB&&self.__SSG_MANIFEST_CB()`

await writeFileUtf8(
path.join(distDir, CLIENT_STATIC_FILES_PATH, buildId, '_ssgManifest.js'),
clientSsgManifestContent
)
}

export default async function build(
dir: string,
reactProductionProfiling = false,
Expand Down Expand Up @@ -2875,52 +2886,34 @@ export default async function build(
prefetchDataRouteRegex: undefined,
}
})

NextBuildContext.previewModeId = previewProps.previewModeId
NextBuildContext.fetchCacheKeyPrefix =
config.experimental.fetchCacheKeyPrefix
NextBuildContext.allowedRevalidateHeaderKeys =
config.experimental.allowedRevalidateHeaderKeys

const prerenderManifest: Readonly<PrerenderManifest> = {
version: 4,
routes: finalPrerenderRoutes,
dynamicRoutes: finalDynamicRoutes,
notFoundRoutes: ssgNotFoundPaths,
preview: previewProps,
}
NextBuildContext.previewModeId = previewProps.previewModeId
NextBuildContext.fetchCacheKeyPrefix =
config.experimental.fetchCacheKeyPrefix
NextBuildContext.allowedRevalidateHeaderKeys =
config.experimental.allowedRevalidateHeaderKeys

await writeManifest(
path.join(distDir, PRERENDER_MANIFEST),
prerenderManifest
)
await writeFileUtf8(
path.join(distDir, PRERENDER_MANIFEST).replace(/\.json$/, '.js'),
`self.__PRERENDER_MANIFEST=${JSON.stringify(
JSON.stringify(prerenderManifest)
)}`
)
await generateClientSsgManifest(prerenderManifest, {
await writePrerenderManifest(distDir, prerenderManifest)
await writeClientSsgManifest(prerenderManifest, {
distDir,
buildId,
locales: config.i18n?.locales || [],
})
} else {
const prerenderManifest: Readonly<PrerenderManifest> = {
await writePrerenderManifest(distDir, {
version: 4,
routes: {},
dynamicRoutes: {},
preview: previewProps,
notFoundRoutes: [],
}
await writeManifest(
path.join(distDir, PRERENDER_MANIFEST),
prerenderManifest
)
await writeFileUtf8(
path.join(distDir, PRERENDER_MANIFEST).replace(/\.json$/, '.js'),
`self.__PRERENDER_MANIFEST=${JSON.stringify(
JSON.stringify(prerenderManifest)
)}`
)
})
}

const images = { ...config.images }
Expand Down
Loading