-
Notifications
You must be signed in to change notification settings - Fork 46
/
render.js
45 lines (40 loc) · 1.45 KB
/
render.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import React from 'react'
import ReactDOM from 'react-dom/server'
import { Provider } from 'react-redux'
import { flushChunkNames } from 'react-universal-component/server'
import flushChunks from 'webpack-flush-chunks'
import configureStore from './configureStore'
import App from '../src/components/App'
export default ({ clientStats }) => async (req, res, next) => {
const store = await configureStore(req, res)
if (!store) return // no store means redirect was already served
const app = createApp(App, store)
const appString = ReactDOM.renderToString(app)
const stateJson = JSON.stringify(store.getState())
const chunkNames = flushChunkNames()
const { js, styles, cssHash } = flushChunks(clientStats, { chunkNames })
console.log('REQUESTED PATH:', req.path)
console.log('CHUNK NAMES', chunkNames)
return res.send(
`<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>redux-first-router-demo</title>
${styles}
<link rel="stylesheet prefetch" href="http://code.ionicframework.com/ionicons/2.0.1/css/ionicons.min.css">
</head>
<body>
<script>window.REDUX_STATE = ${stateJson}</script>
<div id="root">${appString}</div>
${cssHash}
<script type='text/javascript' src='/static/vendor.js'></script>
${js}
</body>
</html>`
)
}
const createApp = (App, store) =>
<Provider store={store}>
<App />
</Provider>