2019-10-04 18:11:39 +02:00
|
|
|
|
import fs from 'fs'
|
|
|
|
|
import { IncomingMessage, ServerResponse } from 'http'
|
|
|
|
|
import { join, normalize, relative as relativePath, sep } from 'path'
|
|
|
|
|
import { promisify } from 'util'
|
|
|
|
|
import webpack from 'webpack'
|
2019-06-24 15:48:26 +02:00
|
|
|
|
import WebpackDevMiddleware from 'webpack-dev-middleware'
|
|
|
|
|
import WebpackHotMiddleware from 'webpack-hot-middleware'
|
2019-10-04 18:11:39 +02:00
|
|
|
|
|
|
|
|
|
import { createEntrypoints, createPagesMapping } from '../build/entries'
|
|
|
|
|
import { watchCompilers } from '../build/output'
|
2018-12-03 14:18:52 +01:00
|
|
|
|
import getBaseWebpackConfig from '../build/webpack-config'
|
2019-10-04 18:11:39 +02:00
|
|
|
|
import { NEXT_PROJECT_ROOT_DIST_CLIENT } from '../lib/constants'
|
|
|
|
|
import { fileExists } from '../lib/file-exists'
|
|
|
|
|
import { recursiveDelete } from '../lib/recursive-delete'
|
2019-05-29 13:57:26 +02:00
|
|
|
|
import {
|
2019-10-04 18:11:39 +02:00
|
|
|
|
BLOCKED_PAGES,
|
|
|
|
|
CLIENT_STATIC_FILES_RUNTIME_AMP,
|
2019-05-29 13:57:26 +02:00
|
|
|
|
IS_BUNDLED_PAGE_REGEX,
|
|
|
|
|
ROUTE_NAME_REGEX,
|
2019-09-04 16:00:54 +02:00
|
|
|
|
} from '../next-server/lib/constants'
|
|
|
|
|
import { route } from '../next-server/server/router'
|
2019-10-04 18:11:39 +02:00
|
|
|
|
import errorOverlayMiddleware from './lib/error-overlay-middleware'
|
2019-02-24 22:08:35 +01:00
|
|
|
|
import { findPageFile } from './lib/find-page-file'
|
2019-10-04 18:11:39 +02:00
|
|
|
|
import onDemandEntryHandler, { normalizePage } from './on-demand-entry-handler'
|
|
|
|
|
import { NextHandleFunction } from 'connect'
|
|
|
|
|
import { UrlObject } from 'url'
|
2019-04-28 20:08:38 +02:00
|
|
|
|
|
|
|
|
|
const access = promisify(fs.access)
|
|
|
|
|
const readFile = promisify(fs.readFile)
|
2018-12-06 16:47:10 +01:00
|
|
|
|
|
2019-10-04 18:11:39 +02:00
|
|
|
|
export async function renderScriptError(res: ServerResponse, error: Error) {
|
2018-12-06 16:47:10 +01:00
|
|
|
|
// Asks CDNs and others to not to cache the errored page
|
2019-05-29 13:57:26 +02:00
|
|
|
|
res.setHeader(
|
|
|
|
|
'Cache-Control',
|
|
|
|
|
'no-cache, no-store, max-age=0, must-revalidate'
|
|
|
|
|
)
|
2018-12-06 16:47:10 +01:00
|
|
|
|
|
2019-10-04 18:11:39 +02:00
|
|
|
|
if (
|
|
|
|
|
(error as any).code === 'ENOENT' ||
|
|
|
|
|
error.message === 'INVALID_BUILD_ID'
|
|
|
|
|
) {
|
2018-12-06 16:47:10 +01:00
|
|
|
|
res.statusCode = 404
|
|
|
|
|
res.end('404 - Not Found')
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
console.error(error.stack)
|
|
|
|
|
res.statusCode = 500
|
|
|
|
|
res.end('500 - Internal Error')
|
|
|
|
|
}
|
2018-10-02 00:55:31 +02:00
|
|
|
|
|
2019-10-04 18:11:39 +02:00
|
|
|
|
function addCorsSupport(req: IncomingMessage, res: ServerResponse) {
|
2018-10-02 00:55:31 +02:00
|
|
|
|
if (!req.headers.origin) {
|
|
|
|
|
return { preflight: false }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
res.setHeader('Access-Control-Allow-Origin', req.headers.origin)
|
|
|
|
|
res.setHeader('Access-Control-Allow-Methods', 'OPTIONS, GET')
|
|
|
|
|
// Based on https://github.com/primus/access-control/blob/4cf1bc0e54b086c91e6aa44fb14966fa5ef7549c/index.js#L158
|
|
|
|
|
if (req.headers['access-control-request-headers']) {
|
2019-10-04 18:11:39 +02:00
|
|
|
|
res.setHeader('Access-Control-Allow-Headers', req.headers[
|
|
|
|
|
'access-control-request-headers'
|
|
|
|
|
] as string)
|
2018-10-02 00:55:31 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (req.method === 'OPTIONS') {
|
|
|
|
|
res.writeHead(200)
|
|
|
|
|
res.end()
|
|
|
|
|
return { preflight: true }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return { preflight: false }
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-29 13:57:26 +02:00
|
|
|
|
const matchNextPageBundleRequest = route(
|
|
|
|
|
'/_next/static/:buildId/pages/:path*.js(.map)?'
|
|
|
|
|
)
|
2018-07-24 11:24:40 +02:00
|
|
|
|
|
|
|
|
|
// Recursively look up the issuer till it ends up at the root
|
2019-10-04 18:11:39 +02:00
|
|
|
|
function findEntryModule(issuer: any): any {
|
2018-07-24 11:24:40 +02:00
|
|
|
|
if (issuer.issuer) {
|
|
|
|
|
return findEntryModule(issuer.issuer)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return issuer
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-04 18:11:39 +02:00
|
|
|
|
function erroredPages(
|
|
|
|
|
compilation: webpack.compilation.Compilation,
|
|
|
|
|
options = { enhanceName: (name: string) => name }
|
|
|
|
|
) {
|
|
|
|
|
const failedPages: { [page: string]: any[] } = {}
|
2018-07-24 11:24:40 +02:00
|
|
|
|
for (const error of compilation.errors) {
|
2019-05-08 03:11:56 +02:00
|
|
|
|
if (!error.origin) {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-24 11:24:40 +02:00
|
|
|
|
const entryModule = findEntryModule(error.origin)
|
2019-02-19 22:45:07 +01:00
|
|
|
|
const { name } = entryModule
|
2018-07-24 11:24:40 +02:00
|
|
|
|
if (!name) {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Only pages have to be reloaded
|
|
|
|
|
if (!IS_BUNDLED_PAGE_REGEX.test(name)) {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const enhancedName = options.enhanceName(name)
|
|
|
|
|
|
|
|
|
|
if (!failedPages[enhancedName]) {
|
|
|
|
|
failedPages[enhancedName] = []
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
failedPages[enhancedName].push(error)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return failedPages
|
|
|
|
|
}
|
2017-06-05 17:07:20 +02:00
|
|
|
|
|
2016-10-14 17:05:08 +02:00
|
|
|
|
export default class HotReloader {
|
2019-10-04 18:11:39 +02:00
|
|
|
|
private dir: string
|
|
|
|
|
private buildId: string
|
|
|
|
|
private middlewares: any[]
|
|
|
|
|
private pagesDir: string
|
|
|
|
|
private webpackDevMiddleware: WebpackDevMiddleware.WebpackDevMiddleware | null
|
|
|
|
|
private webpackHotMiddleware:
|
|
|
|
|
| (NextHandleFunction & WebpackHotMiddleware.EventStream)
|
|
|
|
|
| null
|
|
|
|
|
private initialized: boolean
|
|
|
|
|
private config: any
|
|
|
|
|
private stats: any
|
|
|
|
|
private serverPrevDocumentHash: string | null
|
|
|
|
|
private prevChunkNames?: Set<any>
|
|
|
|
|
private onDemandEntries: any
|
|
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
|
dir: string,
|
|
|
|
|
{
|
|
|
|
|
config,
|
|
|
|
|
pagesDir,
|
|
|
|
|
buildId,
|
|
|
|
|
}: { config: object; pagesDir: string; buildId: string }
|
|
|
|
|
) {
|
2018-06-25 23:06:46 +02:00
|
|
|
|
this.buildId = buildId
|
2016-10-17 09:05:46 +02:00
|
|
|
|
this.dir = dir
|
2016-11-23 19:32:49 +01:00
|
|
|
|
this.middlewares = []
|
2019-09-24 17:15:14 +02:00
|
|
|
|
this.pagesDir = pagesDir
|
2016-11-23 19:32:49 +01:00
|
|
|
|
this.webpackDevMiddleware = null
|
|
|
|
|
this.webpackHotMiddleware = null
|
2016-10-31 11:51:03 +01:00
|
|
|
|
this.initialized = false
|
2016-10-19 14:41:45 +02:00
|
|
|
|
this.stats = null
|
2018-07-24 11:24:40 +02:00
|
|
|
|
this.serverPrevDocumentHash = null
|
2017-02-26 20:45:16 +01:00
|
|
|
|
|
2018-02-14 16:17:41 +01:00
|
|
|
|
this.config = config
|
2016-10-17 09:05:46 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-10-04 18:11:39 +02:00
|
|
|
|
async run(req: IncomingMessage, res: ServerResponse, parsedUrl: UrlObject) {
|
2018-01-30 16:40:52 +01:00
|
|
|
|
// Usually CORS support is not needed for the hot-reloader (this is dev only feature)
|
|
|
|
|
// With when the app runs for multi-zones support behind a proxy,
|
|
|
|
|
// the current page is trying to access this URL via assetPrefix.
|
|
|
|
|
// That's when the CORS support is needed.
|
|
|
|
|
const { preflight } = addCorsSupport(req, res)
|
|
|
|
|
if (preflight) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-25 13:45:42 +02:00
|
|
|
|
// When a request comes in that is a page bundle, e.g. /_next/static/<buildid>/pages/index.js
|
|
|
|
|
// we have to compile the page using on-demand-entries, this middleware will handle doing that
|
|
|
|
|
// by adding the page to on-demand-entries, waiting till it's done
|
|
|
|
|
// and then the bundle will be served like usual by the actual route in server/index.js
|
2019-10-04 18:11:39 +02:00
|
|
|
|
const handlePageBundleRequest = async (
|
|
|
|
|
res: ServerResponse,
|
|
|
|
|
parsedUrl: UrlObject
|
|
|
|
|
) => {
|
2019-02-19 22:45:07 +01:00
|
|
|
|
const { pathname } = parsedUrl
|
2018-07-25 13:45:42 +02:00
|
|
|
|
const params = matchNextPageBundleRequest(pathname)
|
|
|
|
|
if (!params) {
|
|
|
|
|
return {}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (params.buildId !== this.buildId) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const page = `/${params.path.join('/')}`
|
2019-02-03 15:34:28 +01:00
|
|
|
|
if (page === '/_error' || BLOCKED_PAGES.indexOf(page) === -1) {
|
2018-07-25 13:45:42 +02:00
|
|
|
|
try {
|
|
|
|
|
await this.ensurePage(page)
|
|
|
|
|
} catch (error) {
|
2018-12-06 16:47:10 +01:00
|
|
|
|
await renderScriptError(res, error)
|
2019-02-19 22:45:07 +01:00
|
|
|
|
return { finished: true }
|
2018-07-25 13:45:42 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-04-28 20:08:38 +02:00
|
|
|
|
const bundlePath = join(
|
|
|
|
|
this.dir,
|
|
|
|
|
this.config.distDir,
|
2019-05-29 13:57:26 +02:00
|
|
|
|
'static/development/pages',
|
|
|
|
|
page + '.js'
|
2019-04-28 20:08:38 +02:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// make sure to 404 for AMP bundles in case they weren't removed
|
|
|
|
|
try {
|
|
|
|
|
await access(bundlePath)
|
|
|
|
|
const data = await readFile(bundlePath, 'utf8')
|
|
|
|
|
if (data.includes('__NEXT_DROP_CLIENT_FILE__')) {
|
|
|
|
|
res.statusCode = 404
|
|
|
|
|
res.end()
|
|
|
|
|
return { finished: true }
|
|
|
|
|
}
|
|
|
|
|
} catch (_) {}
|
|
|
|
|
|
2018-07-25 13:45:42 +02:00
|
|
|
|
const errors = await this.getCompilationErrors(page)
|
|
|
|
|
if (errors.length > 0) {
|
2018-12-06 16:47:10 +01:00
|
|
|
|
await renderScriptError(res, errors[0])
|
2019-02-19 22:45:07 +01:00
|
|
|
|
return { finished: true }
|
2018-07-25 13:45:42 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {}
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-04 18:11:39 +02:00
|
|
|
|
const { finished } = (await handlePageBundleRequest(res, parsedUrl)) as any
|
2018-07-25 13:45:42 +02:00
|
|
|
|
|
2016-11-23 19:32:49 +01:00
|
|
|
|
for (const fn of this.middlewares) {
|
|
|
|
|
await new Promise((resolve, reject) => {
|
2019-10-04 18:11:39 +02:00
|
|
|
|
fn(req, res, (err: Error) => {
|
2017-01-08 03:02:29 +01:00
|
|
|
|
if (err) return reject(err)
|
2016-11-23 19:32:49 +01:00
|
|
|
|
resolve()
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
}
|
2018-07-25 13:45:42 +02:00
|
|
|
|
|
2019-02-19 22:45:07 +01:00
|
|
|
|
return { finished }
|
2016-10-17 09:05:46 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-10-04 18:11:39 +02:00
|
|
|
|
async clean() {
|
2019-03-05 14:01:42 +01:00
|
|
|
|
return recursiveDelete(join(this.dir, this.config.distDir))
|
2018-02-14 16:17:41 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-10-04 18:11:39 +02:00
|
|
|
|
async getWebpackConfig() {
|
2019-02-24 22:08:35 +01:00
|
|
|
|
const pagePaths = await Promise.all([
|
2019-09-24 17:15:14 +02:00
|
|
|
|
findPageFile(this.pagesDir, '/_app', this.config.pageExtensions),
|
2019-10-04 18:11:39 +02:00
|
|
|
|
findPageFile(this.pagesDir, '/_document', this.config.pageExtensions),
|
2019-02-24 22:08:35 +01:00
|
|
|
|
])
|
|
|
|
|
|
2019-05-29 13:57:26 +02:00
|
|
|
|
const pages = createPagesMapping(
|
2019-10-04 18:11:39 +02:00
|
|
|
|
pagePaths.filter(i => i !== null) as string[],
|
2019-05-29 13:57:26 +02:00
|
|
|
|
this.config.pageExtensions
|
|
|
|
|
)
|
|
|
|
|
const entrypoints = createEntrypoints(
|
|
|
|
|
pages,
|
|
|
|
|
'server',
|
|
|
|
|
this.buildId,
|
|
|
|
|
this.config
|
|
|
|
|
)
|
2019-02-25 23:00:14 +01:00
|
|
|
|
|
2019-10-04 18:11:39 +02:00
|
|
|
|
let additionalClientEntrypoints: { [file: string]: string } = {}
|
2019-05-29 13:57:26 +02:00
|
|
|
|
additionalClientEntrypoints[CLIENT_STATIC_FILES_RUNTIME_AMP] =
|
|
|
|
|
`.${sep}` +
|
2019-06-05 20:15:42 +02:00
|
|
|
|
relativePath(
|
|
|
|
|
this.dir,
|
|
|
|
|
join(NEXT_PROJECT_ROOT_DIST_CLIENT, 'dev', 'amp-dev')
|
|
|
|
|
)
|
2019-02-25 23:00:14 +01:00
|
|
|
|
|
2019-04-22 20:57:02 +02:00
|
|
|
|
return Promise.all([
|
2019-05-29 13:57:26 +02:00
|
|
|
|
getBaseWebpackConfig(this.dir, {
|
|
|
|
|
dev: true,
|
|
|
|
|
isServer: false,
|
|
|
|
|
config: this.config,
|
|
|
|
|
buildId: this.buildId,
|
2019-09-24 17:15:14 +02:00
|
|
|
|
pagesDir: this.pagesDir,
|
2019-10-04 18:11:39 +02:00
|
|
|
|
entrypoints: { ...entrypoints.client, ...additionalClientEntrypoints },
|
2019-05-29 13:57:26 +02:00
|
|
|
|
}),
|
|
|
|
|
getBaseWebpackConfig(this.dir, {
|
|
|
|
|
dev: true,
|
|
|
|
|
isServer: true,
|
|
|
|
|
config: this.config,
|
|
|
|
|
buildId: this.buildId,
|
2019-09-24 17:15:14 +02:00
|
|
|
|
pagesDir: this.pagesDir,
|
2019-10-04 18:11:39 +02:00
|
|
|
|
entrypoints: entrypoints.server,
|
|
|
|
|
}),
|
2019-04-22 20:57:02 +02:00
|
|
|
|
])
|
2019-01-08 23:10:32 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-10-04 18:11:39 +02:00
|
|
|
|
async start() {
|
2018-02-14 16:17:41 +01:00
|
|
|
|
await this.clean()
|
2018-01-30 16:40:52 +01:00
|
|
|
|
|
2019-01-08 23:10:32 +01:00
|
|
|
|
const configs = await this.getWebpackConfig()
|
2016-12-16 21:33:08 +01:00
|
|
|
|
|
2018-07-24 11:24:40 +02:00
|
|
|
|
const multiCompiler = webpack(configs)
|
2018-01-30 16:40:52 +01:00
|
|
|
|
|
2018-07-24 11:24:40 +02:00
|
|
|
|
const buildTools = await this.prepareBuildTools(multiCompiler)
|
2017-06-07 00:32:02 +02:00
|
|
|
|
this.assignBuildTools(buildTools)
|
|
|
|
|
|
2019-10-04 18:11:39 +02:00
|
|
|
|
this.stats = ((await this.waitUntilValid()) as any).stats[0]
|
2016-10-17 09:05:46 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-10-04 18:11:39 +02:00
|
|
|
|
async stop(webpackDevMiddleware?: WebpackDevMiddleware.WebpackDevMiddleware) {
|
2017-06-07 00:32:02 +02:00
|
|
|
|
const middleware = webpackDevMiddleware || this.webpackDevMiddleware
|
|
|
|
|
if (middleware) {
|
2016-12-17 05:04:40 +01:00
|
|
|
|
return new Promise((resolve, reject) => {
|
2019-10-04 18:11:39 +02:00
|
|
|
|
;(middleware.close as any)((err: any) => {
|
2017-01-08 03:02:29 +01:00
|
|
|
|
if (err) return reject(err)
|
2016-12-17 05:04:40 +01:00
|
|
|
|
resolve()
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-04 18:11:39 +02:00
|
|
|
|
async reload() {
|
2017-06-07 00:32:02 +02:00
|
|
|
|
this.stats = null
|
|
|
|
|
|
2018-02-14 16:17:41 +01:00
|
|
|
|
await this.clean()
|
2018-01-30 16:40:52 +01:00
|
|
|
|
|
2019-01-08 23:10:32 +01:00
|
|
|
|
const configs = await this.getWebpackConfig()
|
2018-01-30 16:40:52 +01:00
|
|
|
|
const compiler = webpack(configs)
|
|
|
|
|
|
2017-06-07 00:32:02 +02:00
|
|
|
|
const buildTools = await this.prepareBuildTools(compiler)
|
|
|
|
|
this.stats = await this.waitUntilValid(buildTools.webpackDevMiddleware)
|
|
|
|
|
|
|
|
|
|
const oldWebpackDevMiddleware = this.webpackDevMiddleware
|
|
|
|
|
|
|
|
|
|
this.assignBuildTools(buildTools)
|
2019-10-04 18:11:39 +02:00
|
|
|
|
await this.stop(oldWebpackDevMiddleware!)
|
2017-06-07 00:32:02 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-10-04 18:11:39 +02:00
|
|
|
|
assignBuildTools({
|
2019-05-29 13:57:26 +02:00
|
|
|
|
webpackDevMiddleware,
|
|
|
|
|
webpackHotMiddleware,
|
2019-10-04 18:11:39 +02:00
|
|
|
|
onDemandEntries,
|
|
|
|
|
}: {
|
|
|
|
|
webpackDevMiddleware: WebpackDevMiddleware.WebpackDevMiddleware
|
|
|
|
|
webpackHotMiddleware: NextHandleFunction & WebpackHotMiddleware.EventStream
|
|
|
|
|
onDemandEntries: any
|
2019-05-29 13:57:26 +02:00
|
|
|
|
}) {
|
2017-06-07 00:32:02 +02:00
|
|
|
|
this.webpackDevMiddleware = webpackDevMiddleware
|
|
|
|
|
this.webpackHotMiddleware = webpackHotMiddleware
|
|
|
|
|
this.onDemandEntries = onDemandEntries
|
|
|
|
|
this.middlewares = [
|
|
|
|
|
webpackDevMiddleware,
|
2019-04-21 22:51:09 +02:00
|
|
|
|
// must come before hotMiddleware
|
|
|
|
|
onDemandEntries.middleware(),
|
2017-06-07 00:32:02 +02:00
|
|
|
|
webpackHotMiddleware,
|
2019-10-04 18:11:39 +02:00
|
|
|
|
errorOverlayMiddleware({ dir: this.dir }),
|
2017-06-07 00:32:02 +02:00
|
|
|
|
]
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-04 18:11:39 +02:00
|
|
|
|
async prepareBuildTools(multiCompiler: webpack.MultiCompiler) {
|
2019-05-08 03:11:56 +02:00
|
|
|
|
const tsConfigPath = join(this.dir, 'tsconfig.json')
|
|
|
|
|
const useTypeScript = await fileExists(tsConfigPath)
|
2019-10-28 19:02:03 +01:00
|
|
|
|
const ignoreTypeScriptErrors =
|
|
|
|
|
this.config.typescript && this.config.typescript.ignoreDevErrors
|
2019-06-26 20:54:23 +02:00
|
|
|
|
|
|
|
|
|
watchCompilers(
|
|
|
|
|
multiCompiler.compilers[0],
|
|
|
|
|
multiCompiler.compilers[1],
|
2019-10-28 19:02:03 +01:00
|
|
|
|
useTypeScript && !ignoreTypeScriptErrors,
|
2019-06-26 20:54:23 +02:00
|
|
|
|
({ errors, warnings }) => this.send('typeChecked', { errors, warnings })
|
|
|
|
|
)
|
2019-05-08 03:11:56 +02:00
|
|
|
|
|
2018-07-24 11:24:40 +02:00
|
|
|
|
// This plugin watches for changes to _document.js and notifies the client side that it should reload the page
|
2019-05-29 13:57:26 +02:00
|
|
|
|
multiCompiler.compilers[1].hooks.done.tap(
|
|
|
|
|
'NextjsHotReloaderForServer',
|
|
|
|
|
stats => {
|
|
|
|
|
if (!this.initialized) {
|
|
|
|
|
return
|
|
|
|
|
}
|
2016-10-24 09:22:15 +02:00
|
|
|
|
|
2019-05-29 13:57:26 +02:00
|
|
|
|
const { compilation } = stats
|
2018-07-24 11:24:40 +02:00
|
|
|
|
|
2019-05-29 13:57:26 +02:00
|
|
|
|
// We only watch `_document` for changes on the server compilation
|
|
|
|
|
// the rest of the files will be triggered by the client compilation
|
|
|
|
|
const documentChunk = compilation.chunks.find(
|
|
|
|
|
c => c.name === normalize(`static/${this.buildId}/pages/_document.js`)
|
|
|
|
|
)
|
|
|
|
|
// If the document chunk can't be found we do nothing
|
|
|
|
|
if (!documentChunk) {
|
|
|
|
|
console.warn('_document.js chunk not found')
|
|
|
|
|
return
|
|
|
|
|
}
|
2018-07-24 11:24:40 +02:00
|
|
|
|
|
2019-05-29 13:57:26 +02:00
|
|
|
|
// Initial value
|
|
|
|
|
if (this.serverPrevDocumentHash === null) {
|
|
|
|
|
this.serverPrevDocumentHash = documentChunk.hash
|
|
|
|
|
return
|
|
|
|
|
}
|
2018-07-24 11:24:40 +02:00
|
|
|
|
|
2019-05-29 13:57:26 +02:00
|
|
|
|
// If _document.js didn't change we don't trigger a reload
|
|
|
|
|
if (documentChunk.hash === this.serverPrevDocumentHash) {
|
|
|
|
|
return
|
|
|
|
|
}
|
2018-07-24 11:24:40 +02:00
|
|
|
|
|
2019-05-29 13:57:26 +02:00
|
|
|
|
// Notify reload to reload the page, as _document.js was changed (different hash)
|
|
|
|
|
this.send('reloadPage')
|
|
|
|
|
this.serverPrevDocumentHash = documentChunk.hash
|
|
|
|
|
}
|
|
|
|
|
)
|
2016-10-14 17:05:08 +02:00
|
|
|
|
|
2019-05-29 13:57:26 +02:00
|
|
|
|
multiCompiler.compilers[0].hooks.done.tap(
|
|
|
|
|
'NextjsHotReloaderForClient',
|
|
|
|
|
stats => {
|
|
|
|
|
const { compilation } = stats
|
|
|
|
|
const chunkNames = new Set(
|
|
|
|
|
compilation.chunks
|
|
|
|
|
.map(c => c.name)
|
|
|
|
|
.filter(name => IS_BUNDLED_PAGE_REGEX.test(name))
|
|
|
|
|
)
|
2017-06-05 17:07:20 +02:00
|
|
|
|
|
2019-05-29 13:57:26 +02:00
|
|
|
|
if (this.initialized) {
|
|
|
|
|
// detect chunks which have to be replaced with a new template
|
|
|
|
|
// e.g, pages/index.js <-> pages/_error.js
|
2019-10-04 18:11:39 +02:00
|
|
|
|
const addedPages = diff(chunkNames, this.prevChunkNames!)
|
|
|
|
|
const removedPages = diff(this.prevChunkNames!, chunkNames)
|
2019-05-29 13:57:26 +02:00
|
|
|
|
|
|
|
|
|
if (addedPages.size > 0) {
|
|
|
|
|
for (const addedPage of addedPages) {
|
|
|
|
|
let page =
|
2019-10-04 18:11:39 +02:00
|
|
|
|
'/' + ROUTE_NAME_REGEX.exec(addedPage)![1].replace(/\\/g, '/')
|
2019-05-29 13:57:26 +02:00
|
|
|
|
page = page === '/index' ? '/' : page
|
|
|
|
|
this.send('addedPage', page)
|
|
|
|
|
}
|
2019-01-08 23:10:32 +01:00
|
|
|
|
}
|
2016-11-24 15:03:16 +01:00
|
|
|
|
|
2019-05-29 13:57:26 +02:00
|
|
|
|
if (removedPages.size > 0) {
|
|
|
|
|
for (const removedPage of removedPages) {
|
|
|
|
|
let page =
|
2019-10-04 18:11:39 +02:00
|
|
|
|
'/' + ROUTE_NAME_REGEX.exec(removedPage)![1].replace(/\\/g, '/')
|
2019-05-29 13:57:26 +02:00
|
|
|
|
page = page === '/index' ? '/' : page
|
|
|
|
|
this.send('removedPage', page)
|
|
|
|
|
}
|
2019-01-08 23:10:32 +01:00
|
|
|
|
}
|
2016-11-24 15:03:16 +01:00
|
|
|
|
}
|
2016-10-24 17:20:50 +02:00
|
|
|
|
|
2019-05-29 13:57:26 +02:00
|
|
|
|
this.initialized = true
|
|
|
|
|
this.stats = stats
|
|
|
|
|
this.prevChunkNames = chunkNames
|
|
|
|
|
}
|
|
|
|
|
)
|
2016-10-19 14:41:45 +02:00
|
|
|
|
|
2018-10-28 23:01:45 +01:00
|
|
|
|
// We don’t watch .git/ .next/ and node_modules for changes
|
2017-01-19 08:09:40 +01:00
|
|
|
|
const ignored = [
|
2018-10-28 23:01:45 +01:00
|
|
|
|
/[\\/]\.git[\\/]/,
|
|
|
|
|
/[\\/]\.next[\\/]/,
|
2019-10-04 18:11:39 +02:00
|
|
|
|
/[\\/]node_modules[\\/]/,
|
2017-01-19 08:09:40 +01:00
|
|
|
|
]
|
2016-11-28 15:04:59 +01:00
|
|
|
|
|
2017-05-14 01:44:21 +02:00
|
|
|
|
let webpackDevMiddlewareConfig = {
|
2018-07-24 11:24:40 +02:00
|
|
|
|
publicPath: `/_next/static/webpack`,
|
2016-10-17 09:05:46 +02:00
|
|
|
|
noInfo: true,
|
2018-07-24 11:24:40 +02:00
|
|
|
|
logLevel: 'silent',
|
2018-11-02 19:47:56 +01:00
|
|
|
|
watchOptions: { ignored },
|
2019-10-04 18:11:39 +02:00
|
|
|
|
writeToDisk: true,
|
2017-05-14 01:44:21 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (this.config.webpackDevMiddleware) {
|
2019-05-29 13:57:26 +02:00
|
|
|
|
console.log(
|
|
|
|
|
`> Using "webpackDevMiddleware" config function defined in ${
|
|
|
|
|
this.config.configOrigin
|
|
|
|
|
}.`
|
|
|
|
|
)
|
|
|
|
|
webpackDevMiddlewareConfig = this.config.webpackDevMiddleware(
|
|
|
|
|
webpackDevMiddlewareConfig
|
|
|
|
|
)
|
2017-05-14 01:44:21 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-05-29 13:57:26 +02:00
|
|
|
|
const webpackDevMiddleware = WebpackDevMiddleware(
|
|
|
|
|
multiCompiler,
|
|
|
|
|
webpackDevMiddlewareConfig
|
|
|
|
|
)
|
2016-11-23 19:32:49 +01:00
|
|
|
|
|
2019-05-29 13:57:26 +02:00
|
|
|
|
const webpackHotMiddleware = WebpackHotMiddleware(
|
|
|
|
|
multiCompiler.compilers[0],
|
|
|
|
|
{
|
|
|
|
|
path: '/_next/webpack-hmr',
|
|
|
|
|
log: false,
|
2019-10-04 18:11:39 +02:00
|
|
|
|
heartbeat: 2500,
|
2019-05-29 13:57:26 +02:00
|
|
|
|
}
|
|
|
|
|
)
|
2018-01-30 16:40:52 +01:00
|
|
|
|
|
2019-05-29 13:57:26 +02:00
|
|
|
|
const onDemandEntries = onDemandEntryHandler(
|
|
|
|
|
webpackDevMiddleware,
|
|
|
|
|
multiCompiler,
|
|
|
|
|
{
|
|
|
|
|
dir: this.dir,
|
|
|
|
|
buildId: this.buildId,
|
2019-09-24 17:15:14 +02:00
|
|
|
|
pagesDir: this.pagesDir,
|
2019-05-29 13:57:26 +02:00
|
|
|
|
distDir: this.config.distDir,
|
|
|
|
|
reload: this.reload.bind(this),
|
|
|
|
|
pageExtensions: this.config.pageExtensions,
|
|
|
|
|
publicRuntimeConfig: this.config.publicRuntimeConfig,
|
|
|
|
|
serverRuntimeConfig: this.config.serverRuntimeConfig,
|
2019-10-04 18:11:39 +02:00
|
|
|
|
...this.config.onDemandEntries,
|
2019-05-29 13:57:26 +02:00
|
|
|
|
}
|
|
|
|
|
)
|
2016-11-23 19:32:49 +01:00
|
|
|
|
|
2017-06-07 00:32:02 +02:00
|
|
|
|
return {
|
|
|
|
|
webpackDevMiddleware,
|
|
|
|
|
webpackHotMiddleware,
|
2019-10-04 18:11:39 +02:00
|
|
|
|
onDemandEntries,
|
2017-06-07 00:32:02 +02:00
|
|
|
|
}
|
2016-10-14 17:05:08 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-10-04 18:11:39 +02:00
|
|
|
|
waitUntilValid(
|
|
|
|
|
webpackDevMiddleware?: WebpackDevMiddleware.WebpackDevMiddleware
|
|
|
|
|
) {
|
2017-06-07 00:32:02 +02:00
|
|
|
|
const middleware = webpackDevMiddleware || this.webpackDevMiddleware
|
2019-05-29 13:57:26 +02:00
|
|
|
|
return new Promise(resolve => {
|
2019-10-04 18:11:39 +02:00
|
|
|
|
middleware!.waitUntilValid(resolve)
|
2016-10-14 17:05:08 +02:00
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-04 18:11:39 +02:00
|
|
|
|
async getCompilationErrors(page: string) {
|
2018-07-24 11:24:40 +02:00
|
|
|
|
const normalizedPage = normalizePage(page)
|
2017-06-07 00:32:02 +02:00
|
|
|
|
// When we are reloading, we need to wait until it's reloaded properly.
|
|
|
|
|
await this.onDemandEntries.waitUntilReloaded()
|
|
|
|
|
|
2018-07-24 11:24:40 +02:00
|
|
|
|
if (this.stats.hasErrors()) {
|
2019-02-19 22:45:07 +01:00
|
|
|
|
const { compilation } = this.stats
|
2018-07-24 11:24:40 +02:00
|
|
|
|
const failedPages = erroredPages(compilation, {
|
2019-10-04 18:11:39 +02:00
|
|
|
|
enhanceName(name) {
|
|
|
|
|
return '/' + ROUTE_NAME_REGEX.exec(name)![1]
|
|
|
|
|
},
|
2018-07-24 11:24:40 +02:00
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// If there is an error related to the requesting page we display it instead of the first error
|
2019-05-29 13:57:26 +02:00
|
|
|
|
if (
|
|
|
|
|
failedPages[normalizedPage] &&
|
|
|
|
|
failedPages[normalizedPage].length > 0
|
|
|
|
|
) {
|
2018-07-24 11:24:40 +02:00
|
|
|
|
return failedPages[normalizedPage]
|
2016-10-19 14:41:45 +02:00
|
|
|
|
}
|
2018-07-24 11:24:40 +02:00
|
|
|
|
|
|
|
|
|
// If none were found we still have to show the other errors
|
|
|
|
|
return this.stats.compilation.errors
|
2016-10-19 14:41:45 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-07-24 11:24:40 +02:00
|
|
|
|
return []
|
2016-10-19 14:41:45 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-10-04 18:11:39 +02:00
|
|
|
|
send = (action: string, ...args: any[]) => {
|
|
|
|
|
this.webpackHotMiddleware!.publish({ action, data: args })
|
2016-10-24 09:22:15 +02:00
|
|
|
|
}
|
2017-02-26 20:45:16 +01:00
|
|
|
|
|
2019-10-04 18:11:39 +02:00
|
|
|
|
async ensurePage(page: string) {
|
2018-05-16 00:36:24 +02:00
|
|
|
|
// Make sure we don't re-build or dispose prebuilt pages
|
2019-02-03 15:34:28 +01:00
|
|
|
|
if (page !== '/_error' && BLOCKED_PAGES.indexOf(page) !== -1) {
|
2018-05-16 00:36:24 +02:00
|
|
|
|
return
|
|
|
|
|
}
|
2019-04-11 20:59:26 +02:00
|
|
|
|
return this.onDemandEntries.ensurePage(page)
|
2017-02-26 20:45:16 +01:00
|
|
|
|
}
|
2016-10-14 17:05:08 +02:00
|
|
|
|
}
|
2016-10-19 14:41:45 +02:00
|
|
|
|
|
2019-10-04 18:11:39 +02:00
|
|
|
|
function diff(a: Set<any>, b: Set<any>) {
|
2019-05-29 13:57:26 +02:00
|
|
|
|
return new Set([...a].filter(v => !b.has(v)))
|
2016-10-24 09:22:15 +02:00
|
|
|
|
}
|