Skip to content

Commit 1cefd6c

Browse files
authored
Update server.js
Make header prepending more elegant and simple.
1 parent 90411aa commit 1cefd6c

File tree

1 file changed

+15
-14
lines changed

1 file changed

+15
-14
lines changed

server.js

+15-14
Original file line numberDiff line numberDiff line change
@@ -59,21 +59,22 @@ app.get('*', (req, res) => {
5959
var s = Date.now()
6060
const context = { url: req.url }
6161
const renderStream = renderer.renderToStream(context)
62-
let firstChunk = true
63-
64-
renderStream.on('data', chunk => {
65-
if (firstChunk) {
66-
res.write(html.head)
67-
// embed initial store state
68-
if (context.initialState) {
69-
res.write(
70-
`<script>window.__INITIAL_STATE__=${
71-
serialize(context.initialState, { isJSON: true })
72-
}</script>`
73-
)
74-
}
75-
firstChunk = false
62+
63+
64+
// Write head once
65+
renderStream.once('data', () => {
66+
res.write(html.head)
67+
// embed initial store state
68+
if (context.initialState) {
69+
res.write(
70+
`<script>window.__INITIAL_STATE__=${
71+
serialize(context.initialState, { isJSON: true })
72+
}</script>`
73+
)
7674
}
75+
})
76+
77+
renderStream.on('data', chunk => {
7778
res.write(chunk)
7879
})
7980

0 commit comments

Comments
 (0)