-
Notifications
You must be signed in to change notification settings - Fork 10.3k
/
Copy pathproduction-app.js
128 lines (115 loc) · 3.56 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
import { apiRunner, apiRunnerAsync } from "./api-runner-browser"
import React, { createElement } from "react"
import ReactDOM from "react-dom"
import { Router, navigate } from "@reach/router"
import { ScrollContext } from "gatsby-react-router-scroll"
import domReady from "@mikaelkristiansson/domready"
import {
shouldUpdateScroll,
init as navigationInit,
RouteUpdates,
} from "./navigation"
import emitter from "./emitter"
import PageRenderer from "./page-renderer"
import asyncRequires from "./async-requires"
import matchPaths from "./match-paths.json"
import loader, { setApiRunnerForLoader } from "./loader"
import EnsureResources from "./ensure-resources"
import stripPrefix from "./strip-prefix"
window.asyncRequires = asyncRequires
window.___emitter = emitter
window.___loader = loader
window.___webpackCompilationHash = window.webpackCompilationHash
loader.addProdRequires(asyncRequires)
loader.addMatchPaths(matchPaths)
setApiRunnerForLoader(apiRunner)
navigationInit()
// Let the site/plugins run code very early.
apiRunnerAsync(`onClientEntry`).then(() => {
// Let plugins register a service worker. The plugin just needs
// to return true.
if (apiRunner(`registerServiceWorker`).length > 0) {
require(`./register-service-worker`)
}
class RouteHandler extends React.Component {
render() {
let { location } = this.props
return (
<EnsureResources location={location}>
{({ pageResources, location }) => (
<RouteUpdates location={location}>
<ScrollContext
location={location}
shouldUpdateScroll={shouldUpdateScroll}
>
<PageRenderer
{...this.props}
location={location}
pageResources={pageResources}
{...pageResources.json}
/>
</ScrollContext>
</RouteUpdates>
)}
</EnsureResources>
)
}
}
const { pagePath, location: browserLoc } = window
// Explicitly call navigate if the canonical path (window.pagePath)
// is different to the browser path (window.location.pathname). 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 &&
!(
loader.findMatchPath(stripPrefix(browserLoc.pathname, __BASE_PATH__)) ||
pagePath === `/404.html` ||
pagePath.match(/^\/404\/?$/) ||
pagePath.match(/^\/offline-plugin-app-shell-fallback\/?$/)
)
) {
navigate(__BASE_PATH__ + pagePath + browserLoc.search + browserLoc.hash, {
replace: true,
})
}
loader.loadPage(browserLoc.pathname).then(() => {
const Root = () =>
createElement(
Router,
{
basepath: __BASE_PATH__,
},
createElement(RouteHandler, { path: `/*` })
)
const WrappedRoot = apiRunner(
`wrapRootElement`,
{ element: <Root /> },
<Root />,
({ result }) => {
return { element: result }
}
).pop()
let NewRoot = () => WrappedRoot
const renderer = apiRunner(
`replaceHydrateFunction`,
undefined,
ReactDOM.hydrate
)[0]
domReady(() => {
renderer(
<NewRoot />,
typeof window !== `undefined`
? document.getElementById(`___gatsby`)
: void 0,
() => {
apiRunner(`onInitialClientRender`)
}
)
})
})
})