next.js: Custom componentDidCatch does not work during SSR
Bug report
Describe the bug
It looks like React Error Boundaries are ignored during SSR, which makes it impossible to use components with componentDidCatch lifecycle methods.
To Reproduce
Steps to reproduce the behavior, please provide code snippets or a repository:
- Clone MWE from kachkaev/next-component-did-catch-mwe
yarnyarn build && yarn start(using production mode)- go to http://localhost:3000/
- click open a page that contains something broken to go to http://localhost:3000/contains-something-broken
- observe
Cannot read property '42' of undefinedtriggered fromSomethingBrokenand handled byCustomErrorBoundary(this is expected behavior) - refresh the page to render the same component tree on the server
- observe next.js 500 + an error stack in CLI instead of the same result as in step 6
Expected behavior
I expected componentDidCatch inside CustomErrorBoundary to work both on the on the server, not just on the client. It seems like it’s impossible to rely on React error boundaries until this is the case.
System information
- OS: macos
- Version of Next.js:
7.0.0-canary.8 - Version of React.js:
16.4.2
Additional context
If componentDidCatch can be fixed in production SSR it might be worth thinking about how it could be tested in dev mode too. At the moment the error message overlay shows up regardless of componentDidCatch has worked on the client-side and it’d be great if a popup could either be completely removed or state that an exception of such kind will be mitigated in production. CRA has a similar problem as well: https://github.com/facebook/create-react-app/issues/3627
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Reactions: 20
- Comments: 15 (7 by maintainers)
I think next.js should provide something similar to componentDidCatch but working on both server and client side. This can only be handled by code that calls render() method of react
Or at least make such a feature opt-in
Is it possible to give up SSR and retry rendering on client side when an error was thrown during SSR?
I also have a similar setup:
componentDidCatchinpages/_app.jsreports the event to Google Analytics andgetInitialPropsinpages/_error.jshelps style the error depending on its kind (product not found is not the same as random page not found).Here’s a scenario that does not work though: imagine we have some user-defined JSON that describes a data vis (e.g. vega or a custom one). This JSON gets passed to a React component, which tries to render an SVG. If a JSON is faulty, an error is thrown and this is where a local
componentDidCatchwould really shine. I tried throwing from a render function inside such a vis component and catching this error withcomponentDidCatchone component up, but instead the server was renderingpages/_error.js, which is not what I expected. When a page with the vis was loaded dynamically by clicking on<Link />, the localcomponentDidCatchdid work (although the page got covered with a call stack panel in dev).Warning others that local
componentDidCatchcannot be used during SSR is what I’m talking about.What is this line doing?