Reverting default props for errors. (#12)
This commit is contained in:
parent
ea61fe412a
commit
7a331084dc
3 changed files with 15 additions and 16 deletions
16
Readme.md
16
Readme.md
|
@ -145,9 +145,19 @@ Each top-level component receives a `url` property with the following API:
|
||||||
|
|
||||||
```jsx
|
```jsx
|
||||||
import React from 'react'
|
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>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
### Production deployment
|
### Production deployment
|
||||||
|
|
|
@ -149,13 +149,7 @@ export default class Router {
|
||||||
const cancel = () => { cancelled = true }
|
const cancel = () => { cancelled = true }
|
||||||
this.componentLoadCancel = cancel
|
this.componentLoadCancel = cancel
|
||||||
|
|
||||||
let props = {}
|
const props = await (Component.getInitialProps ? Component.getInitialProps(ctx) : {})
|
||||||
const status = ctx.xhr.status
|
|
||||||
if (status === 404 || status === 500) {
|
|
||||||
props = { statusCode: ctx.xhr.status }
|
|
||||||
} else {
|
|
||||||
props = await (Component.getInitialProps ? Component.getInitialProps(ctx) : {})
|
|
||||||
}
|
|
||||||
|
|
||||||
if (cancel === this.componentLoadCancel) {
|
if (cancel === this.componentLoadCancel) {
|
||||||
this.componentLoadCancel = null
|
this.componentLoadCancel = null
|
||||||
|
|
|
@ -21,8 +21,7 @@ export async function render (url, ctx = {}, {
|
||||||
const mod = require(p)
|
const mod = require(p)
|
||||||
const Component = mod.default || mod
|
const Component = mod.default || mod
|
||||||
|
|
||||||
const { err, res } = ctx
|
const props = await (Component.getInitialProps ? Component.getInitialProps(ctx) : {})
|
||||||
const props = ctx.err ? getErrorProps(ctx, dev) : await (Component.getInitialProps ? Component.getInitialProps(ctx) : {})
|
|
||||||
const component = await read(resolve(dir, '.next', '_bundles', 'pages', path))
|
const component = await read(resolve(dir, '.next', '_bundles', 'pages', path))
|
||||||
|
|
||||||
const { html, css } = StyleSheetServer.renderStatic(() => {
|
const { html, css } = StyleSheetServer.renderStatic(() => {
|
||||||
|
@ -63,7 +62,3 @@ export async function renderJSON (url, { dir = process.cwd() } = {}) {
|
||||||
function getPath (url) {
|
function getPath (url) {
|
||||||
return parse(url || '/').pathname.slice(1).replace(/\.json$/, '')
|
return parse(url || '/').pathname.slice(1).replace(/\.json$/, '')
|
||||||
}
|
}
|
||||||
|
|
||||||
function getErrorProps (ctx, dev) {
|
|
||||||
return { statusCode: ctx.res.statusCode, stacktrace: dev ? ctx.err.message : undefined }
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in a new issue