-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathserver-render-entrypoint.js
More file actions
101 lines (91 loc) · 3.57 KB
/
server-render-entrypoint.js
File metadata and controls
101 lines (91 loc) · 3.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import 'babel-polyfill'
import 'isomorphic-fetch'
import path from 'path'
import fs from 'fs'
import get from 'lodash/get'
import { renderToString } from 'react-dom/server'
import React from 'react'
import Helmet from 'react-helmet'
import Honeybadger from 'honeybadger'
import { createMemoryHistory, match, RouterContext } from 'react-router'
import { syncHistoryWithStore } from 'react-router-redux'
import { Provider } from 'react-redux'
import { updateStrings as updateTimeAgoStrings } from './src/lib/time_ago_in_words'
import { createElloStore } from './src/store'
import createRoutes from './src/routes'
import { serverRoot } from './src/sagas'
const indexStr = fs.readFileSync(path.join(__dirname, './public/index.html'), 'utf-8')
// Return promises for initial loads
function preRender(renderProps, store, sagaTask) {
const promises = renderProps.components.map(component => ((component && component.preRender) ? component.preRender(store, renderProps) : null)).filter(component => !!component)
return Promise.all(promises).then(() => {
store.close()
return sagaTask.done
})
}
function handlePrerender(context) {
const { access_token, originalUrl, url } = context
console.log(`Spun up child process ${process.pid} to render ${url} isomorphically`)
const memoryHistory = createMemoryHistory(originalUrl)
const store = createElloStore(memoryHistory, {
authentication: {
accessToken: access_token,
isLoggedIn: false,
},
})
const isServer = true
const routes = createRoutes(store, isServer)
const history = syncHistoryWithStore(memoryHistory, store)
const sagaTask = store.runSaga(serverRoot)
match({ history, routes, location: url }, (error, redirectLocation, renderProps) => {
// populate the router store object for initial render
if (error) {
console.log('ELLO MATCH ERROR', error)
} else if (redirectLocation) {
console.log('ELLO HANDLE REDIRECT', redirectLocation)
process.send({ type: 'redirect', location: redirectLocation.pathname }, null, {}, () => {
process.exit(0)
})
} else if (!renderProps) {
console.log('NO RENDER PROPS')
process.exit(1)
return
}
const InitialComponent = (
<Provider store={store}>
<RouterContext {...renderProps} />
</Provider>
)
preRender(renderProps, store, sagaTask).then(() => {
const componentHTML = renderToString(InitialComponent)
const head = Helmet.rewind()
const state = store.getState()
if (get(state, 'stream.should404') === true) {
process.send({ type: '404' }, null, {}, () => {
process.exit(1)
})
} else {
const initialStateTag = `<script id="initial-state">window.__INITIAL_STATE__ = ${JSON.stringify(state)}</script>`
// Add helmet's stuff after the last statically rendered meta tag
const html = indexStr.replace(
'rel="copyright">',
`rel="copyright">${head.title.toString()} ${head.meta.toString()} ${head.link.toString()}`
).replace('<div id="root"></div>', `<div id="root">${componentHTML}</div>${initialStateTag}`)
process.send({ type: 'render', body: html }, null, {}, () => {
process.exit(0)
})
}
}).catch((err) => {
// this will give you a js error like:
// `window is not defined`
console.log('ELLO CATCH ERROR', err)
Honeybadger.notify(err);
process.send({ type: 'error' }, null, {}, () => {
process.exit(1)
})
})
renderToString(InitialComponent)
})
}
process.on('message', handlePrerender)
export default handlePrerender