Open History to see the last commit
1import React from "react";
2import ReactDOM from "react-dom/client";
3import { createHashHistory, createBrowserHistory } from "@tanstack/react-router";
4
5import "./index.css";
6
7import { isElectron } from "./env";
8import { hasCloudPublicConfig } from "./cloud/publicConfig";
9import { getRouter } from "./router";
10import {
11 syncDocumentElectronPlatformClasses,
12 syncDocumentWindowControlsOverlayClass,
13} from "./lib/windowControlsOverlay";
14import { AppRoot } from "./AppRoot";
15import { clearChunkReloadGuard, reloadOnceForChunkLoadError } from "./lib/chunkReloadGuard";
16
17// Electron loads the app from a file-backed shell, so hash history avoids path resolution issues.
18const history = isElectron ? createHashHistory() : createBrowserHistory();
19
20const router = getRouter(history);
21
22if (isElectron) {
23 syncDocumentElectronPlatformClasses(navigator.platform);
24 syncDocumentWindowControlsOverlayClass();
25}
26
27const clerkPublishableKey = import.meta.env.VITE_CLERK_PUBLISHABLE_KEY as string | undefined;
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.
31let chunkLoadFailed = false;
32let reloadScheduled = false;
33window.addEventListener("vite:preloadError", (event) => {
34 chunkLoadFailed = true;
35 if (reloadOnceForChunkLoadError()) {
36 reloadScheduled = true;
37 event.preventDefault();
38 }
39});
40
41const app = <AppRoot router={router} />;
42
43// Managed auth is cloud-only, and the Electron Clerk provider bundles the full
44// clerk-js runtime. Loading only the selected runtime as a split chunk keeps
45// every Clerk byte out of the startup graph for local-mode users, and keeps
46// the bundled clerk-js out of the browser build entirely.
47const managedAuthShellModule =
48 clerkPublishableKey && hasCloudPublicConfig()
49 ? isElectron
50 ? import("./components/clerk/ElectronManagedAuthShell")
51 : import("./components/clerk/BrowserManagedAuthShell")
52 : null;
53
54// The index.html boot splash lives inside #root, and React's first commit
55// clears it. Resolve everything that first commit needs, the selected
56// managed-auth runtime and the initial route's split chunks, before
57// rendering, so the splash holds until real UI paints instead of dropping to
58// a blank window while chunks download.
59void Promise.all([managedAuthShellModule?.then((module) => module.default) ?? null, router.load()])
60 .then(([ManagedAuthShell]) => {
61 // A route chunk failure still resolves router.load(): the error is parked in
62 // the lazy component and surfaces through the route error boundary. Skip the
63 // paint when a reload is on its way, and only re-arm the guard after a boot
64 // that fetched every chunk it asked for.
65 if (reloadScheduled) return;
66 if (!chunkLoadFailed) clearChunkReloadGuard();
67 ReactDOM.createRoot(document.getElementById("root") as HTMLElement).render(
68 <React.StrictMode>
69 {ManagedAuthShell && clerkPublishableKey ? (
70 <ManagedAuthShell publishableKey={clerkPublishableKey}>{app}</ManagedAuthShell>
71 ) : (
72 app
73 )}
74 </React.StrictMode>,
75 );
76 })
77 .catch((error: unknown) => {
78 // The auth shell chunk failed and the guarded reload is spent. Say so
79 // instead of leaving the splash up forever.
80 if (reloadScheduled) return;
81 console.error("T3 Code failed to load its startup chunks.", error);
82 const bootShell = document.getElementById("boot-shell");
83 if (bootShell) bootShell.textContent = "T3 Code could not load. Reload to try again.";
84 });