Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #4574: getInitialProps is not called on _error page for client-side errors #4764

Merged
merged 2 commits into from
Jul 11, 2018
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
11 changes: 8 additions & 3 deletions client/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ export async function render (props) {
// 404 and 500 errors are special kind of errors
// and they are still handle via the main render method.
export async function renderError (props) {
const {err, errorInfo} = props
const {App, err, errorInfo} = props

// In development we apply sourcemaps to the error
if (process.env.NODE_ENV !== 'production') {
Expand All @@ -162,8 +162,13 @@ export async function renderError (props) {
}

// In production we do a normal render with the `ErrorComponent` as component.
// `App` will handle the calling of `getInitialProps`, which will include the `err` on the context
await doRender({...props, err, Component: ErrorComponent})
// 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.
const initProps = props.props
? props.props
: await loadGetInitialProps(App, {Component: ErrorComponent, router, ctx: {err, pathname, query, asPath}})

await doRender({...props, err, Component: ErrorComponent, props: initProps})
}

async function doRender ({ App, Component, props, hash, err, emitter: emitterProp = emitter }) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import React from 'react'
export default class ErrorInRenderPage extends React.Component {
render () {
if (typeof window !== 'undefined') {
const error = new Error('An Expected error occured')
// This will be extracted by getInitialProps in the _error page,
// which will result in a different error message being rendered.
error.statusCode = 404
throw error
}
return <div />
}
}
7 changes: 7 additions & 0 deletions test/integration/production/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,13 @@ describe('Production Usage', () => {
const text = await browser.elementByCss('body').text()
expect(text).toMatch(/An unexpected error has occurred\./)
})

it('should call getInitialProps on _error page during a client side component error', async () => {
const browser = await webdriver(appPort, '/error-in-browser-render-status-code')
await waitFor(2000)
const text = await browser.elementByCss('body').text()
expect(text).toMatch(/This page could not be found\./)
})
})

describe('Misc', () => {
Expand Down