Commit cdd54afb authored by JJ Kasper's avatar JJ Kasper Committed by Joe Haddad

Add auto static/dynamic (#7293)

* Add automatic exporting of pages with no getInitialProps

* Add support for exporting serverless to static
and serving the html files during next start

* Fix missing runtimeEnv when requiring page, re-add warning
when trying to export with serverless, and update tests

* Update flying-shuttle test

* revert un-used pagesManifest change

* remove query.amp RegExp test

* Fix windows backslashes not being replaced

* Re-enable serverless support for next start

* bump

* Fix getInitialProps check

* Fix incorrect error check

* Re-add check for reserved pages

* Fix static check

* Update to ignore /api pages and clean up some tests

* Re-add needed next.config for test and correct behavior

* Update RegExp for ignored pages for auto-static

* Add checking for custom getInitialProps in pages/_app

* Update isPageStatic logic to only use default export

* Re-add retrying to CircleCi

* Update query during dev to only have values
available during export for static pages

* Fix test

* Add warning when page without default export is
found and make sure to update pages-manifest
correctly in flying-shuttle mode

* Fix backslashes not being replaced

* Integrate auto-static with flying-shuttle
and make sure AMP is handled in flying-shuttle

* Add autoExport for opting in
parent 4718bd67
......@@ -7,6 +7,7 @@ export const BUILD_MANIFEST = 'build-manifest.json'
export const REACT_LOADABLE_MANIFEST = 'react-loadable-manifest.json'
export const CHUNK_GRAPH_MANIFEST = 'compilation-modules.json'
export const SERVER_DIRECTORY = 'server'
export const SERVERLESS_DIRECTORY = 'serverless'
export const CONFIG_FILE = 'next.config.js'
export const BUILD_ID_FILE = 'BUILD_ID'
export const BLOCKED_PAGES = [
......@@ -27,4 +28,5 @@ export const CLIENT_STATIC_FILES_RUNTIME_WEBPACK = `${CLIENT_STATIC_FILES_RUNTIM
export const IS_BUNDLED_PAGE_REGEX = /^static[/\\][^/\\]+[/\\]pages.*\.js$/
// matches static/<buildid>/pages/:page*.js
export const ROUTE_NAME_REGEX = /^static[/\\][^/\\]+[/\\]pages[/\\](.*)\.js$/
export const SERVERLESS_ROUTE_NAME_REGEX = /^pages[/\\](.*)\.js$/
export const HEAD_BUILD_ID_FILE = `${CLIENT_STATIC_FILES_PATH}/HEAD_BUILD_ID`
......@@ -27,6 +27,7 @@ const defaultConfig: {[key: string]: any} = {
(Number(process.env.CIRCLE_NODE_TOTAL) ||
(os.cpus() || { length: 1 }).length) - 1,
),
autoExport: false,
ampBindInitData: false,
exportTrailingSlash: true,
terserLoader: false,
......
import {BUILD_MANIFEST, CLIENT_STATIC_FILES_PATH, REACT_LOADABLE_MANIFEST, SERVER_DIRECTORY} from '../lib/constants';
import {BUILD_MANIFEST, CLIENT_STATIC_FILES_PATH, REACT_LOADABLE_MANIFEST, SERVER_DIRECTORY, SERVERLESS_DIRECTORY} from '../lib/constants';
import { join } from 'path';
import { requirePage } from './require';
......@@ -7,14 +7,18 @@ export function interopDefault(mod: any) {
return mod.default || mod
}
export async function loadComponents(distDir: string, buildId: string, pathname: string) {
export async function loadComponents(distDir: string, buildId: string, pathname: string, serverless: boolean) {
if (serverless) {
const Component = await requirePage(pathname, distDir, serverless)
return { Component }
}
const documentPath = join(distDir, SERVER_DIRECTORY, CLIENT_STATIC_FILES_PATH, buildId, 'pages', '_document')
const appPath = join(distDir, SERVER_DIRECTORY, CLIENT_STATIC_FILES_PATH, buildId, 'pages', '_app')
const [buildManifest, reactLoadableManifest, Component, Document, App] = await Promise.all([
require(join(distDir, BUILD_MANIFEST)),
require(join(distDir, REACT_LOADABLE_MANIFEST)),
interopDefault(requirePage(pathname, distDir)),
interopDefault(requirePage(pathname, distDir, serverless)),
interopDefault(require(documentPath)),
interopDefault(require(appPath)),
])
......
......@@ -72,14 +72,8 @@ export default class Server {
publicRuntimeConfig,
assetPrefix,
generateEtags,
target,
} = this.nextConfig
if (process.env.NODE_ENV === 'production' && target !== 'server')
throw new Error(
'Cannot start server when target is not server. https://err.sh/zeit/next.js/next-start-serverless',
)
this.buildId = this.readBuildId()
this.renderOpts = {
ampBindInitData: this.nextConfig.experimental.ampBindInitData,
......@@ -257,7 +251,7 @@ export default class Server {
* @param pathname path of request
*/
private resolveApiRequest(pathname: string) {
return getPagePath(pathname, this.distDir)
return getPagePath(pathname, this.distDir, this.nextConfig.target === 'serverless')
}
private generatePublicRoutes(): Route[] {
......@@ -353,7 +347,25 @@ export default class Server {
query: ParsedUrlQuery = {},
opts: any,
) {
const result = await loadComponents(this.distDir, this.buildId, pathname)
const serverless = this.nextConfig.target === 'serverless'
// try serving a static AMP version first
if (query.amp) {
try {
const result = await loadComponents(this.distDir, this.buildId, (pathname === '/' ? '/index' : pathname) + '.amp', serverless)
if (typeof result.Component === 'string') return result.Component
} catch (err) {
if (err.code !== 'ENOENT') throw err
}
}
const result = await loadComponents(this.distDir, this.buildId, pathname, serverless)
// handle static page
if (typeof result.Component === 'string') return result.Component
// handle serverless
if (typeof result.Component === 'object' &&
typeof result.Component.renderReqToHTML === 'function'
) {
return result.Component.renderReqToHTML(req, res)
}
return renderToHTML(req, res, pathname, query, { ...result, ...opts })
}
......
......@@ -252,6 +252,16 @@ export async function renderToHTML(
`The default export is not a React Component in page: "/_document"`,
)
}
const isStaticPage = typeof (Component as any).getInitialProps !== 'function'
const defaultAppGetInitialProps = App.getInitialProps === (App as any).origGetInitialProps
if (isStaticPage && defaultAppGetInitialProps) {
// remove query values except ones that will be set during export
query = {
amp: query.amp,
}
}
}
// @ts-ignore url will always be set
......@@ -304,7 +314,7 @@ export async function renderToHTML(
const ampMode = {
enabled: false,
hasQuery: Boolean(query.amp && /^(y|yes|true|1)/i.test(query.amp.toString())),
hasQuery: Boolean(query.amp),
}
if (ampBindInitData) {
......
import fs from 'fs'
import {join} from 'path'
import {PAGES_MANIFEST, SERVER_DIRECTORY} from '../lib/constants'
import {promisify} from 'util'
import {PAGES_MANIFEST, SERVER_DIRECTORY, SERVERLESS_DIRECTORY} from '../lib/constants'
import { normalizePagePath } from './normalize-page-path'
const readFile = promisify(fs.readFile)
export function pageNotFoundError(page: string): Error {
const err: any = new Error(`Cannot find module for page: ${page}`)
err.code = 'ENOENT'
return err
}
export function getPagePath(page: string, distDir: string): string {
const serverBuildPath = join(distDir, SERVER_DIRECTORY)
export function getPagePath(page: string, distDir: string, serverless: boolean): string {
const serverBuildPath = join(distDir, serverless ? SERVERLESS_DIRECTORY : SERVER_DIRECTORY)
const pagesManifest = require(join(serverBuildPath, PAGES_MANIFEST))
try {
......@@ -32,7 +36,10 @@ export function getPagePath(page: string, distDir: string): string {
return join(serverBuildPath, pagesManifest[page])
}
export function requirePage(page: string, distDir: string): any {
const pagePath = getPagePath(page, distDir)
export function requirePage(page: string, distDir: string, serverless: boolean): any {
const pagePath = getPagePath(page, distDir, serverless)
if (pagePath.endsWith('.html')) {
return readFile(pagePath, 'utf8')
}
return require(pagePath)
}
......@@ -9,6 +9,7 @@ import { promisify } from 'util'
import { recursiveDelete } from '../lib/recursive-delete'
import * as Log from './output/log'
import { PageInfo } from './utils';
const FILE_BUILD_ID = 'HEAD_BUILD_ID'
const FILE_UPDATED_AT = 'UPDATED_AT'
......@@ -220,6 +221,29 @@ export class FlyingShuttle {
return (this._shuttleBuildId = contents)
}
getPageInfos = async (): Promise<Map<string, PageInfo>> => {
const pageInfos: Map<string, PageInfo> = new Map()
const pagesManifest = JSON.parse(await fsReadFile(
path.join(
this.shuttleDirectory, DIR_FILES_NAME, 'serverless/pages-manifest.json'
),
'utf8'
))
Object.keys(pagesManifest).forEach(pg => {
const path = pagesManifest[pg]
const isStatic: boolean = path.endsWith('html')
let isAmp = Boolean(pagesManifest[pg + '.amp'])
if (pg === '/') isAmp = Boolean(pagesManifest['/index.amp'])
pageInfos.set(pg, {
isAmp,
size: 0,
static: isStatic,
serverBundle: path
})
})
return pageInfos
}
getUnchangedPages = async () => {
const manifestPath = path.join(this.shuttleDirectory, CHUNK_GRAPH_MANIFEST)
const manifest = require(manifestPath) as ChunkGraphManifest
......@@ -276,8 +300,36 @@ export class FlyingShuttle {
return unchangedPages
}
restorePage = async (page: string): Promise<boolean> => {
mergePagesManifest = async (): Promise<void> => {
const savedPagesManifest = path.join(
this.shuttleDirectory, DIR_FILES_NAME, 'serverless/pages-manifest.json'
)
if (!(await fsExists(savedPagesManifest))) return
const saved = JSON.parse(await fsReadFile(
savedPagesManifest,
'utf8'
))
const currentPagesManifest = path.join(
this.distDirectory, 'serverless/pages-manifest.json'
)
const current = JSON.parse(await fsReadFile(
currentPagesManifest,
'utf8'
))
await fsWriteFile(currentPagesManifest, JSON.stringify({
...saved,
...current,
}))
}
restorePage = async (
page: string,
pageInfo: PageInfo = {} as PageInfo
): Promise<boolean> => {
await this._restoreSema.acquire()
try {
const manifestPath = path.join(
this.shuttleDirectory,
......@@ -293,10 +345,9 @@ export class FlyingShuttle {
const serverless = path.join(
'serverless/pages',
`${page === '/' ? 'index' : page}.js`
`${page === '/' ? 'index' : page}.${pageInfo.static ? 'html' : 'js'}`
)
const files = [serverless, ...pageChunks[page]]
const filesExists = await Promise.all(
files
.map(f => path.join(this.shuttleDirectory, DIR_FILES_NAME, f))
......@@ -366,7 +417,7 @@ export class FlyingShuttle {
}
}
save = async () => {
save = async (staticPages: Set<string>, pageInfos: Map<string, PageInfo>) => {
Log.wait('docking flying shuttle')
await recursiveDelete(this.shuttleDirectory)
......@@ -419,10 +470,30 @@ export class FlyingShuttle {
const usedChunks = new Set()
const pages = Object.keys(storeManifest.pageChunks)
pages.forEach(page => {
storeManifest.pageChunks[page].forEach(file => usedChunks.add(file))
const info = pageInfos.get(page) || {} as PageInfo
storeManifest.pageChunks[page].forEach((file, idx) => {
if (info.isAmp) {
// AMP pages don't have client bundles
storeManifest.pageChunks[page] = []
return
}
usedChunks.add(file)
})
usedChunks.add(
path.join('serverless/pages', `${page === '/' ? 'index' : page}.js`)
path.join('serverless/pages', `${
page === '/' ? 'index' : page
}.${staticPages.has(page) ? 'html' : 'js'}`)
)
const ampPage = (page === '/' ? '/index' : page) + '.amp'
if (staticPages.has(ampPage)) {
storeManifest.pages[ampPage] = []
storeManifest.pageChunks[ampPage] = []
usedChunks.add(
path.join('serverless/pages', `${ampPage}.html`)
)
}
})
await fsWriteFile(
......@@ -442,6 +513,11 @@ export class FlyingShuttle {
})
)
await fsCopyFile(
path.join(this.distDirectory, 'serverless/pages-manifest.json'),
path.join(this.shuttleDirectory, DIR_FILES_NAME, 'serverless/pages-manifest.json')
)
Log.info(`flying shuttle payload: ${usedChunks.size + 2} files`)
Log.ready('flying shuttle docked')
......
import chalk from 'chalk'
import {
SERVER_DIRECTORY,
SERVERLESS_DIRECTORY,
PAGES_MANIFEST,
CHUNK_GRAPH_MANIFEST,
PHASE_PRODUCTION_BUILD,
} from 'next-server/constants'
import loadConfig from 'next-server/next-config'
import nanoid from 'next/dist/compiled/nanoid/index.js'
import path from 'path'
import fs from 'fs'
import { promisify } from 'util'
import formatWebpackMessages from '../client/dev-error-overlay/format-webpack-messages'
import { recursiveDelete } from '../lib/recursive-delete'
import { verifyTypeScriptSetup } from '../lib/verifyTypeScriptSetup'
......@@ -23,6 +27,8 @@ import {
getSpecifiedPages,
printTreeView,
PageInfo,
isPageStatic,
hasCustomAppGetInitialProps,
} from './utils'
import getBaseWebpackConfig from './webpack-config'
import {
......@@ -30,6 +36,15 @@ import {
getPageChunks,
} from './webpack/plugins/chunk-graph-plugin'
import { writeBuildId } from './write-build-id'
import { recursiveReadDir } from '../lib/recursive-readdir';
import mkdirpOrig from 'mkdirp'
const fsUnlink = promisify(fs.unlink)
const fsRmdir = promisify(fs.rmdir)
const fsMove = promisify(fs.rename)
const fsReadFile = promisify(fs.readFile)
const fsWriteFile = promisify(fs.writeFile)
const mkdirp = promisify(mkdirpOrig)
export default async function build(dir: string, conf = null): Promise<void> {
if (!(await isWriteable(dir))) {
......@@ -109,20 +124,36 @@ export default async function build(dir: string, conf = null): Promise<void> {
} else {
pagePaths = await collectPages(pagesDir, config.pageExtensions)
}
// needed for static exporting since we want to replace with HTML
// files even when flying shuttle doesn't rebuild the files
const allPagePaths = [...pagePaths]
const allStaticPages = new Set<string>()
let allPageInfos = new Map<string, PageInfo>()
if (flyingShuttle && (await flyingShuttle.hasShuttle())) {
allPageInfos = await flyingShuttle.getPageInfos()
const _unchangedPages = new Set(await flyingShuttle.getUnchangedPages())
for (const unchangedPage of _unchangedPages) {
const recalled = await flyingShuttle.restorePage(unchangedPage)
const info = allPageInfos.get(unchangedPage) || {} as PageInfo
if (info.static) allStaticPages.add(unchangedPage)
const recalled = await flyingShuttle.restorePage(
unchangedPage, info
)
if (recalled) {
continue
}
_unchangedPages.delete(unchangedPage)
}
const unchangedPages = await Promise.all(
const unchangedPages = (await Promise.all(
[..._unchangedPages].map(async page => {
if (
page.endsWith('.amp') &&
(allPageInfos.get(page.split('.amp')[0]) || {} as PageInfo).isAmp
) {
return ''
}
const file = await getFileForPage({
page,
pagesDirectory: pagesDir,
......@@ -138,8 +169,8 @@ export default async function build(dir: string, conf = null): Promise<void> {
`Did pageExtensions change? We can't recover from this yet.`
)
)
})
)
}))
).filter(Boolean)
const pageSet = new Set(pagePaths)
for (const unchangedPage of unchangedPages) {
......@@ -148,6 +179,7 @@ export default async function build(dir: string, conf = null): Promise<void> {
pagePaths = [...pageSet]
}
const allMappedPages = createPagesMapping(allPagePaths, config.pageExtensions)
const mappedPages = createPagesMapping(pagePaths, config.pageExtensions)
const entrypoints = createEntrypoints(
mappedPages,
......@@ -240,16 +272,69 @@ export default async function build(dir: string, conf = null): Promise<void> {
console.log(chalk.green('Compiled successfully.\n'))
}
const pageInfos = new Map<string, PageInfo>()
const distPath = path.join(dir, config.distDir)
const pageKeys = Object.keys(mappedPages)
const manifestPath = path.join(distDir, target === 'serverless'
? SERVERLESS_DIRECTORY : SERVER_DIRECTORY, PAGES_MANIFEST)
const { autoExport } = config.experimental
const staticPages = new Set<string>()
const pageInfos = new Map<string, PageInfo>()
let pagesManifest: any = {}
let customAppGetInitialProps: boolean | undefined
if (autoExport) {
pagesManifest = JSON.parse(await fsReadFile(manifestPath, 'utf8'))
}
process.env.NEXT_PHASE = PHASE_PRODUCTION_BUILD
for (const page of pageKeys) {
const chunks = getPageChunks(page)
const actualPage = page === '/' ? '/index' : page
const size = await getPageSizeInKb(actualPage, distPath, buildId)
pageInfos.set(page, { size, chunks })
const bundleRelative = path.join(
target === 'serverless'
? 'pages'
: `static/${buildId}/pages`,
actualPage + '.js'
)
const serverBundle = path.join(
distPath,
target === 'serverless' ? SERVERLESS_DIRECTORY : SERVER_DIRECTORY,
bundleRelative
)
if (autoExport) {
pagesManifest[page] = bundleRelative.replace(/\\/g, '/')
const runtimeEnvConfig = {
publicRuntimeConfig: config.publicRuntimeConfig,
serverRuntimeConfig: config.serverRuntimeConfig
}
const nonReservedPage = !page.match(/^\/(_app|_error|_document|api)/)
if (nonReservedPage && customAppGetInitialProps === undefined) {
customAppGetInitialProps = hasCustomAppGetInitialProps(
target === 'serverless'
? serverBundle
: path.join(distPath, SERVER_DIRECTORY, `/static/${buildId}/pages/_app.js`),
runtimeEnvConfig
)
if (customAppGetInitialProps) {
console.warn('Opting out of automatic exporting due to custom `getInitialProps` in `pages/_app`\n')
}
}
if (customAppGetInitialProps === false && nonReservedPage) {
const isStatic = isPageStatic(serverBundle, runtimeEnvConfig)
if (isStatic) staticPages.add(page)
}
}
pageInfos.set(page, { size, chunks, serverBundle })
}
if (Array.isArray(configs[0].plugins)) {
......@@ -265,11 +350,66 @@ export default async function build(dir: string, conf = null): Promise<void> {
})
}
printTreeView(pageKeys, pageInfos)
await writeBuildId(distDir, buildId, selectivePageBuilding)
if (autoExport && staticPages.size > 0) {
const exportApp = require('../export').default
const exportOptions = {
silent: true,
buildExport: true,
pages: Array.from(staticPages),
outdir: path.join(distDir, 'export'),
}
const exportConfig = {
...config,
exportPathMap: (defaultMap: any) => defaultMap,
experimental: {
...config.experimental,
exportTrailingSlash: false,
}
}
await exportApp(dir, exportOptions, exportConfig)
const toMove = await recursiveReadDir(exportOptions.outdir, /.*\.html$/)
let serverDir: string = ''
// remove server bundles that were exported
for (const page of staticPages) {
const { serverBundle } = pageInfos.get(page)!
if (!serverDir) serverDir = path.dirname(serverBundle)
await fsUnlink(serverBundle)
}
for (const file of toMove) {
const orig = path.join(exportOptions.outdir, file)
const dest = path.join(serverDir, file)
const relativeDest = (target === 'serverless'
? path.join('pages', file)
: path.join('static', buildId, 'pages', file)
).replace(/\\/g, '/')
let page = file.split('.html')[0].replace(/\\/g, '/')
pagesManifest[page] = relativeDest
page = page === '/index' ? '/' : page
pagesManifest[page] = relativeDest
staticPages.add(page)
await mkdirp(path.dirname(dest))
await fsMove(orig, dest)
}
// remove temporary export folder
await recursiveDelete(exportOptions.outdir)
await fsRmdir(exportOptions.outdir)
await fsWriteFile(manifestPath, JSON.stringify(pagesManifest), 'utf8')
}
staticPages.forEach(pg => allStaticPages.add(pg))
pageInfos.forEach((info: PageInfo, key: string) => {
allPageInfos.set(key, info)
})
if (flyingShuttle) {
await flyingShuttle.save()
if (autoExport) await flyingShuttle.mergePagesManifest()
await flyingShuttle.save(allStaticPages, pageInfos)
}
await writeBuildId(distDir, buildId, selectivePageBuilding)
printTreeView(Object.keys(allMappedPages), allPageInfos)
}
......@@ -14,6 +14,7 @@ import { getPageChunks } from './webpack/plugins/chunk-graph-plugin'
const fsStat = promisify(fs.stat)
const fsExists = promisify(fs.exists)
const fsReadFile = promisify(fs.readFile)
const nextEnvConfig = require('next-server/config')
export function collectPages(
directory: string,
......@@ -29,6 +30,8 @@ export interface PageInfo {
isAmp?: boolean
size: number
chunks?: ReturnType<typeof getPageChunks>
static?: boolean
serverBundle: string
}
export function printTreeView(
......@@ -221,3 +224,42 @@ export async function getPageSizeInKb(
} catch (_) {}
return -1
}
export function isPageStatic(
serverBundle: string,
runtimeEnvConfig: any,
): boolean {
try {
nextEnvConfig.setConfig(runtimeEnvConfig)
const Comp = require(serverBundle).default
if (!Comp) {
const pageStartIdx = serverBundle.indexOf('pages/') + 5
console.log(
'not exporting invalid page',
serverBundle.substr(pageStartIdx),
'(no default export)'
)
return false
}
return typeof Comp.getInitialProps !== 'function'
} catch (err) {
if (err.code === 'MODULE_NOT_FOUND') return false
throw err
}
}
export function hasCustomAppGetInitialProps(
_appBundle: string,
runtimeEnvConfig: any,
): boolean {
nextEnvConfig.setConfig(runtimeEnvConfig)
let mod = require(_appBundle)
if (_appBundle.endsWith('_app.js')) {
mod = mod.default
} else {
// since we don't output _app in serverless mode get it from a page
mod = mod._app
}
return mod.getInitialProps !== mod.origGetInitialProps
}
......@@ -501,7 +501,7 @@ export default async function getBaseWebpackConfig(
target === 'serverless' &&
(isServer || selectivePageBuilding) &&
new ServerlessPlugin(buildId, { isServer }),
target !== 'serverless' && isServer && new PagesManifestPlugin(),
isServer && new PagesManifestPlugin(target === 'serverless'),
target !== 'serverless' &&
isServer &&
new NextJsSSRModuleCachePlugin({ outputPath }),
......
......@@ -41,7 +41,9 @@ const nextServerlessLoader: loader.Loader = function () {
import Error from '${absoluteErrorPath}';
import App from '${absoluteAppPath}';
import Component from '${absolutePagePath}';
async function renderReqToHTML(req, res) {
export default Component
export const _app = App
export async function renderReqToHTML(req, res, fromExport) {
const options = {
App,
Document,
......@@ -53,15 +55,18 @@ const nextServerlessLoader: loader.Loader = function () {
ampBindInitData: ${ampBindInitData === true || ampBindInitData === 'true'}
}
const parsedUrl = parse(req.url, true)
const renderOpts = Object.assign(
{
Component,
dataOnly: req.headers && (req.headers.accept || '').indexOf('application/amp.bind+json') !== -1,
},
options,
)
try {
${page === '/_error' ? `res.statusCode = 404` : ''}
const result = await renderToHTML(req, res, "${page}", parsedUrl.query, Object.assign(
{
Component,
dataOnly: req.headers && (req.headers.accept || '').indexOf('application/amp.bind+json') !== -1,
},
options,
))
const result = await renderToHTML(req, res, "${page}", parsedUrl.query, renderOpts)
if (fromExport) return { html: result, renderOpts }
return result
} catch (err) {
if (err.code === 'ENOENT') {
......
import {Compiler, Plugin} from 'webpack'
import { RawSource } from 'webpack-sources'
import { PAGES_MANIFEST, ROUTE_NAME_REGEX } from 'next-server/constants'
import { PAGES_MANIFEST, ROUTE_NAME_REGEX, SERVERLESS_ROUTE_NAME_REGEX } from 'next-server/constants'
// This plugin creates a pages-manifest.json from page entrypoints.
// This is used for mapping paths like `/` to `.next/server/static/<buildid>/pages/index.js` when doing SSR
// It's also used by next export to provide defaultPathMap
export default class PagesManifestPlugin implements Plugin {
serverless: boolean
constructor (serverless: boolean) {
this.serverless = serverless
}
apply (compiler: Compiler): void {
compiler.hooks.emit.tap('NextJsPagesManifest', (compilation) => {
const { chunks } = compilation
const pages: {[page: string]: string} = {}
for (const chunk of chunks) {
const result = ROUTE_NAME_REGEX.exec(chunk.name)
const result = (this.serverless
? SERVERLESS_ROUTE_NAME_REGEX : ROUTE_NAME_REGEX).exec(chunk.name)
if (!result) {
continue
}
......
......@@ -26,7 +26,7 @@ export default async function (dir, options, configuration) {
const distDir = join(dir, nextConfig.distDir)
const subFolders = nextConfig.experimental.exportTrailingSlash
if (nextConfig.target !== 'server') throw new Error('Cannot export when target is not server. https://err.sh/zeit/next.js/next-export-serverless')
if (!options.buildExport && nextConfig.target !== 'server') throw new Error('Cannot export when target is not server. https://err.sh/zeit/next.js/next-export-serverless')
log(`> using build directory: ${distDir}`)
......@@ -35,9 +35,9 @@ export default async function (dir, options, configuration) {
}
const buildId = readFileSync(join(distDir, BUILD_ID_FILE), 'utf8')
const pagesManifest = require(join(distDir, SERVER_DIRECTORY, PAGES_MANIFEST))
const pagesManifest = !options.pages && require(join(distDir, SERVER_DIRECTORY, PAGES_MANIFEST))
const pages = Object.keys(pagesManifest)
const pages = options.pages || Object.keys(pagesManifest)
const defaultPathMap = {}
for (const page of pages) {
......@@ -141,6 +141,7 @@ export default async function (dir, options, configuration) {
}
)
}
const workers = new Set()
await Promise.all(
chunks.map(
......@@ -149,6 +150,7 @@ export default async function (dir, options, configuration) {
const worker = fork(require.resolve('./worker'), [], {
env: process.env
})
workers.add(worker)
worker.send({
distDir,
buildId,
......@@ -158,7 +160,8 @@ export default async function (dir, options, configuration) {
renderOpts,
serverRuntimeConfig,
concurrency,
subFolders
subFolders,
serverless: nextConfig.target === 'serverless'
})
worker.on('message', ({ type, payload }) => {
if (type === 'progress' && progress) {
......@@ -176,6 +179,8 @@ export default async function (dir, options, configuration) {
)
)
workers.forEach(worker => worker.kill())
if (Object.keys(ampValidations).length) {
console.log(formatAmpMessages(ampValidations))
}
......
......@@ -27,7 +27,8 @@ process.on(
renderOpts,
serverRuntimeConfig,
concurrency,
subFolders
subFolders,
serverless
}) => {
const sema = new Sema(concurrency, { capacity: exportPaths.length })
try {
......@@ -40,8 +41,23 @@ process.on(
delete query.ampPath
delete query.amphtml
const req = { url: path }
const res = {}
const headerMocks = {
headers: {},
getHeader: () => ({}),
setHeader: () => {},
hasHeader: () => false,
removeHeader: () => {},
getHeaderNames: () => ([])
}
const req = {
url: path,
...headerMocks
}
const res = {
...headerMocks
}
envConfig.setConfig({
serverRuntimeConfig,
publicRuntimeConfig: renderOpts.runtimeConfig
......@@ -65,9 +81,25 @@ process.on(
const htmlFilepath = join(outDir, htmlFilename)
await mkdirp(baseDir)
const components = await loadComponents(distDir, buildId, page)
const curRenderOpts = { ...components, ...renderOpts, ampPath }
const html = await renderToHTML(req, res, page, query, curRenderOpts)
let html
let curRenderOpts = {}
let renderMethod = renderToHTML
if (serverless) {
renderMethod = require(join(distDir, 'serverless/pages', (page === '/' ? 'index' : page) + '.js')).renderReqToHTML
const result = await renderMethod(req, res, true)
curRenderOpts = result.renderOpts
html = result.html
} else {
const components = await loadComponents(distDir, buildId, page, serverless)
if (typeof components.Component === 'string') {
html = components.Component
} else {
curRenderOpts = { ...components, ...renderOpts, ampPath }
html = await renderMethod(req, res, page, query, curRenderOpts)
}
}
const validateAmp = async (html, page) => {
const validator = await AmpHtmlValidator.getInstance()
......@@ -96,7 +128,10 @@ process.on(
(curRenderOpts.amphtml && !query.amp) || curRenderOpts.hasAmp
) {
// we need to render a clean AMP version
const ampHtmlFilename = `${ampPath}${sep}index.html`
let ampHtmlFilename = `${ampPath}${sep}index.html`
if (!subFolders) {
ampHtmlFilename = `${ampPath}.html`
}
const ampBaseDir = join(outDir, dirname(ampHtmlFilename))
const ampHtmlFilepath = join(outDir, ampHtmlFilename)
......@@ -104,7 +139,13 @@ process.on(
await accessP(ampHtmlFilepath)
} catch (_) {
// make sure it doesn't exist from manual mapping
const ampHtml = await renderToHTML(req, res, page, { ...query, amp: 1 }, curRenderOpts)
let ampHtml
if (serverless) {
req.url += (req.url.includes('?') ? '&' : '?') + 'amp=1'
ampHtml = (await renderMethod(req, res, true)).html
} else {
ampHtml = await renderMethod(req, res, page, { ...query, amp: 1 }, curRenderOpts)
}
await validateAmp(ampHtml, page + '?amp=1')
await mkdirp(ampBaseDir)
......
......@@ -9,15 +9,17 @@ export type AppContext = AppContextType<Router>
export type AppProps<P = {}> = AppPropsType<Router, P>
async function appGetInitialProps({ Component, ctx }: AppContext): Promise<AppInitialProps> {
const pageProps = await loadGetInitialProps(Component, ctx)
return { pageProps }
}
export default class App<P = {}, CP = P> extends React.Component<P & AppProps<CP>> {
static childContextTypes = {
router: PropTypes.object,
}
static async getInitialProps({ Component, ctx }: AppContext): Promise<AppInitialProps> {
const pageProps = await loadGetInitialProps(Component, ctx)
return { pageProps }
}
static origGetInitialProps = appGetInitialProps
static getInitialProps = appGetInitialProps
getChildContext() {
return {
......
module.exports = {
// exportPathMap
experimental: {
amp: true
}
}
......@@ -4,6 +4,6 @@ module.exports = {
maxInactiveAge: 1000 * 60 * 60
},
experimental: {
amp: true
autoExport: true
}
}
......@@ -66,7 +66,7 @@ describe('AMP Usage', () => {
() => accessSync(join(appDir, '.next/static', buildId, 'pages', pg + '.js'))
).toThrow()
expect(
() => accessSync(join(appDir, '.next/server/static', buildId, 'pages', pg + '.js'))
() => accessSync(join(appDir, '.next/server/static', buildId, 'pages', pg + '.html'))
).not.toThrow()
}
})
......@@ -163,7 +163,7 @@ describe('AMP Usage', () => {
$('link[rel=amphtml]')
.first()
.attr('href')
).toBe('/use-amp-hook?amp=1')
).toBe('/use-amp-hook.amp')
})
it('should render link rel amphtml with existing query', async () => {
......@@ -220,7 +220,7 @@ describe('AMP Usage', () => {
$('link[rel=amphtml]')
.first()
.attr('href')
).toBe('/only-amp?amp=1')
).toBe('/only-amp.amp')
})
it('should remove conflicting amp tags', async () => {
......
......@@ -2,5 +2,8 @@ module.exports = {
onDemandEntries: {
// Make sure entries are not getting disposed.
maxInactiveAge: 1000 * 60 * 60
},
experimental: {
autoExport: true
}
}
export default ({ url }) => {
const Page = ({ url }) => {
return <div>
<p id='pathname'>{url.pathname}</p>
<p id='query'>{Object.keys(url.query).length}</p>
<p id='aspath'>{url.asPath}</p>
</div>
}
Page.getInitialProps = () => ({})
export default Page
module.exports = {
experimental: {
amp: true
autoExport: true
}
}
module.exports = {
experimental: {
exportTrailingSlash: false
exportTrailingSlash: false,
autoExport: true
}
}
......@@ -10,7 +10,8 @@ module.exports = (phase) => {
bar: 'bar'
},
experimental: {
// exportTrailingSlash: false
// exportTrailingSlash: false,
autoExport: true
},
exportPathMap: function () {
return {
......
......@@ -53,7 +53,7 @@ describe('Static Export', () => {
await nextBuild(appDir)
await nextExport(appDir, { outdir })
nextConfig.replace(`// exportTrailingSlash: false`, `exportTrailingSlash: false`)
nextConfig.replace(`// exportTrailingSlash: false,`, `exportTrailingSlash: false,`)
await nextBuild(appDir)
await nextExport(appDir, { outdir: outNoTrailSlash })
nextConfig.restore()
......
......@@ -2,5 +2,8 @@ module.exports = {
onDemandEntries: {
// Make sure entries are not getting disposed.
maxInactiveAge: 1000 * 60 * 60
},
experimental: {
autoExport: true
}
}
import React from 'react'
export default class ErrorInRenderPage extends React.Component {
static async getInitialProps () {
return {}
}
render () {
throw new Error('An Expected error occured')
}
......
......@@ -7,6 +7,10 @@ if (!process.browser) {
global.__THIS_SHOULD_ONLY_BE_DEFINED_IN_SERVER_CONTEXT__ = true
}
export default () => (
const ProcessEnv = () => (
<div id='node-env'>{process.env.NODE_ENV}</div>
)
ProcessEnv.getInitialProps = () => ({})
export default ProcessEnv
......@@ -8,4 +8,6 @@ const Page = ({ router: { query } }) => (
</>
)
Page.getInitialProps = () => ({})
export default withRouter(Page)
/* eslint-env jest */
/* global jasmine, browserName */
import webdriver from 'next-webdriver'
import { readFileSync } from 'fs'
import { readFileSync, existsSync } from 'fs'
import { join } from 'path'
import {
nextServer,
......@@ -18,6 +18,7 @@ import security from './security'
import { BUILD_MANIFEST, REACT_LOADABLE_MANIFEST, PAGES_MANIFEST } from 'next-server/constants'
import cheerio from 'cheerio'
const appDir = join(__dirname, '../')
let serverDir
let appPort
let server
let app
......@@ -37,6 +38,9 @@ describe('Production Usage', () => {
server = await startApp(app)
context.appPort = appPort = server.address().port
const buildId = readFileSync(join(appDir, '.next/BUILD_ID'), 'utf8')
serverDir = join(appDir, '.next/server/static/', buildId, 'pages')
})
afterAll(() => stopApp(server))
......@@ -62,13 +66,6 @@ describe('Production Usage', () => {
expect(header).toBe('Next.js')
})
it('should set correct cache-control header when no GIP', async () => {
const url = `http://localhost:${appPort}/`
const header = (await fetch(url)).headers.get('Cache-Control')
expect(header).toBe('s-maxage=86400, stale-while-revalidate')
})
it('should render 404 for routes that do not exist', async () => {
const url = `http://localhost:${appPort}/abcdefghijklmno`
const res = await fetch(url)
......@@ -415,6 +412,22 @@ describe('Production Usage', () => {
expect(html).toMatch(/Bad Request/)
})
it('should replace static pages with HTML files', async () => {
const staticFiles = ['about', 'another', 'counter', 'dynamic', 'prefetch']
for (const file of staticFiles) {
expect(existsSync(join(serverDir, file + '.html'))).toBe(true)
expect(existsSync(join(serverDir, file + '.js'))).toBe(false)
}
})
it('should not replace non-static pages with HTML files', async () => {
const nonStaticFiles = ['api', 'external-and-back', 'finish-response']
for (const file of nonStaticFiles) {
expect(existsSync(join(serverDir, file + '.js'))).toBe(true)
expect(existsSync(join(serverDir, file + '.html'))).toBe(false)
}
})
dynamicImportTests(context, (p, q) => renderViaHTTP(context.appPort, p, q))
processEnv(context)
......
......@@ -5,7 +5,6 @@ module.exports = {
maxInactiveAge: 1000 * 60 * 60
},
experimental: {
amp: true
},
lambdas: true
autoExport: true
}
}
import React from 'react'
import App, { Container } from 'next/app'
class MyApp extends App {
static async getInitialProps ({ Component, ctx }) {
let pageProps = {}
if (Component.getInitialProps) {
pageProps = await Component.getInitialProps(ctx)
}
return { pageProps }
}
render () {
const { Component, pageProps } = this.props
return (
<Container>
<Component {...pageProps} />
</Container>
)
}
}
export default MyApp
const start = require('./server')
start(3000).then(() => console.log('http://localhost:3000'))
const express = require('express')
const http = require('http')
const path = require('path')
module.exports = function start (port = 0) {
return new Promise((resolve, reject) => {
const app = express()
const nextStaticDir = path.join(__dirname, '.next', 'static')
app.use('/_next/static', express.static(nextStaticDir))
app.get('/', (req, res) => {
require('./.next/serverless/pages/index.js').render(req, res)
})
app.get('/abc', (req, res) => {
require('./.next/serverless/pages/abc.js').render(req, res)
})
app.get('/fetch', (req, res) => {
require('./.next/serverless/pages/fetch.js').render(req, res)
})
app.get('/dynamic', (req, res) => {
require('./.next/serverless/pages/dynamic.js').render(req, res)
})
app.get('/dynamic-two', (req, res) => {
require('./.next/serverless/pages/dynamic-two.js').render(req, res)
})
app.get('/amp', (req, res) => {
require('./.next/serverless/pages/some-amp.js').render(req, res)
})
app.get('/404', (req, res) => {
require('./.next/serverless/pages/_error.js').render(req, res)
})
const server = new http.Server(app)
server.listen(port, (err) => {
if (err) {
return reject(err)
}
resolve(server)
})
})
}
/* eslint-env jest */
/* global jasmine, test */
/* global jasmine */
import webdriver from 'next-webdriver'
import { join } from 'path'
import { existsSync } from 'fs'
import {
killApp,
findPort,
nextBuild,
stopApp,
nextStart,
renderViaHTTP
} from 'next-test-utils'
import startServer from '../server'
import fetch from 'node-fetch'
const appDir = join(__dirname, '../')
const serverlessDir = join(appDir, '.next/serverless/pages')
let appPort
let server
let app
jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60 * 5
describe('Serverless', () => {
beforeAll(async () => {
await nextBuild(appDir)
server = await startServer()
appPort = server.address().port
appPort = await findPort()
app = await nextStart(appDir, appPort)
})
afterAll(() => stopApp(server))
afterAll(() => killApp(app))
it('should render the page', async () => {
const html = await renderViaHTTP(appPort, '/')
expect(html).toMatch(/Hello World/)
})
it('should set cache-control header', async () => {
const header = (
await fetch(`http://localhost:${appPort}/`)
).headers.get('Cache-Control')
expect(header).toBe('s-maxage=86400, stale-while-revalidate')
})
it('should render the page with dynamic import', async () => {
const html = await renderViaHTTP(appPort, '/dynamic')
expect(html).toMatch(/Hello!/)
......@@ -53,20 +47,20 @@ describe('Serverless', () => {
})
it('should render an AMP page', async () => {
const html = await renderViaHTTP(appPort, '/amp')
const html = await renderViaHTTP(appPort, '/some-amp')
expect(html).toMatch(/Hi Im an AMP page/)
expect(html).toMatch(/ampproject\.org/)
})
it('should have correct amphtml rel link', async () => {
const html = await renderViaHTTP(appPort, '/amp')
const html = await renderViaHTTP(appPort, '/some-amp')
expect(html).toMatch(/Hi Im an AMP page/)
expect(html).toMatch(/rel="amphtml" href="\/amp\?amp=1"/)
expect(html).toMatch(/rel="amphtml" href="\/some-amp\?amp=1"/)
})
it('should have correct canonical link', async () => {
const html = await renderViaHTTP(appPort, '/amp?amp=1')
expect(html).toMatch(/rel="canonical" href="\/amp"/)
const html = await renderViaHTTP(appPort, '/some-amp?amp=1')
expect(html).toMatch(/rel="canonical" href="\/some-amp"/)
})
it('should render correctly when importing isomorphic-unfetch', async () => {
......@@ -92,11 +86,26 @@ describe('Serverless', () => {
})
it('should not output _app.js and _document.js to serverless build', () => {
const serverlessDir = join(appDir, '.next/serverless/pages')
expect(existsSync(join(serverlessDir, '_app.js'))).toBeFalsy()
expect(existsSync(join(serverlessDir, '_document.js'))).toBeFalsy()
})
it('should replace static pages with HTML files', async () => {
const staticFiles = ['abc', 'dynamic', 'dynamic-two', 'some-amp']
for (const file of staticFiles) {
expect(existsSync(join(serverlessDir, file + '.html'))).toBe(true)
expect(existsSync(join(serverlessDir, file + '.js'))).toBe(false)
}
})
it('should not replace non-static pages with HTML files', async () => {
const nonStaticFiles = ['fetch', '_error']
for (const file of nonStaticFiles) {
expect(existsSync(join(serverlessDir, file + '.js'))).toBe(true)
expect(existsSync(join(serverlessDir, file + '.html'))).toBe(false)
}
})
describe('With basic usage', () => {
it('should allow etag header support', async () => {
const url = `http://localhost:${appPort}/`
......
......@@ -2,7 +2,7 @@ import React from 'react'
const Idk = React.createContext(null)
export default () => {
const Page = () => {
return (
<div>
<Idk.Provider value='hello world'>
......@@ -15,3 +15,5 @@ export default () => {
</div>
)
}
export default Page
......@@ -4,7 +4,8 @@ import { join } from 'path'
import {
killApp,
findPort,
runNextCommand,
nextStart,
nextBuild,
renderViaHTTP
} from 'next-test-utils'
......@@ -16,28 +17,14 @@ let app
describe('Production Usage', () => {
beforeAll(async () => {
await runNextCommand(['build', appDir])
await nextBuild(appDir)
appPort = await findPort()
app = await nextStart(appDir, appPort)
})
afterAll(() => killApp(app))
it('should render a page with context', async () => {
appPort = await findPort()
await new Promise((resolve, reject) => {
runNextCommand(['start', appDir, '-p', appPort], {
instance: (child) => {
app = child
child.stdout.on('data', chunk => {
if (chunk.toString().match(/ready on/i)) resolve()
})
child.stderr.on('data',
chunk => reject(new Error('got error ' + chunk.toString()))
)
}
}).catch(err => reject(err))
})
const html = await renderViaHTTP(appPort, '/')
expect(html).toMatch(/Value: .*?hello world/)
await killApp(app)
})
})
......@@ -160,6 +160,10 @@ export function nextExport (dir, { outdir }) {
return runNextCommand(['export', dir, '--outdir', outdir])
}
export function nextStart (dir, port) {
return runNextCommandDev(['start', '-p', port, dir])
}
// Kill a launched app
export async function killApp (instance) {
await new Promise((resolve, reject) => {
......
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