Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,19 @@ Each top-level component receives a `url` property with the following API:

```jsx
import React from 'react'
export default ({ statusCode }) => (
<p>An error { statusCode } occurred</p>
)

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

render () {
return (
<p>An error { this.props.statusCode } occurred</p>
)
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is so amazing. Nice work @impronunciable @nkzawa

```

### Production deployment
Expand Down
8 changes: 1 addition & 7 deletions lib/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 1 addition & 6 deletions server/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand Down Expand Up @@ -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 }
}