Skip to content

Commit

Permalink
Add writePrerenderManifest
Browse files Browse the repository at this point in the history
  • Loading branch information
timneutkens committed Jan 3, 2024
1 parent 103a486 commit c0f226f
Showing 1 changed file with 50 additions and 57 deletions.
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

0 comments on commit c0f226f

Please sign in to comment.