Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 15 additions & 14 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,21 +59,22 @@ app.get('*', (req, res) => {
var s = Date.now()
const context = { url: req.url }
const renderStream = renderer.renderToStream(context)
let firstChunk = true

renderStream.on('data', chunk => {
if (firstChunk) {
res.write(html.head)
// embed initial store state
if (context.initialState) {
res.write(
`<script>window.__INITIAL_STATE__=${
serialize(context.initialState, { isJSON: true })
}</script>`
)
}
firstChunk = false


// Write head once
renderStream.once('data', () => {
res.write(html.head)
// embed initial store state
if (context.initialState) {
res.write(
`<script>window.__INITIAL_STATE__=${
serialize(context.initialState, { isJSON: true })
}</script>`
)
}
})

renderStream.on('data', chunk => {
res.write(chunk)
})

Expand Down