Unverified Commit 815f2e91 authored by Tim Neutkens's avatar Tim Neutkens Committed by GitHub

Only compile /_error when /_error is needed (#6192)

Followup to removing the /_error in the initial markup we can now compile it using on-demand-entries only when it's needed.
parent 2f304bd1
...@@ -128,7 +128,7 @@ export default class HotReloader { ...@@ -128,7 +128,7 @@ export default class HotReloader {
} }
const page = `/${params.path.join('/')}` const page = `/${params.path.join('/')}`
if (BLOCKED_PAGES.indexOf(page) === -1) { if (page === '/_error' || BLOCKED_PAGES.indexOf(page) === -1) {
try { try {
await this.ensurePage(page) await this.ensurePage(page)
} catch (error) { } catch (error) {
...@@ -174,7 +174,7 @@ export default class HotReloader { ...@@ -174,7 +174,7 @@ export default class HotReloader {
} }
async getWebpackConfig () { async getWebpackConfig () {
const pagePaths = await glob(`+(_app|_document|_error).+(${this.config.pageExtensions.join('|')})`, {cwd: join(this.dir, 'pages')}) const pagePaths = await glob(`+(_app|_document).+(${this.config.pageExtensions.join('|')})`, {cwd: join(this.dir, 'pages')})
const pages = createPagesMapping(pagePaths, this.config.pageExtensions) const pages = createPagesMapping(pagePaths, this.config.pageExtensions)
const entrypoints = createEntrypoints(pages, 'server', this.buildId, this.config) const entrypoints = createEntrypoints(pages, 'server', this.buildId, this.config)
return Promise.all([ return Promise.all([
...@@ -410,7 +410,7 @@ export default class HotReloader { ...@@ -410,7 +410,7 @@ export default class HotReloader {
async ensurePage (page) { async ensurePage (page) {
// Make sure we don't re-build or dispose prebuilt pages // Make sure we don't re-build or dispose prebuilt pages
if (BLOCKED_PAGES.indexOf(page) !== -1) { if (page !== '/_error' && BLOCKED_PAGES.indexOf(page) !== -1) {
return return
} }
await this.onDemandEntries.ensurePage(page) await this.onDemandEntries.ensurePage(page)
......
...@@ -113,6 +113,8 @@ export default class DevServer extends Server { ...@@ -113,6 +113,8 @@ export default class DevServer extends Server {
} }
async renderErrorToHTML (err, req, res, pathname, query) { async renderErrorToHTML (err, req, res, pathname, query) {
await this.hotReloader.ensurePage('/_error')
const compilationErr = await this.getCompilationError(pathname) const compilationErr = await this.getCompilationError(pathname)
if (compilationErr) { if (compilationErr) {
res.statusCode = 500 res.statusCode = 500
......
...@@ -175,7 +175,13 @@ export default function onDemandEntryHandler (devMiddleware, multiCompiler, { ...@@ -175,7 +175,13 @@ export default function onDemandEntryHandler (devMiddleware, multiCompiler, {
const extensions = pageExtensions.join('|') const extensions = pageExtensions.join('|')
const pagesDir = join(dir, 'pages') const pagesDir = join(dir, 'pages')
const paths = await glob(`{${normalizedPagePath.slice(1)}/index,${normalizedPagePath.slice(1)}}.+(${extensions})`, {cwd: pagesDir})
let paths = await glob(`{${normalizedPagePath.slice(1)}/index,${normalizedPagePath.slice(1)}}.+(${extensions})`, {cwd: pagesDir})
// Default the /_error route to the Next.js provided default page
if (page === '/_error' && paths.length === 0) {
paths = ['next/dist/pages/_error']
}
if (paths.length === 0) { if (paths.length === 0) {
throw pageNotFoundError(normalizedPagePath) throw pageNotFoundError(normalizedPagePath)
...@@ -186,7 +192,7 @@ export default function onDemandEntryHandler (devMiddleware, multiCompiler, { ...@@ -186,7 +192,7 @@ export default function onDemandEntryHandler (devMiddleware, multiCompiler, {
pageUrl = pageUrl === '' ? '/' : pageUrl pageUrl = pageUrl === '' ? '/' : pageUrl
const bundleFile = pageUrl === '/' ? '/index.js' : `${pageUrl}.js` const bundleFile = pageUrl === '/' ? '/index.js' : `${pageUrl}.js`
const name = join('static', buildId, 'pages', bundleFile) const name = join('static', buildId, 'pages', bundleFile)
const absolutePagePath = join(pagesDir, pagePath) const absolutePagePath = pagePath.startsWith('next/dist/pages') ? require.resolve(pagePath) : join(pagesDir, pagePath)
await new Promise((resolve, reject) => { await new Promise((resolve, reject) => {
const entryInfo = entries[page] const entryInfo = entries[page]
...@@ -230,7 +236,12 @@ export default function onDemandEntryHandler (devMiddleware, multiCompiler, { ...@@ -230,7 +236,12 @@ export default function onDemandEntryHandler (devMiddleware, multiCompiler, {
return sendJson(ws, { invalid: true }) return sendJson(ws, { invalid: true })
} }
sendJson(ws, { success: true }) // 404 is an on demand entry but when a new page is added we have to refresh the page
if (page === '/_error') {
sendJson(ws, { invalid: true })
} else {
sendJson(ws, { success: true })
}
// We don't need to maintain active state of anything other than BUILT entries // We don't need to maintain active state of anything other than BUILT entries
if (entryInfo.status !== BUILT) return if (entryInfo.status !== BUILT) return
......
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