Skip to content

Instantly share code, notes, and snippets.

@epegzz
Last active October 22, 2017 11:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save epegzz/35d858fc09848d269cf3901f20c021a0 to your computer and use it in GitHub Desktop.
Save epegzz/35d858fc09848d269cf3901f20c021a0 to your computer and use it in GitHub Desktop.
CSS inline styles, CSS Modules, ReactRouter 2
import express from 'express';
import ReactDOM from 'react-dom';
import router from './router.js';
const server = express();
// Server-side rendering of the React app
server.get('*', (req, res, next) => {
const css = new Set();
// The context.insertCss() function will be called by `withStyles`
const context = { insertCss: (...styles) =>
styles.forEach(style => css.add(style._getCss()));
};
router.dispatch({ ...req, context }).then((component, state) => {
const body = ReactDOM.renderToString(component);
const html = `<!doctype html>
<html>
<head>
<script async src="/client.js"></script>
<style type="text/css">${[...css].join('')}</style>
</head>
<body>
<div id="root">${body}</div>
</body>
</html>`;
res.status(state.statusCode).send(html);
}).catch(next);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment