Skip to content

Instantly share code, notes, and snippets.

@zrobit
Created July 31, 2017 22:29
Show Gist options
  • Select an option

  • Save zrobit/a1ae6164ad21d15110eef4961c80c625 to your computer and use it in GitHub Desktop.

Select an option

Save zrobit/a1ae6164ad21d15110eef4961c80c625 to your computer and use it in GitHub Desktop.
server side render react redux react-router
import React from 'react';
import { renderToString } from 'react-dom/server';
import { match, RouterContext } from 'react-router';
import { createStore } from 'redux';
import { Provider } from 'react-redux';
import reducer from '../client/reducers'
// import { Provider, useStaticRendering } from 'mobx-react';
import routes from '../client/routes';
import jsonStringifySafe from 'json-stringify-safe';
// import AppStore from '../client/stores/AppStore'
// import AuthStore from '../client/stores/AuthStore'
// useStaticRendering(true);
export function ssr(req, res, context, template="layout") {
match({ routes, location: req.originalUrl },
(err, redirect, props) => {
if (err){
handleError(res, err);
console.log(err)
return res.status(500).end('Internal server error');
}
else if (props) {
if(!context.state.user){context.state.user = {} }
context.state.user.isAuth = req.isAuthenticated();
if(context.state.user.isAuth){
context.state.user.name = req.user.name
context.state.user.hashId = req.user.hashId
}
// const stores = {
// appStore: AppStore.fromJS(context.state.app),
// authStore: new AuthStore(context.state.auth)
// }
let store = createStore(reducer, context.state)
context.root = renderToString(
<Provider store={store}>
<RouterContext {...props} />
</Provider>
)
context.initialStateJSON = jsonStringifySafe(store.getState())
res.render(template, context)
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment