From 52b6f518d5081fd5a33b7ecab5cd9cda454d2a74 Mon Sep 17 00:00:00 2001 From: JJ Kasper Date: Thu, 15 Oct 2020 20:00:38 -0500 Subject: [PATCH] Fix initialRevalidateSeconds manifest field with i18n --- packages/next/build/index.ts | 10 +- .../i18n-support/test/index.test.js | 104 +++++++++++++++++- 2 files changed, 109 insertions(+), 5 deletions(-) diff --git a/packages/next/build/index.ts b/packages/next/build/index.ts index 15e66148176a..05f371f6d910 100644 --- a/packages/next/build/index.ts +++ b/packages/next/build/index.ts @@ -959,13 +959,19 @@ export default async function build( } if (isSsg) { + const { i18n } = config.experimental + // For a non-dynamic SSG page, we must copy its data file from export. if (!isDynamic) { await moveExportedPage(page, page, file, true, 'json') + const revalidationMapPath = i18n + ? `/${i18n.defaultLocale}${page}` + : page + finalPrerenderRoutes[page] = { initialRevalidateSeconds: - exportConfig.initialPageRevalidationMap[page], + exportConfig.initialPageRevalidationMap[revalidationMapPath], srcRoute: null, dataRoute: path.posix.join('/_next/data', buildId, `${file}.json`), } @@ -973,7 +979,7 @@ export default async function build( const pageInfo = pageInfos.get(page) if (pageInfo) { pageInfo.initialRevalidateSeconds = - exportConfig.initialPageRevalidationMap[page] + exportConfig.initialPageRevalidationMap[revalidationMapPath] pageInfos.set(page, pageInfo) } } else { diff --git a/test/integration/i18n-support/test/index.test.js b/test/integration/i18n-support/test/index.test.js index 57bba02a8bcd..c375fc27b3d4 100644 --- a/test/integration/i18n-support/test/index.test.js +++ b/test/integration/i18n-support/test/index.test.js @@ -5,6 +5,7 @@ import fs from 'fs-extra' import cheerio from 'cheerio' import { join } from 'path' import webdriver from 'next-webdriver' +import escapeRegex from 'escape-string-regexp' import { fetchViaHTTP, findPort, @@ -15,6 +16,7 @@ import { renderViaHTTP, File, waitFor, + normalizeRegEx, } from 'next-test-utils' jest.setTimeout(1000 * 60 * 2) @@ -24,7 +26,7 @@ const nextConfig = new File(join(appDir, 'next.config.js')) let app let appPort let buildPagesDir -// let buildId +let buildId const locales = ['en-US', 'nl-NL', 'nl-BE', 'nl', 'fr-BE', 'fr', 'en'] @@ -59,6 +61,102 @@ function runTests(isDev) { ], }) }) + + it('should output correct prerender-manifest', async () => { + const prerenderManifest = await fs.readJSON( + join(appDir, '.next/prerender-manifest.json') + ) + + for (const key of Object.keys(prerenderManifest.dynamicRoutes)) { + const item = prerenderManifest.dynamicRoutes[key] + item.routeRegex = normalizeRegEx(item.routeRegex) + item.dataRouteRegex = normalizeRegEx(item.dataRouteRegex) + } + + expect(prerenderManifest.routes).toEqual({ + '/en-US/gsp/fallback/first': { + dataRoute: `/_next/data/${buildId}/en-US/gsp/fallback/first.json`, + initialRevalidateSeconds: false, + srcRoute: '/gsp/fallback/[slug]', + }, + '/en-US/gsp/fallback/second': { + dataRoute: `/_next/data/${buildId}/en-US/gsp/fallback/second.json`, + initialRevalidateSeconds: false, + srcRoute: '/gsp/fallback/[slug]', + }, + '/en-US/gsp/no-fallback/first': { + dataRoute: `/_next/data/${buildId}/en-US/gsp/no-fallback/first.json`, + initialRevalidateSeconds: false, + srcRoute: '/gsp/no-fallback/[slug]', + }, + '/en-US/gsp/no-fallback/second': { + dataRoute: `/_next/data/${buildId}/en-US/gsp/no-fallback/second.json`, + initialRevalidateSeconds: false, + srcRoute: '/gsp/no-fallback/[slug]', + }, + '/en-US/not-found/fallback/first': { + dataRoute: `/_next/data/${buildId}/en-US/not-found/fallback/first.json`, + initialRevalidateSeconds: false, + srcRoute: '/not-found/fallback/[slug]', + }, + '/en-US/not-found/fallback/second': { + dataRoute: `/_next/data/${buildId}/en-US/not-found/fallback/second.json`, + initialRevalidateSeconds: false, + srcRoute: '/not-found/fallback/[slug]', + }, + '/gsp': { + dataRoute: `/_next/data/${buildId}/gsp.json`, + srcRoute: null, + initialRevalidateSeconds: false, + }, + '/nl-NL/gsp/no-fallback/second': { + dataRoute: `/_next/data/${buildId}/nl-NL/gsp/no-fallback/second.json`, + initialRevalidateSeconds: false, + srcRoute: '/gsp/no-fallback/[slug]', + }, + '/not-found': { + dataRoute: `/_next/data/${buildId}/not-found.json`, + srcRoute: null, + initialRevalidateSeconds: false, + }, + }) + expect(prerenderManifest.dynamicRoutes).toEqual({ + '/gsp/fallback/[slug]': { + routeRegex: normalizeRegEx( + '^\\/gsp\\/fallback\\/([^\\/]+?)(?:\\/)?$' + ), + dataRoute: `/_next/data/${buildId}/gsp/fallback/[slug].json`, + fallback: '/gsp/fallback/[slug].html', + dataRouteRegex: normalizeRegEx( + `^\\/_next\\/data\\/${escapeRegex( + buildId + )}\\/gsp\\/fallback\\/([^\\/]+?)\\.json$` + ), + }, + '/gsp/no-fallback/[slug]': { + routeRegex: normalizeRegEx( + '^\\/gsp\\/no\\-fallback\\/([^\\/]+?)(?:\\/)?$' + ), + dataRoute: `/_next/data/${buildId}/gsp/no-fallback/[slug].json`, + fallback: false, + dataRouteRegex: normalizeRegEx( + `^/_next/data/${escapeRegex( + buildId + )}/gsp/no\\-fallback/([^/]+?)\\.json$` + ), + }, + '/not-found/fallback/[slug]': { + dataRoute: `/_next/data/${buildId}/not-found/fallback/[slug].json`, + dataRouteRegex: normalizeRegEx( + `^\\/_next\\/data\\/${escapeRegex( + buildId + )}\\/not\\-found\\/fallback\\/([^\\/]+?)\\.json$` + ), + fallback: '/not-found/fallback/[slug].html', + routeRegex: normalizeRegEx('^/not\\-found/fallback/([^/]+?)(?:/)?$'), + }, + }) + }) } it('should navigate with locale prop correctly', async () => { @@ -1145,7 +1243,7 @@ describe('i18n Support', () => { appPort = await findPort() app = await nextStart(appDir, appPort) buildPagesDir = join(appDir, '.next/server/pages') - // buildId = await fs.readFile(join(appDir, '.next/BUILD_ID'), 'utf8') + buildId = await fs.readFile(join(appDir, '.next/BUILD_ID'), 'utf8') }) afterAll(() => killApp(app)) @@ -1161,7 +1259,7 @@ describe('i18n Support', () => { appPort = await findPort() app = await nextStart(appDir, appPort) buildPagesDir = join(appDir, '.next/serverless/pages') - // buildId = await fs.readFile(join(appDir, '.next/BUILD_ID'), 'utf8') + buildId = await fs.readFile(join(appDir, '.next/BUILD_ID'), 'utf8') }) afterAll(async () => { nextConfig.restore()