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:

  1. Clone MWE from kachkaev/next-component-did-catch-mwe
  2. yarn
  3. yarn build && yarn start (using production mode)
  4. go to http://localhost:3000/
  5. click open a page that contains something broken to go to http://localhost:3000/contains-something-broken
  6. observe Cannot read property '42' of undefined triggered from SomethingBroken and handled by CustomErrorBoundary (this is expected behavior)
  7. refresh the page to render the same component tree on the server
  8. 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)

Most upvoted comments

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

Is it possible to give up SSR and retry rendering on client side when an error was thrown during SSR?

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: componentDidCatch in pages/_app.js reports the event to Google Analytics and getInitialProps in pages/_error.js helps 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 componentDidCatch would really shine. I tried throwing from a render function inside such a vis component and catching this error with componentDidCatch one component up, but instead the server was rendering pages/_error.js, which is not what I expected. When a page with the vis was loaded dynamically by clicking on <Link />, the local componentDidCatch did work (although the page got covered with a call stack panel in dev).

Warning others that local componentDidCatch cannot be used during SSR is what I’m talking about.

What is this line doing?

super.componentDidCatch(error, errorInfo)