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 fs from 'fs'
import Worker from 'jest-worker'
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 {
PAGES_MANIFEST,
BUILD_MANIFEST,
PAGES_MANIFEST,
PHASE_PRODUCTION_BUILD,
PRERENDER_MANIFEST,
SERVER_DIRECTORY,
SERVERLESS_DIRECTORY,
} from '../next-server/lib/constants'
import { getRouteRegex, isDynamicRoute } from '../next-server/lib/router/utils'
import loadConfig, {
isTargetLikeServerless,
} 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 {
recordBuildDuration,
recordBuildOptimize,
......@@ -31,6 +34,7 @@ import { CompilerResult, runCompiler } from './compiler'
import { createEntrypoints, createPagesMapping } from './entries'
import { generateBuildId } from './generate-build-id'
import { isWriteable } from './is-writeable'
import createSpinner from './spinner'
import {
collectPages,
getPageSizeInKb,
......@@ -41,10 +45,6 @@ import {
import getBaseWebpackConfig from './webpack-config'
import { getPageChunks } from './webpack/plugins/chunk-graph-plugin'
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 fsRmdir = promisify(fs.rmdir)
......@@ -58,12 +58,20 @@ const staticCheckWorker = require.resolve('./utils')
export type SprRoute = {
initialRevalidateSeconds: number | false
dataRoute: string
}
export type DynamicSprRoute = {
routeRegex: string
dataRoute: string
dataRouteRegex: string
}
export type PrerenderManifest = {
version: number
routes: { [route: string]: SprRoute }
dynamicRoutes: string[]
dynamicRoutes: { [route: string]: DynamicSprRoute }
}
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] = {
initialRevalidateSeconds:
exportConfig.initialPageRevalidationMap[page],
dataRoute: path.posix.join(
'/_next/data',
`${page === '/' ? '/index' : page}.json`
),
}
} else {
// 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> {
finalPrerenderRoutes[route] = {
initialRevalidateSeconds:
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> {
)
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 = {
version: 1,
routes: finalPrerenderRoutes,
dynamicRoutes: tbdPrerenderRoutes.sort(),
dynamicRoutes: finalDynamicRoutes,
}
await fsWriteFile(
......
......@@ -77,7 +77,7 @@ export function initializeSprCache({
fs.readFileSync(path.join(distDir, PRERENDER_MANIFEST), 'utf8')
)
} catch (_) {
prerenderManifest = { version: 1, routes: {}, dynamicRoutes: [] }
prerenderManifest = { version: 1, routes: {}, dynamicRoutes: {} }
}
cache = new LRUCache({
......@@ -144,7 +144,13 @@ export async function setSprCache(
) {
if (sprOptions.dev) return
if (typeof revalidateSeconds !== 'undefined') {
// TODO: This is really bad. We shouldn't be mutating the manifest from the
// build.
prerenderManifest.routes[pathname] = {
dataRoute: path.posix.join(
'/_next/data',
`${pathname === '/' ? '/index' : pathname}.json`
),
initialRevalidateSeconds: revalidateSeconds,
}
}
......@@ -155,6 +161,8 @@ export async function setSprCache(
revalidateAfter: calculateRevalidate(pathname),
})
// TODO: This option needs to cease to exist unless it stops mutating the
// `next build` output's manifest.
if (sprOptions.flushToDisk) {
try {
await writeFile(getSeedPath(pathname, 'html'), data.html, 'utf8')
......
......@@ -152,37 +152,55 @@ const runTests = (dev = false) => {
expect(manifest.version).toBe(1)
expect(manifest.routes).toEqual({
'/': {
dataRoute: '/_next/data/index.json',
initialRevalidateSeconds: 1
},
'/blog/[post3]': {
dataRoute: '/_next/data/blog/[post3].json',
initialRevalidateSeconds: 10
},
'/blog/post-1': {
dataRoute: '/_next/data/blog/post-1.json',
initialRevalidateSeconds: 10
},
'/blog/post-2': {
dataRoute: '/_next/data/blog/post-2.json',
initialRevalidateSeconds: 10
},
'/blog/post-1/comment-1': {
dataRoute: '/_next/data/blog/post-1/comment-1.json',
initialRevalidateSeconds: 2
},
'/blog/post-2/comment-2': {
dataRoute: '/_next/data/blog/post-2/comment-2.json',
initialRevalidateSeconds: 2
},
'/another': {
dataRoute: '/_next/data/another.json',
initialRevalidateSeconds: 0
},
'/default-revalidate': {
dataRoute: '/_next/data/default-revalidate.json',
initialRevalidateSeconds: 1
},
'/something': {
dataRoute: '/_next/data/something.json',
initialRevalidateSeconds: false
}
})
expect(manifest.dynamicRoutes).toEqual([
'/blog/[post]',
'/blog/[post]/[comment]'
])
expect(manifest.dynamicRoutes).toEqual({
'/blog/[post]': {
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 () => {
......
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