-
Notifications
You must be signed in to change notification settings - Fork 10.3k
/
root.js
112 lines (102 loc) · 2.87 KB
/
root.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 React, { createElement } from "react"
import { Router } from "@reach/router"
import { ScrollContext } from "gatsby-react-router-scroll"
import {
shouldUpdateScroll,
init as navigationInit,
RouteUpdates,
} from "./navigation"
import { apiRunner } from "./api-runner-browser"
import syncRequires from "./sync-requires"
import pages from "./pages.json"
import loader from "./loader"
import JSONStore from "./json-store"
import EnsureResources from "./ensure-resources"
import { reportError, clearError } from "./error-overlay-handler"
if (window.__webpack_hot_middleware_reporter__ !== undefined) {
const overlayErrorID = `webpack`
// Report build errors
window.__webpack_hot_middleware_reporter__.useCustomOverlay({
showProblems(type, obj) {
if (type !== `errors`) {
clearError(overlayErrorID)
return
}
reportError(overlayErrorID, obj[0])
},
clear() {
clearError(overlayErrorID)
},
})
}
navigationInit()
class RouteHandler extends React.Component {
render() {
let { location } = this.props
// check if page exists - in dev pages are sync loaded, it's safe to use
// loader.getPage
let page = loader.getPage(location.pathname)
if (page) {
return (
<EnsureResources location={location}>
{locationAndPageResources => (
<RouteUpdates location={location}>
<ScrollContext
location={location}
shouldUpdateScroll={shouldUpdateScroll}
>
<JSONStore
pages={pages}
{...this.props}
{...locationAndPageResources}
/>
</ScrollContext>
</RouteUpdates>
)}
</EnsureResources>
)
} else {
const dev404Page = pages.find(p => /^\/dev-404-page\/?$/.test(p.path))
const custom404 = locationAndPageResources =>
loader.getPage(`/404.html`) ? (
<JSONStore
pages={pages}
{...this.props}
{...locationAndPageResources}
/>
) : null
return (
<EnsureResources location={location}>
{locationAndPageResources =>
createElement(
syncRequires.components[dev404Page.componentChunkName],
{
pages,
custom404: custom404(locationAndPageResources),
...this.props,
}
)
}
</EnsureResources>
)
}
}
}
const Root = () =>
createElement(
Router,
{
basepath: __PATH_PREFIX__,
},
createElement(RouteHandler, { path: `/*` })
)
// Let site, plugins wrap the site e.g. for Redux.
const WrappedRoot = apiRunner(
`wrapRootElement`,
{ element: <Root /> },
<Root />,
({ result, plugin }) => {
return { element: result }
}
).pop()
export default () => WrappedRoot