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'
export const dropBundleIdentifier = '__NEXT_DROP_CLIENT_FILE__'
export const sprStatus = { used: false }
const configKeys = new Set(['amp', 'experimentalPrerender'])
const configKeys = new Set(['amp'])
const pageComponentVar = '__NEXT_COMP'
// this value can't be optimized by terser so the shorter the better
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
function replaceBundle(path: any, t: typeof BabelTypes) {
......@@ -37,6 +39,7 @@ interface ConfigState {
bundleDropped?: boolean
}
// config to parsing pageConfig for client bundles
export default function nextPageConfig({
types: t,
}: {
......@@ -52,17 +55,33 @@ export default function nextPageConfig({
path: NodePath<BabelTypes.ExportNamedDeclaration>,
state: any
) {
if (
state.bundleDropped ||
!path.node.declaration ||
!(path.node.declaration as any).declarations
)
if (state.bundleDropped || !path.node.declaration) {
return
const { declarations } = path.node.declaration as any
}
const { declarations, id } = path.node.declaration as any
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) {
if (declaration.id.name !== 'config') continue
if (declaration.id.name !== 'config') {
continue
}
if (declaration.init.type !== 'ObjectExpression') {
const pageName =
......@@ -90,9 +109,8 @@ export default function nextPageConfig({
state.bundleDropped = true
return
}
state.isPrerender = config.experimentalPrerender === true
sprStatus.used = sprStatus.used || state.isPrerender
// remove export const config from bundle
path.remove()
},
},
state
......
......@@ -43,6 +43,7 @@ import { getPageChunks } from './webpack/plugins/chunk-graph-plugin'
import { writeBuildId } from './write-build-id'
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)
......@@ -52,11 +53,15 @@ const fsReadFile = promisify(fs.readFile)
const fsWriteFile = promisify(fs.writeFile)
const mkdirp = promisify(mkdirpOrig)
const staticCheckWorker = require.resolve('./static-checker')
const staticCheckWorker = require.resolve('./utils')
export type PrerenderFile = {
lambda: string
contentTypes: string[]
export type SprRoute = {
initialRevalidateSeconds: number | false
}
export type PrerenderManifest = {
version: number
routes: { [route: string]: SprRoute }
}
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',
})
const distPath = path.join(dir, config.distDir)
const pageKeys = Object.keys(mappedPages)
const manifestPath = path.join(
distDir,
......@@ -269,6 +273,7 @@ export default async function build(dir: string, conf = null): Promise<void> {
const staticPages = new Set<string>()
const invalidPages = new Set<string>()
const hybridAmpPages = new Set<string>()
const additionalSprPaths = new Map<string, Array<string>>()
const pageInfos = new Map<string, PageInfo>()
const pagesManifest = JSON.parse(await fsReadFile(manifestPath, 'utf8'))
const buildManifest = JSON.parse(await fsReadFile(buildManifestPath, 'utf8'))
......@@ -342,15 +347,24 @@ export default async function build(dir: string, conf = null): Promise<void> {
if (nonReservedPage) {
try {
let result: any = await (staticCheckWorkers as any).default({
let result: any = await (staticCheckWorkers as any).isPageStatic(
page,
serverBundle,
runtimeEnvConfig,
})
runtimeEnvConfig
)
if (result.isHybridAmp) {
hybridAmpPages.add(page)
}
if (result.prerender) {
sprPages.add(page)
if (result.prerenderRoutes) {
additionalSprPaths.set(page, result.prerenderRoutes)
}
}
if (result.static && customAppGetInitialProps === false) {
staticPages.add(page)
isStatic = true
......@@ -394,7 +408,7 @@ export default async function build(dir: string, conf = null): Promise<void> {
}
await writeBuildId(distDir, buildId)
const prerenderFiles: { [path: string]: PrerenderFile } = {}
const finalPrerenderRoutes: { [route: string]: SprRoute } = {}
if (staticPages.size > 0 || sprPages.size > 0) {
const combinedPages = [...staticPages, ...sprPages]
......@@ -406,9 +420,33 @@ export default async function build(dir: string, conf = null): Promise<void> {
pages: combinedPages,
outdir: path.join(distDir, 'export'),
}
const exportConfig = {
const exportConfig: any = {
...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,
}
await exportApp(dir, exportOptions, exportConfig)
......@@ -419,9 +457,13 @@ export default async function build(dir: string, conf = null): Promise<void> {
await fsUnlink(serverBundle)
}
const moveExportedPage = async (page: string, file: string) => {
const isSpr = sprPages.has(page)
file = `${file}.html`
const moveExportedPage = async (
page: string,
file: string,
isSpr: boolean,
ext: 'html' | 'json'
) => {
file = `${file}.${ext}`
const orig = path.join(exportOptions.outdir, file)
const relativeDest = (isLikeServerless
? path.join('pages', file)
......@@ -434,17 +476,7 @@ export default async function build(dir: string, conf = null): Promise<void> {
relativeDest
)
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 {
if (!isSpr) {
pagesManifest[page] = relativeDest
if (page === '/') pagesManifest['/index'] = relativeDest
if (page === '/.amp') pagesManifest['/index.amp'] = relativeDest
......@@ -454,10 +486,43 @@ export default async function build(dir: string, conf = null): Promise<void> {
}
for (const page of combinedPages) {
const isSpr = sprPages.has(page)
const isDynamic = isDynamicRoute(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)
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
......@@ -479,9 +544,14 @@ export default async function build(dir: string, conf = null): Promise<void> {
)
if (sprPages.size > 0) {
const prerenderManifest: PrerenderManifest = {
version: 1,
routes: finalPrerenderRoutes,
}
await fsWriteFile(
path.join(distDir, PRERENDER_MANIFEST),
JSON.stringify({ prerenderFiles }),
JSON.stringify(prerenderManifest),
'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'
import prettyBytes from '../lib/pretty-bytes'
import { recursiveReadDir } from '../lib/recursive-readdir'
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 fileStats: { [k: string]: Promise<fs.Stats> } = {}
......@@ -172,10 +175,16 @@ export async function getPageSizeInKb(
return -1
}
export function isPageStatic(
export async function isPageStatic(
page: string,
serverBundle: string,
runtimeEnvConfig: any
): { static?: boolean; prerender?: boolean; isHybridAmp?: boolean } {
): Promise<{
static?: boolean
prerender?: boolean
isHybridAmp?: boolean
prerenderRoutes?: string[] | undefined
}> {
try {
require('../next-server/lib/runtime-config').setConfig(runtimeEnvConfig)
const mod = require(serverBundle)
......@@ -184,12 +193,78 @@ export function isPageStatic(
if (!Comp || !isValidElementType(Comp) || typeof Comp === 'string') {
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 {
static: typeof (Comp as any).getInitialProps !== 'function',
prerender: config.experimentalPrerender === true,
static: !hasStaticProps && !hasGetInitialProps,
isHybridAmp: config.amp === 'hybrid',
prerenderRoutes: prerenderPaths,
prerender: hasStaticProps,
}
} catch (err) {
if (err.code === 'MODULE_NOT_FOUND') return {}
......
......@@ -84,6 +84,12 @@ const nextServerlessLoader: loader.Loader = function() {
import * as ComponentInfo from '${absolutePagePath}';
const Component = ComponentInfo.default
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 _app = App
export async function renderReqToHTML(req, res, fromExport) {
......@@ -91,12 +97,21 @@ const nextServerlessLoader: loader.Loader = function() {
App,
Document,
buildManifest,
unstable_getStaticProps,
reactLoadableManifest,
canonicalBase: "${canonicalBase}",
buildId: "${buildId}",
assetPrefix: "${assetPrefix}",
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 renderOpts = Object.assign(
......@@ -112,10 +127,10 @@ const nextServerlessLoader: loader.Loader = function() {
${page === '/_error' ? `res.statusCode = 404` : ''}
${
isDynamicRoute(page)
? `const params = fromExport ? {} : getRouteMatcher(getRouteRegex("${page}"))(parsedUrl.pathname) || {};`
? `const params = fromExport && !unstable_getStaticProps ? {} : getRouteMatcher(getRouteRegex("${page}"))(parsedUrl.pathname) || {};`
: `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 }
return result
......
......@@ -3,9 +3,9 @@ import chalk from 'chalk'
import Worker from 'jest-worker'
import { promisify } from 'util'
import mkdirpModule from 'mkdirp'
import { resolve, join } from 'path'
import { resolve, join, dirname } from 'path'
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 { recursiveDelete } from '../lib/recursive-delete'
import { formatAmpMessages } from '../build/output/index'
......@@ -18,12 +18,15 @@ import {
PAGES_MANIFEST,
CONFIG_FILE,
BUILD_ID_FILE,
PRERENDER_MANIFEST,
SERVERLESS_DIRECTORY,
CLIENT_PUBLIC_FILES_PATH,
CLIENT_STATIC_FILES_PATH
} from '../next-server/lib/constants'
import createSpinner from '../build/spinner'
const mkdirp = promisify(mkdirpModule)
const copyFile = promisify(copyFileOrig)
const createProgress = (total, label = 'Exporting') => {
let curProgress = 0
......@@ -78,8 +81,9 @@ export default async function (dir, options, configuration) {
const threads = options.threads || Math.max(cpus().length - 1, 1)
const distDir = join(dir, nextConfig.distDir)
const subFolders = nextConfig.exportTrailingSlash
const isLikeServerless = nextConfig.target !== 'server'
if (!options.buildExport && nextConfig.target !== 'server') {
if (!options.buildExport && isLikeServerless) {
throw new Error(
'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) {
const pagesManifest =
!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 defaultPathMap = {}
......@@ -201,6 +218,7 @@ export default async function (dir, options, configuration) {
}
const progress = !options.silent && createProgress(filteredPaths.length)
const sprDataDir = options.buildExport ? outDir : join(outDir, '_next/data')
const ampValidations = {}
let hadValidationError = false
......@@ -241,9 +259,11 @@ export default async function (dir, options, configuration) {
distDir,
buildId,
outDir,
sprDataDir,
renderOpts,
serverRuntimeConfig,
subFolders,
buildExport: options.buildExport,
serverless: isTargetLikeServerless(nextConfig.target)
})
......@@ -254,12 +274,36 @@ export default async function (dir, options, configuration) {
Array.isArray(result && result.errors) && result.errors.length > 0
}
renderError |= result.error
if (
options.buildExport &&
typeof result.fromBuildExportRevalidate !== 'undefined'
) {
configuration.initialPageRevalidationMap[path] =
result.fromBuildExportRevalidate
}
if (progress) progress()
})
)
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) {
console.log(formatAmpMessages(ampValidations))
}
......
......@@ -24,7 +24,9 @@ export default async function ({
distDir,
buildId,
outDir,
sprDataDir,
renderOpts,
buildExport,
serverRuntimeConfig,
subFolders,
serverless
......@@ -35,7 +37,7 @@ export default async function ({
try {
let { query = {} } = pathMap
const { page, sprPage } = pathMap
const { page } = pathMap
const filePath = path === '/' ? '/index' : path
const ampPath = `${filePath}.amp`
......@@ -71,12 +73,6 @@ export default async function ({
...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({
serverRuntimeConfig,
publicRuntimeConfig: renderOpts.runtimeConfig
......@@ -104,12 +100,24 @@ export default async function ({
let curRenderOpts = {}
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) {
renderMethod = require(join(
const mod = require(join(
distDir,
'serverless/pages',
(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)
curRenderOpts = result.renderOpts || {}
html = result.html
......@@ -125,6 +133,12 @@ export default async function ({
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') {
html = components.Component
} else {
......@@ -184,6 +198,18 @@ export default async function ({
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')
return results
} 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'
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 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 {
(Component as any).__NEXT_SPR
) {
let status: any
const url = ctx.asPath
? ctx.asPath
: format({
pathname: ctx.pathname,
query: ctx.query,
})
const { pathname } = parse(ctx.asPath || ctx.pathname)
props = await fetch(url, {
headers: { 'content-type': 'application/json' },
})
props = await fetch(`/_next/data${pathname}.json`)
.then(res => {
if (!res.ok) {
status = res.status
......@@ -640,11 +633,10 @@ export default class Router implements BaseRouter {
}
return res.json()
})
.then((pageProps: any) => {
return { pageProps }
})
.catch((err: Error) => {
return { error: err.message, status }
console.error(`Failed to load data`, status, err)
window.location.href = pathname!
return new Promise(() => {})
})
} else {
const AppTree = this._wrapApp(App)
......
......@@ -46,6 +46,7 @@ const defaultConfig: { [key: string]: any } = {
modern: false,
profiling: false,
publicDirectory: false,
sprFlushToDisk: true,
},
future: {
excludeDefaultMomentLocales: false,
......
......@@ -15,6 +15,12 @@ export function interopDefault(mod: any) {
export type LoadComponentsReturnType = {
Component: any
pageConfig: PageConfig
unstable_getStaticProps?: (params: {
params: any
}) => {
props: any
revalidate: number | false
}
buildManifest?: any
reactLoadableManifest?: any
Document?: any
......@@ -30,7 +36,11 @@ export async function loadComponents(
): Promise<LoadComponentsReturnType> {
if (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(
distDir,
......@@ -76,5 +86,6 @@ export async function loadComponents(
DocumentMiddleware,
reactLoadableManifest,
pageConfig: ComponentMod.config || {},
unstable_getStaticProps: ComponentMod.unstable_getStaticProps,
}
}
......@@ -4,7 +4,7 @@ import { IncomingMessage, ServerResponse } from 'http'
import { join, resolve, sep } from 'path'
import { parse as parseQs, ParsedUrlQuery } from 'querystring'
import { parse as parseUrl, UrlWithParsedQuery } from 'url'
import { withCoalescedInvoke } from '../../lib/coalesced-function'
import {
BUILD_ID_FILE,
CLIENT_PUBLIC_FILES_PATH,
......@@ -22,7 +22,7 @@ import {
isDynamicRoute,
} from '../lib/router/utils'
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 loadConfig, { isTargetLikeServerless } from './config'
import { recursiveReadDirSync } from './lib/recursive-readdir-sync'
......@@ -33,6 +33,7 @@ import Router, { Params, route, Route, RouteMatch } from './router'
import { sendHTML } from './send-html'
import { serveStatic } from './serve-static'
import { isBlockedPage, isInternalUrl } from './utils'
import { initializeSprCache, getSprCache, setSprCache } from './spr-cache'
type NextConfig = any
......@@ -56,6 +57,7 @@ export type ServerConstructor = {
* Object what you would use in next.config.js - @default {}
*/
conf?: NextConfig
dev?: boolean
}
export default class Server {
......@@ -88,13 +90,13 @@ export default class Server {
staticMarkup = false,
quiet = false,
conf = null,
dev = false,
}: ServerConstructor = {}) {
this.dir = resolve(dir)
this.quiet = quiet
const phase = this.currentPhase()
this.nextConfig = loadConfig(phase, this.dir, conf)
this.distDir = join(this.dir, this.nextConfig.distDir)
// this.pagesDir = join(this.dir, 'pages')
this.publicDir = join(this.dir, CLIENT_PUBLIC_FILES_PATH)
this.pagesManifest = join(
this.distDir,
......@@ -146,8 +148,20 @@ export default class Server {
const routes = this.generateRoutes()
this.router = new Router(routes)
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 {
......@@ -229,6 +243,24 @@ export default class Server {
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*'),
// 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 {
)
}
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(
req: IncomingMessage,
res: ServerResponse,
......@@ -492,24 +531,122 @@ export default class Server {
query: ParsedUrlQuery = {},
result: LoadComponentsReturnType,
opts: any
) {
): Promise<string | null> {
// handle static page
if (typeof result.Component === 'string') {
return result.Component
}
// handle serverless
if (
// check request state
const isLikeServerless =
typeof result.Component === 'object' &&
typeof result.Component.renderReqToHTML === 'function'
) {
return result.Component.renderReqToHTML(req, res)
const isSpr = !!result.unstable_getStaticProps
// 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, {
...result,
...opts,
// If we're here, that means data is missing or it's stale.
// 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(
......@@ -556,9 +693,20 @@ export default class Server {
req,
res,
dynamicRoute.page,
{ ...query, ...params },
// only add params for SPR enabled pages
{
...(result.unstable_getStaticProps
? { _nextSprData: query._nextSprData }
: query),
...params,
},
result,
{ ...this.renderOpts, amphtml, hasAmp, dataOnly }
{
...this.renderOpts,
amphtml,
hasAmp,
dataOnly,
}
)
}
)
......
......@@ -29,6 +29,8 @@ import optimizeAmp from './optimize-amp'
import { isInAmpMode } from '../lib/amp'
// Uses a module path because of the compiled output directory location
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 = {
id: number | string
......@@ -150,6 +152,12 @@ type RenderOpts = {
App: AppType
ErrorDebug?: React.ComponentType<{ error: Error }>
ampValidator?: (html: string, pathname: string) => Promise<void>
unstable_getStaticProps?: (params: {
params: any | undefined
}) => {
props: any
revalidate: number | false
}
}
function renderDocument(
......@@ -259,23 +267,21 @@ export async function renderToHTML(
buildManifest,
reactLoadableManifest,
ErrorDebug,
unstable_getStaticProps,
} = renderOpts
await Loadable.preloadAll() // Make sure all dynamic imports are loaded
const isSpr = !!unstable_getStaticProps
const defaultAppGetInitialProps =
App.getInitialProps === (App as any).origGetInitialProps
let isAutoExport =
typeof (Component as any).getInitialProps !== 'function' &&
defaultAppGetInitialProps
const hasPageGetInitialProps = !!(Component as any).getInitialProps
let isPrerender = pageConfig.experimentalPrerender === true
const isStaticPage = isPrerender || isAutoExport
// TODO: revisit `?_nextPreviewSkeleton=(truthy)`
const isSkeleton = isPrerender && !!query._nextPreviewSkeleton
// remove from query so it doesn't end up in document
delete query._nextPreviewSkeleton
const isAutoExport =
!hasPageGetInitialProps && defaultAppGetInitialProps && !isSpr
if (hasPageGetInitialProps && isSpr) {
throw new Error(SPR_GET_INITIAL_PROPS_CONFLICT + ` ${pathname}`)
}
if (dev) {
const { isValidElementType } = require('react-is')
......@@ -297,7 +303,7 @@ export async function renderToHTML(
)
}
if (isStaticPage) {
if (isAutoExport) {
// remove query values except ones that will be set during export
query = {
amp: query.amp,
......@@ -306,16 +312,17 @@ export async function renderToHTML(
renderOpts.nextExport = true
}
}
if (isSkeleton) renderOpts.nextExport = true
if (isAutoExport) renderOpts.autoExport = true
await Loadable.preloadAll() // Make sure all dynamic imports are loaded
// @ts-ignore url will always be set
const asPath: string = req.url
const router = new ServerRouter(pathname, query, asPath)
const ctx = {
err,
req: isStaticPage ? undefined : req,
res: isStaticPage ? undefined : res,
req: isAutoExport ? undefined : req,
res: isAutoExport ? undefined : res,
pathname,
query,
asPath,
......@@ -328,9 +335,6 @@ export async function renderToHTML(
},
}
let props: any
const isDataPrerender =
pageConfig.experimentalPrerender === true &&
req.headers['content-type'] === 'application/json'
if (documentMiddlewareEnabled && typeof DocumentMiddleware === 'function') {
await DocumentMiddleware(ctx)
......@@ -364,29 +368,30 @@ export async function renderToHTML(
)
try {
props =
isSkeleton && !isDataPrerender
? { pageProps: {} }
: await loadGetInitialProps(App, {
AppTree: ctx.AppTree,
Component,
router,
ctx,
})
props = await loadGetInitialProps(App, {
AppTree: ctx.AppTree,
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) {
if (!dev || !err) throw err
ctx.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
if (isResSent(res)) return null
if (isResSent(res) && !isSpr) return null
const devFiles = buildManifest.devFiles
const files = [
......@@ -490,7 +495,7 @@ export async function renderToHTML(
const docProps = await loadGetInitialProps(Document, { ...ctx, renderPage })
// the response might be finished on the getInitialProps call
if (isResSent(res)) return null
if (isResSent(res) && !isSpr) return null
let dataManagerData = '[]'
if (dataManager) {
......@@ -529,7 +534,6 @@ export async function renderToHTML(
// update renderOpts so export knows current state
renderOpts.inAmpMode = inAmpMode
renderOpts.hybridAmp = hybridAmp
if (isSkeleton) renderOpts.skeleton = true
let html = renderDocument(Document, {
...renderOpts,
......@@ -566,6 +570,7 @@ export async function renderToHTML(
// fix &amp being escaped for amphtml rel link
html = html.replace(/&amp;amp=1/g, '&amp=1')
}
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 @@
"jest-worker": "24.9.0",
"launch-editor": "2.2.1",
"loader-utils": "1.2.3",
"lru-cache": "5.1.1",
"mini-css-extract-plugin": "0.8.0",
"mkdirp": "0.5.1",
"node-fetch": "2.6.0",
......@@ -150,6 +151,7 @@
"@types/find-up": "2.1.1",
"@types/fresh": "0.5.0",
"@types/loader-utils": "1.1.3",
"@types/lru-cache": "5.1.0",
"@types/mini-css-extract-plugin": "0.8.0",
"@types/mkdirp": "0.5.2",
"@types/nanoid": "2.0.0",
......
......@@ -33,7 +33,7 @@ const fsStat = promisify(fs.stat)
export default class DevServer extends Server {
constructor (options) {
super(options)
super({ ...options, dev: true })
this.renderOpts.dev = true
this.renderOpts.ErrorDebug = ErrorDebug
this.devReady = new Promise(resolve => {
......
......@@ -61,7 +61,6 @@ export type PageConfig = {
*/
bodyParser?: { sizeLimit?: number | string } | false
}
experimentalPrerender?: boolean
}
export { NextPageContext, NextComponentType, NextApiResponse, NextApiRequest }
......
module.exports = {
target: 'serverless'
}
import Link from 'next/link'
export const config = { experimentalPrerender: true }
const wrapPage = Comp => {
Comp.getInitialProps = () => ({ world: 'world' })
return Comp
// eslint-disable-next-line camelcase
export async function unstable_getStaticProps () {
return {
props: {
world: 'world',
time: new Date().getTime()
},
revalidate: 0
}
}
export default wrapPage(({ world }) => (
export default ({ world, time }) => (
<>
<p>hello {world}</p>
<span>time: {time}</span>
<Link href='/'>
<a id='home'>to home</a>
</Link>
<br />
<Link href='/something'>
<a id='somethin'>to something</a>
<a id='something'>to something</a>
</Link>
</>
))
)
import React from 'react'
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 }) => (
<>
<p>Comment: {data}</p>
<Link href='/'>
<a id='home'>to home</a>
</Link>
</>
)
// eslint-disable-next-line camelcase
export async function unstable_getStaticProps ({ params }) {
return {
props: {
post: params.post,
comment: params.comment,
time: new Date().getTime()
},
revalidate: 2
}
}
Comment.getInitialProps = () => ({
data: typeof window === 'undefined' ? 'SSR' : 'CSR'
})
export default Comment
export default ({ post, comment, time }) => {
return (
<>
<p>Post: {post}</p>
<p>Comment: {comment}</p>
<span>time: {time}</span>
<Link href='/'>
<a id='home'>to home</a>
</Link>
</>
)
}
import React from 'react'
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 {
static async getInitialProps () {
return {
data: typeof window === 'undefined' ? 'SSR' : 'CSR'
}
// eslint-disable-next-line camelcase
export async function unstable_getStaticProps ({ params }) {
if (params.post === 'post-10') {
await new Promise(resolve => {
setTimeout(() => resolve(), 1000)
})
}
render () {
return (
<>
<p>Post: {this.props.data}</p>
<Link href='/'>
<a id='home'>to home</a>
</Link>
</>
)
return {
props: {
post: params.post,
time: (await import('perf_hooks')).performance.now()
},
revalidate: 10
}
}
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'
export const config = { experimentalPrerender: true }
const Page = ({ world }) => {
const Page = ({ world, time }) => {
return (
<>
<p>hello {world}</p>
<span>time: {time}</span>
<Link href='/another'>
<a id='another'>to another</a>
</Link>
......@@ -29,6 +28,6 @@ const Page = ({ world }) => {
)
}
Page.getInitialProps = () => ({ world: 'world' })
Page.getInitialProps = () => ({ world: 'world', time: new Date().getTime() })
export default Page
import React, { Component } from 'react'
import React from 'react'
import Link from 'next/link'
export const config = { experimentalPrerender: true }
class Page extends Component {
static async getInitialProps () {
return { world: 'world' }
}
render () {
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>
</>
)
// eslint-disable-next-line camelcase
export async function unstable_getStaticProps () {
return {
props: {
world: 'world',
time: new Date().getTime()
},
revalidate: false
}
}
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 */
/* global jasmine */
import fs from 'fs-extra'
import { join } from 'path'
import webdriver from 'next-webdriver'
import {
renderViaHTTP,
fetchViaHTTP,
findPort,
launchApp,
killApp,
waitFor,
nextBuild,
nextStart,
waitFor
nextExport,
startStaticServer
} from 'next-test-utils'
jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60 * 5
jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60 * 2
const appDir = join(__dirname, '..')
const nextConfig = join(appDir, 'next.config.js')
let app
let appPort
let distPagesDir
let exportDir
const runTests = () => {
const navigateTest = () => {
it('should navigate between pages successfully', async () => {
const browser = await webdriver(appPort, '/')
let text = await browser.elementByCss('p').text()
......@@ -50,7 +55,7 @@ const runTests = () => {
await browser.elementByCss('#post-1').click()
await browser.waitForElementByCss('#home')
text = await browser.elementByCss('p').text()
expect(text).toMatch(/Post:.*?SSR/)
expect(text).toMatch(/Post:.*?post-1/)
// go to /
await browser.elementByCss('#home').click()
......@@ -59,17 +64,47 @@ const runTests = () => {
// go to /blog/post-1/comment-1
await browser.elementByCss('#comment-1').click()
await browser.waitForElementByCss('#home')
text = await browser.elementByCss('p').text()
expect(text).toMatch(/Comment:.*?SSR/)
text = await browser.elementByCss('p:nth-child(2)').text()
expect(text).toMatch(/Comment:.*?comment-1/)
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, '/')
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 () => {
const browser = await webdriver(appPort, '/')
let text = await browser.elementByCss('p').text()
......@@ -81,89 +116,163 @@ const runTests = () => {
expect(text).toMatch(/a normal page/)
})
it('should return JSON when content-type is set', async () => {
const data = await fetchViaHTTP(appPort, '/', undefined, {
headers: {
'content-type': 'application/json'
if (!dev) {
it('outputs a prerender-manifest correctly', async () => {
const manifest = require(join(appDir, '.next', 'prerender-manifest.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 () => {
let html = await renderViaHTTP(
appPort,
'/blog/post-1?_nextPreviewSkeleton=1'
)
expect(html).not.toMatch(/Post:.*?SSR/)
vals = await Promise.all(
vals.map(() => renderViaHTTP(appPort, '/blog/post-10'))
)
const val = vals[0]
expect(val).toMatch(/Post:.*?post-10/)
expect(new Set(vals).size).toBe(1)
})
html = await renderViaHTTP(
appPort,
'/blog/post-1/comment-1?_nextPreviewSkeleton=1'
)
expect(html).not.toMatch(/Comment:.*?SSR/)
})
it('should handle revalidating HTML correctly', async () => {
const route = '/blog/post-1/comment-1'
const initialHtml = await renderViaHTTP(appPort, route)
expect(initialHtml).toMatch(/Post:.*?post-1/)
expect(initialHtml).toMatch(/Comment:.*?comment-1/)
it('should call getInitialProps when viewing skeleton', async () => {
let browser = await webdriver(
appPort,
'/blog/post-1?_nextPreviewSkeleton=1'
)
await waitFor(1000)
let text = await browser.elementByCss('p').text()
expect(text).toMatch(/Post:.*?SSR/)
await browser.close()
await waitFor(2 * 1000)
await renderViaHTTP(appPort, route)
browser = await webdriver(
appPort,
'/blog/post-1/comment-1?_nextPreviewSkeleton=1'
)
await waitFor(1000)
text = await browser.elementByCss('p').text()
expect(text).toMatch(/Comment:.*?SSR/)
await browser.close()
})
await waitFor(2 * 1000)
const newHtml = await renderViaHTTP(appPort, route)
expect(newHtml === initialHtml).toBe(false)
expect(newHtml).toMatch(/Post:.*?post-1/)
expect(newHtml).toMatch(/Comment:.*?comment-1/)
})
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('development mode', () => {
describe('dev mode', () => {
beforeAll(async () => {
appPort = await findPort()
app = await launchApp(appDir, appPort)
})
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()
})
describe('production mode', () => {
beforeAll(async () => {
try {
await fs.unlink(nextConfig)
} catch (_) {}
await nextBuild(appDir)
appPort = await findPort()
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))
runTests()
})
it('outputs a prerender-manifest correctly', async () => {
const manifest = require(join(appDir, '.next', 'prerender-manifest.json'))
const prerenderablePaths = Object.keys(manifest.prerenderFiles)
expect(JSON.stringify(prerenderablePaths.sort())).toBe(
JSON.stringify(
[
'/index.html',
'/another.html',
'/something.html',
'/blog/[post].html',
'/blog/[post]/[comment].html'
]
.map(pg => join('.next/serverless/pages/', pg))
.sort()
)
)
describe('export mode', () => {
beforeAll(async () => {
exportDir = join(appDir, 'out')
await nextBuild(appDir)
await nextExport(appDir, { outdir: exportDir })
app = await startStaticServer(exportDir)
appPort = app.address().port
})
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 @@
resolved "https://registry.yarnpkg.com/@types/long/-/long-4.0.0.tgz#719551d2352d301ac8b81db732acb6bdc28dbdef"
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@*":
version "2.0.1"
resolved "https://registry.yarnpkg.com/@types/mime/-/mime-2.0.1.tgz#dc488842312a7f075149312905b5e3c0b054c79d"
......@@ -9274,6 +9279,13 @@ lowercase-keys@^2.0.0:
resolved "https://registry.yarnpkg.com/lowercase-keys/-/lowercase-keys-2.0.0.tgz#2603e78b7b4b0006cbca2fbcc8a3202558ac9479"
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:
version "4.1.5"
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:
pseudomap "^1.0.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:
version "2.2.0"
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