/
static-entry.js
204 lines (176 loc) · 5.06 KB
/
static-entry.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import React from "react"
import { renderToString, renderToStaticMarkup } from "react-dom/server"
import { StaticRouter, Route, withRouter } from "react-router-dom"
import { kebabCase, get, merge, isArray, isString } from "lodash"
import apiRunner from "./api-runner-ssr"
import pages from "./pages.json"
import syncRequires from "./sync-requires"
let Html
try {
Html = require(`../src/html`)
} catch (e) {
Html = require(`./default-html`)
}
const pathChunkName = path => {
const name = path === `/` ? `index` : kebabCase(path)
return `path---${name}`
}
const $ = React.createElement
// Use default layout if one isn't set.
let layout
if (syncRequires.layouts.index) {
layout = syncRequires.layouts.index
} else {
layout = props =>
<div>
{props.children()}
</div>
}
module.exports = (locals, callback) => {
let pathPrefix = `/`
if (__PREFIX_PATHS__) {
pathPrefix = `${__PATH_PREFIX__}/`
}
let bodyHTML = ``
let headComponents = []
let preBodyComponents = []
let postBodyComponents = []
let bodyProps = {}
const replaceBodyHTMLString = body => {
bodyHTML = body
}
const setHeadComponents = components => {
headComponents = headComponents.concat(components)
}
const setPreBodyComponents = components => {
preBodyComponents = preBodyComponents.concat(components)
}
const setPostBodyComponents = components => {
postBodyComponents = postBodyComponents.concat(components)
}
const setBodyProps = props => {
bodyProps = merge({}, bodyProps, props)
}
const bodyComponent = $(
StaticRouter,
{
location: {
pathname: locals.path,
},
context: {},
},
$(withRouter(layout), {
children: layoutProps =>
$(Route, {
children: routeProps => {
const props = layoutProps ? layoutProps : routeProps
const page = pages.find(
page => page.path === props.location.pathname
)
return $(syncRequires.components[page.componentChunkName], {
...props,
...syncRequires.json[page.jsonName],
})
},
}),
})
)
// Let the site or plugin render the page component.
apiRunner(`replaceRenderer`, {
bodyComponent,
replaceBodyHTMLString,
setHeadComponents,
setPreBodyComponents,
setPostBodyComponents,
setBodyProps,
})
// If no one stepped up, we'll handle it.
if (!bodyHTML) {
bodyHTML = renderToString(bodyComponent)
}
apiRunner(`onRenderBody`, {
setHeadComponents,
setPreBodyComponents,
setPostBodyComponents,
setBodyProps,
pathname: locals.path,
})
// Add the chunk-manifest as a head component.
const chunkManifest = require(`!raw!../public/chunk-manifest.json`)
headComponents.unshift(
<script
id="webpack-manifest"
key="webpack-manifest"
dangerouslySetInnerHTML={{
__html: `
//<![CDATA[
window.webpackManifest = ${chunkManifest}
//]]>
`,
}}
/>
)
let stats
try {
stats = require(`../public/stats.json`)
} catch (e) {
// ignore
}
// Create paths to scripts
const scripts = [
`commons`,
`app`,
`layout-component---index`,
pathChunkName(locals.path),
pages.find(page => page.path === locals.path).componentChunkName,
]
.map(s => {
const fetchKey = `assetsByChunkName[${s}]`
let fetchedScript = get(stats, fetchKey)
if (!fetchedScript) {
return null
}
// If sourcemaps are enabled, then the entry will be an array with
// the script name as the first entry.
fetchedScript = isArray(fetchedScript) ? fetchedScript[0] : fetchedScript
const prefixedScript = `${pathPrefix}${fetchedScript}`
// Make sure we found a component.
if (prefixedScript === `/`) {
return null
}
return prefixedScript
})
.filter(s => isString(s))
scripts.forEach(script => {
// Add preload <link>s for scripts.
headComponents.unshift(
<link rel="preload" key={script} href={script} as="script" />
)
})
// Add script loader for page scripts to the head.
// Taken from https://www.html5rocks.com/en/tutorials/speed/script-loading/
const scriptsString = scripts.map(s => `"${s}"`).join(`,`)
headComponents.push(
<script
key={`script-loader`}
dangerouslySetInnerHTML={{
__html: `
!function(e,t,r){function n(){for(;d[0]&&"loaded"==d[0][f];)c=d.shift(),c[o]=!i.parentNode.insertBefore(c,i)}for(var s,a,c,d=[],i=e.scripts[0],o="onreadystatechange",f="readyState";s=r.shift();)a=e.createElement(t),"async"in i?(a.async=!1,e.head.appendChild(a)):i[f]?(d.push(a),a[o]=n):e.write("<"+t+' src="'+s+'" defer></'+t+">"),a.src=s}(document,"script",[
${scriptsString}
])
`,
}}
/>
)
const html = `<!DOCTYPE html>\n ${renderToStaticMarkup(
<Html
{...bodyProps}
headComponents={headComponents}
preBodyComponents={preBodyComponents}
postBodyComponents={postBodyComponents}
body={bodyHTML}
path={locals.path}
/>
)}`
callback(null, html)
}