diff --git a/Readme.md b/Readme.md index b420f89364..5b9fd139a5 100644 --- a/Readme.md +++ b/Readme.md @@ -145,9 +145,19 @@ Each top-level component receives a `url` property with the following API: ```jsx import React from 'react' -export default ({ statusCode }) => ( -

An error { statusCode } occurred

-) + +export default class Error extends React.Component { + static getInitialProps ({ res, xhr }) { + const statusCode = res ? res.statusCode : xhr.status + return { statusCode } + } + + render () { + return ( +

An error { this.props.statusCode } occurred

+ ) + } +} ``` ### Production deployment diff --git a/lib/router.js b/lib/router.js index 57e78be942..6e8a95348f 100644 --- a/lib/router.js +++ b/lib/router.js @@ -149,13 +149,7 @@ export default class Router { const cancel = () => { cancelled = true } this.componentLoadCancel = cancel - let props = {} - const status = ctx.xhr.status - if (status === 404 || status === 500) { - props = { statusCode: ctx.xhr.status } - } else { - props = await (Component.getInitialProps ? Component.getInitialProps(ctx) : {}) - } + const props = await (Component.getInitialProps ? Component.getInitialProps(ctx) : {}) if (cancel === this.componentLoadCancel) { this.componentLoadCancel = null diff --git a/server/render.js b/server/render.js index f7a020af14..670fc5508e 100644 --- a/server/render.js +++ b/server/render.js @@ -21,8 +21,7 @@ export async function render (url, ctx = {}, { const mod = require(p) const Component = mod.default || mod - const { err, res } = ctx - const props = ctx.err ? getErrorProps(ctx, dev) : await (Component.getInitialProps ? Component.getInitialProps(ctx) : {}) + const props = await (Component.getInitialProps ? Component.getInitialProps(ctx) : {}) const component = await read(resolve(dir, '.next', '_bundles', 'pages', path)) const { html, css } = StyleSheetServer.renderStatic(() => { @@ -63,7 +62,3 @@ export async function renderJSON (url, { dir = process.cwd() } = {}) { function getPath (url) { return parse(url || '/').pathname.slice(1).replace(/\.json$/, '') } - -function getErrorProps (ctx, dev) { - return { statusCode: ctx.res.statusCode, stacktrace: dev ? ctx.err.message : undefined } -}