Skip to content
This repository has been archived by the owner on Jan 1, 2024. It is now read-only.

Commit

Permalink
feat(server/ssr): React 16 streaming
Browse files Browse the repository at this point in the history
feat(server/ssr): React 16 streaming
  • Loading branch information
Metnew committed Sep 28, 2017
1 parent c73a8bb commit ed419d1
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions src/server/ssr/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import React from 'react'
import chalk from 'chalk'
// import _ from 'lodash'
import {render as renderToString} from 'rapscallion'
import {renderToNodeStream, renderToStaticNodeStream} from 'react-dom/server'
import {ServerStyleSheet, StyleSheetManager} from 'styled-components'
import {configureRootComponent, configureApp} from 'common/app'
// import {addLocaleData} from 'react-intl'
Expand Down Expand Up @@ -37,25 +37,31 @@ export default async (req: express$Request, res: express$Response) => {
i18n,
SSR: {location, context}
})
const App: string = await renderToString(
const stream = renderToNodeStream(
<StyleSheetManager sheet={sheet.instance}>
{RootComponent}
</StyleSheetManager>
).toPromise()
)

const css: string = sheet.getStyleTags()
const preloadedState: Object = store.getState()
const props = {
css,
assets,
faviconsAssets,
initialState: preloadedState,
i18n,
App
i18n
}
const {beforeAppTag, afterAppTag} = HtmlComponent(props)

res.writeHead(200, {
'Content-Type': 'text/html'
})
res.write(beforeAppTag)
stream.pipe(res, {end: false})

HtmlComponent(props).toStream().pipe(res)
stream.on('end', () => {
res.write(afterAppTag)
res.end()
})
}

0 comments on commit ed419d1

Please sign in to comment.