Commit c97aca50 authored by Arunoda Susiripala's avatar Arunoda Susiripala Committed by GitHub

Show webpack errors in all pages. (#2588)

* Show webpack errors in all pages.
When there's a webpack error that means HMR failed too.
So, showing other pages won't makes sense since user
can't edit them and get changes via HMR.
That's why now we show the error on all pages.

* Remove exact propType checks from the error component.
When there's an error, something this check shows in the console.
That means it could accept more props.
Also this is not a public API. So, we don't want to do propType checks.
parent 24a67ee9
import React from 'react' import React from 'react'
import PropTypes from 'prop-types' import PropTypes from 'prop-types'
import exact from 'prop-types-exact'
import HTTPStatus from 'http-status' import HTTPStatus from 'http-status'
import Head from './head' import Head from './head'
...@@ -10,9 +9,9 @@ export default class Error extends React.Component { ...@@ -10,9 +9,9 @@ export default class Error extends React.Component {
return { statusCode } return { statusCode }
} }
static propTypes = exact({ static propTypes = {
statusCode: PropTypes.number statusCode: PropTypes.number
}) }
render () { render () {
const { statusCode } = this.props const { statusCode } = this.props
......
...@@ -299,19 +299,29 @@ export default class Router { ...@@ -299,19 +299,29 @@ export default class Router {
cancelled = true cancelled = true
} }
const Component = await this.fetchRoute(route) try {
const Component = await this.fetchRoute(route)
if (cancelled) { if (cancelled) {
const error = new Error(`Abort fetching component for route: "${route}"`) const error = new Error(`Abort fetching component for route: "${route}"`)
error.cancelled = true error.cancelled = true
throw error throw error
} }
if (cancel === this.componentLoadCancel) { if (cancel === this.componentLoadCancel) {
this.componentLoadCancel = null this.componentLoadCancel = null
} }
return Component return Component
} catch (err) {
// There's an error in loading the route.
// Usually this happens when there's a failure in the webpack build
// So in that case, we need to load the page with full SSR
// That'll clean the invalid exising client side information.
// (Like cached routes)
window.location.href = as
throw err
}
} }
async getInitialProps (Component, ctx) { async getInitialProps (Component, ctx) {
......
...@@ -12,7 +12,6 @@ import { ...@@ -12,7 +12,6 @@ import {
renderScriptError renderScriptError
} from './render' } from './render'
import Router from './router' import Router from './router'
import { resolveFromList } from './resolve'
import { getAvailableChunks } from './utils' import { getAvailableChunks } from './utils'
import getConfig from './config' import getConfig from './config'
// We need to go up one more level since we are in the `dist` directory // We need to go up one more level since we are in the `dist` directory
...@@ -190,7 +189,7 @@ export default class Server { ...@@ -190,7 +189,7 @@ export default class Server {
return await renderScriptError(req, res, page, error, {}, this.renderOpts) return await renderScriptError(req, res, page, error, {}, this.renderOpts)
} }
const compilationErr = await this.getCompilationError(page, req, res) const compilationErr = await this.getCompilationError()
if (compilationErr) { if (compilationErr) {
const customFields = { statusCode: 500 } const customFields = { statusCode: 500 }
return await renderScriptError(req, res, page, compilationErr, customFields, this.renderOpts) return await renderScriptError(req, res, page, compilationErr, customFields, this.renderOpts)
...@@ -273,7 +272,7 @@ export default class Server { ...@@ -273,7 +272,7 @@ export default class Server {
async renderToHTML (req, res, pathname, query) { async renderToHTML (req, res, pathname, query) {
if (this.dev) { if (this.dev) {
const compilationErr = await this.getCompilationError(pathname) const compilationErr = await this.getCompilationError()
if (compilationErr) { if (compilationErr) {
res.statusCode = 500 res.statusCode = 500
return this.renderErrorToHTML(compilationErr, req, res, pathname, query) return this.renderErrorToHTML(compilationErr, req, res, pathname, query)
...@@ -301,7 +300,7 @@ export default class Server { ...@@ -301,7 +300,7 @@ export default class Server {
async renderErrorToHTML (err, req, res, pathname, query) { async renderErrorToHTML (err, req, res, pathname, query) {
if (this.dev) { if (this.dev) {
const compilationErr = await this.getCompilationError('/_error') const compilationErr = await this.getCompilationError()
if (compilationErr) { if (compilationErr) {
res.statusCode = 500 res.statusCode = 500
return renderErrorToHTML(compilationErr, req, res, pathname, query, this.renderOpts) return renderErrorToHTML(compilationErr, req, res, pathname, query, this.renderOpts)
...@@ -382,15 +381,14 @@ export default class Server { ...@@ -382,15 +381,14 @@ export default class Server {
return true return true
} }
async getCompilationError (page, req, res) { async getCompilationError () {
if (!this.hotReloader) return if (!this.hotReloader) return
const errors = await this.hotReloader.getCompilationErrors() const errors = await this.hotReloader.getCompilationErrors()
if (!errors.size) return if (!errors.size) return
const id = join(this.dir, this.dist, 'bundles', 'pages', page) // Return the very first error we found.
const p = resolveFromList(id, errors.keys()) return Array.from(errors.values())[0][0]
if (p) return errors.get(p)[0]
} }
handleBuildHash (filename, hash, res) { handleBuildHash (filename, hash, res) {
......
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