Commit 85e720a0 authored by JJ Kasper's avatar JJ Kasper Committed by Tim Neutkens

Add experimental SPR support (#8832)

* initial commit for SPRv2

* Add initial SPR cache handling

* update SPR handling

* Implement SPR handling in render

* Update tests, handle caching with serverless next
start, add TODOs, and update manifest generating

* Handle no prerender-manifest from not being used

* Fix url.parse error

* Apply suggestions from code review
Co-Authored-By: 's avatarJoe Haddad <joe.haddad@zeit.co>

* Replace set with constants in next-page-config

* simplify sprStatus.used

* Add error if getStaticProps is used with getInitialProps

* Remove stale TODO

* Update revalidate values in SPR cache for non-seeded routes

* Apply suggestions from code review

* Remove concurrency type

* Rename variable for clarity

* Add copying prerender files during export

* Add comment for clarity

* Fix exporting

* Update comment

* Add additional note

* Rename variable

* Update to not re-export SPR pages from build

* Hard navigate when fetching data fails

* Remove default extension

* Add brackets

* Add checking output files to prerender tests

* Adjust export move logic

* Clarify behavior of export aggregation

* Update variable names for clarity

* Update tests

* Add comment

* s/an oxymoron/contradictory/

* rename

* Extract error case

* Add tests for exporting SPR pages and update
/_next/data endpoint to end with .json

* Relocate variable

* Adjust route building

* Rename to unstable

* Rename unstable_getStaticParams

* Fix linting

* Only add this when a data request

* Update prerender data tests

* s/isServerless/isLikeServerless/

* Don't rely on query for `next start` in serverless mode

* Rename var

* Update renderedDuringBuild check

* Add test for dynamic param with bracket

* Fix serverless next start handling

* remove todo

* Adjust comment

* Update calculateRevalidate

* Remove cache logic from render.tsx

* Remove extra imports

* Move SPR cache logic to next-server

* Remove old isDynamic prop

* Add calling App getInitialProps for SPR pages

* Update revalidate logic

* Add isStale to SprCacheValue

* Update headers for SPR

* add awaiting pendingRevalidation

* Dont return null for revalidation render

* Adjust logic

* Be sure to remove coalesced render

* Fix data for serverless

* Create a method coalescing utility

* Remove TODO

* Extract send payload helper

* Wrap in-line

* Move around some code

* Add tests for de-duping and revalidating

* Update prerender manifest test
parent 82f7bb4b
...@@ -6,10 +6,12 @@ import { PageConfig } from '../../../types' ...@@ -6,10 +6,12 @@ import { PageConfig } from '../../../types'
export const dropBundleIdentifier = '__NEXT_DROP_CLIENT_FILE__' export const dropBundleIdentifier = '__NEXT_DROP_CLIENT_FILE__'
export const sprStatus = { used: false } export const sprStatus = { used: false }
const configKeys = new Set(['amp', 'experimentalPrerender']) const configKeys = new Set(['amp'])
const pageComponentVar = '__NEXT_COMP' const pageComponentVar = '__NEXT_COMP'
// this value can't be optimized by terser so the shorter the better // this value can't be optimized by terser so the shorter the better
const prerenderId = '__NEXT_SPR' const prerenderId = '__NEXT_SPR'
const EXPORT_NAME_GET_STATIC_PROPS = 'unstable_getStaticProps'
const EXPORT_NAME_GET_STATIC_PARAMS = 'unstable_getStaticParams'
// replace program path with just a variable with the drop identifier // replace program path with just a variable with the drop identifier
function replaceBundle(path: any, t: typeof BabelTypes) { function replaceBundle(path: any, t: typeof BabelTypes) {
...@@ -37,6 +39,7 @@ interface ConfigState { ...@@ -37,6 +39,7 @@ interface ConfigState {
bundleDropped?: boolean bundleDropped?: boolean
} }
// config to parsing pageConfig for client bundles
export default function nextPageConfig({ export default function nextPageConfig({
types: t, types: t,
}: { }: {
...@@ -52,17 +55,33 @@ export default function nextPageConfig({ ...@@ -52,17 +55,33 @@ export default function nextPageConfig({
path: NodePath<BabelTypes.ExportNamedDeclaration>, path: NodePath<BabelTypes.ExportNamedDeclaration>,
state: any state: any
) { ) {
if ( if (state.bundleDropped || !path.node.declaration) {
state.bundleDropped ||
!path.node.declaration ||
!(path.node.declaration as any).declarations
)
return return
const { declarations } = path.node.declaration as any }
const { declarations, id } = path.node.declaration as any
const config: PageConfig = {} const config: PageConfig = {}
// drop SSR Exports for client bundles
if (
id &&
(id.name === EXPORT_NAME_GET_STATIC_PROPS ||
id.name === EXPORT_NAME_GET_STATIC_PARAMS)
) {
if (id.name === EXPORT_NAME_GET_STATIC_PROPS) {
state.isPrerender = true
sprStatus.used = true
}
path.remove()
return
}
if (!declarations) {
return
}
for (const declaration of declarations) { for (const declaration of declarations) {
if (declaration.id.name !== 'config') continue if (declaration.id.name !== 'config') {
continue
}
if (declaration.init.type !== 'ObjectExpression') { if (declaration.init.type !== 'ObjectExpression') {
const pageName = const pageName =
...@@ -90,9 +109,8 @@ export default function nextPageConfig({ ...@@ -90,9 +109,8 @@ export default function nextPageConfig({
state.bundleDropped = true state.bundleDropped = true
return return
} }
// remove export const config from bundle
state.isPrerender = config.experimentalPrerender === true path.remove()
sprStatus.used = sprStatus.used || state.isPrerender
}, },
}, },
state state
......
...@@ -43,6 +43,7 @@ import { getPageChunks } from './webpack/plugins/chunk-graph-plugin' ...@@ -43,6 +43,7 @@ import { getPageChunks } from './webpack/plugins/chunk-graph-plugin'
import { writeBuildId } from './write-build-id' import { writeBuildId } from './write-build-id'
import createSpinner from './spinner' import createSpinner from './spinner'
import { PUBLIC_DIR_MIDDLEWARE_CONFLICT } from '../lib/constants' 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)
...@@ -52,11 +53,15 @@ const fsReadFile = promisify(fs.readFile) ...@@ -52,11 +53,15 @@ const fsReadFile = promisify(fs.readFile)
const fsWriteFile = promisify(fs.writeFile) const fsWriteFile = promisify(fs.writeFile)
const mkdirp = promisify(mkdirpOrig) const mkdirp = promisify(mkdirpOrig)
const staticCheckWorker = require.resolve('./static-checker') const staticCheckWorker = require.resolve('./utils')
export type PrerenderFile = { export type SprRoute = {
lambda: string initialRevalidateSeconds: number | false
contentTypes: string[] }
export type PrerenderManifest = {
version: number
routes: { [route: string]: SprRoute }
} }
export default async function build(dir: string, conf = null): Promise<void> { export default async function build(dir: string, conf = null): Promise<void> {
...@@ -256,7 +261,6 @@ export default async function build(dir: string, conf = null): Promise<void> { ...@@ -256,7 +261,6 @@ export default async function build(dir: string, conf = null): Promise<void> {
prefixText: 'Automatically optimizing pages', prefixText: 'Automatically optimizing pages',
}) })
const distPath = path.join(dir, config.distDir)
const pageKeys = Object.keys(mappedPages) const pageKeys = Object.keys(mappedPages)
const manifestPath = path.join( const manifestPath = path.join(
distDir, distDir,
...@@ -269,6 +273,7 @@ export default async function build(dir: string, conf = null): Promise<void> { ...@@ -269,6 +273,7 @@ export default async function build(dir: string, conf = null): Promise<void> {
const staticPages = new Set<string>() const staticPages = new Set<string>()
const invalidPages = new Set<string>() const invalidPages = new Set<string>()
const hybridAmpPages = new Set<string>() const hybridAmpPages = new Set<string>()
const additionalSprPaths = new Map<string, Array<string>>()
const pageInfos = new Map<string, PageInfo>() const pageInfos = new Map<string, PageInfo>()
const pagesManifest = JSON.parse(await fsReadFile(manifestPath, 'utf8')) const pagesManifest = JSON.parse(await fsReadFile(manifestPath, 'utf8'))
const buildManifest = JSON.parse(await fsReadFile(buildManifestPath, 'utf8')) const buildManifest = JSON.parse(await fsReadFile(buildManifestPath, 'utf8'))
...@@ -342,15 +347,24 @@ export default async function build(dir: string, conf = null): Promise<void> { ...@@ -342,15 +347,24 @@ export default async function build(dir: string, conf = null): Promise<void> {
if (nonReservedPage) { if (nonReservedPage) {
try { try {
let result: any = await (staticCheckWorkers as any).default({ let result: any = await (staticCheckWorkers as any).isPageStatic(
page,
serverBundle, serverBundle,
runtimeEnvConfig, runtimeEnvConfig
}) )
if (result.isHybridAmp) { if (result.isHybridAmp) {
hybridAmpPages.add(page) hybridAmpPages.add(page)
} }
if (result.prerender) {
sprPages.add(page)
if (result.prerenderRoutes) {
additionalSprPaths.set(page, result.prerenderRoutes)
}
}
if (result.static && customAppGetInitialProps === false) { if (result.static && customAppGetInitialProps === false) {
staticPages.add(page) staticPages.add(page)
isStatic = true isStatic = true
...@@ -394,7 +408,7 @@ export default async function build(dir: string, conf = null): Promise<void> { ...@@ -394,7 +408,7 @@ export default async function build(dir: string, conf = null): Promise<void> {
} }
await writeBuildId(distDir, buildId) await writeBuildId(distDir, buildId)
const prerenderFiles: { [path: string]: PrerenderFile } = {} const finalPrerenderRoutes: { [route: string]: SprRoute } = {}
if (staticPages.size > 0 || sprPages.size > 0) { if (staticPages.size > 0 || sprPages.size > 0) {
const combinedPages = [...staticPages, ...sprPages] const combinedPages = [...staticPages, ...sprPages]
...@@ -406,9 +420,33 @@ export default async function build(dir: string, conf = null): Promise<void> { ...@@ -406,9 +420,33 @@ export default async function build(dir: string, conf = null): Promise<void> {
pages: combinedPages, pages: combinedPages,
outdir: path.join(distDir, 'export'), outdir: path.join(distDir, 'export'),
} }
const exportConfig = { const exportConfig: any = {
...config, ...config,
exportPathMap: (defaultMap: any) => defaultMap, initialPageRevalidationMap: {},
// Default map will be the collection of automatic statically exported
// pages and SPR pages.
// n.b. we cannot handle this above in combinedPages because the dynamic
// page must be in the `pages` array, but not in the mapping.
exportPathMap: (defaultMap: any) => {
// Remove dynamically routed pages from the default path map. These
// pages cannot be prerendered because we don't have enough information
// to do so.
//
// Note: prerendering disables automatic static optimization.
sprPages.forEach(page => {
if (isDynamicRoute(page)) {
delete defaultMap[page]
}
})
// Append the "well-known" routes we should prerender for, e.g. blog
// post slugs.
additionalSprPaths.forEach((routes, page) => {
routes.forEach(route => {
defaultMap[route] = { page }
})
})
return defaultMap
},
exportTrailingSlash: false, exportTrailingSlash: false,
} }
await exportApp(dir, exportOptions, exportConfig) await exportApp(dir, exportOptions, exportConfig)
...@@ -419,9 +457,13 @@ export default async function build(dir: string, conf = null): Promise<void> { ...@@ -419,9 +457,13 @@ export default async function build(dir: string, conf = null): Promise<void> {
await fsUnlink(serverBundle) await fsUnlink(serverBundle)
} }
const moveExportedPage = async (page: string, file: string) => { const moveExportedPage = async (
const isSpr = sprPages.has(page) page: string,
file = `${file}.html` file: string,
isSpr: boolean,
ext: 'html' | 'json'
) => {
file = `${file}.${ext}`
const orig = path.join(exportOptions.outdir, file) const orig = path.join(exportOptions.outdir, file)
const relativeDest = (isLikeServerless const relativeDest = (isLikeServerless
? path.join('pages', file) ? path.join('pages', file)
...@@ -434,17 +476,7 @@ export default async function build(dir: string, conf = null): Promise<void> { ...@@ -434,17 +476,7 @@ export default async function build(dir: string, conf = null): Promise<void> {
relativeDest relativeDest
) )
if (isSpr) { if (!isSpr) {
const projectRelativeDest = path.join(
config.distDir,
isLikeServerless ? SERVERLESS_DIRECTORY : SERVER_DIRECTORY,
relativeDest
)
prerenderFiles[projectRelativeDest] = {
lambda: projectRelativeDest.replace(/\.html$/, '.js'),
contentTypes: ['application/json', 'text/html'],
}
} else {
pagesManifest[page] = relativeDest pagesManifest[page] = relativeDest
if (page === '/') pagesManifest['/index'] = relativeDest if (page === '/') pagesManifest['/index'] = relativeDest
if (page === '/.amp') pagesManifest['/index.amp'] = relativeDest if (page === '/.amp') pagesManifest['/index.amp'] = relativeDest
...@@ -454,10 +486,43 @@ export default async function build(dir: string, conf = null): Promise<void> { ...@@ -454,10 +486,43 @@ export default async function build(dir: string, conf = null): Promise<void> {
} }
for (const page of combinedPages) { for (const page of combinedPages) {
const isSpr = sprPages.has(page)
const isDynamic = isDynamicRoute(page)
let file = page === '/' ? '/index' : page let file = page === '/' ? '/index' : page
await moveExportedPage(page, file) // The dynamic version of SPR pages are not prerendered. Below, we handle
// the specific prerenders of these.
if (!(isSpr && isDynamic)) {
await moveExportedPage(page, file, isSpr, 'html')
}
const hasAmp = hybridAmpPages.has(page) const hasAmp = hybridAmpPages.has(page)
if (hasAmp) await moveExportedPage(`${page}.amp`, `${file}.amp`) if (hasAmp) {
await moveExportedPage(`${page}.amp`, `${file}.amp`, isSpr, 'html')
}
if (isSpr) {
// For a non-dynamic SPR page, we must copy its data file from export.
if (!isDynamic) {
await moveExportedPage(page, page, true, 'json')
finalPrerenderRoutes[page] = {
initialRevalidateSeconds:
exportConfig.initialPageRevalidationMap[page],
}
} else {
// For a dynamic SPR page, we did not copy its html nor data exports.
// Instead, we must copy specific versions of this page as defined by
// `unstable_getStaticParams` (additionalSprPaths).
const extraRoutes = additionalSprPaths.get(page) || []
for (const route of extraRoutes) {
await moveExportedPage(route, route, true, 'html')
await moveExportedPage(route, route, true, 'json')
finalPrerenderRoutes[route] = {
initialRevalidateSeconds:
exportConfig.initialPageRevalidationMap[route],
}
}
}
}
} }
// remove temporary export folder // remove temporary export folder
...@@ -479,9 +544,14 @@ export default async function build(dir: string, conf = null): Promise<void> { ...@@ -479,9 +544,14 @@ export default async function build(dir: string, conf = null): Promise<void> {
) )
if (sprPages.size > 0) { if (sprPages.size > 0) {
const prerenderManifest: PrerenderManifest = {
version: 1,
routes: finalPrerenderRoutes,
}
await fsWriteFile( await fsWriteFile(
path.join(distDir, PRERENDER_MANIFEST), path.join(distDir, PRERENDER_MANIFEST),
JSON.stringify({ prerenderFiles }), JSON.stringify(prerenderManifest),
'utf8' 'utf8'
) )
} }
......
import { isPageStatic } from './utils'
export default function worker(options: {
serverBundle: string
runtimeEnvConfig: any
}) {
const { serverBundle, runtimeEnvConfig } = options || ({} as any)
return isPageStatic(serverBundle, runtimeEnvConfig)
}
...@@ -9,6 +9,9 @@ import { isValidElementType } from 'react-is' ...@@ -9,6 +9,9 @@ import { isValidElementType } from 'react-is'
import prettyBytes from '../lib/pretty-bytes' import prettyBytes from '../lib/pretty-bytes'
import { recursiveReadDir } from '../lib/recursive-readdir' import { recursiveReadDir } from '../lib/recursive-readdir'
import { getPageChunks } from './webpack/plugins/chunk-graph-plugin' import { getPageChunks } from './webpack/plugins/chunk-graph-plugin'
import { isDynamicRoute } from '../next-server/lib/router/utils/is-dynamic'
import { getRouteMatcher, getRouteRegex } from '../next-server/lib/router/utils'
import { SPR_GET_INITIAL_PROPS_CONFLICT } from '../lib/constants'
const fsStatPromise = promisify(fs.stat) const fsStatPromise = promisify(fs.stat)
const fileStats: { [k: string]: Promise<fs.Stats> } = {} const fileStats: { [k: string]: Promise<fs.Stats> } = {}
...@@ -172,10 +175,16 @@ export async function getPageSizeInKb( ...@@ -172,10 +175,16 @@ export async function getPageSizeInKb(
return -1 return -1
} }
export function isPageStatic( export async function isPageStatic(
page: string,
serverBundle: string, serverBundle: string,
runtimeEnvConfig: any runtimeEnvConfig: any
): { static?: boolean; prerender?: boolean; isHybridAmp?: boolean } { ): Promise<{
static?: boolean
prerender?: boolean
isHybridAmp?: boolean
prerenderRoutes?: string[] | undefined
}> {
try { try {
require('../next-server/lib/runtime-config').setConfig(runtimeEnvConfig) require('../next-server/lib/runtime-config').setConfig(runtimeEnvConfig)
const mod = require(serverBundle) const mod = require(serverBundle)
...@@ -184,12 +193,78 @@ export function isPageStatic( ...@@ -184,12 +193,78 @@ export function isPageStatic(
if (!Comp || !isValidElementType(Comp) || typeof Comp === 'string') { if (!Comp || !isValidElementType(Comp) || typeof Comp === 'string') {
throw new Error('INVALID_DEFAULT_EXPORT') throw new Error('INVALID_DEFAULT_EXPORT')
} }
const config = mod.config || {}
const hasGetInitialProps = !!(Comp as any).getInitialProps
const hasStaticProps = !!mod.unstable_getStaticProps
const hasStaticParams = !!mod.unstable_getStaticParams
// A page cannot be prerendered _and_ define a data requirement. That's
// contradictory!
if (hasGetInitialProps && hasStaticProps) {
throw new Error(SPR_GET_INITIAL_PROPS_CONFLICT)
}
// A page cannot have static parameters if it is not a dynamic page.
if (hasStaticProps && hasStaticParams && !isDynamicRoute(page)) {
throw new Error(
`unstable_getStaticParams can only be used with dynamic pages. https://nextjs.org/docs#dynamic-routing`
)
}
let prerenderPaths: string[] | undefined
if (hasStaticProps && hasStaticParams) {
prerenderPaths = [] as string[]
const _routeRegex = getRouteRegex(page)
const _routeMatcher = getRouteMatcher(_routeRegex)
// Get the default list of allowed params.
const _validParamKeys = Object.keys(_routeMatcher(page))
const toPrerender: Array<
{ [key: string]: string } | string
> = await mod.unstable_getStaticParams()
toPrerender.forEach(entry => {
// For a string-provided path, we must make sure it matches the dynamic
// route.
if (typeof entry === 'string') {
const result = _routeMatcher(entry)
if (!result) {
throw new Error(
`The provided path \`${entry}\` does not match the page: \`${page}\`.`
)
}
prerenderPaths!.push(entry)
}
// For the object-provided path, we must make sure it specifies all
// required keys.
else {
let builtPage = page
_validParamKeys.forEach(validParamKey => {
if (typeof entry[validParamKey] !== 'string') {
throw new Error(
`A required parameter (${validParamKey}) was not provided as a string.`
)
}
builtPage = builtPage.replace(
`[${validParamKey}]`,
encodeURIComponent(entry[validParamKey])
)
})
prerenderPaths!.push(builtPage)
}
})
}
const config = mod.config || {}
return { return {
static: typeof (Comp as any).getInitialProps !== 'function', static: !hasStaticProps && !hasGetInitialProps,
prerender: config.experimentalPrerender === true,
isHybridAmp: config.amp === 'hybrid', isHybridAmp: config.amp === 'hybrid',
prerenderRoutes: prerenderPaths,
prerender: hasStaticProps,
} }
} catch (err) { } catch (err) {
if (err.code === 'MODULE_NOT_FOUND') return {} if (err.code === 'MODULE_NOT_FOUND') return {}
......
...@@ -84,6 +84,12 @@ const nextServerlessLoader: loader.Loader = function() { ...@@ -84,6 +84,12 @@ const nextServerlessLoader: loader.Loader = function() {
import * as ComponentInfo from '${absolutePagePath}'; import * as ComponentInfo from '${absolutePagePath}';
const Component = ComponentInfo.default const Component = ComponentInfo.default
export default Component export default Component
export const unstable_getStaticProps = ComponentInfo['unstable_getStaticProp' + 's']
${
isDynamicRoute(page)
? "export const unstable_getStaticParams = ComponentInfo['unstable_getStaticParam' + 's']"
: ''
}
export const config = ComponentInfo['confi' + 'g'] || {} export const config = ComponentInfo['confi' + 'g'] || {}
export const _app = App export const _app = App
export async function renderReqToHTML(req, res, fromExport) { export async function renderReqToHTML(req, res, fromExport) {
...@@ -91,12 +97,21 @@ const nextServerlessLoader: loader.Loader = function() { ...@@ -91,12 +97,21 @@ const nextServerlessLoader: loader.Loader = function() {
App, App,
Document, Document,
buildManifest, buildManifest,
unstable_getStaticProps,
reactLoadableManifest, reactLoadableManifest,
canonicalBase: "${canonicalBase}", canonicalBase: "${canonicalBase}",
buildId: "${buildId}", buildId: "${buildId}",
assetPrefix: "${assetPrefix}", assetPrefix: "${assetPrefix}",
ampBindInitData: ${ampBindInitData === true || ampBindInitData: ${ampBindInitData === true ||
ampBindInitData === 'true'} ampBindInitData === 'true'},
}
let sprData = false
if (req.url.match(/_next\\/data/)) {
sprData = true
req.url = req.url
.replace(/\\/_next\\/data\\//, '/')
.replace(/\\.json$/, '')
} }
const parsedUrl = parse(req.url, true) const parsedUrl = parse(req.url, true)
const renderOpts = Object.assign( const renderOpts = Object.assign(
...@@ -112,10 +127,10 @@ const nextServerlessLoader: loader.Loader = function() { ...@@ -112,10 +127,10 @@ const nextServerlessLoader: loader.Loader = function() {
${page === '/_error' ? `res.statusCode = 404` : ''} ${page === '/_error' ? `res.statusCode = 404` : ''}
${ ${
isDynamicRoute(page) isDynamicRoute(page)
? `const params = fromExport ? {} : getRouteMatcher(getRouteRegex("${page}"))(parsedUrl.pathname) || {};` ? `const params = fromExport && !unstable_getStaticProps ? {} : getRouteMatcher(getRouteRegex("${page}"))(parsedUrl.pathname) || {};`
: `const params = {};` : `const params = {};`
} }
const result = await renderToHTML(req, res, "${page}", Object.assign({}, parsedUrl.query, params), renderOpts) const result = await renderToHTML(req, res, "${page}", Object.assign({}, unstable_getStaticProps ? {} : parsedUrl.query, params, sprData ? { _nextSprData: '1' } : {}), renderOpts)
if (fromExport) return { html: result, renderOpts } if (fromExport) return { html: result, renderOpts }
return result return result
......
...@@ -3,9 +3,9 @@ import chalk from 'chalk' ...@@ -3,9 +3,9 @@ import chalk from 'chalk'
import Worker from 'jest-worker' import Worker from 'jest-worker'
import { promisify } from 'util' import { promisify } from 'util'
import mkdirpModule from 'mkdirp' import mkdirpModule from 'mkdirp'
import { resolve, join } from 'path' import { resolve, join, dirname } from 'path'
import { API_ROUTE } from '../lib/constants' import { API_ROUTE } from '../lib/constants'
import { existsSync, readFileSync } from 'fs' import { existsSync, readFileSync, copyFile as copyFileOrig } from 'fs'
import { recursiveCopy } from '../lib/recursive-copy' import { recursiveCopy } from '../lib/recursive-copy'
import { recursiveDelete } from '../lib/recursive-delete' import { recursiveDelete } from '../lib/recursive-delete'
import { formatAmpMessages } from '../build/output/index' import { formatAmpMessages } from '../build/output/index'
...@@ -18,12 +18,15 @@ import { ...@@ -18,12 +18,15 @@ import {
PAGES_MANIFEST, PAGES_MANIFEST,
CONFIG_FILE, CONFIG_FILE,
BUILD_ID_FILE, BUILD_ID_FILE,
PRERENDER_MANIFEST,
SERVERLESS_DIRECTORY,
CLIENT_PUBLIC_FILES_PATH, CLIENT_PUBLIC_FILES_PATH,
CLIENT_STATIC_FILES_PATH CLIENT_STATIC_FILES_PATH
} from '../next-server/lib/constants' } from '../next-server/lib/constants'
import createSpinner from '../build/spinner' import createSpinner from '../build/spinner'
const mkdirp = promisify(mkdirpModule) const mkdirp = promisify(mkdirpModule)
const copyFile = promisify(copyFileOrig)
const createProgress = (total, label = 'Exporting') => { const createProgress = (total, label = 'Exporting') => {
let curProgress = 0 let curProgress = 0
...@@ -78,8 +81,9 @@ export default async function (dir, options, configuration) { ...@@ -78,8 +81,9 @@ export default async function (dir, options, configuration) {
const threads = options.threads || Math.max(cpus().length - 1, 1) const threads = options.threads || Math.max(cpus().length - 1, 1)
const distDir = join(dir, nextConfig.distDir) const distDir = join(dir, nextConfig.distDir)
const subFolders = nextConfig.exportTrailingSlash const subFolders = nextConfig.exportTrailingSlash
const isLikeServerless = nextConfig.target !== 'server'
if (!options.buildExport && nextConfig.target !== 'server') { if (!options.buildExport && isLikeServerless) {
throw new Error( throw new Error(
'Cannot export when target is not server. https://err.sh/zeit/next.js/next-export-serverless' 'Cannot export when target is not server. https://err.sh/zeit/next.js/next-export-serverless'
) )
...@@ -97,6 +101,19 @@ export default async function (dir, options, configuration) { ...@@ -97,6 +101,19 @@ export default async function (dir, options, configuration) {
const pagesManifest = const pagesManifest =
!options.pages && require(join(distDir, SERVER_DIRECTORY, PAGES_MANIFEST)) !options.pages && require(join(distDir, SERVER_DIRECTORY, PAGES_MANIFEST))
let prerenderManifest
try {
prerenderManifest = require(join(distDir, PRERENDER_MANIFEST))
} catch (_) {}
const distPagesDir = join(
distDir,
isLikeServerless
? SERVERLESS_DIRECTORY
: join(SERVER_DIRECTORY, 'static', buildId),
'pages'
)
const pages = options.pages || Object.keys(pagesManifest) const pages = options.pages || Object.keys(pagesManifest)
const defaultPathMap = {} const defaultPathMap = {}
...@@ -201,6 +218,7 @@ export default async function (dir, options, configuration) { ...@@ -201,6 +218,7 @@ export default async function (dir, options, configuration) {
} }
const progress = !options.silent && createProgress(filteredPaths.length) const progress = !options.silent && createProgress(filteredPaths.length)
const sprDataDir = options.buildExport ? outDir : join(outDir, '_next/data')
const ampValidations = {} const ampValidations = {}
let hadValidationError = false let hadValidationError = false
...@@ -241,9 +259,11 @@ export default async function (dir, options, configuration) { ...@@ -241,9 +259,11 @@ export default async function (dir, options, configuration) {
distDir, distDir,
buildId, buildId,
outDir, outDir,
sprDataDir,
renderOpts, renderOpts,
serverRuntimeConfig, serverRuntimeConfig,
subFolders, subFolders,
buildExport: options.buildExport,
serverless: isTargetLikeServerless(nextConfig.target) serverless: isTargetLikeServerless(nextConfig.target)
}) })
...@@ -254,12 +274,36 @@ export default async function (dir, options, configuration) { ...@@ -254,12 +274,36 @@ export default async function (dir, options, configuration) {
Array.isArray(result && result.errors) && result.errors.length > 0 Array.isArray(result && result.errors) && result.errors.length > 0
} }
renderError |= result.error renderError |= result.error
if (
options.buildExport &&
typeof result.fromBuildExportRevalidate !== 'undefined'
) {
configuration.initialPageRevalidationMap[path] =
result.fromBuildExportRevalidate
}
if (progress) progress() if (progress) progress()
}) })
) )
worker.end() worker.end()
// copy prerendered routes to outDir
if (!options.buildExport && prerenderManifest) {
await Promise.all(
Object.keys(prerenderManifest.routes).map(async route => {
const orig = join(distPagesDir, route)
const htmlDest = join(outDir, `${route}.html`)
const jsonDest = join(sprDataDir, `${route}.json`)
await mkdirp(dirname(htmlDest))
await mkdirp(dirname(jsonDest))
await copyFile(`${orig}.html`, htmlDest)
await copyFile(`${orig}.json`, jsonDest)
})
)
}
if (Object.keys(ampValidations).length) { if (Object.keys(ampValidations).length) {
console.log(formatAmpMessages(ampValidations)) console.log(formatAmpMessages(ampValidations))
} }
......
...@@ -24,7 +24,9 @@ export default async function ({ ...@@ -24,7 +24,9 @@ export default async function ({
distDir, distDir,
buildId, buildId,
outDir, outDir,
sprDataDir,
renderOpts, renderOpts,
buildExport,
serverRuntimeConfig, serverRuntimeConfig,
subFolders, subFolders,
serverless serverless
...@@ -35,7 +37,7 @@ export default async function ({ ...@@ -35,7 +37,7 @@ export default async function ({
try { try {
let { query = {} } = pathMap let { query = {} } = pathMap
const { page, sprPage } = pathMap const { page } = pathMap
const filePath = path === '/' ? '/index' : path const filePath = path === '/' ? '/index' : path
const ampPath = `${filePath}.amp` const ampPath = `${filePath}.amp`
...@@ -71,12 +73,6 @@ export default async function ({ ...@@ -71,12 +73,6 @@ export default async function ({
...headerMocks ...headerMocks
} }
if (sprPage && isDynamicRoute(page)) {
query._nextPreviewSkeleton = 1
// pass via `req` to avoid adding code to serverless bundle
req.url += (req.url.includes('?') ? '&' : '?') + '_nextPreviewSkeleton=1'
}
envConfig.setConfig({ envConfig.setConfig({
serverRuntimeConfig, serverRuntimeConfig,
publicRuntimeConfig: renderOpts.runtimeConfig publicRuntimeConfig: renderOpts.runtimeConfig
...@@ -104,12 +100,24 @@ export default async function ({ ...@@ -104,12 +100,24 @@ export default async function ({
let curRenderOpts = {} let curRenderOpts = {}
let renderMethod = renderToHTML let renderMethod = renderToHTML
// eslint-disable-next-line camelcase
const renderedDuringBuild = unstable_getStaticProps => {
// eslint-disable-next-line camelcase
return !buildExport && unstable_getStaticProps && !isDynamicRoute(path)
}
if (serverless) { if (serverless) {
renderMethod = require(join( const mod = require(join(
distDir, distDir,
'serverless/pages', 'serverless/pages',
(page === '/' ? 'index' : page) + '.js' (page === '/' ? 'index' : page) + '.js'
)).renderReqToHTML ))
// for non-dynamic SPR pages we should have already
// prerendered the file
if (renderedDuringBuild(mod.unstable_getStaticProps)) return results
renderMethod = mod.renderReqToHTML
const result = await renderMethod(req, res, true) const result = await renderMethod(req, res, true)
curRenderOpts = result.renderOpts || {} curRenderOpts = result.renderOpts || {}
html = result.html html = result.html
...@@ -125,6 +133,12 @@ export default async function ({ ...@@ -125,6 +133,12 @@ export default async function ({
serverless serverless
) )
// for non-dynamic SPR pages we should have already
// prerendered the file
if (renderedDuringBuild(components.unstable_getStaticProps)) {
return results
}
if (typeof components.Component === 'string') { if (typeof components.Component === 'string') {
html = components.Component html = components.Component
} else { } else {
...@@ -184,6 +198,18 @@ export default async function ({ ...@@ -184,6 +198,18 @@ export default async function ({
await writeFileP(ampHtmlFilepath, ampHtml, 'utf8') await writeFileP(ampHtmlFilepath, ampHtml, 'utf8')
} }
} }
if (curRenderOpts.sprData) {
const dataFile = join(
sprDataDir,
htmlFilename.replace(/\.html$/, '.json')
)
await mkdirp(dirname(dataFile))
await writeFileP(dataFile, JSON.stringify(curRenderOpts.sprData), 'utf8')
}
results.fromBuildExportRevalidate = curRenderOpts.revalidate
await writeFileP(htmlFilepath, html, 'utf8') await writeFileP(htmlFilepath, html, 'utf8')
return results return results
} catch (error) { } catch (error) {
......
type CoalescedInvoke<T> = {
isOrigin: boolean
value: T
}
type UnwrapPromise<T> = T extends Promise<infer U> ? U : T
const globalInvokeCache = new Map<string, Promise<CoalescedInvoke<unknown>>>()
export function withCoalescedInvoke<F extends (...args: any) => any>(
func: F
): (
key: string,
args: Parameters<F>
) => Promise<CoalescedInvoke<UnwrapPromise<ReturnType<F>>>> {
return async function(key: string, args: Parameters<F>) {
const entry = globalInvokeCache.get(key)
if (entry) {
return entry.then(res => ({
isOrigin: false,
value: res.value as UnwrapPromise<ReturnType<F>>,
}))
}
async function __wrapper() {
return await func.apply(undefined, args)
}
const future = __wrapper()
.then(res => {
globalInvokeCache.delete(key)
return { isOrigin: true, value: res as UnwrapPromise<ReturnType<F>> }
})
.catch(err => {
globalInvokeCache.delete(key)
return Promise.reject(err)
})
globalInvokeCache.set(key, future)
return future
}
}
...@@ -24,3 +24,5 @@ export const PAGES_DIR_ALIAS = 'private-next-pages' ...@@ -24,3 +24,5 @@ export const PAGES_DIR_ALIAS = 'private-next-pages'
export const DOT_NEXT_ALIAS = 'private-dot-next' export const DOT_NEXT_ALIAS = 'private-dot-next'
export const PUBLIC_DIR_MIDDLEWARE_CONFLICT = `You can not have a '_next' folder inside of your public folder. This conflicts with the internal '/_next' route. https://err.sh/zeit/next.js/public-next-folder-conflict` export const PUBLIC_DIR_MIDDLEWARE_CONFLICT = `You can not have a '_next' folder inside of your public folder. This conflicts with the internal '/_next' route. https://err.sh/zeit/next.js/public-next-folder-conflict`
export const SPR_GET_INITIAL_PROPS_CONFLICT = `You can not use getInitialProps with unstable_getStaticProps. To use SPR, please remove your getInitialProps`
...@@ -623,16 +623,9 @@ export default class Router implements BaseRouter { ...@@ -623,16 +623,9 @@ export default class Router implements BaseRouter {
(Component as any).__NEXT_SPR (Component as any).__NEXT_SPR
) { ) {
let status: any let status: any
const url = ctx.asPath const { pathname } = parse(ctx.asPath || ctx.pathname)
? ctx.asPath
: format({
pathname: ctx.pathname,
query: ctx.query,
})
props = await fetch(url, { props = await fetch(`/_next/data${pathname}.json`)
headers: { 'content-type': 'application/json' },
})
.then(res => { .then(res => {
if (!res.ok) { if (!res.ok) {
status = res.status status = res.status
...@@ -640,11 +633,10 @@ export default class Router implements BaseRouter { ...@@ -640,11 +633,10 @@ export default class Router implements BaseRouter {
} }
return res.json() return res.json()
}) })
.then((pageProps: any) => {
return { pageProps }
})
.catch((err: Error) => { .catch((err: Error) => {
return { error: err.message, status } console.error(`Failed to load data`, status, err)
window.location.href = pathname!
return new Promise(() => {})
}) })
} else { } else {
const AppTree = this._wrapApp(App) const AppTree = this._wrapApp(App)
......
...@@ -46,6 +46,7 @@ const defaultConfig: { [key: string]: any } = { ...@@ -46,6 +46,7 @@ const defaultConfig: { [key: string]: any } = {
modern: false, modern: false,
profiling: false, profiling: false,
publicDirectory: false, publicDirectory: false,
sprFlushToDisk: true,
}, },
future: { future: {
excludeDefaultMomentLocales: false, excludeDefaultMomentLocales: false,
......
...@@ -15,6 +15,12 @@ export function interopDefault(mod: any) { ...@@ -15,6 +15,12 @@ export function interopDefault(mod: any) {
export type LoadComponentsReturnType = { export type LoadComponentsReturnType = {
Component: any Component: any
pageConfig: PageConfig pageConfig: PageConfig
unstable_getStaticProps?: (params: {
params: any
}) => {
props: any
revalidate: number | false
}
buildManifest?: any buildManifest?: any
reactLoadableManifest?: any reactLoadableManifest?: any
Document?: any Document?: any
...@@ -30,7 +36,11 @@ export async function loadComponents( ...@@ -30,7 +36,11 @@ export async function loadComponents(
): Promise<LoadComponentsReturnType> { ): Promise<LoadComponentsReturnType> {
if (serverless) { if (serverless) {
const Component = await requirePage(pathname, distDir, serverless) const Component = await requirePage(pathname, distDir, serverless)
return { Component, pageConfig: Component.config || {} } return {
Component,
pageConfig: Component.config || {},
unstable_getStaticProps: Component.unstable_getStaticProps,
}
} }
const documentPath = join( const documentPath = join(
distDir, distDir,
...@@ -76,5 +86,6 @@ export async function loadComponents( ...@@ -76,5 +86,6 @@ export async function loadComponents(
DocumentMiddleware, DocumentMiddleware,
reactLoadableManifest, reactLoadableManifest,
pageConfig: ComponentMod.config || {}, pageConfig: ComponentMod.config || {},
unstable_getStaticProps: ComponentMod.unstable_getStaticProps,
} }
} }
...@@ -4,7 +4,7 @@ import { IncomingMessage, ServerResponse } from 'http' ...@@ -4,7 +4,7 @@ import { IncomingMessage, ServerResponse } from 'http'
import { join, resolve, sep } from 'path' import { join, resolve, sep } from 'path'
import { parse as parseQs, ParsedUrlQuery } from 'querystring' import { parse as parseQs, ParsedUrlQuery } from 'querystring'
import { parse as parseUrl, UrlWithParsedQuery } from 'url' import { parse as parseUrl, UrlWithParsedQuery } from 'url'
import { withCoalescedInvoke } from '../../lib/coalesced-function'
import { import {
BUILD_ID_FILE, BUILD_ID_FILE,
CLIENT_PUBLIC_FILES_PATH, CLIENT_PUBLIC_FILES_PATH,
...@@ -22,7 +22,7 @@ import { ...@@ -22,7 +22,7 @@ import {
isDynamicRoute, isDynamicRoute,
} from '../lib/router/utils' } from '../lib/router/utils'
import * as envConfig from '../lib/runtime-config' import * as envConfig from '../lib/runtime-config'
import { NextApiRequest, NextApiResponse } from '../lib/utils' import { NextApiRequest, NextApiResponse, isResSent } from '../lib/utils'
import { apiResolver } from './api-utils' import { apiResolver } from './api-utils'
import loadConfig, { isTargetLikeServerless } from './config' import loadConfig, { isTargetLikeServerless } from './config'
import { recursiveReadDirSync } from './lib/recursive-readdir-sync' import { recursiveReadDirSync } from './lib/recursive-readdir-sync'
...@@ -33,6 +33,7 @@ import Router, { Params, route, Route, RouteMatch } from './router' ...@@ -33,6 +33,7 @@ import Router, { Params, route, Route, RouteMatch } from './router'
import { sendHTML } from './send-html' import { sendHTML } from './send-html'
import { serveStatic } from './serve-static' import { serveStatic } from './serve-static'
import { isBlockedPage, isInternalUrl } from './utils' import { isBlockedPage, isInternalUrl } from './utils'
import { initializeSprCache, getSprCache, setSprCache } from './spr-cache'
type NextConfig = any type NextConfig = any
...@@ -56,6 +57,7 @@ export type ServerConstructor = { ...@@ -56,6 +57,7 @@ export type ServerConstructor = {
* Object what you would use in next.config.js - @default {} * Object what you would use in next.config.js - @default {}
*/ */
conf?: NextConfig conf?: NextConfig
dev?: boolean
} }
export default class Server { export default class Server {
...@@ -88,13 +90,13 @@ export default class Server { ...@@ -88,13 +90,13 @@ export default class Server {
staticMarkup = false, staticMarkup = false,
quiet = false, quiet = false,
conf = null, conf = null,
dev = false,
}: ServerConstructor = {}) { }: ServerConstructor = {}) {
this.dir = resolve(dir) this.dir = resolve(dir)
this.quiet = quiet this.quiet = quiet
const phase = this.currentPhase() const phase = this.currentPhase()
this.nextConfig = loadConfig(phase, this.dir, conf) this.nextConfig = loadConfig(phase, this.dir, conf)
this.distDir = join(this.dir, this.nextConfig.distDir) this.distDir = join(this.dir, this.nextConfig.distDir)
// this.pagesDir = join(this.dir, 'pages')
this.publicDir = join(this.dir, CLIENT_PUBLIC_FILES_PATH) this.publicDir = join(this.dir, CLIENT_PUBLIC_FILES_PATH)
this.pagesManifest = join( this.pagesManifest = join(
this.distDir, this.distDir,
...@@ -146,8 +148,20 @@ export default class Server { ...@@ -146,8 +148,20 @@ export default class Server {
const routes = this.generateRoutes() const routes = this.generateRoutes()
this.router = new Router(routes) this.router = new Router(routes)
this.setAssetPrefix(assetPrefix) this.setAssetPrefix(assetPrefix)
initializeSprCache({
dev,
distDir: this.distDir,
pagesDir: join(
this.distDir,
this._isLikeServerless
? SERVERLESS_DIRECTORY
: `${SERVER_DIRECTORY}/static/${this.buildId}`,
'pages'
),
flushToDisk: this.nextConfig.experimental.sprFlushToDisk,
})
} }
private currentPhase(): string { private currentPhase(): string {
...@@ -229,6 +243,24 @@ export default class Server { ...@@ -229,6 +243,24 @@ export default class Server {
await this.serveStatic(req, res, p, parsedUrl) await this.serveStatic(req, res, p, parsedUrl)
}, },
}, },
{
match: route('/_next/data/:path*'),
fn: async (req, res, params, _parsedUrl) => {
// Make sure to 404 for /_next/data/ itself
if (!params.path) return this.render404(req, res, _parsedUrl)
// TODO: force `.json` to be present
const pathname = `/${params.path.join('/')}`.replace(/\.json$/, '')
req.url = pathname
const parsedUrl = parseUrl(pathname, true)
await this.render(
req,
res,
pathname,
{ _nextSprData: '1' },
parsedUrl
)
},
},
{ {
match: route('/_next/:path*'), match: route('/_next/:path*'),
// This path is needed because `render()` does a check for `/_next` and the calls the routing again // This path is needed because `render()` does a check for `/_next` and the calls the routing again
...@@ -485,6 +517,13 @@ export default class Server { ...@@ -485,6 +517,13 @@ export default class Server {
) )
} }
private __sendPayload(res: ServerResponse, payload: any, header: string) {
// TODO: ETag? Cache-Control headers? Next-specific headers?
res.setHeader('Content-Type', header)
res.setHeader('Content-Length', Buffer.byteLength(payload))
res.end(payload)
}
private async renderToHTMLWithComponents( private async renderToHTMLWithComponents(
req: IncomingMessage, req: IncomingMessage,
res: ServerResponse, res: ServerResponse,
...@@ -492,24 +531,122 @@ export default class Server { ...@@ -492,24 +531,122 @@ export default class Server {
query: ParsedUrlQuery = {}, query: ParsedUrlQuery = {},
result: LoadComponentsReturnType, result: LoadComponentsReturnType,
opts: any opts: any
) { ): Promise<string | null> {
// handle static page // handle static page
if (typeof result.Component === 'string') { if (typeof result.Component === 'string') {
return result.Component return result.Component
} }
// handle serverless // check request state
if ( const isLikeServerless =
typeof result.Component === 'object' && typeof result.Component === 'object' &&
typeof result.Component.renderReqToHTML === 'function' typeof result.Component.renderReqToHTML === 'function'
) { const isSpr = !!result.unstable_getStaticProps
return result.Component.renderReqToHTML(req, res)
// non-spr requests should render like normal
if (!isSpr) {
// handle serverless
if (isLikeServerless) {
return result.Component.renderReqToHTML(req, res)
}
return renderToHTML(req, res, pathname, query, {
...result,
...opts,
})
}
// Toggle whether or not this is an SPR Data request
const isSprData = isSpr && query._nextSprData
if (isSprData) {
delete query._nextSprData
}
// Compute the SPR cache key
const sprCacheKey = parseUrl(req.url || '').pathname!
// Complete the response with cached data if its present
const cachedData = await getSprCache(sprCacheKey)
if (cachedData) {
const data = isSprData
? JSON.stringify(cachedData.pageData)
: cachedData.html
this.__sendPayload(
res,
data,
isSprData ? 'application/json' : 'text/html; charset=utf-8'
)
// Stop the request chain here if the data we sent was up-to-date
if (!cachedData.isStale) {
return null
}
} }
return renderToHTML(req, res, pathname, query, { // If we're here, that means data is missing or it's stale.
...result,
...opts, // Serverless requests need its URL transformed back into the original
// request path (to emulate lambda behavior in production)
if (isLikeServerless && isSprData) {
const curUrl = parseUrl(req.url || '', true)
req.url = `/_next/data${curUrl.pathname}.json`
}
const doRender = withCoalescedInvoke(async function(): Promise<{
html: string | null
sprData: any
sprRevalidate: number | false
}> {
let sprData: any
let html: string | null
let sprRevalidate: number | false
let renderResult
// handle serverless
if (isLikeServerless) {
renderResult = await result.Component.renderReqToHTML(req, res, true)
html = renderResult.html
sprData = renderResult.renderOpts.sprData
sprRevalidate = renderResult.renderOpts.revalidate
} else {
const renderOpts = {
...result,
...opts,
}
renderResult = await renderToHTML(req, res, pathname, query, renderOpts)
html = renderResult
sprData = renderOpts.sprData
sprRevalidate = renderOpts.revalidate
}
return { html, sprData, sprRevalidate }
}) })
return doRender(sprCacheKey, []).then(
async ({ isOrigin, value: { html, sprData, sprRevalidate } }) => {
// Respond to the request if a payload wasn't sent above (from cache)
if (!isResSent(res)) {
this.__sendPayload(
res,
isSprData ? JSON.stringify(sprData) : html,
isSprData ? 'application/json' : 'text/html; charset=utf-8'
)
}
// Update the SPR cache if the head request
if (isOrigin) {
await setSprCache(
sprCacheKey,
{ html: html!, pageData: sprData },
sprRevalidate
)
}
return null
}
)
} }
public renderToHTML( public renderToHTML(
...@@ -556,9 +693,20 @@ export default class Server { ...@@ -556,9 +693,20 @@ export default class Server {
req, req,
res, res,
dynamicRoute.page, dynamicRoute.page,
{ ...query, ...params }, // only add params for SPR enabled pages
{
...(result.unstable_getStaticProps
? { _nextSprData: query._nextSprData }
: query),
...params,
},
result, result,
{ ...this.renderOpts, amphtml, hasAmp, dataOnly } {
...this.renderOpts,
amphtml,
hasAmp,
dataOnly,
}
) )
} }
) )
......
...@@ -29,6 +29,8 @@ import optimizeAmp from './optimize-amp' ...@@ -29,6 +29,8 @@ import optimizeAmp from './optimize-amp'
import { isInAmpMode } from '../lib/amp' import { isInAmpMode } from '../lib/amp'
// Uses a module path because of the compiled output directory location // Uses a module path because of the compiled output directory location
import { PageConfig } from 'next/types' import { PageConfig } from 'next/types'
import { isDynamicRoute } from '../lib/router/utils/is-dynamic'
import { SPR_GET_INITIAL_PROPS_CONFLICT } from '../../lib/constants'
export type ManifestItem = { export type ManifestItem = {
id: number | string id: number | string
...@@ -150,6 +152,12 @@ type RenderOpts = { ...@@ -150,6 +152,12 @@ type RenderOpts = {
App: AppType App: AppType
ErrorDebug?: React.ComponentType<{ error: Error }> ErrorDebug?: React.ComponentType<{ error: Error }>
ampValidator?: (html: string, pathname: string) => Promise<void> ampValidator?: (html: string, pathname: string) => Promise<void>
unstable_getStaticProps?: (params: {
params: any | undefined
}) => {
props: any
revalidate: number | false
}
} }
function renderDocument( function renderDocument(
...@@ -259,23 +267,21 @@ export async function renderToHTML( ...@@ -259,23 +267,21 @@ export async function renderToHTML(
buildManifest, buildManifest,
reactLoadableManifest, reactLoadableManifest,
ErrorDebug, ErrorDebug,
unstable_getStaticProps,
} = renderOpts } = renderOpts
await Loadable.preloadAll() // Make sure all dynamic imports are loaded const isSpr = !!unstable_getStaticProps
const defaultAppGetInitialProps = const defaultAppGetInitialProps =
App.getInitialProps === (App as any).origGetInitialProps App.getInitialProps === (App as any).origGetInitialProps
let isAutoExport = const hasPageGetInitialProps = !!(Component as any).getInitialProps
typeof (Component as any).getInitialProps !== 'function' &&
defaultAppGetInitialProps
let isPrerender = pageConfig.experimentalPrerender === true const isAutoExport =
const isStaticPage = isPrerender || isAutoExport !hasPageGetInitialProps && defaultAppGetInitialProps && !isSpr
// TODO: revisit `?_nextPreviewSkeleton=(truthy)`
const isSkeleton = isPrerender && !!query._nextPreviewSkeleton if (hasPageGetInitialProps && isSpr) {
// remove from query so it doesn't end up in document throw new Error(SPR_GET_INITIAL_PROPS_CONFLICT + ` ${pathname}`)
delete query._nextPreviewSkeleton }
if (dev) { if (dev) {
const { isValidElementType } = require('react-is') const { isValidElementType } = require('react-is')
...@@ -297,7 +303,7 @@ export async function renderToHTML( ...@@ -297,7 +303,7 @@ export async function renderToHTML(
) )
} }
if (isStaticPage) { if (isAutoExport) {
// remove query values except ones that will be set during export // remove query values except ones that will be set during export
query = { query = {
amp: query.amp, amp: query.amp,
...@@ -306,16 +312,17 @@ export async function renderToHTML( ...@@ -306,16 +312,17 @@ export async function renderToHTML(
renderOpts.nextExport = true renderOpts.nextExport = true
} }
} }
if (isSkeleton) renderOpts.nextExport = true
if (isAutoExport) renderOpts.autoExport = true if (isAutoExport) renderOpts.autoExport = true
await Loadable.preloadAll() // Make sure all dynamic imports are loaded
// @ts-ignore url will always be set // @ts-ignore url will always be set
const asPath: string = req.url const asPath: string = req.url
const router = new ServerRouter(pathname, query, asPath) const router = new ServerRouter(pathname, query, asPath)
const ctx = { const ctx = {
err, err,
req: isStaticPage ? undefined : req, req: isAutoExport ? undefined : req,
res: isStaticPage ? undefined : res, res: isAutoExport ? undefined : res,
pathname, pathname,
query, query,
asPath, asPath,
...@@ -328,9 +335,6 @@ export async function renderToHTML( ...@@ -328,9 +335,6 @@ export async function renderToHTML(
}, },
} }
let props: any let props: any
const isDataPrerender =
pageConfig.experimentalPrerender === true &&
req.headers['content-type'] === 'application/json'
if (documentMiddlewareEnabled && typeof DocumentMiddleware === 'function') { if (documentMiddlewareEnabled && typeof DocumentMiddleware === 'function') {
await DocumentMiddleware(ctx) await DocumentMiddleware(ctx)
...@@ -364,29 +368,30 @@ export async function renderToHTML( ...@@ -364,29 +368,30 @@ export async function renderToHTML(
) )
try { try {
props = props = await loadGetInitialProps(App, {
isSkeleton && !isDataPrerender AppTree: ctx.AppTree,
? { pageProps: {} } Component,
: await loadGetInitialProps(App, { router,
AppTree: ctx.AppTree, ctx,
Component, })
router,
ctx, if (isSpr) {
}) const data = await unstable_getStaticProps!({
params: isDynamicRoute(pathname) ? query : undefined,
})
props.pageProps = data.props
// pass up revalidate and props for export
;(renderOpts as any).revalidate = data.revalidate
;(renderOpts as any).sprData = props
}
} catch (err) { } catch (err) {
if (!dev || !err) throw err if (!dev || !err) throw err
ctx.err = err ctx.err = err
renderOpts.err = err renderOpts.err = err
} }
if (isDataPrerender) {
res.setHeader('content-type', 'application/json')
res.end(JSON.stringify(props.pageProps || {}))
return null
}
// the response might be finished on the getInitialProps call // the response might be finished on the getInitialProps call
if (isResSent(res)) return null if (isResSent(res) && !isSpr) return null
const devFiles = buildManifest.devFiles const devFiles = buildManifest.devFiles
const files = [ const files = [
...@@ -490,7 +495,7 @@ export async function renderToHTML( ...@@ -490,7 +495,7 @@ export async function renderToHTML(
const docProps = await loadGetInitialProps(Document, { ...ctx, renderPage }) const docProps = await loadGetInitialProps(Document, { ...ctx, renderPage })
// the response might be finished on the getInitialProps call // the response might be finished on the getInitialProps call
if (isResSent(res)) return null if (isResSent(res) && !isSpr) return null
let dataManagerData = '[]' let dataManagerData = '[]'
if (dataManager) { if (dataManager) {
...@@ -529,7 +534,6 @@ export async function renderToHTML( ...@@ -529,7 +534,6 @@ export async function renderToHTML(
// update renderOpts so export knows current state // update renderOpts so export knows current state
renderOpts.inAmpMode = inAmpMode renderOpts.inAmpMode = inAmpMode
renderOpts.hybridAmp = hybridAmp renderOpts.hybridAmp = hybridAmp
if (isSkeleton) renderOpts.skeleton = true
let html = renderDocument(Document, { let html = renderDocument(Document, {
...renderOpts, ...renderOpts,
...@@ -566,6 +570,7 @@ export async function renderToHTML( ...@@ -566,6 +570,7 @@ export async function renderToHTML(
// fix &amp being escaped for amphtml rel link // fix &amp being escaped for amphtml rel link
html = html.replace(/&amp;amp=1/g, '&amp=1') html = html.replace(/&amp;amp=1/g, '&amp=1')
} }
return html return html
} }
......
import fs from 'fs'
import path from 'path'
import LRUCache from 'lru-cache'
import { promisify } from 'util'
import { PrerenderManifest } from '../../build'
import { PRERENDER_MANIFEST } from '../lib/constants'
import { normalizePagePath } from './normalize-page-path'
const readFile = promisify(fs.readFile)
const writeFile = promisify(fs.writeFile)
type SprCacheValue = {
html: string
pageData: any
isStale?: boolean
// milliseconds to revalidate after
revalidateAfter: number | false
}
let cache: LRUCache<string, SprCacheValue>
let prerenderManifest: PrerenderManifest
let sprOptions: {
flushToDisk?: boolean
pagesDir?: string
distDir?: string
dev?: boolean
} = {}
const getSeedPath = (pathname: string, ext: string): string => {
return path.join(sprOptions.pagesDir!, `${pathname}.${ext}`)
}
export const calculateRevalidate = (pathname: string): number | false => {
// in development we don't have a prerender-manifest
// and default to always revalidating to allow easier debugging
const curTime = new Date().getTime()
if (!sprOptions.dev) return curTime
const { initialRevalidateSeconds } = prerenderManifest.routes[pathname] || {
initialRevalidateSeconds: 1,
}
const revalidateAfter =
typeof initialRevalidateSeconds === 'number'
? initialRevalidateSeconds * 1000 + curTime
: initialRevalidateSeconds
return revalidateAfter
}
// initialize the SPR cache
export function initializeSprCache({
max,
dev,
distDir,
pagesDir,
flushToDisk,
}: {
dev: boolean
max?: number
distDir: string
pagesDir: string
flushToDisk?: boolean
}) {
sprOptions = {
dev,
distDir,
pagesDir,
flushToDisk:
!dev && (typeof flushToDisk !== 'undefined' ? flushToDisk : true),
}
try {
prerenderManifest = dev
? { routes: {} }
: JSON.parse(
fs.readFileSync(path.join(distDir, PRERENDER_MANIFEST), 'utf8')
)
} catch (_) {
prerenderManifest = { version: 1, routes: {} }
}
cache = new LRUCache({
// default to 50MB limit
max: max || 50 * 1024 * 1024,
length(val) {
// rough estimate of size of cache value
return val.html.length + JSON.stringify(val.pageData).length
},
})
}
// get data from SPR cache if available
export async function getSprCache(
pathname: string
): Promise<SprCacheValue | undefined> {
pathname = normalizePagePath(pathname)
let data: SprCacheValue | undefined = cache.get(pathname)
// let's check the disk for seed data
if (!data) {
try {
const html = await readFile(getSeedPath(pathname, 'html'), 'utf8')
const pageData = JSON.parse(
await readFile(getSeedPath(pathname, 'json'), 'utf8')
)
data = {
html,
pageData,
revalidateAfter: calculateRevalidate(pathname),
}
cache.set(pathname, data)
} catch (_) {
// unable to get data from disk
}
}
if (
data &&
data.revalidateAfter !== false &&
data.revalidateAfter < new Date().getTime()
) {
data.isStale = true
}
return data
}
// populate the SPR cache with new data
export async function setSprCache(
pathname: string,
data: {
html: string
pageData: any
},
revalidateSeconds?: number | false
) {
if (typeof revalidateSeconds !== 'undefined') {
prerenderManifest.routes[pathname] = {
initialRevalidateSeconds: revalidateSeconds,
}
}
pathname = normalizePagePath(pathname)
cache.set(pathname, {
...data,
revalidateAfter: calculateRevalidate(pathname),
})
if (sprOptions.flushToDisk) {
try {
await writeFile(getSeedPath(pathname, 'html'), data.html, 'utf8')
await writeFile(
getSeedPath(pathname, 'json'),
JSON.stringify(data.pageData),
'utf8'
)
} catch (error) {
// failed to flush to disk
console.warn('Failed to update prerender files for', pathname, error)
}
}
}
...@@ -97,6 +97,7 @@ ...@@ -97,6 +97,7 @@
"jest-worker": "24.9.0", "jest-worker": "24.9.0",
"launch-editor": "2.2.1", "launch-editor": "2.2.1",
"loader-utils": "1.2.3", "loader-utils": "1.2.3",
"lru-cache": "5.1.1",
"mini-css-extract-plugin": "0.8.0", "mini-css-extract-plugin": "0.8.0",
"mkdirp": "0.5.1", "mkdirp": "0.5.1",
"node-fetch": "2.6.0", "node-fetch": "2.6.0",
...@@ -150,6 +151,7 @@ ...@@ -150,6 +151,7 @@
"@types/find-up": "2.1.1", "@types/find-up": "2.1.1",
"@types/fresh": "0.5.0", "@types/fresh": "0.5.0",
"@types/loader-utils": "1.1.3", "@types/loader-utils": "1.1.3",
"@types/lru-cache": "5.1.0",
"@types/mini-css-extract-plugin": "0.8.0", "@types/mini-css-extract-plugin": "0.8.0",
"@types/mkdirp": "0.5.2", "@types/mkdirp": "0.5.2",
"@types/nanoid": "2.0.0", "@types/nanoid": "2.0.0",
......
...@@ -33,7 +33,7 @@ const fsStat = promisify(fs.stat) ...@@ -33,7 +33,7 @@ const fsStat = promisify(fs.stat)
export default class DevServer extends Server { export default class DevServer extends Server {
constructor (options) { constructor (options) {
super(options) super({ ...options, dev: true })
this.renderOpts.dev = true this.renderOpts.dev = true
this.renderOpts.ErrorDebug = ErrorDebug this.renderOpts.ErrorDebug = ErrorDebug
this.devReady = new Promise(resolve => { this.devReady = new Promise(resolve => {
......
...@@ -61,7 +61,6 @@ export type PageConfig = { ...@@ -61,7 +61,6 @@ export type PageConfig = {
*/ */
bodyParser?: { sizeLimit?: number | string } | false bodyParser?: { sizeLimit?: number | string } | false
} }
experimentalPrerender?: boolean
} }
export { NextPageContext, NextComponentType, NextApiResponse, NextApiRequest } export { NextPageContext, NextComponentType, NextApiResponse, NextApiRequest }
......
module.exports = {
target: 'serverless'
}
import Link from 'next/link' import Link from 'next/link'
export const config = { experimentalPrerender: true } // eslint-disable-next-line camelcase
export async function unstable_getStaticProps () {
const wrapPage = Comp => { return {
Comp.getInitialProps = () => ({ world: 'world' }) props: {
return Comp world: 'world',
time: new Date().getTime()
},
revalidate: 0
}
} }
export default wrapPage(({ world }) => ( export default ({ world, time }) => (
<> <>
<p>hello {world}</p> <p>hello {world}</p>
<span>time: {time}</span>
<Link href='/'> <Link href='/'>
<a id='home'>to home</a> <a id='home'>to home</a>
</Link> </Link>
<br /> <br />
<Link href='/something'> <Link href='/something'>
<a id='somethin'>to something</a> <a id='something'>to something</a>
</Link> </Link>
</> </>
)) )
import React from 'react'
import Link from 'next/link' import Link from 'next/link'
export const config = { experimentalPrerender: true } // eslint-disable-next-line camelcase
export async function unstable_getStaticParams () {
return [
'/blog/post-1/comment-1',
{ post: 'post-2', comment: 'comment-2' }
]
}
const Comment = ({ data }) => ( // eslint-disable-next-line camelcase
<> export async function unstable_getStaticProps ({ params }) {
<p>Comment: {data}</p> return {
<Link href='/'> props: {
<a id='home'>to home</a> post: params.post,
</Link> comment: params.comment,
</> time: new Date().getTime()
) },
revalidate: 2
}
}
Comment.getInitialProps = () => ({ export default ({ post, comment, time }) => {
data: typeof window === 'undefined' ? 'SSR' : 'CSR' return (
}) <>
<p>Post: {post}</p>
export default Comment <p>Comment: {comment}</p>
<span>time: {time}</span>
<Link href='/'>
<a id='home'>to home</a>
</Link>
</>
)
}
import React from 'react' import React from 'react'
import Link from 'next/link' import Link from 'next/link'
export const config = { experimentalPrerender: true } // eslint-disable-next-line camelcase
export async function unstable_getStaticParams () {
return [
'/blog/post-1',
{ post: 'post-2' },
'/blog/[post3]'
]
}
export default class Post extends React.Component { // eslint-disable-next-line camelcase
static async getInitialProps () { export async function unstable_getStaticProps ({ params }) {
return { if (params.post === 'post-10') {
data: typeof window === 'undefined' ? 'SSR' : 'CSR' await new Promise(resolve => {
} setTimeout(() => resolve(), 1000)
})
} }
render () { return {
return ( props: {
<> post: params.post,
<p>Post: {this.props.data}</p> time: (await import('perf_hooks')).performance.now()
<Link href='/'> },
<a id='home'>to home</a> revalidate: 10
</Link>
</>
)
} }
} }
export default ({ post, time }) => {
return (
<>
<p>Post: {post}</p>
<span>time: {time}</span>
<Link href='/'>
<a id='home'>to home</a>
</Link>
</>
)
}
import Link from 'next/link' import Link from 'next/link'
export const config = { experimentalPrerender: true } const Page = ({ world, time }) => {
const Page = ({ world }) => {
return ( return (
<> <>
<p>hello {world}</p> <p>hello {world}</p>
<span>time: {time}</span>
<Link href='/another'> <Link href='/another'>
<a id='another'>to another</a> <a id='another'>to another</a>
</Link> </Link>
...@@ -29,6 +28,6 @@ const Page = ({ world }) => { ...@@ -29,6 +28,6 @@ const Page = ({ world }) => {
) )
} }
Page.getInitialProps = () => ({ world: 'world' }) Page.getInitialProps = () => ({ world: 'world', time: new Date().getTime() })
export default Page export default Page
import React, { Component } from 'react' import React from 'react'
import Link from 'next/link' import Link from 'next/link'
export const config = { experimentalPrerender: true } // eslint-disable-next-line camelcase
export async function unstable_getStaticProps () {
class Page extends Component { return {
static async getInitialProps () { props: {
return { world: 'world' } world: 'world',
} time: new Date().getTime()
},
render () { revalidate: false
return (
<>
<p>hello {this.props.world}</p>
<Link href='/'>
<a id='home'>to home</a>
</Link>
<br />
<Link href='/another'>
<a id='another'>to another</a>
</Link>
</>
)
} }
} }
export default Page export default ({ world, time }) => {
return (
<>
<p>hello: {world}</p>
<span>time: {time}</span>
<Link href='/'>
<a id='home'>to home</a>
</Link>
<br />
<Link href='/another'>
<a id='another'>to another</a>
</Link>
</>
)
}
/* eslint-env jest */ /* eslint-env jest */
/* global jasmine */ /* global jasmine */
import fs from 'fs-extra'
import { join } from 'path' import { join } from 'path'
import webdriver from 'next-webdriver' import webdriver from 'next-webdriver'
import { import {
renderViaHTTP, renderViaHTTP,
fetchViaHTTP,
findPort, findPort,
launchApp, launchApp,
killApp, killApp,
waitFor,
nextBuild, nextBuild,
nextStart, nextStart,
waitFor nextExport,
startStaticServer
} from 'next-test-utils' } from 'next-test-utils'
jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60 * 5 jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60 * 2
const appDir = join(__dirname, '..') const appDir = join(__dirname, '..')
const nextConfig = join(appDir, 'next.config.js')
let app let app
let appPort let appPort
let distPagesDir
let exportDir
const runTests = () => { const navigateTest = () => {
it('should navigate between pages successfully', async () => { it('should navigate between pages successfully', async () => {
const browser = await webdriver(appPort, '/') const browser = await webdriver(appPort, '/')
let text = await browser.elementByCss('p').text() let text = await browser.elementByCss('p').text()
...@@ -50,7 +55,7 @@ const runTests = () => { ...@@ -50,7 +55,7 @@ const runTests = () => {
await browser.elementByCss('#post-1').click() await browser.elementByCss('#post-1').click()
await browser.waitForElementByCss('#home') await browser.waitForElementByCss('#home')
text = await browser.elementByCss('p').text() text = await browser.elementByCss('p').text()
expect(text).toMatch(/Post:.*?SSR/) expect(text).toMatch(/Post:.*?post-1/)
// go to / // go to /
await browser.elementByCss('#home').click() await browser.elementByCss('#home').click()
...@@ -59,17 +64,47 @@ const runTests = () => { ...@@ -59,17 +64,47 @@ const runTests = () => {
// go to /blog/post-1/comment-1 // go to /blog/post-1/comment-1
await browser.elementByCss('#comment-1').click() await browser.elementByCss('#comment-1').click()
await browser.waitForElementByCss('#home') await browser.waitForElementByCss('#home')
text = await browser.elementByCss('p').text() text = await browser.elementByCss('p:nth-child(2)').text()
expect(text).toMatch(/Comment:.*?SSR/) expect(text).toMatch(/Comment:.*?comment-1/)
await browser.close() await browser.close()
}) })
}
it('should SSR content correctly', async () => { const runTests = (dev = false) => {
navigateTest()
it('should SSR normal page correctly', async () => {
const html = await renderViaHTTP(appPort, '/') const html = await renderViaHTTP(appPort, '/')
expect(html).toMatch(/hello.*?world/) expect(html).toMatch(/hello.*?world/)
}) })
it('should SSR SPR page correctly', async () => {
const html = await renderViaHTTP(appPort, '/blog/post-1')
expect(html).toMatch(/Post:.*?post-1/)
})
it('should return data correctly', async () => {
const data = JSON.parse(
await renderViaHTTP(appPort, '/_next/data/something.json')
)
expect(data.pageProps.world).toBe('world')
})
it('should return data correctly for dynamic page', async () => {
const data = JSON.parse(
await renderViaHTTP(appPort, '/_next/data/blog/post-1.json')
)
expect(data.pageProps.post).toBe('post-1')
})
it('should return data correctly for dynamic page (non-seeded)', async () => {
const data = JSON.parse(
await renderViaHTTP(appPort, '/_next/data/blog/post-3.json')
)
expect(data.pageProps.post).toBe('post-3')
})
it('should navigate to a normal page and back', async () => { it('should navigate to a normal page and back', async () => {
const browser = await webdriver(appPort, '/') const browser = await webdriver(appPort, '/')
let text = await browser.elementByCss('p').text() let text = await browser.elementByCss('p').text()
...@@ -81,89 +116,163 @@ const runTests = () => { ...@@ -81,89 +116,163 @@ const runTests = () => {
expect(text).toMatch(/a normal page/) expect(text).toMatch(/a normal page/)
}) })
it('should return JSON when content-type is set', async () => { if (!dev) {
const data = await fetchViaHTTP(appPort, '/', undefined, { it('outputs a prerender-manifest correctly', async () => {
headers: { const manifest = require(join(appDir, '.next', 'prerender-manifest.json'))
'content-type': 'application/json'
expect(manifest.version).toBe(1)
expect(manifest.routes).toEqual({
'/blog/[post3]': {
initialRevalidateSeconds: 10
},
'/blog/post-1': {
initialRevalidateSeconds: 10
},
'/blog/post-2': {
initialRevalidateSeconds: 10
},
'/blog/post-1/comment-1': {
initialRevalidateSeconds: 2
},
'/blog/post-2/comment-2': {
initialRevalidateSeconds: 2
},
'/another': {
initialRevalidateSeconds: 0
},
'/something': {
initialRevalidateSeconds: false
}
})
})
it('outputs prerendered files correctly', async () => {
const routes = [
'/another',
'/something',
'/blog/post-1',
'/blog/post-2/comment-2'
]
for (const route of routes) {
await fs.access(join(distPagesDir, `${route}.html`), fs.constants.F_OK)
await fs.access(join(distPagesDir, `${route}.json`), fs.constants.F_OK)
} }
}).then(res => res.ok && res.json()) })
expect(data.world).toMatch('world') it('should handle de-duping correctly', async () => {
}) let vals = new Array(10).fill(null)
it('should generate skeleton without calling getInitialProps', async () => { vals = await Promise.all(
let html = await renderViaHTTP( vals.map(() => renderViaHTTP(appPort, '/blog/post-10'))
appPort, )
'/blog/post-1?_nextPreviewSkeleton=1' const val = vals[0]
) expect(val).toMatch(/Post:.*?post-10/)
expect(html).not.toMatch(/Post:.*?SSR/) expect(new Set(vals).size).toBe(1)
})
html = await renderViaHTTP( it('should handle revalidating HTML correctly', async () => {
appPort, const route = '/blog/post-1/comment-1'
'/blog/post-1/comment-1?_nextPreviewSkeleton=1' const initialHtml = await renderViaHTTP(appPort, route)
) expect(initialHtml).toMatch(/Post:.*?post-1/)
expect(html).not.toMatch(/Comment:.*?SSR/) expect(initialHtml).toMatch(/Comment:.*?comment-1/)
})
it('should call getInitialProps when viewing skeleton', async () => { await waitFor(2 * 1000)
let browser = await webdriver( await renderViaHTTP(appPort, route)
appPort,
'/blog/post-1?_nextPreviewSkeleton=1'
)
await waitFor(1000)
let text = await browser.elementByCss('p').text()
expect(text).toMatch(/Post:.*?SSR/)
await browser.close()
browser = await webdriver( await waitFor(2 * 1000)
appPort, const newHtml = await renderViaHTTP(appPort, route)
'/blog/post-1/comment-1?_nextPreviewSkeleton=1' expect(newHtml === initialHtml).toBe(false)
) expect(newHtml).toMatch(/Post:.*?post-1/)
await waitFor(1000) expect(newHtml).toMatch(/Comment:.*?comment-1/)
text = await browser.elementByCss('p').text() })
expect(text).toMatch(/Comment:.*?SSR/)
await browser.close() it('should handle revalidating JSON correctly', async () => {
}) const route = '/_next/data/blog/post-2/comment-3'
const initialJson = await renderViaHTTP(appPort, route)
expect(initialJson).toMatch(/post-2/)
expect(initialJson).toMatch(/comment-3/)
await waitFor(2 * 1000)
await renderViaHTTP(appPort, route)
await waitFor(2 * 1000)
const newJson = await renderViaHTTP(appPort, route)
expect(newJson === initialJson).toBe(false)
expect(newJson).toMatch(/post-2/)
expect(newJson).toMatch(/comment-3/)
})
}
} }
describe('SPR Prerender', () => { describe('SPR Prerender', () => {
describe('development mode', () => { describe('dev mode', () => {
beforeAll(async () => { beforeAll(async () => {
appPort = await findPort() appPort = await findPort()
app = await launchApp(appDir, appPort) app = await launchApp(appDir, appPort)
}) })
afterAll(() => killApp(app)) afterAll(() => killApp(app))
runTests(true)
})
describe('serverless mode', () => {
beforeAll(async () => {
await fs.writeFile(
nextConfig,
`module.exports = { target: 'serverless' }`,
'utf8'
)
await nextBuild(appDir)
appPort = await findPort()
app = await nextStart(appDir, appPort)
distPagesDir = join(appDir, '.next/serverless/pages')
})
afterAll(() => killApp(app))
runTests() runTests()
}) })
describe('production mode', () => { describe('production mode', () => {
beforeAll(async () => { beforeAll(async () => {
try {
await fs.unlink(nextConfig)
} catch (_) {}
await nextBuild(appDir) await nextBuild(appDir)
appPort = await findPort() appPort = await findPort()
app = await nextStart(appDir, appPort) app = await nextStart(appDir, appPort)
const buildId = await fs.readFile(join(appDir, '.next/BUILD_ID'), 'utf8')
distPagesDir = join(appDir, '.next/server/static', buildId, 'pages')
}) })
afterAll(() => killApp(app)) afterAll(() => killApp(app))
runTests() runTests()
})
it('outputs a prerender-manifest correctly', async () => { describe('export mode', () => {
const manifest = require(join(appDir, '.next', 'prerender-manifest.json')) beforeAll(async () => {
const prerenderablePaths = Object.keys(manifest.prerenderFiles) exportDir = join(appDir, 'out')
await nextBuild(appDir)
expect(JSON.stringify(prerenderablePaths.sort())).toBe( await nextExport(appDir, { outdir: exportDir })
JSON.stringify( app = await startStaticServer(exportDir)
[ appPort = app.address().port
'/index.html',
'/another.html',
'/something.html',
'/blog/[post].html',
'/blog/[post]/[comment].html'
]
.map(pg => join('.next/serverless/pages/', pg))
.sort()
)
)
}) })
afterAll(() => killApp(app))
it('should copy prerender files correctly', async () => {
const routes = [
'/another',
'/something',
'/blog/post-1',
'/blog/post-2/comment-2'
]
for (const route of routes) {
await fs.access(join(exportDir, `${route}.html`))
await fs.access(join(exportDir, '_next/data', `${route}.json`))
}
})
navigateTest()
}) })
}) })
...@@ -2200,6 +2200,11 @@ ...@@ -2200,6 +2200,11 @@
resolved "https://registry.yarnpkg.com/@types/long/-/long-4.0.0.tgz#719551d2352d301ac8b81db732acb6bdc28dbdef" resolved "https://registry.yarnpkg.com/@types/long/-/long-4.0.0.tgz#719551d2352d301ac8b81db732acb6bdc28dbdef"
integrity sha512-1w52Nyx4Gq47uuu0EVcsHBxZFJgurQ+rTKS3qMHxR1GY2T8c2AJYd6vZoZ9q1rupaDjU0yT+Jc2XTyXkjeMA+Q== integrity sha512-1w52Nyx4Gq47uuu0EVcsHBxZFJgurQ+rTKS3qMHxR1GY2T8c2AJYd6vZoZ9q1rupaDjU0yT+Jc2XTyXkjeMA+Q==
"@types/lru-cache@5.1.0":
version "5.1.0"
resolved "https://registry.yarnpkg.com/@types/lru-cache/-/lru-cache-5.1.0.tgz#57f228f2b80c046b4a1bd5cac031f81f207f4f03"
integrity sha512-RaE0B+14ToE4l6UqdarKPnXwVDuigfFv+5j9Dze/Nqr23yyuqdNvzcZi3xB+3Agvi5R4EOgAksfv3lXX4vBt9w==
"@types/mime@*": "@types/mime@*":
version "2.0.1" version "2.0.1"
resolved "https://registry.yarnpkg.com/@types/mime/-/mime-2.0.1.tgz#dc488842312a7f075149312905b5e3c0b054c79d" resolved "https://registry.yarnpkg.com/@types/mime/-/mime-2.0.1.tgz#dc488842312a7f075149312905b5e3c0b054c79d"
...@@ -9274,6 +9279,13 @@ lowercase-keys@^2.0.0: ...@@ -9274,6 +9279,13 @@ lowercase-keys@^2.0.0:
resolved "https://registry.yarnpkg.com/lowercase-keys/-/lowercase-keys-2.0.0.tgz#2603e78b7b4b0006cbca2fbcc8a3202558ac9479" resolved "https://registry.yarnpkg.com/lowercase-keys/-/lowercase-keys-2.0.0.tgz#2603e78b7b4b0006cbca2fbcc8a3202558ac9479"
integrity sha512-tqNXrS78oMOE73NMxK4EMLQsQowWf8jKooH9g7xPavRT706R6bkQJ6DY2Te7QukaZsulxa30wQ7bk0pm4XiHmA== integrity sha512-tqNXrS78oMOE73NMxK4EMLQsQowWf8jKooH9g7xPavRT706R6bkQJ6DY2Te7QukaZsulxa30wQ7bk0pm4XiHmA==
lru-cache@5.1.1, lru-cache@^5.1.1:
version "5.1.1"
resolved "https://registry.yarnpkg.com/lru-cache/-/lru-cache-5.1.1.tgz#1da27e6710271947695daf6848e847f01d84b920"
integrity sha512-KpNARQA3Iwv+jTA0utUVVbrh+Jlrr1Fv0e56GGzAFOXN7dk/FviaDW8LHmK52DlcH4WP2n6gI8vN1aesBFgo9w==
dependencies:
yallist "^3.0.2"
lru-cache@^4.0.1, lru-cache@^4.1.2, lru-cache@^4.1.3: lru-cache@^4.0.1, lru-cache@^4.1.2, lru-cache@^4.1.3:
version "4.1.5" version "4.1.5"
resolved "https://registry.yarnpkg.com/lru-cache/-/lru-cache-4.1.5.tgz#8bbe50ea85bed59bc9e33dcab8235ee9bcf443cd" resolved "https://registry.yarnpkg.com/lru-cache/-/lru-cache-4.1.5.tgz#8bbe50ea85bed59bc9e33dcab8235ee9bcf443cd"
...@@ -9282,13 +9294,6 @@ lru-cache@^4.0.1, lru-cache@^4.1.2, lru-cache@^4.1.3: ...@@ -9282,13 +9294,6 @@ lru-cache@^4.0.1, lru-cache@^4.1.2, lru-cache@^4.1.3:
pseudomap "^1.0.2" pseudomap "^1.0.2"
yallist "^2.1.2" yallist "^2.1.2"
lru-cache@^5.1.1:
version "5.1.1"
resolved "https://registry.yarnpkg.com/lru-cache/-/lru-cache-5.1.1.tgz#1da27e6710271947695daf6848e847f01d84b920"
integrity sha512-KpNARQA3Iwv+jTA0utUVVbrh+Jlrr1Fv0e56GGzAFOXN7dk/FviaDW8LHmK52DlcH4WP2n6gI8vN1aesBFgo9w==
dependencies:
yallist "^3.0.2"
macos-release@^2.2.0: macos-release@^2.2.0:
version "2.2.0" version "2.2.0"
resolved "https://registry.yarnpkg.com/macos-release/-/macos-release-2.2.0.tgz#ab58d55dd4714f0a05ad4b0e90f4370fef5cdea8" resolved "https://registry.yarnpkg.com/macos-release/-/macos-release-2.2.0.tgz#ab58d55dd4714f0a05ad4b0e90f4370fef5cdea8"
......
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