-
Notifications
You must be signed in to change notification settings - Fork 47k
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
componentDidCatch doesn't work in React 16's renderToString #10442
Comments
This is intentional / a known limitation. With streaming rendering it's impossible to "call back" markup that has already been sent, and we opted to keep renderToString and renderToNodeStream's output identical. @sebmarkbage has ideas about how we could potentially support something along these lines one day if we have a small client runtime (ex: outputting script tags that remove previously-sent content). |
That's intentional. The next next out-of-order server renderer will have error boundaries. But it doesn't really make sense on an in-order streaming server rendering. Since we could've already flushed the boundary. We could buffer inside error boundaries but that would mean that we don't actually do any streaming rendering since the best practice is to have an error boundary at the root. We could support this in the renderToString API but that's easy-ish to implement yourself around it so not high pri and would likely go into the work of the out-of-order renderer. |
Ah, got it. My fault! ;) Thanks, and closing this issue. |
This looks like a dirty but a much needed hack to overcome the intentional limitation: https://github.com/doochik/babel-plugin-transform-react-ssr-try-catch Would be nice to have something like that built-in. |
In your case you can use https://github.com/zekchan/react-ssr-error-boundary - it is component using renderToStaticMarkup on server for render your component |
Anyway, I solved it already by inheriting |
Workaround with try-catch'ing render method will help you only if error throw in that render method. But if error occurs deep in react tree it will not. |
It is true, that's why I'm creation all components from |
try-catch'ing the renderToString method can also be helpful at server side as mentioned in following issue #3730 |
try https://www.npmjs.com/package/react-isomorphic-error-boundary. This helps a lot. |
Thank you @abhirajambasta for react-isomorphic-error-boundary package. However there's a limitation to it (see nayaknotnull/react-isomorphic-error-boundary#6). Any update about this subject please ? |
This introduced a bug where a thrown error in the `masthead` would fail on server because of SSR limitations of `componentDidCatch`. See: facebook/react#10442 (comment) We should explore ways to get around this, but for now lets go back to the sad way. This reverts commit 306b641.
I tried out a simple test case for the new
componentDidCatch
error handling code in React 16 SSR with the following code:and the result with
16.0.0-beta.5
was:As far as I can tell, this code should not throw but should rather output
<h1>Something went wrong.</h1>
.I'm afraid that maybe we didn't add
componentDidCatch
to the new server renderer.The text was updated successfully, but these errors were encountered: