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

Commit

Permalink
feat(ssr): renderToString instead of renderToStream
Browse files Browse the repository at this point in the history
feat(ssr): renderToString instead of renderToStream
  • Loading branch information
Metnew committed Feb 18, 2018
1 parent ba552af commit a7a7843
Showing 1 changed file with 12 additions and 26 deletions.
38 changes: 12 additions & 26 deletions src/server/ssr/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,29 @@
*/
import React from 'react'
import _ from 'lodash'
import {renderToNodeStream} from 'react-dom/server'
import {renderToString} from 'react-dom/server'
import {ServerStyleSheet, StyleSheetManager} from 'styled-components'
import {configureRootComponent, configureApp} from 'common/app'
import asyncBootstrapper from 'react-async-bootstrapper'
import {AsyncComponentProvider, createAsyncContext} from 'react-async-component'
import HTMLComponent from './HTMLComponent'
import getI18nData from 'server/i18n'
import {matchPath} from 'react-router'
import getStats from './stats'
import {getRouterRoutes} from 'routing'
import getAssets from './stats'

export default async (req: express$Request, res: express$Response) => {
// probably, it'd better to define these objs in global scope
const {assets, faviconsAssets} = await getStats()
const {isLoggedIn, language} = req.user
const meState = {auth: {isLoggedIn}}
const initialState: Object = {me: meState}
const assets = await getAssets()
const {language} = req.user
const initialState: Object = {}
const i18n = getI18nData(language)
const sheet = new ServerStyleSheet()
const location: string = req.url
const context = {}
const {store, history, routes} = configureApp(initialState)
const {store, history} = configureApp(initialState)
const RootComponent: React$Node = configureRootComponent({
store,
history,
routes,
i18n,
SSR: {location, context}
})
Expand All @@ -42,37 +40,25 @@ export default async (req: express$Request, res: express$Response) => {
</AsyncComponentProvider>
)

const routes = getRouterRoutes()
// if true - > throw 404, if match found -> 200
const noRequestURLMatch = !_.find(routes, a => matchPath(req.url, a.path))

asyncBootstrapper(app).then(() => {
const appStream = renderToNodeStream(app)
const renderedApp = renderToString(app)
const css: string = sheet.getStyleTags()
const preloadedState: Object = store.getState()
const responseStatusCode = noRequestURLMatch ? 404 : 200
const asyncState = asyncContext.getState()
const props = {
css,
assets,
faviconsAssets,
asyncState,
initialState: preloadedState,
app: renderedApp,
i18n
}

const {beforeAppTag, afterAppTag} = HTMLComponent(props)
const responseStatusCode = noRequestURLMatch ? 404 : 200

res.writeHead(responseStatusCode, {
'Content-Type': 'text/html'
})
res.write(beforeAppTag)
res.write(`<div id="app">`)
appStream.pipe(res, {end: false})

appStream.on('end', () => {
res.write('</div>')
res.write(afterAppTag)
res.end()
})
res.status(responseStatusCode).send(HTMLComponent(props))
})
}

0 comments on commit a7a7843

Please sign in to comment.