-
Notifications
You must be signed in to change notification settings - Fork 10
/
ssr.js
112 lines (94 loc) · 3 KB
/
ssr.js
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
102
103
104
105
106
107
108
109
110
111
112
import path from 'path'
import React from 'react'
import { StaticRouter } from 'react-router-dom'
import { ApolloProvider, getDataFromTree } from 'react-apollo'
import { renderToString } from 'react-dom/server'
import { ChunkExtractor } from '@loadable/server'
import asyncHandler from 'express-async-handler'
import { getContext, createApolloClient } from './apollo'
import ApolloState from './components/ApolloState'
import SmoothError from './components/SmoothError'
import { onRenderBody, wrapRootElement } from '../plugin/nodeHooks'
export default function ssrMiddleware({
config,
schema,
fragmentTypes,
error = null,
}) {
return asyncHandler(async (req, res) => {
const nodeStats = path.resolve(
config.cachePath,
'node/static/loadable-stats.json',
)
const webStats = path.resolve(
config.cachePath,
'web/static/loadable-stats.json',
)
const nodeExtractor = new ChunkExtractor({
statsFile: nodeStats,
outputPath: path.join(config.cachePath, 'node/static'),
})
const {
Root,
Html,
ErrorContextProvider,
} = nodeExtractor.requireEntrypoint()
const webExtractor = new ChunkExtractor({ statsFile: webStats })
const routerContext = {}
const apolloClient = createApolloClient({
schema,
fragmentTypes,
context: operation =>
getContext({ req, config, operationContext: operation.getContext() }),
})
let jsx = (
<ErrorContextProvider error={error}>
<ApolloProvider client={apolloClient}>
<StaticRouter location={req.url} context={routerContext}>
<Root error={error} />
</StaticRouter>
</ApolloProvider>
</ErrorContextProvider>
)
// Generate unique request id
const requestId = Math.random()
.toString(36)
.substring(7)
const rootElement = wrapRootElement(config)({
element: jsx,
pathname: req.url,
requestId,
})
// Loadable components
jsx = webExtractor.collectChunks(jsx)
await getDataFromTree(jsx)
const apolloState = apolloClient.cache.extract()
// Render app HTML
const appHtml = renderToString(rootElement)
// Handle React router status
if (routerContext.status) {
res.status(routerContext.status)
}
// Handle React Router redirection
if (routerContext.url) {
const status = routerContext.status === 301 ? 301 : 302
res.redirect(status, routerContext.url)
return
}
const headComponents = webExtractor.getStyleElements()
const postBodyComponents = [
<SmoothError key="smooth-error" error={error} />,
<ApolloState key="apollo-state" state={apolloState} />,
...webExtractor.getScriptElements(),
]
const pluginProps = onRenderBody(config)({
headComponents,
postBodyComponents,
pathname: req.url,
requestId,
})
const html = renderToString(<Html {...pluginProps} body={appHtml} />)
res.set('content-type', 'text/html')
res.end(`<!DOCTYPE html>${html}`)
})
}