-
Notifications
You must be signed in to change notification settings - Fork 10.3k
/
root.js
141 lines (123 loc) · 3.41 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
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
import React, { createElement } from "react"
import {
BrowserRouter as Router,
Route,
matchPath,
withRouter,
} from "react-router-dom"
import { ScrollContext } from "react-router-scroll"
import createHistory from "history/createBrowserHistory"
import apiRunner from "./api-runner-browser"
import syncRequires from "./sync-requires"
import pages from "./pages.json"
import ComponentRenderer from "./component-renderer"
import loader from "./loader"
loader.addPagesArray(pages)
loader.addDevRequires(syncRequires)
window.___loader = loader
const history = createHistory()
// Call onRouteUpdate on the initial page load.
apiRunner(`onRouteUpdate`, {
location: history.location,
action: history.action,
})
function attachToHistory(history) {
if (!window.___history) {
window.___history = history
history.listen((location, action) => {
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
}
let noMatch
for (let i = 0; i < paths.length; i++) {
if (paths[i].path === `/dev-404-page/`) {
noMatch = paths[i]
break
}
}
const addNotFoundRoute = () => {
if (noMatch) {
return createElement(Route, {
key: `404-page`,
component: props =>
createElement(syncRequires.components[noMatch.componentChunkName], {
...props,
...syncRequires.json[noMatch.jsonName],
}),
})
} else {
return null
}
}
const navigateTo = pathname => {
window.___history.push(pathname)
}
window.___navigateTo = navigateTo
const AltRouter = apiRunner(`replaceRouterComponent`, { history })[0]
const DefaultRouter = ({ children }) =>
<Router history={history}>
{children}
</Router>
// Use default layout if one isn't set.
let layout
if (syncRequires.layouts[`index`]) {
layout = syncRequires.layouts[`index`]
} else {
layout = ({ children }) =>
<div>
{children()}
</div>
}
// Always have to have one top-level layout
// can have ones below that. Find page, if has different
// parent layout(s), loop through those until finally the
// page. Tricky part is avoiding re-mounting I think...
const Root = () =>
createElement(
AltRouter ? AltRouter : DefaultRouter,
null,
createElement(
ScrollContext,
{ shouldUpdateScroll },
createElement(withRouter(layout), {
children: layoutProps =>
createElement(Route, {
render: routeProps => {
attachToHistory(routeProps.history)
const props = layoutProps ? layoutProps : routeProps
const pageResources = loader.getResourcesForPathname(
props.location.pathname
)
if (pageResources) {
return createElement(ComponentRenderer, {
...props,
pageResources,
})
} else {
return addNotFoundRoute()
}
},
}),
})
)
)
// Let site, plugins wrap the site e.g. for Redux.
const WrappedRoot = apiRunner(`wrapRootComponent`, { Root }, Root)[0]
export default WrappedRoot