-
Notifications
You must be signed in to change notification settings - Fork 10.3k
/
production-app.js
324 lines (286 loc) · 9.94 KB
/
production-app.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
import { apiRunner, apiRunnerAsync } from "./api-runner-browser"
import React from "react"
import { Router, navigate, Location, BaseContext } from "@gatsbyjs/reach-router"
import { ScrollContext } from "gatsby-react-router-scroll"
import { StaticQueryContext } from "./static-query"
import {
SlicesMapContext,
SlicesContext,
SlicesResultsContext,
} from "./slice/context"
import {
shouldUpdateScroll,
init as navigationInit,
RouteUpdates,
} from "./navigation"
import emitter from "./emitter"
import PageRenderer from "./page-renderer"
import asyncRequires from "$virtual/async-requires"
import {
setLoader,
ProdLoader,
publicLoader,
PageResourceStatus,
getStaticQueryResults,
getSliceResults,
} from "./loader"
import EnsureResources from "./ensure-resources"
import stripPrefix from "./strip-prefix"
// Generated during bootstrap
import matchPaths from "$virtual/match-paths.json"
import { reactDOMUtils } from "./react-dom-utils"
const loader = new ProdLoader(asyncRequires, matchPaths, window.pageData)
setLoader(loader)
loader.setApiRunner(apiRunner)
const { render, hydrate } = reactDOMUtils()
window.asyncRequires = asyncRequires
window.___emitter = emitter
window.___loader = publicLoader
navigationInit()
const reloadStorageKey = `gatsby-reload-compilation-hash-match`
apiRunnerAsync(`onClientEntry`).then(() => {
// Let plugins register a service worker. The plugin just needs
// to return true.
if (apiRunner(`registerServiceWorker`).filter(Boolean).length > 0) {
require(`./register-service-worker`)
}
// In gatsby v2 if Router is used in page using matchPaths
// paths need to contain full path.
// For example:
// - page have `/app/*` matchPath
// - inside template user needs to use `/app/xyz` as path
// Resetting `basepath`/`baseuri` keeps current behaviour
// to not introduce breaking change.
// Remove this in v3
const RouteHandler = props => (
<BaseContext.Provider
value={{
baseuri: `/`,
basepath: `/`,
}}
>
<PageRenderer {...props} />
</BaseContext.Provider>
)
const DataContext = React.createContext({})
const slicesContext = {
renderEnvironment: `browser`,
}
class GatsbyRoot extends React.Component {
render() {
const { children } = this.props
return (
<Location>
{({ location }) => (
<EnsureResources location={location}>
{({ pageResources, location }) => {
const staticQueryResults = getStaticQueryResults()
const sliceResults = getSliceResults()
return (
<StaticQueryContext.Provider value={staticQueryResults}>
<SlicesContext.Provider value={slicesContext}>
<SlicesResultsContext.Provider value={sliceResults}>
<SlicesMapContext.Provider
value={pageResources.page.slicesMap}
>
<DataContext.Provider
value={{ pageResources, location }}
>
{children}
</DataContext.Provider>
</SlicesMapContext.Provider>
</SlicesResultsContext.Provider>
</SlicesContext.Provider>
</StaticQueryContext.Provider>
)
}}
</EnsureResources>
)}
</Location>
)
}
}
class LocationHandler extends React.Component {
render() {
return (
<DataContext.Consumer>
{({ pageResources, location }) => (
<RouteUpdates location={location}>
<ScrollContext
location={location}
shouldUpdateScroll={shouldUpdateScroll}
>
<Router
basepath={__BASE_PATH__}
location={location}
id="gatsby-focus-wrapper"
>
<RouteHandler
path={
pageResources.page.path === `/404.html` ||
pageResources.page.path === `/500.html`
? stripPrefix(location.pathname, __BASE_PATH__)
: encodeURI(
(
pageResources.page.matchPath ||
pageResources.page.path
).split(`?`)[0]
)
}
{...this.props}
location={location}
pageResources={pageResources}
{...pageResources.json}
/>
</Router>
</ScrollContext>
</RouteUpdates>
)}
</DataContext.Consumer>
)
}
}
const { pagePath, location: browserLoc } = window
// Explicitly call navigate if the canonical path (window.pagePath)
// is different to the browser path (window.location.pathname). SSR
// page paths might include search params, while SSG and DSG won't.
// If page path include search params we also compare query params.
// But only if NONE of the following conditions hold:
//
// - The url matches a client side route (page.matchPath)
// - it's a 404 page
// - it's the offline plugin shell (/offline-plugin-app-shell-fallback/)
if (
pagePath &&
__BASE_PATH__ + pagePath !==
browserLoc.pathname + (pagePath.includes(`?`) ? browserLoc.search : ``) &&
!(
loader.findMatchPath(stripPrefix(browserLoc.pathname, __BASE_PATH__)) ||
pagePath.match(/^\/(404|500)(\/?|.html)$/) ||
pagePath.match(/^\/offline-plugin-app-shell-fallback\/?$/)
)
) {
navigate(
__BASE_PATH__ +
pagePath +
(!pagePath.includes(`?`) ? browserLoc.search : ``) +
browserLoc.hash,
{
replace: true,
}
)
}
// It's possible that sessionStorage can throw an exception if access is not granted, see https://github.com/gatsbyjs/gatsby/issues/34512
const getSessionStorage = () => {
try {
return sessionStorage
} catch {
return null
}
}
publicLoader.loadPage(browserLoc.pathname + browserLoc.search).then(page => {
const sessionStorage = getSessionStorage()
if (
page?.page?.webpackCompilationHash &&
page.page.webpackCompilationHash !== window.___webpackCompilationHash
) {
// Purge plugin-offline cache
if (
`serviceWorker` in navigator &&
navigator.serviceWorker.controller !== null &&
navigator.serviceWorker.controller.state === `activated`
) {
navigator.serviceWorker.controller.postMessage({
gatsbyApi: `clearPathResources`,
})
}
// We have not matching html + js (inlined `window.___webpackCompilationHash`)
// with our data (coming from `app-data.json` file). This can cause issues such as
// errors trying to load static queries (as list of static queries is inside `page-data`
// which might not match to currently loaded `.js` scripts).
// We are making attempt to reload if hashes don't match, but we also have to handle case
// when reload doesn't fix it (possibly broken deploy) so we don't end up in infinite reload loop
if (sessionStorage) {
const isReloaded = sessionStorage.getItem(reloadStorageKey) === `1`
if (!isReloaded) {
sessionStorage.setItem(reloadStorageKey, `1`)
window.location.reload(true)
return
}
}
}
if (sessionStorage) {
sessionStorage.removeItem(reloadStorageKey)
}
if (!page || page.status === PageResourceStatus.Error) {
const message = `page resources for ${browserLoc.pathname} not found. Not rendering React`
// if the chunk throws an error we want to capture the real error
// This should help with https://github.com/gatsbyjs/gatsby/issues/19618
if (page && page.error) {
console.error(message)
throw page.error
}
throw new Error(message)
}
const SiteRoot = apiRunner(
`wrapRootElement`,
{ element: <LocationHandler /> },
<LocationHandler />,
({ result }) => {
return { element: result }
}
).pop()
const App = function App() {
const onClientEntryRanRef = React.useRef(false)
React.useEffect(() => {
if (!onClientEntryRanRef.current) {
onClientEntryRanRef.current = true
if (performance.mark) {
performance.mark(`onInitialClientRender`)
}
apiRunner(`onInitialClientRender`)
}
}, [])
return <GatsbyRoot>{SiteRoot}</GatsbyRoot>
}
const focusEl = document.getElementById(`gatsby-focus-wrapper`)
// Client only pages have any empty body so we just do a normal
// render to avoid React complaining about hydration mis-matches.
let defaultRenderer = render
if (focusEl && focusEl.children.length) {
defaultRenderer = hydrate
}
const renderer = apiRunner(
`replaceHydrateFunction`,
undefined,
defaultRenderer
)[0]
function runRender() {
const rootElement =
typeof window !== `undefined`
? document.getElementById(`___gatsby`)
: null
renderer(<App />, rootElement)
}
// https://github.com/madrobby/zepto/blob/b5ed8d607f67724788ec9ff492be297f64d47dfc/src/zepto.js#L439-L450
// TODO remove IE 10 support
const doc = document
if (
doc.readyState === `complete` ||
(doc.readyState !== `loading` && !doc.documentElement.doScroll)
) {
setTimeout(function () {
runRender()
}, 0)
} else {
const handler = function () {
doc.removeEventListener(`DOMContentLoaded`, handler, false)
window.removeEventListener(`load`, handler, false)
runRender()
}
doc.addEventListener(`DOMContentLoaded`, handler, false)
window.addEventListener(`load`, handler, false)
}
return
})
})