Skip to content

Instantly share code, notes, and snippets.

@faceyspacey
Last active June 13, 2017 12:30
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 faceyspacey/180fd6e27931fad4deba2fac8e908a20 to your computer and use it in GitHub Desktop.
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
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