-
Notifications
You must be signed in to change notification settings - Fork 0
/
render.js
74 lines (63 loc) · 1.75 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import path from 'path'
import React from 'react'
import ReactDOM from 'react-dom/server'
import { flushModuleIds } from 'react-universal-component/server'
import flushChunks from 'webpack-flush-chunks'
import App from '../src/components/App'
export default ({ clientStats, outputPath }) => (req, res, next) => {
const app = ReactDOM.renderToString(<App />)
const moduleIds = flushModuleIds()
const {
// react components:
Js,
Styles, // external stylesheets
Css, // raw css
// strings:
js,
styles, // external stylesheets
css, // raw css
// arrays of file names (not including publicPath):
scripts,
stylesheets,
publicPath
} = flushChunks(clientStats, {
moduleIds,
before: ['bootstrap'],
after: ['main'],
rootDir: path.join(__dirname, '..'),
// only needed if serving css rather than an external stylesheet
// note: during development css still serves as a stylesheet
outputPath
})
console.log('PATH', req.path)
console.log('SERVED SCRIPTS', scripts)
console.log('SERVED STYLESHEETS', stylesheets)
res.send(
`<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>react-universal-component-boilerplate</title>
${css}
</head>
<body>
<div id="root">${app}</div>
${js}
</body>
</html>`
)
// COMMENT the above `res.send` call
// and UNCOMMENT below to test rendering React components:
// const html = ReactDOM.renderToStaticMarkup(
// <html>
// <head>
// <Css />
// </head>
// <body>
// <div id="root" dangerouslySetInnerHTML={{ __html: app }} />
// <Js />
// </body>
// </html>
// )
// res.send(`<!DOCTYPE html>${html}`)
}