| 1 | import React from "react"; |
| 2 | import ReactDOM from "react-dom/client"; |
| 3 | import { createHashHistory, createBrowserHistory } from "@tanstack/react-router"; |
| 4 | |
| 5 | import "./index.css"; |
| 6 | |
| 7 | import { prepareProviderAuthDelivery } from "./providerAuthDelivery"; |
| 8 | import { isElectron } from "./env"; |
| 9 | import { getRouter } from "./router"; |
| 10 | import { |
| 11 | syncDocumentElectronPlatformClasses, |
| 12 | syncDocumentWindowControlsOverlayClass, |
| 13 | } from "./lib/windowControlsOverlay"; |
| 14 | import { AppRoot } from "./AppRoot"; |
| 15 | import { clearChunkReloadGuard, reloadOnceForChunkLoadError } from "./lib/chunkReloadGuard"; |
| 16 | |
| 17 | prepareProviderAuthDelivery(); |
| 18 | |
| 19 | // Electron loads the app from a file-backed shell, so hash history avoids path resolution issues. |
| 20 | const history = isElectron ? createHashHistory() : createBrowserHistory(); |
| 21 | |
| 22 | const router = getRouter(history); |
| 23 | |
| 24 | if (isElectron) { |
| 25 | syncDocumentElectronPlatformClasses(navigator.platform); |
| 26 | syncDocumentWindowControlsOverlayClass(); |
| 27 | } |
| 28 | |
| 29 | // A failed split-chunk fetch usually means the hashed assets went stale under |
| 30 | // a deploy; one guarded reload picks up the fresh index.html. |
| 31 | let chunkLoadFailed = false; |
| 32 | let reloadScheduled = false; |
| 33 | window.addEventListener("vite:preloadError", (event) => { |
| 34 | chunkLoadFailed = true; |
| 35 | if (reloadOnceForChunkLoadError()) { |
| 36 | reloadScheduled = true; |
| 37 | event.preventDefault(); |
| 38 | } |
| 39 | }); |
| 40 | |
| 41 | const app = <AppRoot router={router} />; |
| 42 | |
| 43 | export const startup = router |
| 44 | .load() |
| 45 | .then(() => { |
| 46 | if (reloadScheduled) return; |
| 47 | if (!chunkLoadFailed) clearChunkReloadGuard(); |
| 48 | ReactDOM.createRoot(document.getElementById("root") as HTMLElement).render( |
| 49 | <React.StrictMode>{app}</React.StrictMode>, |
| 50 | ); |
| 51 | }) |
| 52 | .catch((error: unknown) => { |
| 53 | // Let the bootstrap entry show the error unless a reload is already scheduled. |
| 54 | if (reloadScheduled) return; |
| 55 | throw error; |
| 56 | }); |