-
Notifications
You must be signed in to change notification settings - Fork 10.3k
/
production-app.js
190 lines (168 loc) · 5.55 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
if (__POLYFILL__) {
require(`core-js/modules/es6.promise`)
}
import { apiRunner, apiRunnerAsync } from "./api-runner-browser"
import React, { createElement } from "react"
import ReactDOM from "react-dom"
import { Router, Route, withRouter, matchPath } from "react-router-dom"
import { ScrollContext } from "gatsby-react-router-scroll"
import domReady from "domready"
import history from "./history"
window.___history = history
import emitter from "./emitter"
window.___emitter = emitter
import pages from "./pages.json"
import redirects from "./redirects.json"
import ComponentRenderer from "./component-renderer"
import asyncRequires from "./async-requires"
import loader from "./loader"
loader.addPagesArray(pages)
loader.addProdRequires(asyncRequires)
window.asyncRequires = asyncRequires
window.___loader = loader
window.matchPath = matchPath
// Convert to a map for faster lookup in maybeRedirect()
const redirectMap = redirects.reduce((map, redirect) => {
map[redirect.fromPath] = redirect
return map
}, {})
const maybeRedirect = pathname => {
const redirect = redirectMap[pathname]
if (redirect != null) {
history.replace(redirect.toPath)
return true
} else {
return false
}
}
// Check for initial page-load redirect
maybeRedirect(window.location.pathname)
// 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`)
}
const navigateTo = pathname => {
const redirect = redirectMap[pathname]
// If we're redirecting, just replace the passed in pathname
// to the one we want to redirect to.
if (redirect) {
pathname = redirect.toPath
}
// If we're already at this path, do nothing.
if (window.location.pathname === pathname) {
return
}
// Listen to loading events. If page resources load before
// a second, navigate immediately.
function eventHandler(e) {
if (e.page.path === loader.getPage(pathname).path) {
emitter.off(`onPostLoadPageResources`, eventHandler)
clearTimeout(timeoutId)
window.___history.push(pathname)
}
}
// Start a timer to wait for a second before transitioning and showing a
// loader in case resources aren't around yet.
const timeoutId = setTimeout(() => {
emitter.off(`onPostLoadPageResources`, eventHandler)
emitter.emit(`onDelayedLoadPageResources`, { pathname })
window.___history.push(pathname)
}, 1000)
if (loader.getResourcesForPathname(pathname)) {
// The resources are already loaded so off we go.
clearTimeout(timeoutId)
window.___history.push(pathname)
} else {
// They're not loaded yet so let's add a listener for when
// they finish loading.
emitter.on(`onPostLoadPageResources`, eventHandler)
}
}
// window.___loadScriptsForPath = loadScriptsForPath
window.___navigateTo = navigateTo
// Call onRouteUpdate on the initial page load.
apiRunner(`onRouteUpdate`, {
location: history.location,
action: history.action,
})
let initialAttachDone = false
function attachToHistory(history) {
if (!window.___history || initialAttachDone === false) {
window.___history = history
initialAttachDone = true
history.listen((location, action) => {
if (!maybeRedirect(location.pathname)) {
apiRunner(`onRouteUpdate`, { location, action })
}
})
}
}
function shouldUpdateScroll(prevRouterProps, { location: { pathname } }) {
const results = apiRunner(`shouldUpdateScroll`, {
prevRouterProps,
pathname,
})
if (results.length > 0) {
return results[0]
}
if (prevRouterProps) {
const { location: { pathname: oldPathname } } = prevRouterProps
if (oldPathname === pathname) {
return false
}
}
return true
}
const AltRouter = apiRunner(`replaceRouterComponent`, { history })[0]
const DefaultRouter = ({ children }) => (
<Router history={history}>{children}</Router>
)
const ComponentRendererWithRouter = withRouter(ComponentRenderer)
loader.getResourcesForPathname(window.location.pathname, () => {
const Root = () =>
createElement(
AltRouter ? AltRouter : DefaultRouter,
null,
createElement(
ScrollContext,
{ shouldUpdateScroll },
createElement(ComponentRendererWithRouter, {
layout: true,
children: layoutProps =>
createElement(Route, {
render: routeProps => {
attachToHistory(routeProps.history)
const props = layoutProps ? layoutProps : routeProps
if (loader.getPage(props.location.pathname)) {
return createElement(ComponentRenderer, {
page: true,
...props,
})
} else {
return createElement(ComponentRenderer, {
page: true,
location: { pathname: `/404.html` },
})
}
},
}),
})
)
)
const NewRoot = apiRunner(`wrapRootComponent`, { Root }, Root)[0]
domReady(() =>
ReactDOM.render(
<NewRoot />,
typeof window !== `undefined`
? document.getElementById(`___gatsby`)
: void 0,
() => {
apiRunner(`onInitialClientRender`)
}
)
)
})
})