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