Skip to content

Instantly share code, notes, and snippets.

@faceyspacey
Last active April 8, 2018 00:04
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/0c6e1aa59e7502ef60a0442d9e814ae4 to your computer and use it in GitHub Desktop.
Save faceyspacey/0c6e1aa59e7502ef60a0442d9e814ae4 to your computer and use it in GitHub Desktop.
import express from 'express'
import routes from './routes';
import { render, build } from 'react-universal-starter';
const app = express();
const handler = build(async (req, res, stats) => {
try {
const html = await render(routes, stats);
res.send(html);
} catch (error) {
res.json(error);
}
})
app.get('/*', handler)
// build.js
export default (callback) => {
const compiler = webpack([clientConfig, serverConfig])
const hotServerMw = webpackHotServerMiddleware(compiler)
const compressionMw = compression()
return (req, res, next) => {
compressionMw(req, res, next) // this may or may not be the correct usage, but it's the blueprint of what must be done
const stats = hotServerMw(req, res, next) // we're gonna have to modify how webpackHotServerMiddleware works
if (stats) return callback(req, res, next, stats)
throw new Error('stats not ready yet') // you get the idea -- `hotServerMw` will return the stats every time it's called after the build is completed
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment