From 1a416b688ec90fe657512af059aedae16b71b2e4 Mon Sep 17 00:00:00 2001 From: Sultan Tarimo Date: Thu, 31 Jan 2019 23:45:36 +0300 Subject: [PATCH] Load ErrorComponent dynamically (#6171) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Closes #6152. - Only dynamically load /_error.js when an exception occurs. - Fix websocket “/_error.js” ping oddity. --- packages/next-server/lib/router/router.js | 7 +++---- packages/next/client/index.js | 4 ++-- packages/next/pages/_document.js | 2 -- test/integration/production/test/index.test.js | 2 +- 4 files changed, 6 insertions(+), 9 deletions(-) diff --git a/packages/next-server/lib/router/router.js b/packages/next-server/lib/router/router.js index 54d06d01..9d625470 100644 --- a/packages/next-server/lib/router/router.js +++ b/packages/next-server/lib/router/router.js @@ -8,7 +8,7 @@ import { loadGetInitialProps, getURL } from '../utils' export default class Router { static events = mitt() - constructor (pathname, query, as, { initialProps, pageLoader, App, Component, ErrorComponent, err } = {}) { + constructor (pathname, query, as, { initialProps, pageLoader, App, Component, err } = {}) { // represents the current component key this.route = toRoute(pathname) @@ -17,7 +17,7 @@ export default class Router { // We should not keep the cache, if there's an error // Otherwise, this cause issues when when going back and // come again to the errored page. - if (Component !== ErrorComponent) { + if (pathname !== '/_error') { this.components[this.route] = { Component, props: initialProps, err } } @@ -28,7 +28,6 @@ export default class Router { this.events = Router.events this.pageLoader = pageLoader - this.ErrorComponent = ErrorComponent this.pathname = pathname this.query = query this.asPath = as @@ -291,7 +290,7 @@ export default class Router { return { error: err } } - const Component = this.ErrorComponent + const Component = await this.fetchComponent('/_error') routeInfo = { Component, err } const ctx = { err, pathname, query } try { diff --git a/packages/next/client/index.js b/packages/next/client/index.js index c6ed446f..4b013fc9 100644 --- a/packages/next/client/index.js +++ b/packages/next/client/index.js @@ -73,7 +73,6 @@ export default async ({ if (process.env.NODE_ENV === 'development') { webpackHMR = passedWebpackHMR } - ErrorComponent = await pageLoader.loadPage('/_error') App = await pageLoader.loadPage('/_app') let initialErr = err @@ -99,7 +98,6 @@ export default async ({ pageLoader, App, Component, - ErrorComponent, err: initialErr }) @@ -138,6 +136,8 @@ export async function renderError (props) { // Make sure we log the error to the console, otherwise users can't track down issues. console.error(err) + ErrorComponent = await pageLoader.loadPage('/_error') + // In production we do a normal render with the `ErrorComponent` as component. // If we've gotten here upon initial render, we can use the props from the server. // Otherwise, we need to call `getInitialProps` on `App` before mounting. diff --git a/packages/next/pages/_document.js b/packages/next/pages/_document.js index 5d653d5d..e3fa8ea6 100644 --- a/packages/next/pages/_document.js +++ b/packages/next/pages/_document.js @@ -125,7 +125,6 @@ export class Head extends Component { {head} {page !== '/_error' && } - {this.getPreloadDynamicChunks()} {this.getPreloadMainLinks()} {this.getCssLinks()} @@ -221,7 +220,6 @@ export class NextScript extends Component { }} />} {page !== '/_error' &&