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

SSR support #78

Open
sammdec opened this issue Feb 27, 2018 · 3 comments
Open

SSR support #78

sammdec opened this issue Feb 27, 2018 · 3 comments

Comments

@sammdec
Copy link

sammdec commented Feb 27, 2018

This might be out of the scope of the project but is it possible to do server side rendering/ rehydration with unistore?

@developit
Copy link
Owner

Not out-of-scope at all! I don't know that it'd end up being in the Unistore repo, but certainly I'd love to show an example of how to do it.

Here's roughly how it looks:

function serverRender() {
  const store = createStore();
  let html = renderToString(<App store={store} />);
  const data = store.getState();
  html = html.replace(/(<body(\s.?)?>)/, '$1<script>window.DATA='+JSON.stringify(data)+'</script>');
  return html;
}

function clientRender() {
  const store = createStore(window.DATA);
  render(<App store={store} />, document.body);
}

@stereobooster
Copy link

use devalue instead of JSON.stringify to prevent XSS

'$1<script>window.DATA='+devalue(data)+'</script>'

@developit
Copy link
Owner

That is a fantastic suggestion @stereobooster.

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

3 participants