Skip to content
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

recover from exception for server side rendering #1141

Closed
wangzuo opened this issue Dec 28, 2018 · 5 comments
Closed

recover from exception for server side rendering #1141

wangzuo opened this issue Dec 28, 2018 · 5 comments

Comments

@wangzuo
Copy link

wangzuo commented Dec 28, 2018

Here is a simple test case for the problem:

const Test = ({ x }) => {
  if (x === 1) {
    throw new Error('test');
  }
  return <p>{x}</p>;
};

const App = ({ x }) => (
  <Row>
    <Test x={x} />
  </Row>
);

try {
  const html = renderToString(<App x={2} />); // contains <style>
  renderToString(<App x={1} />); // throw error
} catch (e) {
  const html = renderToString(<App x={2} />); // no <style> tag
}

For example, reach-router handles redirect by catching a redirect error on server side. link. It seems once the rendering fails on server side, later rendering will output no inline <style> tag due to the emotion cache. Is there any way to get around this problem?

@wangzuo
Copy link
Author

wangzuo commented Dec 28, 2018

Right now recreating cache for each request fixes the issue

import { CacheProvider } from '@emotion/core';
import createCache from '@emotion/cache';

<CacheProvider value={createCache()}>
  <App />
</CacheProvider>

@wangzuo wangzuo changed the title recover from exception for ssr rendering recover from exception for server side rendering Dec 28, 2018
@emmatown
Copy link
Member

This is because of a bug in React. facebook/react#14502

@wangzuo
Copy link
Author

wangzuo commented Dec 29, 2018

@mitchellhamilton thank you for the quick feedback, just wondering if anything missing from the CacheProvider work around.

@emmatown
Copy link
Member

Nope, that’ll work fine.

@wangzuo
Copy link
Author

wangzuo commented Dec 29, 2018

Thank you, really like the new emotion 10!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants