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

Commit

Permalink
feat(server): rename HtmlComponent, remove unused vars
Browse files Browse the repository at this point in the history
feat(server): rename HtmlComponent, remove unused vars
  • Loading branch information
Metnew committed Sep 13, 2017
1 parent 89e6964 commit e4116c2
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 8 deletions.
File renamed without changes.
70 changes: 62 additions & 8 deletions src/server/ssr/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,20 @@
* @desc
*/
import React from 'react'
import {readFileSync} from 'fs'
import {readFileSync, readFile} from 'fs'
import path from 'path'
import _ from 'lodash'
import {sync as globSync} from 'glob'
import {renderToString} from 'react-dom/server'
import Helmet from 'react-helmet'
import createHistory from 'history/createMemoryHistory'
import {ServerStyleSheet, StyleSheetManager} from 'styled-components'
import {configureRootComponent, configureApp} from 'common/app'
import IndexHTMLComponent from './IndexHTMLComponent'
import type {Node} from 'react'
import {addLocaleData} from 'react-intl'
import HtmlComponent from './HtmlComponent'
import assets from 'webpack-assets'
import faviconsAssets from 'favicons-assets'

import type {Node} from 'react'

const translations = globSync('locals/*.json')
.map(filename => [
path.basename(filename, '.json'),
Expand All @@ -27,11 +28,19 @@ const translations = globSync('locals/*.json')
return collection
}, {})

export default (req: Object, res: Object, next: () => void) => {
export default (req: express$Request, res: express$Response) => {
const {isLoggedIn, token, lang} = req.user
const initialState: Object = isLoggedIn
? {me: {auth: {isLoggedIn, token}}}
: {}
const localeData = require('react-intl/locale-data/' + lang)
const i18n = {
lang,
localeData,
locale: lang,
messages: translations[lang]
}
//
const sheet = new ServerStyleSheet()
const location: string = req.url
const context = {}
Expand All @@ -40,6 +49,7 @@ export default (req: Object, res: Object, next: () => void) => {
store,
history,
routes,
i18n,
SSR: {location, context}
})
const App: string = renderToString(
Expand All @@ -54,9 +64,53 @@ export default (req: Object, res: Object, next: () => void) => {
assets,
faviconsAssets,
initialState: preloadedState,
translation: translations[lang],
i18n,
App
}
const html: string = IndexHTMLComponent(props)

Object.keys(assets).map(key => {
const bundle = assets[key]
Object.keys(bundle).map(async filetype => {
const filename = bundle[filetype]
const isJS = filetype === 'js'
const isCSS = filetype === 'css'

var stream = res.push(filename, {
status: 200, // optional
method: 'GET', // optional
request: {
accept: '*/*'
},
response: {
'content-type': isJS
? 'application/javascript'
: isCSS ? 'text/css' : 'text/html'
}
})

const readFileAsync = path => {
return new Promise((resolve, reject) => {
try {
readFile(path, (err, data) => {
if (err) {
reject(err)
}
resolve(data)
})
} catch (e) {
reject(e)
}
})
}

const memoizedReadFileAsync = _.memoize(readFileAsync)
const dataToStream = await memoizedReadFileAsync(
`${process.env.CLIENT_DIST_PATH}/${filename}`
)
stream.end(dataToStream)
})
})

const html: string = HtmlComponent(props)
res.send(html)
}

0 comments on commit e4116c2

Please sign in to comment.