From f43e1a95f1e6e34bf198b76a249124a3af8abf58 Mon Sep 17 00:00:00 2001 From: Joe Haddad Date: Tue, 12 Feb 2019 21:32:25 -0500 Subject: [PATCH] Set default `Error` status code to 404 (#6276) * Set default `Error` status code to 404 This is an appropriate default behavior because: 1. When the server encounters an error, the `err` property is set. 2. When the client-side application crashes, the `err` property is set. This means the "only" way to render the `/_error` page without an error is when a page is not found (special condition). Fixes #6243 Closes #5437 * Add new integration test for client side 404 * single quotes * Remove unused variable * Standard needs to go away * Whoops * Check for null status code in res and err * Only check response for valid statusCode --- packages/next/pages/_error.js | 42 ++++++++++++------- test/integration/client-404/next.config.js | 6 +++ test/integration/client-404/pages/_error.js | 23 ++++++++++ test/integration/client-404/pages/index.js | 1 + .../client-404/test/client-navigation.js | 27 ++++++++++++ .../integration/client-404/test/index.test.js | 23 ++++++++++ 6 files changed, 106 insertions(+), 16 deletions(-) create mode 100644 test/integration/client-404/next.config.js create mode 100644 test/integration/client-404/pages/_error.js create mode 100644 test/integration/client-404/pages/index.js create mode 100644 test/integration/client-404/test/client-navigation.js create mode 100644 test/integration/client-404/test/index.test.js diff --git a/packages/next/pages/_error.js b/packages/next/pages/_error.js index 3acc6d483d1bd..5bfeda6f8f1db 100644 --- a/packages/next/pages/_error.js +++ b/packages/next/pages/_error.js @@ -7,29 +7,38 @@ export default class Error extends React.Component { static displayName = 'ErrorPage' static getInitialProps ({ res, err }) { - const statusCode = res ? res.statusCode : (err ? err.statusCode : null) + const statusCode = + res && res.statusCode ? res.statusCode : err ? err.statusCode : 404 return { statusCode } } render () { const { statusCode } = this.props - const title = statusCode === 404 - ? 'This page could not be found' - : HTTPStatus[statusCode] || 'An unexpected error has occurred' + const title = + statusCode === 404 + ? 'This page could not be found' + : HTTPStatus[statusCode] || 'An unexpected error has occurred' - return
- - - {statusCode}: {title} - -
-