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): move server push code to own file
Browse files Browse the repository at this point in the history
feat(server/ssr): move server push code to own file and comment it
  • Loading branch information
Metnew committed Sep 13, 2017
1 parent 4080878 commit 37b7e2e
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 47 deletions.
50 changes: 3 additions & 47 deletions src/server/ssr/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
* @desc
*/
import React from 'react'
import {readFileSync, readFile} from 'fs'
import {readFileSync} from 'fs' // readFile
import path from 'path'
import _ from 'lodash'
// import _ from 'lodash'
import {sync as globSync} from 'glob'
import {renderToString} from 'react-dom/server'
import {ServerStyleSheet, StyleSheetManager} from 'styled-components'
Expand All @@ -14,7 +14,6 @@ import {configureRootComponent, configureApp} from 'common/app'
import HtmlComponent from './HtmlComponent'
import assets from 'webpack-assets'
import faviconsAssets from 'favicons-assets'

import type {Node} from 'react'

const translations = globSync('locals/*.json')
Expand All @@ -33,7 +32,7 @@ export default (req: express$Request, res: express$Response) => {
const initialState: Object = isLoggedIn
? {me: {auth: {isLoggedIn, token}}}
: {}
const localeData = require('react-intl/locale-data/' + lang)
const localeData = require('react-intl/locale-data/' + (lang || 'en'))
const i18n = {
lang,
localeData,
Expand Down Expand Up @@ -68,49 +67,6 @@ export default (req: express$Request, res: express$Response) => {
App
}

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)
}
42 changes: 42 additions & 0 deletions src/server/ssr/server_push.wip.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// 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)
// })
// })

0 comments on commit 37b7e2e

Please sign in to comment.