@@ -12,6 +12,7 @@ import {
1212 syncDocumentWindowControlsOverlayClass ,
1313} from "./lib/windowControlsOverlay" ;
1414import { AppRoot } from "./AppRoot" ;
15+ import { clearChunkReloadGuard , reloadOnceForChunkLoadError } from "./lib/chunkReloadGuard" ;
1516
1617// Electron loads the app from a file-backed shell, so hash history avoids path resolution issues.
1718const history = isElectron ? createHashHistory ( ) : createBrowserHistory ( ) ;
@@ -25,38 +26,16 @@ if (isElectron) {
2526
2627const clerkPublishableKey = import . meta. env . VITE_CLERK_PUBLISHABLE_KEY as string | undefined ;
2728
28- // With split chunks, a deploy (or desktop server swap) between page load and a
29- // lazy fetch can 404 the old hashed assets. Reload once to pick up the fresh
30- // index.html; the guard keeps a persistent failure from becoming a reload loop
31- // and instead lets the error surface through the normal paths.
32- const CHUNK_RELOAD_GUARD_KEY = "t3code:chunk-load-reloaded" ;
33- const readChunkReloadGuard = ( ) => {
34- // Blocked storage degrades to reloading on every chunk failure, which is
35- // still better than stranding the page.
36- try {
37- return window . sessionStorage . getItem ( CHUNK_RELOAD_GUARD_KEY ) === "1" ;
38- } catch {
39- return false ;
40- }
41- } ;
42- const writeChunkReloadGuard = ( reloaded : boolean ) => {
43- try {
44- if ( reloaded ) {
45- window . sessionStorage . setItem ( CHUNK_RELOAD_GUARD_KEY , "1" ) ;
46- } else {
47- window . sessionStorage . removeItem ( CHUNK_RELOAD_GUARD_KEY ) ;
48- }
49- } catch {
50- // See readChunkReloadGuard.
51- }
52- } ;
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 ;
5333window . addEventListener ( "vite:preloadError" , ( event ) => {
54- if ( readChunkReloadGuard ( ) ) {
55- return ;
34+ chunkLoadFailed = true ;
35+ if ( reloadOnceForChunkLoadError ( ) ) {
36+ reloadScheduled = true ;
37+ event . preventDefault ( ) ;
5638 }
57- writeChunkReloadGuard ( true ) ;
58- event . preventDefault ( ) ;
59- window . location . reload ( ) ;
6039} ) ;
6140
6241const app = < AppRoot router = { router } /> ;
@@ -73,22 +52,33 @@ const managedAuthShellModule =
7352 : null ;
7453
7554// The index.html boot splash lives inside #root, and React's first commit
76- // clears it. Resolve everything that first commit needs — the selected
77- // managed-auth runtime and the initial route's split chunks — before
55+ // clears it. Resolve everything that first commit needs, the selected
56+ // managed-auth runtime and the initial route's split chunks, before
7857// rendering, so the splash holds until real UI paints instead of dropping to
7958// a blank window while chunks download.
80- void Promise . all ( [
81- managedAuthShellModule ?. then ( ( module ) => module . default ) ?? null ,
82- router . load ( ) ,
83- ] ) . then ( ( [ ManagedAuthShell ] ) => {
84- writeChunkReloadGuard ( false ) ;
85- ReactDOM . createRoot ( document . getElementById ( "root" ) as HTMLElement ) . render (
86- < React . StrictMode >
87- { ManagedAuthShell && clerkPublishableKey ? (
88- < ManagedAuthShell publishableKey = { clerkPublishableKey } > { app } </ ManagedAuthShell >
89- ) : (
90- app
91- ) }
92- </ React . StrictMode > ,
93- ) ;
94- } ) ;
59+ void 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+ } ) ;
0 commit comments