Last active
September 18, 2017 22:29
-
-
Save whilelucky/f4d08e0391e015ee295a53ff3ccabafe to your computer and use it in GitHub Desktop.
server-side-rendering
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const serverRenderedHtml = async (req, res, renderProps) => { | |
const store = configureStore(); | |
//call, wait, and set api responses into redux store's state (ghub.io/redux-connect) | |
await loadOnServer({ ...renderProps, store }); | |
//render the html template | |
const template = html( | |
renderToString( | |
<Provider store={store} key="provider"> | |
<ReduxAsyncConnect {...renderProps} /> | |
</Provider>, | |
), | |
store.getState(), | |
); | |
res.send(template); | |
}; | |
const html = (app, initialState) => ` | |
<!doctype html> | |
<html lang="en"> | |
<head> | |
<link rel="stylesheet" href="${assets.main.css}"> | |
</head> | |
<body> | |
<div id="root">${app}</div> | |
<script>window.__INITIAL_STATE__ = ${JSON.stringify(initialState)}</script> | |
<script src="${assets.main.js}"></script> | |
</body> | |
</html> | |
`; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment