Fix #4574: getInitialProps is not called on _error page for client-side errors#4764
Merged
timneutkens merged 2 commits intovercel:canaryfrom Jul 11, 2018
Merged
Conversation
timneutkens
approved these changes
Jul 11, 2018
Member
timneutkens
left a comment
There was a problem hiding this comment.
Awesome 🙏 fix looks okay to me, great that you've added a test 👍 Thank you very much
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
getInitialPropsduring 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
getInitialPropson 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
getInitialPropswas removed from the app's top-level error handler. Specifically, this line was removed and replaced by a comment that says that "Appwill handle the calling ofgetInitialProps".I believe the sentiment about "
Appwill handle callinggetInitialProps" is mistaken. In fact, it really doesn't make sense on its face, since it would require an instance lifecycle method ofApp(which is mounted immediately after the comment) to invoke thestatic getInitialPropsmethod 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
getInitialPropscould certainly be improved. (The code in this conditional speaks to the unnecessary complexity around this.)