Skip to content

Comments

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

Merged
timneutkens merged 2 commits intovercel:canaryfrom
jamesreggio:fix-error-getinitialprops
Jul 11, 2018
Merged

Fix #4574: getInitialProps is not called on _error page for client-side errors#4764
timneutkens merged 2 commits intovercel:canaryfrom
jamesreggio:fix-error-getinitialprops

Conversation

@jamesreggio
Copy link
Contributor

@jamesreggio jamesreggio commented Jul 11, 2018

What's wrong

This problem is specific to errors that happen on the client after the initial mounting of the component. (The router has special logic to handle exceptions thrown in getInitialProps during a client-side navigation, and I've confirmed this logic is correct.)

Specifically, if the page is mounted, and you raise an exception on the page, the exception will cause the error page to be mounted without ever invoking getInitialProps on the new App/Error page pairing.

This has been illustrated with multiple repros in #4574.

Why is it broken

This regression was introduced two months ago in #4156, where the invocation of getInitialProps was removed from the app's top-level error handler. Specifically, this line was removed and replaced by a comment that says that "App will handle the calling of getInitialProps".

I believe the sentiment about "App will handle calling getInitialProps" is mistaken. In fact, it really doesn't make sense on its face, since it would require an instance lifecycle method of App (which is mounted immediately after the comment) to invoke the static getInitialProps method on the error page.

How I fixed it

I've fixed this in a fork by restoring Lines 146 – 148 that were removed in #4156. I think this is the right fix, but Next.js's handling of getInitialProps could certainly be improved. (The code in this conditional speaks to the unnecessary complexity around this.)

Copy link
Member

@timneutkens timneutkens left a comment

Choose a reason for hiding this comment

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

Awesome 🙏 fix looks okay to me, great that you've added a test 👍 Thank you very much

@timneutkens timneutkens merged commit 4cc691c into vercel:canary Jul 11, 2018
@jamesreggio jamesreggio deleted the fix-error-getinitialprops branch July 12, 2018 16:20
@lock lock bot locked as resolved and limited conversation to collaborators Jul 12, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants