Unverified Commit 847e892d authored by Joe Haddad's avatar Joe Haddad Committed by GitHub

Remove Flying Shuttle experiment (#8433)

* Remove Flying Shuttle experiment

* Remove more old tests

* Remove minify loader
parent 0e01435f
...@@ -6,7 +6,6 @@ export const PAGES_MANIFEST = 'pages-manifest.json' ...@@ -6,7 +6,6 @@ export const PAGES_MANIFEST = 'pages-manifest.json'
export const BUILD_MANIFEST = 'build-manifest.json' export const BUILD_MANIFEST = 'build-manifest.json'
export const PRERENDER_MANIFEST = 'prerender-manifest.json' export const PRERENDER_MANIFEST = 'prerender-manifest.json'
export const REACT_LOADABLE_MANIFEST = 'react-loadable-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 SERVER_DIRECTORY = 'server'
export const SERVERLESS_DIRECTORY = 'serverless' export const SERVERLESS_DIRECTORY = 'serverless'
export const CONFIG_FILE = 'next.config.js' export const CONFIG_FILE = 'next.config.js'
...@@ -27,4 +26,3 @@ export const IS_BUNDLED_PAGE_REGEX = /^static[/\\][^/\\]+[/\\]pages.*\.js$/ ...@@ -27,4 +26,3 @@ export const IS_BUNDLED_PAGE_REGEX = /^static[/\\][^/\\]+[/\\]pages.*\.js$/
// matches static/<buildid>/pages/:page*.js // matches static/<buildid>/pages/:page*.js
export const ROUTE_NAME_REGEX = /^static[/\\][^/\\]+[/\\]pages[/\\](.*)\.js$/ export const ROUTE_NAME_REGEX = /^static[/\\][^/\\]+[/\\]pages[/\\](.*)\.js$/
export const SERVERLESS_ROUTE_NAME_REGEX = /^pages[/\\](.*)\.js$/ export const SERVERLESS_ROUTE_NAME_REGEX = /^pages[/\\](.*)\.js$/
export const HEAD_BUILD_ID_FILE = `${CLIENT_STATIC_FILES_PATH}/HEAD_BUILD_ID`
...@@ -58,7 +58,6 @@ export type NEXT_DATA = { ...@@ -58,7 +58,6 @@ export type NEXT_DATA = {
page: string page: string
query: ParsedUrlQuery query: ParsedUrlQuery
buildId: string buildId: string
dynamicBuildId: boolean
assetPrefix?: string assetPrefix?: string
runtimeConfig?: { [key: string]: any } runtimeConfig?: { [key: string]: any }
nextExport?: boolean nextExport?: boolean
......
...@@ -36,9 +36,7 @@ const defaultConfig: { [key: string]: any } = { ...@@ -36,9 +36,7 @@ const defaultConfig: { [key: string]: any } = {
(os.cpus() || { length: 1 }).length) - 1 (os.cpus() || { length: 1 }).length) - 1
), ),
ampBindInitData: false, ampBindInitData: false,
terserLoader: false,
profiling: false, profiling: false,
flyingShuttle: false,
documentMiddleware: false, documentMiddleware: false,
granularChunks: false, granularChunks: false,
publicDirectory: false, publicDirectory: false,
......
...@@ -127,7 +127,6 @@ type RenderOpts = { ...@@ -127,7 +127,6 @@ type RenderOpts = {
staticMarkup: boolean staticMarkup: boolean
buildId: string buildId: string
canonicalBase: string canonicalBase: string
dynamicBuildId?: boolean
runtimeConfig?: { [key: string]: any } runtimeConfig?: { [key: string]: any }
dangerousAsPath: string dangerousAsPath: string
assetPrefix?: string assetPrefix?: string
...@@ -161,7 +160,6 @@ function renderDocument( ...@@ -161,7 +160,6 @@ function renderDocument(
query, query,
buildId, buildId,
canonicalBase, canonicalBase,
dynamicBuildId = false,
assetPrefix, assetPrefix,
runtimeConfig, runtimeConfig,
nextExport, nextExport,
...@@ -206,7 +204,6 @@ function renderDocument( ...@@ -206,7 +204,6 @@ function renderDocument(
page: pathname, // The rendered page page: pathname, // The rendered page
query, // querystring parsed / passed by the user query, // querystring parsed / passed by the user
buildId, // buildId is used to facilitate caching of page bundles, we send it to the client so that pageloader knows where to load bundles buildId, // buildId is used to facilitate caching of page bundles, we send it to the client so that pageloader knows where to load bundles
dynamicBuildId, // Specifies if the buildId should by dynamically fetched
assetPrefix: assetPrefix === '' ? undefined : assetPrefix, // send assetPrefix to the client side when configured, otherwise don't sent in the resulting HTML assetPrefix: assetPrefix === '' ? undefined : assetPrefix, // send assetPrefix to the client side when configured, otherwise don't sent in the resulting HTML
runtimeConfig, // runtimeConfig if provided, otherwise don't sent in the resulting HTML runtimeConfig, // runtimeConfig if provided, otherwise don't sent in the resulting HTML
nextExport, // If this is a page exported by `next export` nextExport, // If this is a page exported by `next export`
......
...@@ -49,7 +49,6 @@ export function createEntrypoints( ...@@ -49,7 +49,6 @@ export function createEntrypoints(
pages: PagesMapping, pages: PagesMapping,
target: 'server' | 'serverless' | 'experimental-serverless-trace', target: 'server' | 'serverless' | 'experimental-serverless-trace',
buildId: string, buildId: string,
dynamicBuildId: boolean,
config: any config: any
): Entrypoints { ): Entrypoints {
const client: WebpackEntrypoints = {} const client: WebpackEntrypoints = {}
...@@ -64,7 +63,6 @@ export function createEntrypoints( ...@@ -64,7 +63,6 @@ export function createEntrypoints(
generateEtags: config.generateEtags, generateEtags: config.generateEtags,
ampBindInitData: config.experimental.ampBindInitData, ampBindInitData: config.experimental.ampBindInitData,
canonicalBase: config.canonicalBase, canonicalBase: config.canonicalBase,
dynamicBuildId,
} }
Object.keys(pages).forEach(page => { Object.keys(pages).forEach(page => {
......
This diff is collapsed.
...@@ -3,7 +3,6 @@ import chalk from 'chalk' ...@@ -3,7 +3,6 @@ import chalk from 'chalk'
import fs from 'fs' import fs from 'fs'
import mkdirpOrig from 'mkdirp' import mkdirpOrig from 'mkdirp'
import { import {
CHUNK_GRAPH_MANIFEST,
PAGES_MANIFEST, PAGES_MANIFEST,
PHASE_PRODUCTION_BUILD, PHASE_PRODUCTION_BUILD,
PRERENDER_MANIFEST, PRERENDER_MANIFEST,
...@@ -23,24 +22,17 @@ import { recursiveDelete } from '../lib/recursive-delete' ...@@ -23,24 +22,17 @@ import { recursiveDelete } from '../lib/recursive-delete'
import { verifyTypeScriptSetup } from '../lib/verifyTypeScriptSetup' import { verifyTypeScriptSetup } from '../lib/verifyTypeScriptSetup'
import { CompilerResult, runCompiler } from './compiler' import { CompilerResult, runCompiler } from './compiler'
import { createEntrypoints, createPagesMapping } from './entries' import { createEntrypoints, createPagesMapping } from './entries'
import { FlyingShuttle } from './flying-shuttle'
import { generateBuildId } from './generate-build-id' import { generateBuildId } from './generate-build-id'
import { isWriteable } from './is-writeable' import { isWriteable } from './is-writeable'
import { import {
collectPages, collectPages,
getCacheIdentifier,
getFileForPage,
getPageSizeInKb, getPageSizeInKb,
getSpecifiedPages,
hasCustomAppGetInitialProps, hasCustomAppGetInitialProps,
PageInfo, PageInfo,
printTreeView, printTreeView,
} from './utils' } from './utils'
import getBaseWebpackConfig from './webpack-config' import getBaseWebpackConfig from './webpack-config'
import { import { getPageChunks } from './webpack/plugins/chunk-graph-plugin'
exportManifest,
getPageChunks,
} from './webpack/plugins/chunk-graph-plugin'
import { writeBuildId } from './write-build-id' import { writeBuildId } from './write-build-id'
const fsUnlink = promisify(fs.unlink) const fsUnlink = promisify(fs.unlink)
...@@ -82,120 +74,22 @@ export default async function build(dir: string, conf = null): Promise<void> { ...@@ -82,120 +74,22 @@ export default async function build(dir: string, conf = null): Promise<void> {
tracer.profiler.startProfiling() tracer.profiler.startProfiling()
} }
const isFlyingShuttle = Boolean(
config.experimental.flyingShuttle &&
!process.env.__NEXT_BUILDER_EXPERIMENTAL_PAGE
)
const selectivePageBuilding = Boolean(
isFlyingShuttle || process.env.__NEXT_BUILDER_EXPERIMENTAL_PAGE
)
const isLikeServerless = isTargetLikeServerless(target) const isLikeServerless = isTargetLikeServerless(target)
if (selectivePageBuilding && target !== 'serverless') { const pagePaths: string[] = await collectPages(
throw new Error( pagesDir,
`Cannot use ${ config.pageExtensions
isFlyingShuttle ? 'flying shuttle' : '`now dev`' )
} without the \`serverless\` target.`
)
}
const selectivePageBuildingCacheIdentifier = selectivePageBuilding
? await getCacheIdentifier({
pagesDirectory: pagesDir,
env: config.env || {},
})
: 'noop'
let flyingShuttle: FlyingShuttle | undefined
if (isFlyingShuttle) {
console.log(chalk.magenta('Building with Flying Shuttle enabled ...'))
console.log()
await recursiveDelete(distDir, /^(?!cache(?:[\/\\]|$)).*$/)
await recursiveDelete(path.join(distDir, 'cache', 'next-minifier'))
await recursiveDelete(path.join(distDir, 'cache', 'next-babel-loader'))
flyingShuttle = new FlyingShuttle({
buildId,
pagesDirectory: pagesDir,
distDirectory: distDir,
cacheIdentifier: selectivePageBuildingCacheIdentifier,
})
}
let pagePaths: string[]
if (process.env.__NEXT_BUILDER_EXPERIMENTAL_PAGE) {
pagePaths = await getSpecifiedPages(
dir,
process.env.__NEXT_BUILDER_EXPERIMENTAL_PAGE!,
config.pageExtensions
)
} else {
pagePaths = await collectPages(pagesDir, config.pageExtensions)
}
// needed for static exporting since we want to replace with HTML // needed for static exporting since we want to replace with HTML
// files even when flying shuttle doesn't rebuild the files // files
const allPagePaths = [...pagePaths] const allPagePaths = [...pagePaths]
const allStaticPages = new Set<string>() const allStaticPages = new Set<string>()
let allPageInfos = new Map<string, PageInfo>() 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 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(
[..._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,
pageExtensions: config.pageExtensions,
})
if (file) {
return file
}
return Promise.reject(
new Error(
`Failed to locate page file: ${page}. ` +
`Did pageExtensions change? We can't recover from this yet.`
)
)
})
)).filter(Boolean)
const pageSet = new Set(pagePaths)
for (const unchangedPage of unchangedPages) {
pageSet.delete(unchangedPage)
}
pagePaths = [...pageSet]
}
const allMappedPages = createPagesMapping(allPagePaths, config.pageExtensions) const allMappedPages = createPagesMapping(allPagePaths, config.pageExtensions)
const mappedPages = createPagesMapping(pagePaths, config.pageExtensions) const mappedPages = createPagesMapping(pagePaths, config.pageExtensions)
const entrypoints = createEntrypoints( const entrypoints = createEntrypoints(mappedPages, target, buildId, config)
mappedPages,
target,
buildId,
/* dynamicBuildId */ selectivePageBuilding,
config
)
const configs = await Promise.all([ const configs = await Promise.all([
getBaseWebpackConfig(dir, { getBaseWebpackConfig(dir, {
tracer, tracer,
...@@ -204,7 +98,6 @@ export default async function build(dir: string, conf = null): Promise<void> { ...@@ -204,7 +98,6 @@ export default async function build(dir: string, conf = null): Promise<void> {
config, config,
target, target,
entrypoints: entrypoints.client, entrypoints: entrypoints.client,
selectivePageBuilding,
}), }),
getBaseWebpackConfig(dir, { getBaseWebpackConfig(dir, {
tracer, tracer,
...@@ -213,7 +106,6 @@ export default async function build(dir: string, conf = null): Promise<void> { ...@@ -213,7 +106,6 @@ export default async function build(dir: string, conf = null): Promise<void> {
config, config,
target, target,
entrypoints: entrypoints.server, entrypoints: entrypoints.server,
selectivePageBuilding,
}), }),
]) ])
...@@ -256,16 +148,6 @@ export default async function build(dir: string, conf = null): Promise<void> { ...@@ -256,16 +148,6 @@ export default async function build(dir: string, conf = null): Promise<void> {
result = formatWebpackMessages(result) result = formatWebpackMessages(result)
if (isFlyingShuttle) {
console.log()
exportManifest({
dir: dir,
fileName: path.join(distDir, CHUNK_GRAPH_MANIFEST),
selectivePageBuildingCacheIdentifier,
})
}
if (result.errors.length > 0) { if (result.errors.length > 0) {
// Only keep the first error. Others are often indicative // Only keep the first error. Others are often indicative
// of the same problem, but confuse the reader with noise. // of the same problem, but confuse the reader with noise.
...@@ -447,7 +329,7 @@ export default async function build(dir: string, conf = null): Promise<void> { ...@@ -447,7 +329,7 @@ export default async function build(dir: string, conf = null): Promise<void> {
}) })
} }
await writeBuildId(distDir, buildId, selectivePageBuilding) await writeBuildId(distDir, buildId)
if (staticPages.size > 0 || sprPages.size > 0) { if (staticPages.size > 0 || sprPages.size > 0) {
const combinedPages = [...staticPages, ...sprPages] const combinedPages = [...staticPages, ...sprPages]
...@@ -531,11 +413,6 @@ export default async function build(dir: string, conf = null): Promise<void> { ...@@ -531,11 +413,6 @@ export default async function build(dir: string, conf = null): Promise<void> {
allPageInfos.set(key, info) allPageInfos.set(key, info)
}) })
if (flyingShuttle) {
await flyingShuttle.mergeManifests()
await flyingShuttle.save(allStaticPages, pageInfos)
}
printTreeView(Object.keys(allMappedPages), allPageInfos, isLikeServerless) printTreeView(Object.keys(allMappedPages), allPageInfos, isLikeServerless)
if (tracer) { if (tracer) {
......
import chalk from 'chalk' import chalk from 'chalk'
import crypto from 'crypto'
import findUp from 'find-up'
import fs from 'fs' import fs from 'fs'
import textTable from 'next/dist/compiled/text-table' import textTable from 'next/dist/compiled/text-table'
import path from 'path' import path from 'path'
...@@ -10,11 +8,9 @@ import { promisify } from 'util' ...@@ -10,11 +8,9 @@ import { promisify } from 'util'
import { isValidElementType } from 'react-is' import { isValidElementType } from 'react-is'
import prettyBytes from '../lib/pretty-bytes' import prettyBytes from '../lib/pretty-bytes'
import { recursiveReadDir } from '../lib/recursive-readdir' import { recursiveReadDir } from '../lib/recursive-readdir'
import { fileExists } from '../lib/file-exists'
import { getPageChunks } from './webpack/plugins/chunk-graph-plugin' import { getPageChunks } from './webpack/plugins/chunk-graph-plugin'
const fsStat = promisify(fs.stat) const fsStat = promisify(fs.stat)
const fsReadFile = promisify(fs.readFile)
export function collectPages( export function collectPages(
directory: string, directory: string,
...@@ -133,119 +129,6 @@ export function printTreeView( ...@@ -133,119 +129,6 @@ export function printTreeView(
console.log() console.log()
} }
function flatten<T>(arr: T[][]): T[] {
return arr.reduce((acc, val) => acc.concat(val), [] as T[])
}
function getPossibleFiles(pageExtensions: string[], pages: string[]) {
const res = pages.map(page =>
pageExtensions
.map(e => `${page}.${e}`)
.concat(pageExtensions.map(e => `${path.join(page, 'index')}.${e}`))
.concat(page)
)
return flatten<string>(res)
}
export async function getFileForPage({
page,
pagesDirectory,
pageExtensions,
}: {
page: string
pagesDirectory: string
pageExtensions: string[]
}) {
const theFile = getPossibleFiles(pageExtensions, [
path.join(pagesDirectory, page),
]).find(f => fs.existsSync(f) && fs.lstatSync(f).isFile())
if (theFile) {
return path.sep + path.relative(pagesDirectory, theFile)
}
return theFile
}
export async function getSpecifiedPages(
dir: string,
pagesString: string,
pageExtensions: string[]
) {
const pagesDir = path.join(dir, 'pages')
const reservedPages = ['/_app', '/_document', '/_error']
const explodedPages = [
...new Set([...pagesString.split(','), ...reservedPages]),
].map(p => {
let resolvedPage: string | undefined
if (path.isAbsolute(p)) {
resolvedPage = getPossibleFiles(pageExtensions, [
path.join(pagesDir, p),
p,
]).find(f => fs.existsSync(f) && fs.lstatSync(f).isFile())
} else {
resolvedPage = getPossibleFiles(pageExtensions, [
path.join(pagesDir, p),
path.join(dir, p),
]).find(f => fs.existsSync(f) && fs.lstatSync(f).isFile())
}
return { original: p, resolved: resolvedPage || null }
})
const missingPage = explodedPages.find(
({ original, resolved }) => !resolved && !reservedPages.includes(original)
)
if (missingPage) {
throw new Error(`Unable to identify page: ${missingPage.original}`)
}
const resolvedPagePaths = explodedPages
.filter(page => page.resolved)
.map(page => '/' + path.relative(pagesDir, page.resolved!))
return resolvedPagePaths.sort()
}
export async function getCacheIdentifier({
pagesDirectory,
env = {},
}: {
pagesDirectory: string
env?: any
}) {
let selectivePageBuildingCacheIdentifier = ''
const envObject = env
? Object.keys(env)
.sort()
// eslint-disable-next-line
.reduce((a, c) => ((a[c] = env[c]), a), {} as any)
: {}
selectivePageBuildingCacheIdentifier += JSON.stringify(envObject)
const pkgPath = await findUp('package.json', { cwd: pagesDirectory })
if (pkgPath) {
const yarnLock = path.join(path.dirname(pkgPath), 'yarn.lock')
const packageLock = path.join(path.dirname(pkgPath), 'package-lock.json')
if (await fileExists(yarnLock)) {
selectivePageBuildingCacheIdentifier += await fsReadFile(yarnLock, 'utf8')
} else if (await fileExists(packageLock)) {
selectivePageBuildingCacheIdentifier += await fsReadFile(
packageLock,
'utf8'
)
} else {
selectivePageBuildingCacheIdentifier += JSON.stringify(require(pkgPath))
}
}
return crypto
.createHash('sha1')
.update(selectivePageBuildingCacheIdentifier)
.digest('hex')
}
export async function getPageSizeInKb( export async function getPageSizeInKb(
page: string, page: string,
distPath: string, distPath: string,
......
...@@ -19,12 +19,10 @@ import { ...@@ -19,12 +19,10 @@ import {
} from '../lib/constants' } from '../lib/constants'
import { fileExists } from '../lib/file-exists' import { fileExists } from '../lib/file-exists'
import { WebpackEntrypoints } from './entries' import { WebpackEntrypoints } from './entries'
import { AllModulesIdentifiedPlugin } from './webpack/plugins/all-modules-identified-plugin'
import BuildManifestPlugin from './webpack/plugins/build-manifest-plugin' import BuildManifestPlugin from './webpack/plugins/build-manifest-plugin'
import { ChunkGraphPlugin } from './webpack/plugins/chunk-graph-plugin' import { ChunkGraphPlugin } from './webpack/plugins/chunk-graph-plugin'
import ChunkNamesPlugin from './webpack/plugins/chunk-names-plugin' import ChunkNamesPlugin from './webpack/plugins/chunk-names-plugin'
import { importAutoDllPlugin } from './webpack/plugins/dll-import' import { importAutoDllPlugin } from './webpack/plugins/dll-import'
import { HashedChunkIdsPlugin } from './webpack/plugins/hashed-chunk-ids-plugin'
import { DropClientPage } from './webpack/plugins/next-drop-client-page-plugin' import { DropClientPage } from './webpack/plugins/next-drop-client-page-plugin'
import NextEsmPlugin from './webpack/plugins/next-esm-plugin' import NextEsmPlugin from './webpack/plugins/next-esm-plugin'
import NextJsSsrImportPlugin from './webpack/plugins/nextjs-ssr-import' import NextJsSsrImportPlugin from './webpack/plugins/nextjs-ssr-import'
...@@ -32,7 +30,6 @@ import NextJsSSRModuleCachePlugin from './webpack/plugins/nextjs-ssr-module-cach ...@@ -32,7 +30,6 @@ import NextJsSSRModuleCachePlugin from './webpack/plugins/nextjs-ssr-module-cach
import PagesManifestPlugin from './webpack/plugins/pages-manifest-plugin' import PagesManifestPlugin from './webpack/plugins/pages-manifest-plugin'
import { ReactLoadablePlugin } from './webpack/plugins/react-loadable-plugin' import { ReactLoadablePlugin } from './webpack/plugins/react-loadable-plugin'
import { ServerlessPlugin } from './webpack/plugins/serverless-plugin' import { ServerlessPlugin } from './webpack/plugins/serverless-plugin'
import { SharedRuntimePlugin } from './webpack/plugins/shared-runtime-plugin'
import { TerserPlugin } from './webpack/plugins/terser-webpack-plugin/src/index' import { TerserPlugin } from './webpack/plugins/terser-webpack-plugin/src/index'
// @ts-ignore: JS file // @ts-ignore: JS file
import { ProfilingPlugin } from './webpack/plugins/profiling-plugin' import { ProfilingPlugin } from './webpack/plugins/profiling-plugin'
...@@ -54,7 +51,6 @@ export default async function getBaseWebpackConfig( ...@@ -54,7 +51,6 @@ export default async function getBaseWebpackConfig(
config, config,
target = 'server', target = 'server',
entrypoints, entrypoints,
selectivePageBuilding = false,
tracer, tracer,
}: { }: {
tracer?: any tracer?: any
...@@ -64,7 +60,6 @@ export default async function getBaseWebpackConfig( ...@@ -64,7 +60,6 @@ export default async function getBaseWebpackConfig(
config: any config: any
target?: string target?: string
entrypoints: WebpackEntrypoints entrypoints: WebpackEntrypoints
selectivePageBuilding?: boolean
} }
): Promise<webpack.Configuration> { ): Promise<webpack.Configuration> {
const distDir = path.join(dir, config.distDir) const distDir = path.join(dir, config.distDir)
...@@ -76,7 +71,7 @@ export default async function getBaseWebpackConfig( ...@@ -76,7 +71,7 @@ export default async function getBaseWebpackConfig(
hasModern: !!config.experimental.modern, hasModern: !!config.experimental.modern,
distDir, distDir,
cwd: dir, cwd: dir,
cache: !selectivePageBuilding, cache: true,
}, },
}, },
// Backwards compat // Backwards compat
...@@ -165,7 +160,7 @@ export default async function getBaseWebpackConfig( ...@@ -165,7 +160,7 @@ export default async function getBaseWebpackConfig(
const terserPluginConfig = { const terserPluginConfig = {
parallel: true, parallel: true,
sourceMap: false, sourceMap: false,
cache: !selectivePageBuilding, cache: true,
cpus: config.experimental.cpus, cpus: config.experimental.cpus,
distDir: distDir, distDir: distDir,
} }
...@@ -202,17 +197,6 @@ export default async function getBaseWebpackConfig( ...@@ -202,17 +197,6 @@ export default async function getBaseWebpackConfig(
vendors: false, vendors: false,
}, },
}, },
selective: {
cacheGroups: {
default: false,
vendors: false,
react: {
name: 'commons',
chunks: 'all',
test: /[\\/]node_modules[\\/](react|react-dom|scheduler)[\\/]/,
},
},
},
prod: { prod: {
chunks: 'all', chunks: 'all',
cacheGroups: { cacheGroups: {
...@@ -300,8 +284,6 @@ export default async function getBaseWebpackConfig( ...@@ -300,8 +284,6 @@ export default async function getBaseWebpackConfig(
let splitChunksConfig: webpack.Options.SplitChunksOptions let splitChunksConfig: webpack.Options.SplitChunksOptions
if (dev) { if (dev) {
splitChunksConfig = splitChunksConfigs.dev splitChunksConfig = splitChunksConfigs.dev
} else if (selectivePageBuilding) {
splitChunksConfig = splitChunksConfigs.selective
} else { } else {
splitChunksConfig = config.experimental.granularChunks splitChunksConfig = config.experimental.granularChunks
? splitChunksConfigs.prodGranular ? splitChunksConfigs.prodGranular
...@@ -394,51 +376,22 @@ export default async function getBaseWebpackConfig( ...@@ -394,51 +376,22 @@ export default async function getBaseWebpackConfig(
return callback() return callback()
}, },
], ],
optimization: Object.assign( optimization: {
{ checkWasmTypes: false,
checkWasmTypes: false, nodeEnv: false,
nodeEnv: false, splitChunks: isServer ? false : splitChunksConfig,
}, runtimeChunk: isServer
isServer ? undefined
? { : { name: CLIENT_STATIC_FILES_RUNTIME_WEBPACK },
splitChunks: false, minimize: !(dev || isServer),
minimize: false, minimizer: [
} new TerserPlugin({
: { ...terserPluginConfig,
runtimeChunk: selectivePageBuilding terserOptions,
? false }),
: { ],
name: CLIENT_STATIC_FILES_RUNTIME_WEBPACK, },
}, recordsPath: path.join(outputPath, 'records.json'),
splitChunks: splitChunksConfig,
minimize: !dev,
minimizer: !dev
? [
new TerserPlugin({
...terserPluginConfig,
terserOptions: {
...terserOptions,
// Disable compress when using terser loader
...(selectivePageBuilding ||
config.experimental.terserLoader
? { compress: false }
: undefined),
},
}),
]
: undefined,
},
selectivePageBuilding
? {
providedExports: false,
usedExports: false,
concatenateModules: false,
}
: undefined
),
recordsPath: selectivePageBuilding
? undefined
: path.join(outputPath, 'records.json'),
context: dir, context: dir,
// Kept as function to be backwards compatible // Kept as function to be backwards compatible
entry: async () => { entry: async () => {
...@@ -485,20 +438,6 @@ export default async function getBaseWebpackConfig( ...@@ -485,20 +438,6 @@ export default async function getBaseWebpackConfig(
module: { module: {
strictExportPresence: true, strictExportPresence: true,
rules: [ rules: [
(selectivePageBuilding || config.experimental.terserLoader) &&
!isServer && {
test: /\.(js|mjs|jsx)$/,
exclude: /\.min\.(js|mjs|jsx)$/,
use: {
loader: 'next-minify-loader',
options: {
terserOptions: {
...terserOptions,
mangle: false,
},
},
},
},
config.experimental.ampBindInitData && config.experimental.ampBindInitData &&
!isServer && { !isServer && {
test: /\.(tsx|ts|js|mjs|jsx)$/, test: /\.(tsx|ts|js|mjs|jsx)$/,
...@@ -549,9 +488,6 @@ export default async function getBaseWebpackConfig( ...@@ -549,9 +488,6 @@ export default async function getBaseWebpackConfig(
'process.env.NODE_ENV': JSON.stringify(webpackMode), 'process.env.NODE_ENV': JSON.stringify(webpackMode),
'process.crossOrigin': JSON.stringify(crossOrigin), 'process.crossOrigin': JSON.stringify(crossOrigin),
'process.browser': JSON.stringify(!isServer), 'process.browser': JSON.stringify(!isServer),
'process.env.__NEXT_EXPERIMENTAL_SELECTIVEPAGEBUILDING': JSON.stringify(
selectivePageBuilding
),
// This is used in client/dev-error-overlay/hot-dev-client.js to replace the dist directory // This is used in client/dev-error-overlay/hot-dev-client.js to replace the dist directory
...(dev && !isServer ...(dev && !isServer
? { ? {
...@@ -563,7 +499,7 @@ export default async function getBaseWebpackConfig( ...@@ -563,7 +499,7 @@ export default async function getBaseWebpackConfig(
), ),
'process.env.__NEXT_MODERN_BUILD': config.experimental.modern && !dev, 'process.env.__NEXT_MODERN_BUILD': config.experimental.modern && !dev,
'process.env.__NEXT_GRANULAR_CHUNKS': 'process.env.__NEXT_GRANULAR_CHUNKS':
config.experimental.granularChunks && !selectivePageBuilding && !dev, config.experimental.granularChunks && !dev,
...(isServer ...(isServer
? { ? {
// Fix bad-actors in the npm ecosystem (e.g. `node-formidable`) // Fix bad-actors in the npm ecosystem (e.g. `node-formidable`)
...@@ -623,14 +559,6 @@ export default async function getBaseWebpackConfig( ...@@ -623,14 +559,6 @@ export default async function getBaseWebpackConfig(
})() })()
: []), : []),
!dev && new webpack.HashedModuleIdsPlugin(), !dev && new webpack.HashedModuleIdsPlugin(),
// This must come after HashedModuleIdsPlugin (it sets any modules that
// were missed by HashedModuleIdsPlugin)
!dev && selectivePageBuilding && new AllModulesIdentifiedPlugin(dir),
// This sets chunk ids to be hashed versions of their names to reduce
// bundle churn
!dev && selectivePageBuilding && new HashedChunkIdsPlugin(buildId),
// On the client we want to share the same runtime cache
!isServer && selectivePageBuilding && new SharedRuntimePlugin(),
!dev && !dev &&
new webpack.IgnorePlugin({ new webpack.IgnorePlugin({
checkResource: (resource: string) => { checkResource: (resource: string) => {
...@@ -646,7 +574,6 @@ export default async function getBaseWebpackConfig( ...@@ -646,7 +574,6 @@ export default async function getBaseWebpackConfig(
isLikeServerless && isLikeServerless &&
new ServerlessPlugin(buildId, { new ServerlessPlugin(buildId, {
isServer, isServer,
isFlyingShuttle: selectivePageBuilding,
isTrace: isServerlessTrace, isTrace: isServerlessTrace,
}), }),
isServer && new PagesManifestPlugin(isLikeServerless), isServer && new PagesManifestPlugin(isLikeServerless),
......
import loaderUtils from 'loader-utils'
import { loader } from 'webpack'
import minify from '../plugins/terser-webpack-plugin/src/minify'
const nextMiniferLoader: loader.Loader = function(source) {
this.cacheable()
const options = loaderUtils.getOptions(this) || {}
const { error, code } = minify({
file: 'noop',
input: source as string,
terserOptions: { ...options.terserOptions, sourceMap: false },
})
if (error) {
this.callback(new Error(`Error from Terser: ${error.message}`))
return
}
this.callback(undefined, code)
return
}
export default nextMiniferLoader
...@@ -15,7 +15,6 @@ export type ServerlessLoaderQuery = { ...@@ -15,7 +15,6 @@ export type ServerlessLoaderQuery = {
assetPrefix: string assetPrefix: string
ampBindInitData: boolean | string ampBindInitData: boolean | string
generateEtags: string generateEtags: string
dynamicBuildId?: string | boolean
canonicalBase: string canonicalBase: string
} }
...@@ -31,7 +30,6 @@ const nextServerlessLoader: loader.Loader = function() { ...@@ -31,7 +30,6 @@ const nextServerlessLoader: loader.Loader = function() {
absoluteDocumentPath, absoluteDocumentPath,
absoluteErrorPath, absoluteErrorPath,
generateEtags, generateEtags,
dynamicBuildId,
}: ServerlessLoaderQuery = }: ServerlessLoaderQuery =
typeof this.query === 'string' ? parse(this.query.substr(1)) : this.query typeof this.query === 'string' ? parse(this.query.substr(1)) : this.query
const buildManifest = join(distDir, BUILD_MANIFEST).replace(/\\/g, '/') const buildManifest = join(distDir, BUILD_MANIFEST).replace(/\\/g, '/')
...@@ -91,7 +89,6 @@ const nextServerlessLoader: loader.Loader = function() { ...@@ -91,7 +89,6 @@ const nextServerlessLoader: loader.Loader = function() {
reactLoadableManifest, reactLoadableManifest,
canonicalBase: "${canonicalBase}", canonicalBase: "${canonicalBase}",
buildId: "__NEXT_REPLACE__BUILD_ID__", buildId: "__NEXT_REPLACE__BUILD_ID__",
dynamicBuildId: ${dynamicBuildId === true || dynamicBuildId === 'true'},
assetPrefix: "${assetPrefix}", assetPrefix: "${assetPrefix}",
ampBindInitData: ${ampBindInitData === true || ampBindInitData: ${ampBindInitData === true ||
ampBindInitData === 'true'} ampBindInitData === 'true'}
......
import { Compiler, Plugin } from 'webpack'
import { createHash } from 'crypto'
import path from 'path'
/**
* From escape-string-regexp: https://github.com/sindresorhus/escape-string-regexp
* brought here to reduce the bundle size
* MIT License
* Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
*/
const matchOperatorsRe = /[|\\{}()[\]^$+*?.-]/g
const escapeRegex = (str: string) => {
if (typeof str !== 'string') {
throw new TypeError('Expected a string')
}
return str.replace(matchOperatorsRe, '\\$&')
}
function getRawModuleIdentifier(m: any, dir: string) {
// webpack impl:
// new RawModule(
// "/* (ignored) */",
// `ignored ${context} ${request}`,
// `${request} (ignored)`
// )
let request = m.readableIdentifierStr.replace(/ \(ignored\)$/, '')
let context = m.identifierStr
.match(new RegExp(`^ignored (.*) ${escapeRegex(request)}$`))
.pop()
if (path.isAbsolute(request)) {
request = path.relative(dir, request)
}
if (path.isAbsolute(context)) {
context = path.relative(dir, context)
}
return `${context}::${request}`
}
function getMultiModuleIdentifier(m: any) {
const mods = m.dependencies.map((d: any) => d.module)
if (mods.some((d: any) => !Boolean(d))) {
throw new Error('Cannot handle a MultiModule with moduleless dependencies')
}
const ids = mods.map((m: any) => m.id)
if (ids.some((i: any) => !Boolean(i))) {
throw new Error(
'Cannot handle a MultiModule dependency without a module id'
)
}
return ids.sort().join('::')
}
export class AllModulesIdentifiedPlugin implements Plugin {
private dir: string
constructor(dir: string) {
this.dir = dir
}
apply(compiler: Compiler) {
compiler.hooks.compilation.tap(
'AllModulesIdentifiedPlugin',
compilation => {
compilation.hooks.beforeModuleIds.tap(
'AllModulesIdentifiedPlugin',
modules => {
;(modules as any[]).forEach(m => {
if (m.id != null) {
return
}
let identifier: string
if (m.constructor && m.constructor.name === 'RawModule') {
identifier = getRawModuleIdentifier(m, this.dir)
} else if (
m.constructor &&
m.constructor.name === 'MultiModule'
) {
identifier = getMultiModuleIdentifier(m)
} else {
throw new Error(
`Do not know how to handle module: ${
m.name
}, ${m.identifier && m.identifier()}, ${
m.type
}, ${m.constructor && m.constructor.name}`
)
}
// This hashing algorithm is consistent with how the rest of
// webpack does it (n.b. HashedModuleIdsPlugin)
m.id = createHash('md4')
.update(identifier)
.digest('hex')
.substr(0, 4)
})
}
)
}
)
}
}
import { createHash } from 'crypto'
import fs from 'fs'
import { CLIENT_STATIC_FILES_RUNTIME_MAIN } from 'next-server/constants' import { CLIENT_STATIC_FILES_RUNTIME_MAIN } from 'next-server/constants'
import { EOL } from 'os'
import path from 'path' import path from 'path'
import { parse } from 'querystring' import { parse } from 'querystring'
import { Compiler, Plugin } from 'webpack' import { Compiler, Plugin } from 'webpack'
...@@ -80,45 +77,6 @@ export function getPageChunks( ...@@ -80,45 +77,6 @@ export function getPageChunks(
} }
} }
export function exportManifest({
dir,
fileName,
selectivePageBuildingCacheIdentifier,
}: {
dir: string
fileName: string
selectivePageBuildingCacheIdentifier: string
}) {
const finalManifest = {
...manifest,
hashes: {} as { [pageName: string]: string },
}
const allFiles = new Set<string>(manifest.sharedFiles)
for (const page of Object.keys(finalManifest.pages)) {
finalManifest.pages[page].forEach(f => allFiles.add(f))
}
finalManifest.hashes = [...allFiles].sort().reduce(
(acc, cur) =>
Object.assign(
acc,
fs.existsSync(path.join(dir, cur))
? {
[cur]: createHash('sha1')
.update(selectivePageBuildingCacheIdentifier)
.update(fs.readFileSync(path.join(dir, cur)))
.digest('hex'),
}
: undefined
),
{}
)
const json = JSON.stringify(finalManifest, null, 2) + EOL
fs.writeFileSync(fileName, json)
}
function getFiles(dir: string, modules: any[]): string[] { function getFiles(dir: string, modules: any[]): string[] {
if (!(modules && modules.length)) { if (!(modules && modules.length)) {
return [] return []
......
import { Compiler, Plugin } from 'webpack'
import { createHash } from 'crypto'
export class HashedChunkIdsPlugin implements Plugin {
buildId: string
constructor(buildId: string) {
this.buildId = buildId
}
apply(compiler: Compiler) {
compiler.hooks.compilation.tap('HashedChunkIdsPlugin', compilation => {
compilation.hooks.beforeChunkIds.tap('HashedChunkIdsPlugin', chunks => {
for (const chunk of chunks) {
if (chunk.id === null && chunk.name) {
const id = chunk.name.replace(this.buildId, '')
chunk.id = createHash('md4')
.update(id)
.digest('hex')
.substr(0, 4)
continue
}
const ids = [...chunk.modulesIterable]
.map(m => m.id)
.sort() as string[]
const h = createHash('md4')
ids.forEach(id => h.update(id))
chunk.id = h.digest('hex').substr(0, 4)
}
})
})
}
}
...@@ -55,38 +55,18 @@ export class ServerlessPlugin { ...@@ -55,38 +55,18 @@ export class ServerlessPlugin {
private buildId: string private buildId: string
private isServer: boolean private isServer: boolean
private isTrace: boolean private isTrace: boolean
private isFlyingShuttle: boolean
constructor( constructor(
buildId: string, buildId: string,
{ { isServer, isTrace }: { isServer: boolean; isTrace: boolean }
isServer,
isTrace,
isFlyingShuttle,
}: { isServer: boolean; isTrace: boolean; isFlyingShuttle: boolean }
) { ) {
this.buildId = buildId this.buildId = buildId
this.isServer = isServer this.isServer = isServer
this.isTrace = isTrace this.isTrace = isTrace
this.isFlyingShuttle = isFlyingShuttle
} }
apply(compiler: Compiler) { apply(compiler: Compiler) {
if (!this.isServer) { if (!this.isServer) {
if (this.isFlyingShuttle) {
compiler.hooks.emit.tap('ServerlessPlugin', compilation => {
const assetNames = Object.keys(compilation.assets).filter(f =>
f.includes(this.buildId)
)
for (const name of assetNames) {
compilation.assets[
name
.replace(new RegExp(`${this.buildId}[\\/\\\\]`), 'client/')
.replace(/[.]js$/, `.${this.buildId}.js`)
] = compilation.assets[name]
}
})
}
return return
} }
......
import { Compiler, Plugin } from 'webpack'
export class SharedRuntimePlugin implements Plugin {
apply(compiler: Compiler) {
const installedModules = [
'// The module cache',
'var installedModules = {};',
].join('\n')
const nextInstalledModules = [
'// The Next.js shared module cache',
'var installedModules = window.__next_installed_modules__ || (window.__next_installed_modules__ = {});',
].join('\n')
compiler.hooks.compilation.tap('SharedRuntimePlugin', compilation => {
;(compilation.mainTemplate as any).hooks.localVars.intercept({
register: (tapInfo: any) => {
if (!(tapInfo.name === 'MainTemplate' && tapInfo.type === 'sync')) {
return tapInfo
}
const { fn } = tapInfo
return {
...tapInfo,
fn: function() {
const ret = fn.apply(this, arguments)
return typeof ret === 'string'
? ret.replace(installedModules, nextInstalledModules)
: ret
},
}
},
})
})
}
}
import fs from 'fs' import fs from 'fs'
import { promisify } from 'util' import { promisify } from 'util'
import { join } from 'path' import { join } from 'path'
import { BUILD_ID_FILE, HEAD_BUILD_ID_FILE } from 'next-server/constants' import { BUILD_ID_FILE } from 'next-server/constants'
const writeFile = promisify(fs.writeFile) const writeFile = promisify(fs.writeFile)
export async function writeBuildId( export async function writeBuildId(
distDir: string, distDir: string,
buildId: string, buildId: string
headBuildId: boolean
): Promise<void> { ): Promise<void> {
const buildIdPath = join(distDir, BUILD_ID_FILE) const buildIdPath = join(distDir, BUILD_ID_FILE)
await writeFile(buildIdPath, buildId, 'utf8') await writeFile(buildIdPath, buildId, 'utf8')
if (headBuildId) {
const headBuildIdPath = join(distDir, HEAD_BUILD_ID_FILE)
await writeFile(headBuildIdPath, buildId, 'utf8')
}
} }
...@@ -36,7 +36,6 @@ const { ...@@ -36,7 +36,6 @@ const {
page, page,
query, query,
buildId, buildId,
dynamicBuildId,
assetPrefix, assetPrefix,
runtimeConfig, runtimeConfig,
dynamicIds dynamicIds
...@@ -162,12 +161,6 @@ export default async ({ webpackHMR: passedWebpackHMR } = {}) => { ...@@ -162,12 +161,6 @@ export default async ({ webpackHMR: passedWebpackHMR } = {}) => {
await window.__NEXT_PRELOADREADY(dynamicIds) await window.__NEXT_PRELOADREADY(dynamicIds)
} }
if (process.env.__NEXT_EXPERIMENTAL_SELECTIVEPAGEBUILDING) {
if (dynamicBuildId === true) {
pageLoader.onDynamicBuildId()
}
}
router = createRouter(page, query, asPath, { router = createRouter(page, query, asPath, {
initialProps: props, initialProps: props,
pageLoader, pageLoader,
......
...@@ -28,7 +28,6 @@ export default class PageLoader { ...@@ -28,7 +28,6 @@ export default class PageLoader {
this.pageCache = {} this.pageCache = {}
this.pageRegisterEvents = mitt() this.pageRegisterEvents = mitt()
this.loadingRoutes = {} this.loadingRoutes = {}
this.promisedBuildId = Promise.resolve()
if (process.env.__NEXT_GRANULAR_CHUNKS) { if (process.env.__NEXT_GRANULAR_CHUNKS) {
this.promisedBuildManifest = new Promise(resolve => { this.promisedBuildManifest = new Promise(resolve => {
if (window.__BUILD_MANIFEST) { if (window.__BUILD_MANIFEST) {
...@@ -40,39 +39,6 @@ export default class PageLoader { ...@@ -40,39 +39,6 @@ export default class PageLoader {
} }
}) })
} }
if (process.env.__NEXT_EXPERIMENTAL_SELECTIVEPAGEBUILDING) {
this.promisedBuildId = Promise.resolve()
this.onDynamicBuildId = () => {
this.promisedBuildId = new Promise(resolve => {
const unfetch = require('unfetch')
unfetch(`${this.assetPrefix}/_next/static/HEAD_BUILD_ID`)
.then(res => {
if (res.ok) {
return res
}
const err = new Error('Failed to fetch HEAD buildId')
err.res = res
throw err
})
.then(res => res.text())
.then(buildId => {
this.buildId = buildId.trim()
})
.catch(() => {
// When this fails it's not a _huge_ deal, preload wont work and page
// navigation will 404, triggering a SSR refresh
console.warn(
'Failed to load BUILD_ID from server. ' +
'The following client-side page transition will likely 404 and cause a SSR.\n' +
'http://err.sh/zeit/next.js/head-build-id'
)
})
.then(resolve, resolve)
})
}
}
} }
// Returns a promise for the dependencies for a particular route // Returns a promise for the dependencies for a particular route
...@@ -144,10 +110,6 @@ export default class PageLoader { ...@@ -144,10 +110,6 @@ export default class PageLoader {
} }
async loadRoute (route) { async loadRoute (route) {
if (process.env.__NEXT_EXPERIMENTAL_SELECTIVEPAGEBUILDING) {
await this.promisedBuildId
}
route = this.normalizeRoute(route) route = this.normalizeRoute(route)
let scriptRoute = route === '/' ? '/index.js' : `${route}.js` let scriptRoute = route === '/' ? '/index.js' : `${route}.js`
...@@ -211,10 +173,6 @@ export default class PageLoader { ...@@ -211,10 +173,6 @@ export default class PageLoader {
} }
async prefetch (route, isDependency) { async prefetch (route, isDependency) {
if (process.env.__NEXT_EXPERIMENTAL_SELECTIVEPAGEBUILDING) {
await this.promisedBuildId
}
route = this.normalizeRoute(route) route = this.normalizeRoute(route)
let scriptRoute = `${route === '/' ? '/index' : route}.js` let scriptRoute = `${route === '/' ? '/index' : route}.js`
......
...@@ -235,7 +235,7 @@ export class Head extends Component< ...@@ -235,7 +235,7 @@ export class Head extends Component<
dangerousAsPath, dangerousAsPath,
} = this.context._documentProps } = this.context._documentProps
const { _devOnlyInvalidateCacheQueryString } = this.context const { _devOnlyInvalidateCacheQueryString } = this.context
const { page, buildId, dynamicBuildId } = __NEXT_DATA__ const { page, buildId } = __NEXT_DATA__
let { head } = this.context._documentProps let { head } = this.context._documentProps
let children = this.props.children let children = this.props.children
...@@ -402,12 +402,7 @@ export class Head extends Component< ...@@ -402,12 +402,7 @@ export class Head extends Component<
href={ href={
assetPrefix + assetPrefix +
getOptionalModernScriptVariant( getOptionalModernScriptVariant(
dynamicBuildId `/_next/static/${buildId}/pages${getPageFile(page)}`
? `/_next/static/client/pages${getPageFile(
page,
buildId
)}`
: `/_next/static/${buildId}/pages${getPageFile(page)}`
) + ) +
_devOnlyInvalidateCacheQueryString _devOnlyInvalidateCacheQueryString
} }
...@@ -421,9 +416,7 @@ export class Head extends Component< ...@@ -421,9 +416,7 @@ export class Head extends Component<
href={ href={
assetPrefix + assetPrefix +
getOptionalModernScriptVariant( getOptionalModernScriptVariant(
dynamicBuildId `/_next/static/${buildId}/pages/_app.js`
? `/_next/static/client/pages/_app.${buildId}.js`
: `/_next/static/${buildId}/pages/_app.js`
) + ) +
_devOnlyInvalidateCacheQueryString _devOnlyInvalidateCacheQueryString
} }
...@@ -602,7 +595,7 @@ export class NextScript extends Component<OriginProps> { ...@@ -602,7 +595,7 @@ export class NextScript extends Component<OriginProps> {
) )
} }
const { page, buildId, dynamicBuildId } = __NEXT_DATA__ const { page, buildId } = __NEXT_DATA__
if (process.env.NODE_ENV !== 'production') { if (process.env.NODE_ENV !== 'production') {
if (this.props.crossOrigin) if (this.props.crossOrigin)
...@@ -618,9 +611,7 @@ export class NextScript extends Component<OriginProps> { ...@@ -618,9 +611,7 @@ export class NextScript extends Component<OriginProps> {
key={page} key={page}
src={ src={
assetPrefix + assetPrefix +
(dynamicBuildId `/_next/static/${buildId}/pages${getPageFile(page)}` +
? `/_next/static/client/pages${getPageFile(page, buildId)}`
: `/_next/static/${buildId}/pages${getPageFile(page)}`) +
_devOnlyInvalidateCacheQueryString _devOnlyInvalidateCacheQueryString
} }
nonce={this.props.nonce} nonce={this.props.nonce}
...@@ -635,9 +626,7 @@ export class NextScript extends Component<OriginProps> { ...@@ -635,9 +626,7 @@ export class NextScript extends Component<OriginProps> {
src={ src={
assetPrefix + assetPrefix +
getOptionalModernScriptVariant( getOptionalModernScriptVariant(
dynamicBuildId `/_next/static/${buildId}/pages${getPageFile(page)}`
? `/_next/static/client/pages${getPageFile(page, buildId)}`
: `/_next/static/${buildId}/pages${getPageFile(page)}`
) + ) +
_devOnlyInvalidateCacheQueryString _devOnlyInvalidateCacheQueryString
} }
...@@ -654,9 +643,7 @@ export class NextScript extends Component<OriginProps> { ...@@ -654,9 +643,7 @@ export class NextScript extends Component<OriginProps> {
data-next-page="/_app" data-next-page="/_app"
src={ src={
assetPrefix + assetPrefix +
(dynamicBuildId `/_next/static/${buildId}/pages/_app.js` +
? `/_next/static/client/pages/_app.${buildId}.js`
: `/_next/static/${buildId}/pages/_app.js`) +
_devOnlyInvalidateCacheQueryString _devOnlyInvalidateCacheQueryString
} }
key="_app" key="_app"
...@@ -670,9 +657,7 @@ export class NextScript extends Component<OriginProps> { ...@@ -670,9 +657,7 @@ export class NextScript extends Component<OriginProps> {
data-next-page="/_app" data-next-page="/_app"
src={ src={
assetPrefix + assetPrefix +
(dynamicBuildId `/_next/static/${buildId}/pages/_app.module.js` +
? `/_next/static/client/pages/_app.${buildId}.module.js`
: `/_next/static/${buildId}/pages/_app.module.js`) +
_devOnlyInvalidateCacheQueryString _devOnlyInvalidateCacheQueryString
} }
key="_app-modern" key="_app-modern"
......
...@@ -218,7 +218,6 @@ export default class HotReloader { ...@@ -218,7 +218,6 @@ export default class HotReloader {
pages, pages,
'server', 'server',
this.buildId, this.buildId,
false,
this.config this.config
) )
......
...@@ -802,6 +802,7 @@ declare module 'webpack' { ...@@ -802,6 +802,7 @@ declare module 'webpack' {
portableRecords?: boolean portableRecords?: boolean
/** Tells webpack which algorithm to use when choosing chunk ids. */ /** Tells webpack which algorithm to use when choosing chunk ids. */
chunkIds?: false | 'natural' | 'named' | 'size' | 'total-size' chunkIds?: false | 'natural' | 'named' | 'size' | 'total-size'
checkWasmTypes?: boolean
} }
} }
......
module.exports = {
target: 'serverless',
experimental: {
flyingShuttle: true
}
}
export default () => <p>hello abc</p>
export default () => <p>hello another</p>
/* eslint-env jest */
/* global jasmine */
import path from 'path'
import fs from 'fs-extra'
import {
nextBuild,
nextStart,
findPort,
renderViaHTTP,
killApp
} from 'next-test-utils'
jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60 * 5
const appDir = path.join(__dirname, '..')
let appPort
let app
const pages = ['/another.js', '/abc.js', '/123/hello.js']
const origContents = []
describe('Auto Export Flying Shuttle', () => {
beforeAll(async () => {
await fs.remove(path.join(appDir, '.next'))
for (const page of pages) {
origContents.push(
await fs.readFile(path.join(appDir, 'pages', page), 'utf8')
)
}
})
afterAll(async () => {
for (let i = 0; i < pages.length; i++) {
const page = pages[i]
const content = origContents[i]
await fs.writeFile(path.join(appDir, 'pages', page), content, 'utf8')
}
})
it('does an initial build', async () => {
await nextBuild(appDir)
appPort = await findPort()
app = await nextStart(appDir, appPort)
const checks = [
['/another', /hello another/],
['/abc', /hello abc/],
['/123/hello', /hello 123/]
]
for (const check of checks) {
const html = await renderViaHTTP(appPort, check[0])
expect(html).toMatch(check[1])
}
await killApp(app)
})
it('rebuilds one page', async () => {
const curPage = path.join(appDir, 'pages/123/hello.js')
const origContent = await fs.readFile(curPage, 'utf8')
await fs.writeFile(curPage, origContent.replace('123', '321'))
await nextBuild(appDir)
appPort = await findPort()
app = await nextStart(appDir, appPort)
const checks = [
['/another', /hello another/],
['/abc', /hello abc/],
['/123/hello', /hello 321/]
]
for (const check of checks) {
const html = await renderViaHTTP(appPort, check[0])
expect(html).toMatch(check[1])
}
await killApp(app)
})
it('rebuilds multiple pages', async () => {
const abcPath = path.join(appDir, 'pages/abc.js')
const anotherPath = path.join(appDir, 'pages/another.js')
const abcContent = await fs.readFile(abcPath, 'utf8')
await fs.writeFile(abcPath, abcContent.replace('abc', 'cba'), 'utf8')
const anotherContent = await fs.readFile(anotherPath, 'utf8')
await fs.writeFile(
anotherPath,
anotherContent.replace('another', 'nothing'),
'utf8'
)
await nextBuild(appDir)
appPort = await findPort()
app = await nextStart(appDir, appPort)
const checks = [
['/another', /hello nothing/],
['/abc', /hello cba/],
['/123/hello', /hello 321/]
]
for (const check of checks) {
const html = await renderViaHTTP(appPort, check[0])
expect(html).toMatch(check[1])
}
await killApp(app)
})
})
import React from 'react'
import NextHead from 'next/head'
import { string } from 'prop-types'
const defaultDescription = ''
const defaultOGURL = ''
const defaultOGImage = ''
const Head = props => (
<NextHead>
<meta charSet='UTF-8' />
<title>{props.title || ''}</title>
<meta
name='description'
content={props.description || defaultDescription}
/>
<meta name='viewport' content='width=device-width, initial-scale=1' />
<link rel='icon' sizes='192x192' href='/static/touch-icon.png' />
<link rel='apple-touch-icon' href='/static/touch-icon.png' />
<link rel='mask-icon' href='/static/favicon-mask.svg' color='#49B882' />
<link rel='icon' href='/static/favicon.ico' />
<meta property='og:url' content={props.url || defaultOGURL} />
<meta property='og:title' content={props.title || ''} />
<meta
property='og:description'
content={props.description || defaultDescription}
/>
<meta name='twitter:site' content={props.url || defaultOGURL} />
<meta name='twitter:card' content='summary_large_image' />
<meta name='twitter:image' content={props.ogImage || defaultOGImage} />
<meta property='og:image' content={props.ogImage || defaultOGImage} />
<meta property='og:image:width' content='1200' />
<meta property='og:image:height' content='630' />
</NextHead>
)
Head.propTypes = {
title: string,
description: string,
url: string,
ogImage: string
}
export default Head
import React from 'react'
import Link from 'next/link'
const Nav = () => (
<nav>
<ul>
<li>
<Link prefetch href='/'>
<a>Home</a>
</Link>
</li>
<li>
<Link prefetch href='/other'>
<a>Other</a>
</Link>
</li>
<li>
<Link prefetch href='/about'>
<a>About</a>
</Link>
</li>
</ul>
<style jsx>{`
:global(body) {
margin: 0;
font-family: -apple-system, BlinkMacSystemFont, Avenir Next, Avenir,
Helvetica, sans-serif;
}
nav {
text-align: center;
}
ul {
display: flex;
justify-content: space-between;
}
nav > ul {
padding: 4px 16px;
}
li {
display: flex;
padding: 6px 8px;
}
a {
color: #067df7;
text-decoration: none;
font-size: 13px;
}
`}</style>
</nav>
)
export default Nav
import dynamic from 'next/dynamic'
const DynamicComponent = dynamic(() => import('./something2'))
export default () => (
<>
<div>lol</div>
<DynamicComponent />
</>
)
module.exports = {
target: 'serverless',
experimental: {
flyingShuttle: true
}
}
import React from 'react'
import Head from '../../components/head'
import Nav from '../../components/nav'
import dynamic from 'next/dynamic'
const DynamicComponent = dynamic(() => import('../../components/something'))
const About = () => (
<div>
<Head title='About' />
<Nav />
<div className='hero'>
<h1 className='title'>All About Next.js!</h1>
<p className='description'>
To get started, edit <code>pages/about/index.js</code> and save to
reload.
</p>
</div>
<DynamicComponent />
<style jsx>{`
.hero {
width: 100%;
color: #333;
}
.title {
margin: 0;
width: 100%;
padding-top: 80px;
line-height: 1.15;
font-size: 48px;
}
.title,
.description {
text-align: center;
}
`}</style>
</div>
)
About.getInitialProps = () => ({})
export default About
import React from 'react'
import Head from '../../components/head'
import Nav from '../../components/nav'
import dynamic from 'next/dynamic'
const DynamicComponent = dynamic(() => import('../../components/something'))
const About = () => (
<div>
<Head title='About' />
<Nav />
<div className='hero'>
<h1 className='title'>Everything About Next.js!</h1>
<p className='description'>
To get started, edit <code>pages/about/index.js</code> and save to
reload.
</p>
</div>
<DynamicComponent />
<style jsx>{`
.hero {
width: 100%;
color: #333;
}
.title {
margin: 0;
width: 100%;
padding-top: 80px;
line-height: 1.15;
font-size: 48px;
}
.title,
.description {
text-align: center;
}
`}</style>
</div>
)
About.getInitialProps = () => ({})
export default About
import React from 'react'
import Head from '../components/head'
import Nav from '../components/nav'
import dynamic from 'next/dynamic'
const DynamicComponent = dynamic(() => import('../components/something'))
const Home = () => (
<div>
<Head title='Home' />
<Nav />
<div className='hero'>
<h1 className='title'>Welcome to Next.js!</h1>
<p className='description'>
To get started, edit <code>pages/index.js</code> and save to reload.
</p>
</div>
<DynamicComponent />
<style jsx>{`
.hero {
width: 100%;
color: #333;
}
.title {
margin: 0;
width: 100%;
padding-top: 80px;
line-height: 1.15;
font-size: 48px;
}
.title,
.description {
text-align: center;
}
`}</style>
</div>
)
Home.getInitialProps = () => ({})
export default Home
import React from 'react'
import Head from '../components/head'
import Nav from '../components/nav'
import dynamic from 'next/dynamic'
const DynamicComponent = dynamic(() => import('../components/something'))
const Home = () => (
<div>
<Head title='Other' />
<Nav />
<div className='hero'>
<h1 className='title'>Welcome to Other Next.js!</h1>
<p className='description'>
To get started, edit <code>pages/other.js</code> and save to reload.
</p>
</div>
<DynamicComponent />
<style jsx>{`
.hero {
width: 100%;
color: #333;
}
.title {
margin: 0;
width: 100%;
padding-top: 80px;
line-height: 1.15;
font-size: 48px;
}
.title,
.description {
text-align: center;
}
`}</style>
</div>
)
Home.getInitialProps = () => ({})
export default Home
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,
directory = path.join(__dirname, '.next')
) {
return new Promise((resolve, reject) => {
const app = express()
const nextStaticDir = path.join(directory, 'static')
app.use('/_next/static', express.static(nextStaticDir))
app.get('/', (req, res) => {
require(path.join(directory, 'serverless/pages/index.js')).render(
req,
res
)
})
try {
const o = require(path.join(directory, 'serverless/pages/other.js'))
app.get('/other', (req, res) => {
o.render(req, res)
})
} catch (_) {
// ignored
}
app.get('/about', (req, res) => {
require(path.join(directory, 'serverless/pages/about.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 */
import webdriver from 'next-webdriver'
import path from 'path'
import {
runNextCommand,
stopApp,
renderViaHTTP,
waitFor
} from 'next-test-utils'
import stripAnsi from 'strip-ansi'
import startServer from '../server'
import fs from 'fs-extra'
const appDir = path.join(__dirname, '../')
let appPort
let secondAppPort
let server
let secondServer
jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60 * 5
describe('Flying Shuttle', () => {
beforeAll(async () => {
{
await fs.remove(path.join(appDir, '.next-first'))
await fs.mkdirp(path.join(appDir, '.next-first'))
const { stdout } = await runNextCommand(['build', appDir], {
stdout: true
})
const buildText = stripAnsi(stdout)
expect(buildText).not.toMatch(/flying shuttle is docked/)
await fs.copy(
path.join(appDir, '.next'),
path.join(appDir, '.next-first')
)
server = await startServer(0, path.join(appDir, '.next-first'))
appPort = server.address().port
}
{
await fs.remove(path.join(appDir, '.next-second'))
await fs.mkdirp(path.join(appDir, '.next-second'))
await fs.rename(
path.join(appDir, 'pages', 'other.js'),
path.join(appDir, 'pages', 'other.js.hide')
)
await fs.rename(
path.join(appDir, 'pages', 'about', 'index.js'),
path.join(appDir, 'pages', 'about', 'index.js.hide')
)
await fs.rename(
path.join(appDir, 'pages', 'about', 'index.js2'),
path.join(appDir, 'pages', 'about', 'index.js')
)
const { stdout } = await runNextCommand(['build', appDir], {
stdout: true
})
const buildText = stripAnsi(stdout)
expect(buildText).toMatch(/found 2 changed and 1 unchanged page/)
await fs.copy(
path.join(appDir, '.next'),
path.join(appDir, '.next-second')
)
secondServer = await startServer(0, path.join(appDir, '.next-second'))
secondAppPort = secondServer.address().port
}
})
afterAll(async () => {
if (server) await stopApp(server)
if (secondServer) await stopApp(secondServer)
await fs.remove(path.join(appDir, '.next-first'))
await fs.remove(path.join(appDir, '.next-second'))
await fs.rename(
path.join(appDir, 'pages', 'other.js.hide'),
path.join(appDir, 'pages', 'other.js')
)
await fs.rename(
path.join(appDir, 'pages', 'about', 'index.js'),
path.join(appDir, 'pages', 'about', 'index.js2')
)
await fs.rename(
path.join(appDir, 'pages', 'about', 'index.js.hide'),
path.join(appDir, 'pages', 'about', 'index.js')
)
})
it('should render /index', async () => {
const html = await renderViaHTTP(appPort, '/')
expect(html).toMatch(/omega lol/)
expect(html).toMatch(/Welcome to Next/)
})
it('should render /other', async () => {
const html = await renderViaHTTP(appPort, '/other')
expect(html).toMatch(/omega lol/)
expect(html).toMatch(/Welcome to Other Next/)
})
it('should render /about', async () => {
const html = await renderViaHTTP(appPort, '/about')
expect(html).toMatch(/omega lol/)
expect(html).toMatch(/All About/)
})
it('should hydrate correctly /index', async () => {
const browser = await webdriver(appPort, '/')
try {
let text = await browser.text()
await waitFor(3000)
text = await browser.text()
expect(text).toMatch(/omega lol/)
expect(text).toMatch(/Welcome to Next/)
} finally {
await browser.close()
}
})
it('should hydrate correctly /other', async () => {
const browser = await webdriver(appPort, '/other')
try {
let text = await browser.text()
await waitFor(3000)
text = await browser.text()
expect(text).toMatch(/omega lol/)
expect(text).toMatch(/Welcome to Other Next/)
} finally {
await browser.close()
}
})
it('should hydrate correctly /about', async () => {
const browser = await webdriver(appPort, '/about')
try {
let text = await browser.text()
await waitFor(3000)
text = await browser.text()
expect(text).toMatch(/omega lol/)
expect(text).toMatch(/All About/)
} finally {
await browser.close()
}
})
it('should not re-prefetch for the page its already on', async () => {
const browser = await webdriver(appPort, '/')
const links = await browser.elementsByCss('link[rel=preload]')
let found = false
for (const link of links) {
const href = await link.getAttribute('href')
if (href.endsWith('index.js')) {
found = true
break
}
}
expect(found).toBe(false)
if (browser) await browser.close()
})
it('should render /index', async () => {
const html = await renderViaHTTP(secondAppPort, '/')
expect(html).toMatch(/omega lol/)
expect(html).toMatch(/Welcome to Next/)
})
it('should 404 /other', async () => {
const html = await renderViaHTTP(secondAppPort, '/other')
expect(html).toMatch(/Cannot GET \/other/)
})
it('should render /about', async () => {
const html = await renderViaHTTP(secondAppPort, '/about')
expect(html).toMatch(/omega lol/)
expect(html).toMatch(/Everything About/)
})
it('should hydrate correctly /index', async () => {
const browser = await webdriver(secondAppPort, '/')
try {
let text = await browser.text()
await waitFor(3000)
text = await browser.text()
expect(text).toMatch(/omega lol/)
expect(text).toMatch(/Welcome to Next/)
} finally {
await browser.close()
}
})
it('should 404 on /other', async () => {
const browser = await webdriver(secondAppPort, '/other')
try {
const text = await browser.text()
expect(text).toMatch(/Cannot GET \/other/)
} finally {
await browser.close()
}
})
it('should hydrate correctly /about', async () => {
const browser = await webdriver(secondAppPort, '/about')
try {
let text = await browser.text()
await waitFor(3000)
text = await browser.text()
expect(text).toMatch(/omega lol/)
expect(text).toMatch(/Everything About/)
} finally {
await browser.close()
}
})
})
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`getSpecifiedPages should only choose selected 1`] = `
Array [
"/_app.js",
"/_document.js",
"/a.js",
]
`;
exports[`getSpecifiedPages should only choose selected 2`] = `
Array [
"/_app.js",
"/_document.js",
"/a.js",
"/b.jsx",
]
`;
exports[`getSpecifiedPages should only choose selected 3`] = `
Array [
"/_app.js",
"/_document.js",
"/a.js",
"/b.jsx",
]
`;
exports[`getSpecifiedPages should only choose selected 4`] = `
Array [
"/_app.js",
"/_document.js",
"/a.js",
"/b.jsx",
"/c/index.mdx",
]
`;
/* eslint-env jest */
import { getSpecifiedPages as _getSpecifiedPages } from 'next/dist/build/utils'
const getSpecifiedPages = async (...args) =>
(await _getSpecifiedPages(...args)).map(pg => pg.replace(/\\+/g, '/'))
describe('getSpecifiedPages', () => {
it('should only choose selected', async () => {
expect(await getSpecifiedPages(__dirname, 'a', ['js'])).toMatchSnapshot()
let err
try {
await getSpecifiedPages(__dirname, 'a,b', ['js'])
} catch (e) {
err = e
}
expect(err).toBeTruthy()
expect(
await getSpecifiedPages(__dirname, 'a,b', ['js', 'jsx'])
).toMatchSnapshot()
expect(
await getSpecifiedPages(__dirname, 'a,b', ['js', 'jsx', 'mdx'])
).toMatchSnapshot()
expect(
await getSpecifiedPages(__dirname, 'a,b,c', ['js', 'jsx', 'mdx'])
).toMatchSnapshot()
})
})
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