Unverified Commit afa73ae4 authored by Joe Haddad's avatar Joe Haddad Committed by GitHub

Update Prerender Manifest (#8918)

* Update Prerender Manifest

* Fix typescript
parent 0bcd1fc3
import chalk from 'chalk' import chalk from 'chalk'
import fs from 'fs' import fs from 'fs'
import Worker from 'jest-worker'
import mkdirpOrig from 'mkdirp' import mkdirpOrig from 'mkdirp'
import nanoid from 'next/dist/compiled/nanoid/index.js'
import path from 'path'
import { promisify } from 'util'
import formatWebpackMessages from '../client/dev/error-overlay/format-webpack-messages'
import { PUBLIC_DIR_MIDDLEWARE_CONFLICT } from '../lib/constants'
import { findPagesDir } from '../lib/find-pages-dir'
import { recursiveDelete } from '../lib/recursive-delete'
import { recursiveReadDir } from '../lib/recursive-readdir'
import { verifyTypeScriptSetup } from '../lib/verifyTypeScriptSetup'
import { import {
PAGES_MANIFEST,
BUILD_MANIFEST, BUILD_MANIFEST,
PAGES_MANIFEST,
PHASE_PRODUCTION_BUILD, PHASE_PRODUCTION_BUILD,
PRERENDER_MANIFEST, PRERENDER_MANIFEST,
SERVER_DIRECTORY, SERVER_DIRECTORY,
SERVERLESS_DIRECTORY, SERVERLESS_DIRECTORY,
} from '../next-server/lib/constants' } from '../next-server/lib/constants'
import { getRouteRegex, isDynamicRoute } from '../next-server/lib/router/utils'
import loadConfig, { import loadConfig, {
isTargetLikeServerless, isTargetLikeServerless,
} from '../next-server/server/config' } from '../next-server/server/config'
import nanoid from 'next/dist/compiled/nanoid/index.js'
import path from 'path'
import { promisify } from 'util'
import Worker from 'jest-worker'
import formatWebpackMessages from '../client/dev/error-overlay/format-webpack-messages'
import { recursiveDelete } from '../lib/recursive-delete'
import { recursiveReadDir } from '../lib/recursive-readdir'
import { verifyTypeScriptSetup } from '../lib/verifyTypeScriptSetup'
import { import {
recordBuildDuration, recordBuildDuration,
recordBuildOptimize, recordBuildOptimize,
...@@ -31,6 +34,7 @@ import { CompilerResult, runCompiler } from './compiler' ...@@ -31,6 +34,7 @@ import { CompilerResult, runCompiler } from './compiler'
import { createEntrypoints, createPagesMapping } from './entries' import { createEntrypoints, createPagesMapping } from './entries'
import { generateBuildId } from './generate-build-id' import { generateBuildId } from './generate-build-id'
import { isWriteable } from './is-writeable' import { isWriteable } from './is-writeable'
import createSpinner from './spinner'
import { import {
collectPages, collectPages,
getPageSizeInKb, getPageSizeInKb,
...@@ -41,10 +45,6 @@ import { ...@@ -41,10 +45,6 @@ import {
import getBaseWebpackConfig from './webpack-config' import getBaseWebpackConfig from './webpack-config'
import { getPageChunks } from './webpack/plugins/chunk-graph-plugin' import { getPageChunks } from './webpack/plugins/chunk-graph-plugin'
import { writeBuildId } from './write-build-id' import { writeBuildId } from './write-build-id'
import { findPagesDir } from '../lib/find-pages-dir'
import createSpinner from './spinner'
import { PUBLIC_DIR_MIDDLEWARE_CONFLICT } from '../lib/constants'
import { isDynamicRoute } from '../next-server/lib/router/utils'
const fsUnlink = promisify(fs.unlink) const fsUnlink = promisify(fs.unlink)
const fsRmdir = promisify(fs.rmdir) const fsRmdir = promisify(fs.rmdir)
...@@ -58,12 +58,20 @@ const staticCheckWorker = require.resolve('./utils') ...@@ -58,12 +58,20 @@ const staticCheckWorker = require.resolve('./utils')
export type SprRoute = { export type SprRoute = {
initialRevalidateSeconds: number | false initialRevalidateSeconds: number | false
dataRoute: string
}
export type DynamicSprRoute = {
routeRegex: string
dataRoute: string
dataRouteRegex: string
} }
export type PrerenderManifest = { export type PrerenderManifest = {
version: number version: number
routes: { [route: string]: SprRoute } routes: { [route: string]: SprRoute }
dynamicRoutes: string[] dynamicRoutes: { [route: string]: DynamicSprRoute }
} }
export default async function build(dir: string, conf = null): Promise<void> { export default async function build(dir: string, conf = null): Promise<void> {
...@@ -513,6 +521,10 @@ export default async function build(dir: string, conf = null): Promise<void> { ...@@ -513,6 +521,10 @@ export default async function build(dir: string, conf = null): Promise<void> {
finalPrerenderRoutes[page] = { finalPrerenderRoutes[page] = {
initialRevalidateSeconds: initialRevalidateSeconds:
exportConfig.initialPageRevalidationMap[page], exportConfig.initialPageRevalidationMap[page],
dataRoute: path.posix.join(
'/_next/data',
`${page === '/' ? '/index' : page}.json`
),
} }
} else { } else {
// For a dynamic SPR page, we did not copy its html nor data exports. // For a dynamic SPR page, we did not copy its html nor data exports.
...@@ -525,6 +537,10 @@ export default async function build(dir: string, conf = null): Promise<void> { ...@@ -525,6 +537,10 @@ export default async function build(dir: string, conf = null): Promise<void> {
finalPrerenderRoutes[route] = { finalPrerenderRoutes[route] = {
initialRevalidateSeconds: initialRevalidateSeconds:
exportConfig.initialPageRevalidationMap[route], exportConfig.initialPageRevalidationMap[route],
dataRoute: path.posix.join(
'/_next/data',
`${route === '/' ? '/index' : route}.json`
),
} }
} }
} }
...@@ -550,10 +566,25 @@ export default async function build(dir: string, conf = null): Promise<void> { ...@@ -550,10 +566,25 @@ export default async function build(dir: string, conf = null): Promise<void> {
) )
if (sprPages.size > 0) { if (sprPages.size > 0) {
const finalDynamicRoutes: PrerenderManifest['dynamicRoutes'] = {}
tbdPrerenderRoutes.forEach(tbdRoute => {
const dataRoute = path.posix.join(
'/_next/data',
`${tbdRoute === '/' ? '/index' : tbdRoute}.json`
)
finalDynamicRoutes[tbdRoute] = {
routeRegex: getRouteRegex(tbdRoute).re.source,
dataRoute,
dataRouteRegex: getRouteRegex(
dataRoute.replace(/\.json$/, '')
).re.source.replace(/\(\?:\\\/\)\?\$$/, '\\.json$'),
}
})
const prerenderManifest: PrerenderManifest = { const prerenderManifest: PrerenderManifest = {
version: 1, version: 1,
routes: finalPrerenderRoutes, routes: finalPrerenderRoutes,
dynamicRoutes: tbdPrerenderRoutes.sort(), dynamicRoutes: finalDynamicRoutes,
} }
await fsWriteFile( await fsWriteFile(
......
...@@ -77,7 +77,7 @@ export function initializeSprCache({ ...@@ -77,7 +77,7 @@ export function initializeSprCache({
fs.readFileSync(path.join(distDir, PRERENDER_MANIFEST), 'utf8') fs.readFileSync(path.join(distDir, PRERENDER_MANIFEST), 'utf8')
) )
} catch (_) { } catch (_) {
prerenderManifest = { version: 1, routes: {}, dynamicRoutes: [] } prerenderManifest = { version: 1, routes: {}, dynamicRoutes: {} }
} }
cache = new LRUCache({ cache = new LRUCache({
...@@ -144,7 +144,13 @@ export async function setSprCache( ...@@ -144,7 +144,13 @@ export async function setSprCache(
) { ) {
if (sprOptions.dev) return if (sprOptions.dev) return
if (typeof revalidateSeconds !== 'undefined') { if (typeof revalidateSeconds !== 'undefined') {
// TODO: This is really bad. We shouldn't be mutating the manifest from the
// build.
prerenderManifest.routes[pathname] = { prerenderManifest.routes[pathname] = {
dataRoute: path.posix.join(
'/_next/data',
`${pathname === '/' ? '/index' : pathname}.json`
),
initialRevalidateSeconds: revalidateSeconds, initialRevalidateSeconds: revalidateSeconds,
} }
} }
...@@ -155,6 +161,8 @@ export async function setSprCache( ...@@ -155,6 +161,8 @@ export async function setSprCache(
revalidateAfter: calculateRevalidate(pathname), revalidateAfter: calculateRevalidate(pathname),
}) })
// TODO: This option needs to cease to exist unless it stops mutating the
// `next build` output's manifest.
if (sprOptions.flushToDisk) { if (sprOptions.flushToDisk) {
try { try {
await writeFile(getSeedPath(pathname, 'html'), data.html, 'utf8') await writeFile(getSeedPath(pathname, 'html'), data.html, 'utf8')
......
...@@ -152,37 +152,55 @@ const runTests = (dev = false) => { ...@@ -152,37 +152,55 @@ const runTests = (dev = false) => {
expect(manifest.version).toBe(1) expect(manifest.version).toBe(1)
expect(manifest.routes).toEqual({ expect(manifest.routes).toEqual({
'/': { '/': {
dataRoute: '/_next/data/index.json',
initialRevalidateSeconds: 1 initialRevalidateSeconds: 1
}, },
'/blog/[post3]': { '/blog/[post3]': {
dataRoute: '/_next/data/blog/[post3].json',
initialRevalidateSeconds: 10 initialRevalidateSeconds: 10
}, },
'/blog/post-1': { '/blog/post-1': {
dataRoute: '/_next/data/blog/post-1.json',
initialRevalidateSeconds: 10 initialRevalidateSeconds: 10
}, },
'/blog/post-2': { '/blog/post-2': {
dataRoute: '/_next/data/blog/post-2.json',
initialRevalidateSeconds: 10 initialRevalidateSeconds: 10
}, },
'/blog/post-1/comment-1': { '/blog/post-1/comment-1': {
dataRoute: '/_next/data/blog/post-1/comment-1.json',
initialRevalidateSeconds: 2 initialRevalidateSeconds: 2
}, },
'/blog/post-2/comment-2': { '/blog/post-2/comment-2': {
dataRoute: '/_next/data/blog/post-2/comment-2.json',
initialRevalidateSeconds: 2 initialRevalidateSeconds: 2
}, },
'/another': { '/another': {
dataRoute: '/_next/data/another.json',
initialRevalidateSeconds: 0 initialRevalidateSeconds: 0
}, },
'/default-revalidate': { '/default-revalidate': {
dataRoute: '/_next/data/default-revalidate.json',
initialRevalidateSeconds: 1 initialRevalidateSeconds: 1
}, },
'/something': { '/something': {
dataRoute: '/_next/data/something.json',
initialRevalidateSeconds: false initialRevalidateSeconds: false
} }
}) })
expect(manifest.dynamicRoutes).toEqual([ expect(manifest.dynamicRoutes).toEqual({
'/blog/[post]', '/blog/[post]': {
'/blog/[post]/[comment]' dataRoute: '/_next/data/blog/[post].json',
]) dataRouteRegex: '^\\/_next\\/data\\/blog\\/([^\\/]+?)\\.json$',
routeRegex: '^\\/blog\\/([^\\/]+?)(?:\\/)?$'
},
'/blog/[post]/[comment]': {
dataRoute: '/_next/data/blog/[post]/[comment].json',
dataRouteRegex:
'^\\/_next\\/data\\/blog\\/([^\\/]+?)\\/([^\\/]+?)\\.json$',
routeRegex: '^\\/blog\\/([^\\/]+?)\\/([^\\/]+?)(?:\\/)?$'
}
})
}) })
it('outputs prerendered files correctly', async () => { it('outputs prerendered files correctly', async () => {
......
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