1 | import { apiRunner, apiRunnerAsync } from "./api-runner-browser"
|
2 | import React from "react"
|
3 | import ReactDOM from "react-dom"
|
4 | import { Router, navigate, Location, BaseContext } from "@reach/router"
|
5 | import { ScrollContext } from "gatsby-react-router-scroll"
|
6 | import domReady from "@mikaelkristiansson/domready"
|
7 | import {
|
8 | shouldUpdateScroll,
|
9 | init as navigationInit,
|
10 | RouteUpdates,
|
11 | } from "./navigation"
|
12 | import emitter from "./emitter"
|
13 | import PageRenderer from "./page-renderer"
|
14 | import asyncRequires from "./async-requires"
|
15 | import { setLoader, ProdLoader, publicLoader } from "./loader"
|
16 | import EnsureResources from "./ensure-resources"
|
17 | import stripPrefix from "./strip-prefix"
|
18 |
|
19 |
|
20 | import matchPaths from "./match-paths.json"
|
21 |
|
22 | const loader = new ProdLoader(asyncRequires, matchPaths)
|
23 | setLoader(loader)
|
24 | loader.setApiRunner(apiRunner)
|
25 |
|
26 | window.asyncRequires = asyncRequires
|
27 | window.___emitter = emitter
|
28 | window.___loader = publicLoader
|
29 |
|
30 | navigationInit()
|
31 |
|
32 | apiRunnerAsync(`onClientEntry`).then(() => {
|
33 |
|
34 |
|
35 | if (apiRunner(`registerServiceWorker`).length > 0) {
|
36 | require(`./register-service-worker`)
|
37 | }
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 | const RouteHandler = props => (
|
48 | <BaseContext.Provider
|
49 | value={{
|
50 | baseuri: `/`,
|
51 | basepath: `/`,
|
52 | }}
|
53 | >
|
54 | <PageRenderer {...props} />
|
55 | </BaseContext.Provider>
|
56 | )
|
57 |
|
58 | class LocationHandler extends React.Component {
|
59 | render() {
|
60 | const { location } = this.props
|
61 | return (
|
62 | <EnsureResources location={location}>
|
63 | {({ pageResources, location }) => (
|
64 | <RouteUpdates location={location}>
|
65 | <ScrollContext
|
66 | location={location}
|
67 | shouldUpdateScroll={shouldUpdateScroll}
|
68 | >
|
69 | <Router
|
70 | basepath={__BASE_PATH__}
|
71 | location={location}
|
72 | id="gatsby-focus-wrapper"
|
73 | >
|
74 | <RouteHandler
|
75 | path={
|
76 | pageResources.page.path === `/404.html`
|
77 | ? stripPrefix(location.pathname, __BASE_PATH__)
|
78 | : encodeURI(
|
79 | pageResources.page.matchPath ||
|
80 | pageResources.page.path
|
81 | )
|
82 | }
|
83 | {...this.props}
|
84 | location={location}
|
85 | pageResources={pageResources}
|
86 | {...pageResources.json}
|
87 | />
|
88 | </Router>
|
89 | </ScrollContext>
|
90 | </RouteUpdates>
|
91 | )}
|
92 | </EnsureResources>
|
93 | )
|
94 | }
|
95 | }
|
96 |
|
97 | const { pagePath, location: browserLoc } = window
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 | if (
|
107 | pagePath &&
|
108 | __BASE_PATH__ + pagePath !== browserLoc.pathname &&
|
109 | !(
|
110 | loader.findMatchPath(stripPrefix(browserLoc.pathname, __BASE_PATH__)) ||
|
111 | pagePath === `/404.html` ||
|
112 | pagePath.match(/^\/404\/?$/) ||
|
113 | pagePath.match(/^\/offline-plugin-app-shell-fallback\/?$/)
|
114 | )
|
115 | ) {
|
116 | navigate(__BASE_PATH__ + pagePath + browserLoc.search + browserLoc.hash, {
|
117 | replace: true,
|
118 | })
|
119 | }
|
120 |
|
121 | publicLoader.loadPage(browserLoc.pathname).then(page => {
|
122 | if (!page || page.status === `error`) {
|
123 | throw new Error(
|
124 | `page resources for ${browserLoc.pathname} not found. Not rendering React`
|
125 | )
|
126 | }
|
127 |
|
128 | window.___webpackCompilationHash = page.page.webpackCompilationHash
|
129 |
|
130 | const Root = () => (
|
131 | <Location>
|
132 | {locationContext => <LocationHandler {...locationContext} />}
|
133 | </Location>
|
134 | )
|
135 |
|
136 | const WrappedRoot = apiRunner(
|
137 | `wrapRootElement`,
|
138 | { element: <Root /> },
|
139 | <Root />,
|
140 | ({ result }) => {
|
141 | return { element: result }
|
142 | }
|
143 | ).pop()
|
144 |
|
145 | const NewRoot = () => WrappedRoot
|
146 |
|
147 | const renderer = apiRunner(
|
148 | `replaceHydrateFunction`,
|
149 | undefined,
|
150 | ReactDOM.hydrate
|
151 | )[0]
|
152 |
|
153 | domReady(() => {
|
154 | renderer(
|
155 | <NewRoot />,
|
156 | typeof window !== `undefined`
|
157 | ? document.getElementById(`___gatsby`)
|
158 | : void 0,
|
159 | () => {
|
160 | apiRunner(`onInitialClientRender`)
|
161 | }
|
162 | )
|
163 | })
|
164 | })
|
165 | })
|
166 |
|
\ | No newline at end of file |