Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

main.tsx

84 lines3.2 KB

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();
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.
59export const startup = Promise.all([
60 managedAuthShellModule?.then((module) => module.default) ?? null,
61 router.load(),
62])
63 .then(([ManagedAuthShell]) => {
64 // A route chunk failure still resolves router.load(): the error is parked in
65 // the lazy component and surfaces through the route error boundary. Skip the
66 // paint when a reload is on its way, and only re-arm the guard after a boot
67 // that fetched every chunk it asked for.
68 if (reloadScheduled) return;
69 if (!chunkLoadFailed) clearChunkReloadGuard();
70 ReactDOM.createRoot(document.getElementById("root") as HTMLElement).render(
71 <React.StrictMode>
72 {ManagedAuthShell && clerkPublishableKey ? (
73 <ManagedAuthShell publishableKey={clerkPublishableKey}>{app}</ManagedAuthShell>
74 ) : (
75 app
76 )}
77 </React.StrictMode>,
78 );
79 })
80 .catch((error: unknown) => {
81 // Let the bootstrap entry show the error unless a reload is already scheduled.
82 if (reloadScheduled) return;
83 throw error;
84 });