Last active
October 22, 2017 11:28
-
-
Save dsc8x/35d858fc09848d269cf3901f20c021a0 to your computer and use it in GitHub Desktop.
CSS inline styles, CSS Modules, ReactRouter 2
This file contains 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
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