Unverified Commit 2170dfd1 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Update to generate auto static pages with all locales (#17730)

Follow-up PR to #17370 this adds generating auto-export, non-dynamic SSG, and fallback pages with all locales. Dynamic SSG pages still control which locales the pages are generated with using `getStaticPaths`. To further control which locales non-dynamic SSG pages will be prerendered with a follow-up PR adding handling for 404 behavior from `getStaticProps` will be needed. 

x-ref: https://github.com/vercel/next.js/issues/17110
parent 62b9183e
...@@ -725,6 +725,8 @@ export default async function build( ...@@ -725,6 +725,8 @@ 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.experimental
// 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
...@@ -738,7 +740,14 @@ export default async function build( ...@@ -738,7 +740,14 @@ export default async function build(
if (ssgStaticFallbackPages.has(page)) { if (ssgStaticFallbackPages.has(page)) {
// Override the rendering for the dynamic page to be treated as a // Override the rendering for the dynamic page to be treated as a
// fallback render. // fallback render.
if (i18n) {
defaultMap[`/${i18n.defaultLocale}${page}`] = {
page,
query: { __nextFallback: true },
}
} else {
defaultMap[page] = { page, query: { __nextFallback: true } } defaultMap[page] = { page, query: { __nextFallback: true } }
}
} else { } else {
// Remove dynamically routed pages from the default path map when // Remove dynamically routed pages from the default path map when
// fallback behavior is disabled. // fallback behavior is disabled.
...@@ -760,6 +769,39 @@ export default async function build( ...@@ -760,6 +769,39 @@ export default async function build(
} }
} }
if (i18n) {
for (const page of [
...staticPages,
...ssgPages,
...(useStatic404 ? ['/404'] : []),
]) {
const isSsg = ssgPages.has(page)
const isDynamic = isDynamicRoute(page)
const isFallback = isSsg && ssgStaticFallbackPages.has(page)
for (const locale of i18n.locales) {
if (!isSsg && locale === i18n.defaultLocale) continue
// skip fallback generation for SSG pages without fallback mode
if (isSsg && isDynamic && !isFallback) continue
const outputPath = `/${locale}${page === '/' ? '' : page}`
defaultMap[outputPath] = {
page: defaultMap[page].page,
query: { __nextLocale: locale },
}
if (isFallback) {
defaultMap[outputPath].query.__nextFallback = true
}
}
if (isSsg && !isFallback) {
// remove non-locale prefixed variant from defaultMap
delete defaultMap[page]
}
}
}
return defaultMap return defaultMap
}, },
trailingSlash: false, trailingSlash: false,
...@@ -786,7 +828,8 @@ export default async function build( ...@@ -786,7 +828,8 @@ export default async function build(
page: string, page: string,
file: string, file: string,
isSsg: boolean, isSsg: boolean,
ext: 'html' | 'json' ext: 'html' | 'json',
additionalSsgFile = false
) => { ) => {
file = `${file}.${ext}` file = `${file}.${ext}`
const orig = path.join(exportOptions.outdir, file) const orig = path.join(exportOptions.outdir, file)
...@@ -820,10 +863,60 @@ export default async function build( ...@@ -820,10 +863,60 @@ export default async function build(
if (!isSsg) { if (!isSsg) {
pagesManifest[page] = relativeDest pagesManifest[page] = relativeDest
} }
const { i18n } = config.experimental
// for SSG files with i18n the non-prerendered variants are
// output with the locale prefixed so don't attempt moving
// without the prefix
if (!i18n || !isSsg || additionalSsgFile) {
await promises.mkdir(path.dirname(dest), { recursive: true }) await promises.mkdir(path.dirname(dest), { recursive: true })
await promises.rename(orig, dest) await promises.rename(orig, dest)
} }
if (i18n) {
if (additionalSsgFile) return
for (const locale of i18n.locales) {
// auto-export default locale files exist at root
// TODO: should these always be prefixed with locale
// similar to SSG prerender/fallback files?
if (!isSsg && locale === i18n.defaultLocale) {
continue
}
const localeExt = page === '/' ? path.extname(file) : ''
const relativeDestNoPages = relativeDest.substr('pages/'.length)
const updatedRelativeDest = path.join(
'pages',
locale + localeExt,
// if it's the top-most index page we want it to be locale.EXT
// instead of locale/index.html
page === '/' ? '' : relativeDestNoPages
)
const updatedOrig = path.join(
exportOptions.outdir,
locale + localeExt,
page === '/' ? '' : file
)
const updatedDest = path.join(
distDir,
isLikeServerless ? SERVERLESS_DIRECTORY : SERVER_DIRECTORY,
updatedRelativeDest
)
if (!isSsg) {
pagesManifest[
`/${locale}${page === '/' ? '' : page}`
] = updatedRelativeDest
}
await promises.mkdir(path.dirname(updatedDest), { recursive: true })
await promises.rename(updatedOrig, updatedDest)
}
}
}
// Only move /404 to /404 when there is no custom 404 as in that case we don't know about the 404 page // Only move /404 to /404 when there is no custom 404 as in that case we don't know about the 404 page
if (!hasPages404 && useStatic404) { if (!hasPages404 && useStatic404) {
await moveExportedPage('/_error', '/404', '/404', false, 'html') await moveExportedPage('/_error', '/404', '/404', false, 'html')
...@@ -877,13 +970,13 @@ export default async function build( ...@@ -877,13 +970,13 @@ export default async function build(
const extraRoutes = additionalSsgPaths.get(page) || [] const extraRoutes = additionalSsgPaths.get(page) || []
for (const route of extraRoutes) { for (const route of extraRoutes) {
const pageFile = normalizePagePath(route) const pageFile = normalizePagePath(route)
await moveExportedPage(page, route, pageFile, true, 'html') await moveExportedPage(page, route, pageFile, true, 'html', true)
await moveExportedPage(page, route, pageFile, true, 'json') await moveExportedPage(page, route, pageFile, true, 'json', true)
if (hasAmp) { if (hasAmp) {
const ampPage = `${pageFile}.amp` const ampPage = `${pageFile}.amp`
await moveExportedPage(page, ampPage, ampPage, true, 'html') await moveExportedPage(page, ampPage, ampPage, true, 'html', true)
await moveExportedPage(page, ampPage, ampPage, true, 'json') await moveExportedPage(page, ampPage, ampPage, true, 'json', true)
} }
finalPrerenderRoutes[route] = { finalPrerenderRoutes[route] = {
......
...@@ -242,6 +242,7 @@ const nextServerlessLoader: loader.Loader = function () { ...@@ -242,6 +242,7 @@ const nextServerlessLoader: loader.Loader = function () {
detectedLocale = detectedLocale || i18n.defaultLocale detectedLocale = detectedLocale || i18n.defaultLocale
if ( if (
!fromExport &&
!nextStartMode && !nextStartMode &&
i18n.localeDetection !== false && i18n.localeDetection !== false &&
(shouldAddLocalePrefix || shouldStripDefaultLocale) (shouldAddLocalePrefix || shouldStripDefaultLocale)
......
...@@ -100,14 +100,21 @@ export default async function exportPage({ ...@@ -100,14 +100,21 @@ export default async function exportPage({
const { page } = pathMap const { page } = pathMap
const filePath = normalizePagePath(path) const filePath = normalizePagePath(path)
const ampPath = `${filePath}.amp` const ampPath = `${filePath}.amp`
const isDynamic = isDynamicRoute(page)
let query = { ...originalQuery } let query = { ...originalQuery }
let params: { [key: string]: string | string[] } | undefined let params: { [key: string]: string | string[] } | undefined
let updatedPath = path
let locale = query.__nextLocale || renderOpts.locale
delete query.__nextLocale
if (renderOpts.locale) {
const localePathResult = normalizeLocalePath(path, renderOpts.locales) const localePathResult = normalizeLocalePath(path, renderOpts.locales)
if (localePathResult.detectedLocale) { if (localePathResult.detectedLocale) {
path = localePathResult.pathname updatedPath = localePathResult.pathname
renderOpts.locale = localePathResult.detectedLocale locale = localePathResult.detectedLocale
}
} }
// We need to show a warning if they try to provide query values // We need to show a warning if they try to provide query values
...@@ -122,8 +129,8 @@ export default async function exportPage({ ...@@ -122,8 +129,8 @@ export default async function exportPage({
} }
// Check if the page is a specified dynamic route // Check if the page is a specified dynamic route
if (isDynamicRoute(page) && page !== path) { if (isDynamic && page !== path) {
params = getRouteMatcher(getRouteRegex(page))(path) || undefined params = getRouteMatcher(getRouteRegex(page))(updatedPath) || undefined
if (params) { if (params) {
// we have to pass these separately for serverless // we have to pass these separately for serverless
if (!serverless) { if (!serverless) {
...@@ -134,7 +141,7 @@ export default async function exportPage({ ...@@ -134,7 +141,7 @@ export default async function exportPage({
} }
} else { } else {
throw new Error( throw new Error(
`The provided export path '${path}' doesn't match the '${page}' page.\nRead more: https://err.sh/vercel/next.js/export-path-mismatch` `The provided export path '${updatedPath}' doesn't match the '${page}' page.\nRead more: https://err.sh/vercel/next.js/export-path-mismatch`
) )
} }
} }
...@@ -149,7 +156,7 @@ export default async function exportPage({ ...@@ -149,7 +156,7 @@ export default async function exportPage({
} }
const req = ({ const req = ({
url: path, url: updatedPath,
...headerMocks, ...headerMocks,
} as unknown) as IncomingMessage } as unknown) as IncomingMessage
const res = ({ const res = ({
...@@ -239,7 +246,7 @@ export default async function exportPage({ ...@@ -239,7 +246,7 @@ export default async function exportPage({
fontManifest: optimizeFonts fontManifest: optimizeFonts
? requireFontManifest(distDir, serverless) ? requireFontManifest(distDir, serverless)
: null, : null,
locale: renderOpts.locale!, locale: locale!,
locales: renderOpts.locales!, locales: renderOpts.locales!,
}, },
// @ts-ignore // @ts-ignore
...@@ -298,6 +305,7 @@ export default async function exportPage({ ...@@ -298,6 +305,7 @@ export default async function exportPage({
fontManifest: optimizeFonts fontManifest: optimizeFonts
? requireFontManifest(distDir, serverless) ? requireFontManifest(distDir, serverless)
: null, : null,
locale: locale as string,
} }
// @ts-ignore // @ts-ignore
html = await renderMethod(req, res, page, query, curRenderOpts) html = await renderMethod(req, res, page, query, curRenderOpts)
......
...@@ -354,7 +354,7 @@ export default class Server { ...@@ -354,7 +354,7 @@ export default class Server {
parsedUrl.pathname = localePathResult.pathname parsedUrl.pathname = localePathResult.pathname
} }
;(req as any)._nextLocale = detectedLocale || i18n.defaultLocale parsedUrl.query.__nextLocale = detectedLocale || i18n.defaultLocale
} }
res.statusCode = 200 res.statusCode = 200
...@@ -510,7 +510,7 @@ export default class Server { ...@@ -510,7 +510,7 @@ export default class Server {
pathname = localePathResult.pathname pathname = localePathResult.pathname
detectedLocale = localePathResult.detectedLocale detectedLocale = localePathResult.detectedLocale
} }
;(req as any)._nextLocale = detectedLocale || i18n.defaultLocale _parsedUrl.query.__nextLocale = detectedLocale || i18n.defaultLocale
} }
pathname = getRouteFromAssetPath(pathname, '.json') pathname = getRouteFromAssetPath(pathname, '.json')
...@@ -1015,11 +1015,21 @@ export default class Server { ...@@ -1015,11 +1015,21 @@ export default class Server {
query: ParsedUrlQuery = {}, query: ParsedUrlQuery = {},
params: Params | null = null params: Params | null = null
): Promise<FindComponentsResult | null> { ): Promise<FindComponentsResult | null> {
const paths = [ let paths = [
// try serving a static AMP version first // try serving a static AMP version first
query.amp ? normalizePagePath(pathname) + '.amp' : null, query.amp ? normalizePagePath(pathname) + '.amp' : null,
pathname, pathname,
].filter(Boolean) ].filter(Boolean)
if (query.__nextLocale) {
paths = [
...paths.map(
(path) => `/${query.__nextLocale}${path === '/' ? '' : path}`
),
...paths,
]
}
for (const pagePath of paths) { for (const pagePath of paths) {
try { try {
const components = await loadComponents( const components = await loadComponents(
...@@ -1031,7 +1041,11 @@ export default class Server { ...@@ -1031,7 +1041,11 @@ export default class Server {
components, components,
query: { query: {
...(components.getStaticProps ...(components.getStaticProps
? { _nextDataReq: query._nextDataReq, amp: query.amp } ? {
amp: query.amp,
_nextDataReq: query._nextDataReq,
__nextLocale: query.__nextLocale,
}
: query), : query),
...(params || {}), ...(params || {}),
}, },
...@@ -1141,7 +1155,8 @@ export default class Server { ...@@ -1141,7 +1155,8 @@ export default class Server {
urlPathname = stripNextDataPath(urlPathname) urlPathname = stripNextDataPath(urlPathname)
} }
const locale = (req as any)._nextLocale const locale = query.__nextLocale as string
delete query.__nextLocale
const ssgCacheKey = const ssgCacheKey =
isPreviewMode || !isSSG isPreviewMode || !isSSG
...@@ -1214,7 +1229,7 @@ export default class Server { ...@@ -1214,7 +1229,7 @@ export default class Server {
'passthrough', 'passthrough',
{ {
fontManifest: this.renderOpts.fontManifest, fontManifest: this.renderOpts.fontManifest,
locale: (req as any)._nextLocale, locale,
locales: this.renderOpts.locales, locales: this.renderOpts.locales,
} }
) )
...@@ -1235,7 +1250,7 @@ export default class Server { ...@@ -1235,7 +1250,7 @@ export default class Server {
...opts, ...opts,
isDataReq, isDataReq,
resolvedUrl, resolvedUrl,
locale: (req as any)._nextLocale, locale,
// For getServerSideProps we need to ensure we use the original URL // For getServerSideProps we need to ensure we use the original URL
// and not the resolved URL to prevent a hydration mismatch on // and not the resolved URL to prevent a hydration mismatch on
// asPath // asPath
...@@ -1321,7 +1336,9 @@ export default class Server { ...@@ -1321,7 +1336,9 @@ export default class Server {
// Production already emitted the fallback as static HTML. // Production already emitted the fallback as static HTML.
if (isProduction) { if (isProduction) {
html = await this.incrementalCache.getFallback(pathname) html = await this.incrementalCache.getFallback(
locale ? `/${locale}${pathname}` : pathname
)
} }
// We need to generate the fallback on-demand for development. // We need to generate the fallback on-demand for development.
else { else {
...@@ -1442,7 +1459,6 @@ export default class Server { ...@@ -1442,7 +1459,6 @@ export default class Server {
res.statusCode = 500 res.statusCode = 500
return await this.renderErrorToHTML(err, req, res, pathname, query) return await this.renderErrorToHTML(err, req, res, pathname, query)
} }
res.statusCode = 404 res.statusCode = 404
return await this.renderErrorToHTML(null, req, res, pathname, query) return await this.renderErrorToHTML(null, req, res, pathname, query)
} }
...@@ -1488,7 +1504,7 @@ export default class Server { ...@@ -1488,7 +1504,7 @@ export default class Server {
// use static 404 page if available and is 404 response // use static 404 page if available and is 404 response
if (is404) { if (is404) {
result = await this.findPageComponents('/404') result = await this.findPageComponents('/404', query)
using404Page = result !== null using404Page = result !== null
} }
......
...@@ -413,6 +413,7 @@ export async function renderToHTML( ...@@ -413,6 +413,7 @@ export async function renderToHTML(
const isFallback = !!query.__nextFallback const isFallback = !!query.__nextFallback
delete query.__nextFallback delete query.__nextFallback
delete query.__nextLocale
const isSSG = !!getStaticProps const isSSG = !!getStaticProps
const isBuildTimeSSG = isSSG && renderOpts.nextExport const isBuildTimeSSG = isSSG && renderOpts.nextExport
...@@ -506,9 +507,6 @@ export async function renderToHTML( ...@@ -506,9 +507,6 @@ export async function renderToHTML(
} }
if (isAutoExport) renderOpts.autoExport = true if (isAutoExport) renderOpts.autoExport = true
if (isSSG) renderOpts.nextExport = false if (isSSG) renderOpts.nextExport = false
// don't set default locale for fallback pages since this needs to be
// handled at request time
if (isFallback) renderOpts.locale = undefined
await Loadable.preloadAll() // Make sure all dynamic imports are loaded await Loadable.preloadAll() // Make sure all dynamic imports are loaded
......
import Link from 'next/link'
import { useRouter } from 'next/router'
export default function Page(props) {
const router = useRouter()
return (
<>
<p id="auto-export">auto-export page</p>
<p id="props">{JSON.stringify(props)}</p>
<p id="router-locale">{router.locale}</p>
<p id="router-locales">{JSON.stringify(router.locales)}</p>
<p id="router-query">{JSON.stringify(router.query)}</p>
<p id="router-pathname">{router.pathname}</p>
<p id="router-as-path">{router.asPath}</p>
<Link href="/">
<a id="to-index">to /</a>
</Link>
</>
)
}
...@@ -44,12 +44,3 @@ export default function Page(props) { ...@@ -44,12 +44,3 @@ export default function Page(props) {
</> </>
) )
} }
export const getServerSideProps = ({ locale, locales }) => {
return {
props: {
locale,
locales,
},
}
}
...@@ -27,6 +27,52 @@ let appPort ...@@ -27,6 +27,52 @@ let appPort
const locales = ['en-US', 'nl-NL', 'nl-BE', 'nl', 'en'] const locales = ['en-US', 'nl-NL', 'nl-BE', 'nl', 'en']
function runTests() { function runTests() {
it('should generate fallbacks with all locales', async () => {
for (const locale of locales) {
const html = await renderViaHTTP(
appPort,
`/${locale}/gsp/fallback/${Math.random()}`
)
const $ = cheerio.load(html)
expect($('html').attr('lang')).toBe(locale)
}
})
it('should generate auto-export page with all locales', async () => {
for (const locale of locales) {
const html = await renderViaHTTP(appPort, `/${locale}`)
const $ = cheerio.load(html)
expect($('html').attr('lang')).toBe(locale)
expect($('#router-locale').text()).toBe(locale)
expect($('#router-as-path').text()).toBe('/')
expect($('#router-pathname').text()).toBe('/')
expect(JSON.parse($('#router-locales').text())).toEqual(locales)
const html2 = await renderViaHTTP(appPort, `/${locale}/auto-export`)
const $2 = cheerio.load(html2)
expect($2('html').attr('lang')).toBe(locale)
expect($2('#router-locale').text()).toBe(locale)
expect($2('#router-as-path').text()).toBe('/auto-export')
expect($2('#router-pathname').text()).toBe('/auto-export')
expect(JSON.parse($2('#router-locales').text())).toEqual(locales)
}
})
it('should generate non-dynamic SSG page with all locales', async () => {
for (const locale of locales) {
const html = await renderViaHTTP(appPort, `/${locale}/gsp`)
const $ = cheerio.load(html)
expect($('html').attr('lang')).toBe(locale)
expect($('#router-locale').text()).toBe(locale)
expect($('#router-as-path').text()).toBe('/gsp')
expect($('#router-pathname').text()).toBe('/gsp')
expect(JSON.parse($('#router-locales').text())).toEqual(locales)
}
})
// TODO: SSG 404 behavior to opt-out of generating specific locale
// for non-dynamic SSG pages
it('should remove un-necessary locale prefix for default locale', async () => { it('should remove un-necessary locale prefix for default locale', async () => {
const res = await fetchViaHTTP(appPort, '/en-US', undefined, { const res = await fetchViaHTTP(appPort, '/en-US', undefined, {
redirect: 'manual', redirect: 'manual',
...@@ -92,9 +138,7 @@ function runTests() { ...@@ -92,9 +138,7 @@ function runTests() {
).toEqual({ ).toEqual({
slug: 'another', slug: 'another',
}) })
// TODO: this will be fixed after the fallback is generated for all locales expect(await browser.elementByCss('#router-locale').text()).toBe('en-US')
// instead of delaying populating the locale on the client
// expect(await browser.elementByCss('#router-locale').text()).toBe('en')
expect( expect(
JSON.parse(await browser.elementByCss('#router-locales').text()) JSON.parse(await browser.elementByCss('#router-locales').text())
).toEqual(locales) ).toEqual(locales)
...@@ -189,12 +233,12 @@ function runTests() { ...@@ -189,12 +233,12 @@ function runTests() {
}) })
it('should load getStaticProps fallback non-prerender page correctly', async () => { it('should load getStaticProps fallback non-prerender page correctly', async () => {
const browser = await webdriver(appPort, '/en-US/gsp/fallback/another') const browser = await webdriver(appPort, '/en/gsp/fallback/another')
await browser.waitForElementByCss('#props') await browser.waitForElementByCss('#props')
expect(JSON.parse(await browser.elementByCss('#props').text())).toEqual({ expect(JSON.parse(await browser.elementByCss('#props').text())).toEqual({
locale: 'en-US', locale: 'en',
locales, locales,
params: { params: {
slug: 'another', slug: 'another',
...@@ -205,16 +249,12 @@ function runTests() { ...@@ -205,16 +249,12 @@ function runTests() {
).toEqual({ ).toEqual({
slug: 'another', slug: 'another',
}) })
expect(await browser.elementByCss('#router-locale').text()).toBe('en-US') expect(await browser.elementByCss('#router-locale').text()).toBe('en')
expect( expect(
JSON.parse(await browser.elementByCss('#router-locales').text()) JSON.parse(await browser.elementByCss('#router-locales').text())
).toEqual(locales) ).toEqual(locales)
// TODO: this will be fixed after fallback pages are generated expect(await browser.elementByCss('html').getAttribute('lang')).toBe('en')
// for all locales
// expect(
// await browser.elementByCss('html').getAttribute('lang')
// ).toBe('en-US')
}) })
it('should load getServerSideProps page correctly SSR (default locale no prefix)', async () => { it('should load getServerSideProps page correctly SSR (default locale no prefix)', async () => {
...@@ -239,10 +279,6 @@ function runTests() { ...@@ -239,10 +279,6 @@ function runTests() {
await browser.get(browser.initUrl) await browser.get(browser.initUrl)
const checkIndexValues = async () => { const checkIndexValues = async () => {
expect(JSON.parse(await browser.elementByCss('#props').text())).toEqual({
locale: 'en-US',
locales,
})
expect(await browser.elementByCss('#router-locale').text()).toBe('en-US') expect(await browser.elementByCss('#router-locale').text()).toBe('en-US')
expect( expect(
JSON.parse(await browser.elementByCss('#router-locales').text()) JSON.parse(await browser.elementByCss('#router-locales').text())
...@@ -542,6 +578,7 @@ function runTests() { ...@@ -542,6 +578,7 @@ function runTests() {
} }
describe('i18n Support', () => { describe('i18n Support', () => {
// TODO: test with next export?
describe('dev mode', () => { describe('dev mode', () => {
beforeAll(async () => { beforeAll(async () => {
await fs.remove(join(appDir, '.next')) await fs.remove(join(appDir, '.next'))
......
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