UNPKG

2.06 kBJavaScriptView Raw
1import React from "react"
2import ReactDOM from "react-dom"
3import domReady from "@mikaelkristiansson/domready"
4
5import socketIo from "./socketIo"
6import emitter from "./emitter"
7import { apiRunner, apiRunnerAsync } from "./api-runner-browser"
8import { setLoader } from "./loader"
9import DevLoader from "./dev-loader"
10import syncRequires from "./sync-requires"
11// Generated during bootstrap
12import matchPaths from "./match-paths.json"
13
14window.___emitter = emitter
15
16const loader = new DevLoader(syncRequires, matchPaths)
17setLoader(loader)
18loader.setApiRunner(apiRunner)
19
20// Let the site/plugins run code very early.
21apiRunnerAsync(`onClientEntry`).then(() => {
22 // Hook up the client to socket.io on server
23 const socket = socketIo()
24 if (socket) {
25 socket.on(`reload`, () => {
26 window.location.reload()
27 })
28 }
29
30 /**
31 * Service Workers are persistent by nature. They stick around,
32 * serving a cached version of the site if they aren't removed.
33 * This is especially frustrating when you need to test the
34 * production build on your local machine.
35 *
36 * Let's warn if we find service workers in development.
37 */
38 if (`serviceWorker` in navigator) {
39 navigator.serviceWorker.getRegistrations().then(registrations => {
40 if (registrations.length > 0)
41 console.warn(
42 `Warning: found one or more service workers present.`,
43 `If your site isn't behaving as expected, you might want to remove these.`,
44 registrations
45 )
46 })
47 }
48
49 const rootElement = document.getElementById(`___gatsby`)
50
51 const renderer = apiRunner(
52 `replaceHydrateFunction`,
53 undefined,
54 ReactDOM.render
55 )[0]
56
57 Promise.all([
58 loader.loadPage(`/dev-404-page/`),
59 loader.loadPage(`/404.html`),
60 loader.loadPage(window.location.pathname),
61 ]).then(() => {
62 const preferDefault = m => (m && m.default) || m
63 let Root = preferDefault(require(`./root`))
64 domReady(() => {
65 renderer(<Root />, rootElement, () => {
66 apiRunner(`onInitialClientRender`)
67 })
68 })
69 })
70})
71
\No newline at end of file