Last active
June 13, 2017 12:30
-
-
Save faceyspacey/180fd6e27931fad4deba2fac8e908a20 to your computer and use it in GitHub Desktop.
how to use Webpack magic comments to flush chunks with React Universal Component and Webpack Flush Chunks
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
export default webpackStats => (req, res) => { | |
const appString = ReactDOM.renderToString(<App />) | |
const chunkNames = flushChunkNames() | |
const assets = webpackStats.assetsByChunkName | |
const filesForChunk = chunk => assets[chunk] | |
const files = flatten(chunkNames.map(filesForChunk)) | |
const scripts = files.filter(f => f.endsWith('.js')) | |
const stylesheets = files.filter(f => f.endsWith('.css')) | |
const base = webpackStats.publicPath | |
res.send( | |
`<!doctype html> | |
<html> | |
<head> | |
${stylesheets | |
.map(f => `<link rel='stylesheet' href='${base}/${f}' />`) | |
.join('\n') | |
} | |
</head> | |
<body> | |
<div id="root">${appString}</div> | |
${scripts | |
.map(f => `<script src='${base}/${f}'></script>`) | |
.join('\n') | |
} | |
</body> | |
</html>` | |
) | |
} | |
const flatten = (array) => [].concat(...array) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment