Unverified Commit 87ed37d5 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Ensure prerender-manifest contains all i18n revalidate values (#21404)

This insures we add entries for each locale version of a non-dynamic SSG page since they can have unique revalidate values. This requires a version bump in the `prerender-manifest` since the static routes now contain additional values which need to be handled separately. 

Fixes: https://github.com/vercel/next.js/issues/21568 
parent b785fbc5
...@@ -85,6 +85,7 @@ import getBaseWebpackConfig from './webpack-config' ...@@ -85,6 +85,7 @@ import getBaseWebpackConfig from './webpack-config'
import { PagesManifest } from './webpack/plugins/pages-manifest-plugin' import { PagesManifest } from './webpack/plugins/pages-manifest-plugin'
import { writeBuildId } from './write-build-id' import { writeBuildId } from './write-build-id'
import opentelemetryApi from '@opentelemetry/api' import opentelemetryApi from '@opentelemetry/api'
import { normalizeLocalePath } from '../next-server/lib/i18n/normalize-locale-path'
const staticCheckWorker = require.resolve('./utils') const staticCheckWorker = require.resolve('./utils')
...@@ -102,7 +103,7 @@ export type DynamicSsgRoute = { ...@@ -102,7 +103,7 @@ export type DynamicSsgRoute = {
} }
export type PrerenderManifest = { export type PrerenderManifest = {
version: 2 version: 3
routes: { [route: string]: SsgRoute } routes: { [route: string]: SsgRoute }
dynamicRoutes: { [route: string]: DynamicSsgRoute } dynamicRoutes: { [route: string]: DynamicSsgRoute }
notFoundRoutes: string[] notFoundRoutes: string[]
...@@ -880,6 +881,8 @@ export default async function build( ...@@ -880,6 +881,8 @@ export default async function build(
if (postCompileSpinner) postCompileSpinner.stopAndPersist() if (postCompileSpinner) postCompileSpinner.stopAndPersist()
const { i18n } = config
await traceAsyncFn(tracer.startSpan('static-generation'), async () => { await traceAsyncFn(tracer.startSpan('static-generation'), async () => {
if (staticPages.size > 0 || ssgPages.size > 0 || useStatic404) { if (staticPages.size > 0 || ssgPages.size > 0 || useStatic404) {
const combinedPages = [...staticPages, ...ssgPages] const combinedPages = [...staticPages, ...ssgPages]
...@@ -910,8 +913,6 @@ export default async function build( ...@@ -910,8 +913,6 @@ export default async function build(
// n.b. we cannot handle this above in combinedPages because the dynamic // n.b. we cannot handle this above in combinedPages because the dynamic
// page must be in the `pages` array, but not in the mapping. // page must be in the `pages` array, but not in the mapping.
exportPathMap: (defaultMap: any) => { exportPathMap: (defaultMap: any) => {
const { i18n } = config
// Dynamically routed pages should be prerendered to be used as // Dynamically routed pages should be prerendered to be used as
// a client-side skeleton (fallback) while data is being fetched. // a client-side skeleton (fallback) while data is being fetched.
// This ensures the end-user never sees a 500 or slow response from the // This ensures the end-user never sees a 500 or slow response from the
...@@ -1061,7 +1062,6 @@ export default async function build( ...@@ -1061,7 +1062,6 @@ export default async function build(
pagesManifest[page] = relativeDest pagesManifest[page] = relativeDest
} }
const { i18n } = config
const isNotFound = ssgNotFoundPaths.includes(page) const isNotFound = ssgNotFoundPaths.includes(page)
// for SSG files with i18n the non-prerendered variants are // for SSG files with i18n the non-prerendered variants are
...@@ -1136,9 +1136,12 @@ export default async function build( ...@@ -1136,9 +1136,12 @@ export default async function build(
const hasAmp = hybridAmpPages.has(page) const hasAmp = hybridAmpPages.has(page)
const file = normalizePagePath(page) const file = normalizePagePath(page)
// The dynamic version of SSG pages are only prerendered if the fallback // The dynamic version of SSG pages are only prerendered if the
// is enabled. Below, we handle the specific prerenders of these. // fallback is enabled. Below, we handle the specific prerenders
if (!(isSsg && isDynamic && !isStaticSsgFallback)) { // of these.
const hasHtmlOutput = !(isSsg && isDynamic && !isStaticSsgFallback)
if (hasHtmlOutput) {
await moveExportedPage(page, page, file, isSsg, 'html') await moveExportedPage(page, page, file, isSsg, 'html')
} }
...@@ -1152,31 +1155,46 @@ export default async function build( ...@@ -1152,31 +1155,46 @@ export default async function build(
} }
if (isSsg) { if (isSsg) {
const { i18n } = config // For a non-dynamic SSG page, we must copy its data file
// from export, we already moved the HTML file above
// For a non-dynamic SSG page, we must copy its data file from export.
if (!isDynamic) { if (!isDynamic) {
await moveExportedPage(page, page, file, true, 'json') await moveExportedPage(page, page, file, isSsg, 'json')
const revalidationMapPath = i18n if (i18n) {
? `/${i18n.defaultLocale}${page === '/' ? '' : page}` // TODO: do we want to show all locale variants in build output
: page for (const locale of i18n.locales) {
const localePage = `/${locale}${page === '/' ? '' : page}`
finalPrerenderRoutes[page] = {
initialRevalidateSeconds: if (!ssgNotFoundPaths.includes(localePage)) {
exportConfig.initialPageRevalidationMap[revalidationMapPath], finalPrerenderRoutes[localePage] = {
srcRoute: null, initialRevalidateSeconds:
dataRoute: path.posix.join( exportConfig.initialPageRevalidationMap[localePage],
'/_next/data', srcRoute: null,
buildId, dataRoute: path.posix.join(
`${file}.json` '/_next/data',
), buildId,
`${file}.json`
),
}
}
}
} else {
finalPrerenderRoutes[page] = {
initialRevalidateSeconds:
exportConfig.initialPageRevalidationMap[page],
srcRoute: null,
dataRoute: path.posix.join(
'/_next/data',
buildId,
`${file}.json`
),
}
} }
// Set Page Revalidation Interval // Set Page Revalidation Interval
const pageInfo = pageInfos.get(page) const pageInfo = pageInfos.get(page)
if (pageInfo) { if (pageInfo) {
pageInfo.initialRevalidateSeconds = pageInfo.initialRevalidateSeconds =
exportConfig.initialPageRevalidationMap[revalidationMapPath] exportConfig.initialPageRevalidationMap[page]
pageInfos.set(page, pageInfo) pageInfos.set(page, pageInfo)
} }
} else { } else {
...@@ -1191,7 +1209,7 @@ export default async function build( ...@@ -1191,7 +1209,7 @@ export default async function build(
page, page,
route, route,
pageFile, pageFile,
true, isSsg,
'html', 'html',
true true
) )
...@@ -1199,7 +1217,7 @@ export default async function build( ...@@ -1199,7 +1217,7 @@ export default async function build(
page, page,
route, route,
pageFile, pageFile,
true, isSsg,
'json', 'json',
true true
) )
...@@ -1210,7 +1228,7 @@ export default async function build( ...@@ -1210,7 +1228,7 @@ export default async function build(
page, page,
ampPage, ampPage,
ampPage, ampPage,
true, isSsg,
'html', 'html',
true true
) )
...@@ -1218,7 +1236,7 @@ export default async function build( ...@@ -1218,7 +1236,7 @@ export default async function build(
page, page,
ampPage, ampPage,
ampPage, ampPage,
true, isSsg,
'json', 'json',
true true
) )
...@@ -1306,7 +1324,7 @@ export default async function build( ...@@ -1306,7 +1324,7 @@ export default async function build(
} }
}) })
const prerenderManifest: PrerenderManifest = { const prerenderManifest: PrerenderManifest = {
version: 2, version: 3,
routes: finalPrerenderRoutes, routes: finalPrerenderRoutes,
dynamicRoutes: finalDynamicRoutes, dynamicRoutes: finalDynamicRoutes,
notFoundRoutes: ssgNotFoundPaths, notFoundRoutes: ssgNotFoundPaths,
...@@ -1321,10 +1339,11 @@ export default async function build( ...@@ -1321,10 +1339,11 @@ export default async function build(
await generateClientSsgManifest(prerenderManifest, { await generateClientSsgManifest(prerenderManifest, {
distDir, distDir,
buildId, buildId,
locales: config.i18n?.locales || [],
}) })
} else { } else {
const prerenderManifest: PrerenderManifest = { const prerenderManifest: PrerenderManifest = {
version: 2, version: 3,
routes: {}, routes: {},
dynamicRoutes: {}, dynamicRoutes: {},
preview: previewProps, preview: previewProps,
...@@ -1408,13 +1427,17 @@ export type ClientSsgManifest = Set<string> ...@@ -1408,13 +1427,17 @@ export type ClientSsgManifest = Set<string>
function generateClientSsgManifest( function generateClientSsgManifest(
prerenderManifest: PrerenderManifest, prerenderManifest: PrerenderManifest,
{ buildId, distDir }: { buildId: string; distDir: string } {
buildId,
distDir,
locales,
}: { buildId: string; distDir: string; locales: string[] }
) { ) {
const ssgPages: ClientSsgManifest = new Set<string>([ const ssgPages: ClientSsgManifest = new Set<string>([
...Object.entries(prerenderManifest.routes) ...Object.entries(prerenderManifest.routes)
// Filter out dynamic routes // Filter out dynamic routes
.filter(([, { srcRoute }]) => srcRoute == null) .filter(([, { srcRoute }]) => srcRoute == null)
.map(([route]) => route), .map(([route]) => normalizeLocalePath(route, locales).pathname),
...Object.keys(prerenderManifest.dynamicRoutes), ...Object.keys(prerenderManifest.dynamicRoutes),
]) ])
......
...@@ -3,7 +3,6 @@ import LRUCache from 'next/dist/compiled/lru-cache' ...@@ -3,7 +3,6 @@ import LRUCache from 'next/dist/compiled/lru-cache'
import path from 'path' import path from 'path'
import { PrerenderManifest } from '../../build' import { PrerenderManifest } from '../../build'
import { PRERENDER_MANIFEST } from '../lib/constants' import { PRERENDER_MANIFEST } from '../lib/constants'
import { normalizeLocalePath } from '../lib/i18n/normalize-locale-path'
import { normalizePagePath } from './normalize-page-path' import { normalizePagePath } from './normalize-page-path'
function toRoute(pathname: string): string { function toRoute(pathname: string): string {
...@@ -89,10 +88,6 @@ export class IncrementalCache { ...@@ -89,10 +88,6 @@ export class IncrementalCache {
private calculateRevalidate(pathname: string): number | false { private calculateRevalidate(pathname: string): number | false {
pathname = toRoute(pathname) pathname = toRoute(pathname)
if (!this.prerenderManifest.routes[pathname]) {
pathname = toRoute(normalizeLocalePath(pathname, this.locales).pathname)
}
// in development we don't have a prerender-manifest // in development we don't have a prerender-manifest
// and default to always revalidating to allow easier debugging // and default to always revalidating to allow easier debugging
const curTime = new Date().getTime() const curTime = new Date().getTime()
......
...@@ -1434,7 +1434,7 @@ const runTests = (dev = false, isEmulatedServerless = false) => { ...@@ -1434,7 +1434,7 @@ const runTests = (dev = false, isEmulatedServerless = false) => {
} }
}) })
expect(manifest.version).toBe(2) expect(manifest.version).toBe(3)
expect(manifest.routes).toEqual(expectedManifestRoutes()) expect(manifest.routes).toEqual(expectedManifestRoutes())
expect(manifest.dynamicRoutes).toEqual({ expect(manifest.dynamicRoutes).toEqual({
'/api-docs/[...slug]': { '/api-docs/[...slug]': {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment