@devries-cameron20 / t3code Mirror · read-only / commits / 6d535841a3
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 18:30 UTC

fix(web): a chunk that fails after boot no longer reloads the page in a loop (#29306)

Co-authored-by: Julius Marminge <julius0216@outlook.com>

Guillaume Rodriguez committed Nov 12, 2026 at 23:09 UTC 6d535841a3ea9b5cc5d3d299dfa51b495860b407
4 files changed +48 -20
apps/web/src/lib/chunkReloadGuard.test.ts
+6 -8
@@ -1,6 +1,6 @@
1 import { describe, expect, it, vi } from "vite-plus/test";
2
3 -import { clearChunkReloadGuard, reloadOnceForChunkLoadError } from "./chunkReloadGuard";
3 +import { reloadOnceForChunkLoadError } from "./chunkReloadGuard";
4
5 function createStorageStub(): Storage {
6 const store = new Map<string, string>();
@@ -25,21 +25,20 @@ describe("reloadOnceForChunkLoadError", () => {
25 const storage = createStorageStub();
26 const reload = vi.fn();
27
28 - expect(reloadOnceForChunkLoadError(() => storage, reload)).toBe(true);
28 + expect(reloadOnceForChunkLoadError(() => storage, reload, "1.0.0")).toBe(true);
29 expect(reload).toHaveBeenCalledTimes(1);
30
31 - expect(reloadOnceForChunkLoadError(() => storage, reload)).toBe(false);
31 + expect(reloadOnceForChunkLoadError(() => storage, reload, "1.0.0")).toBe(false);
32 expect(reload).toHaveBeenCalledTimes(1);
33 });
34
35 - it("reloads again after a successful boot cleared the guard", () => {
35 + it("reloads again once a deploy changed the build", () => {
36 const storage = createStorageStub();
37 const reload = vi.fn();
38
39 - reloadOnceForChunkLoadError(() => storage, reload);
40 - clearChunkReloadGuard(() => storage);
39 + reloadOnceForChunkLoadError(() => storage, reload, "1.0.0");
40
42 - expect(reloadOnceForChunkLoadError(() => storage, reload)).toBe(true);
41 + expect(reloadOnceForChunkLoadError(() => storage, reload, "1.0.1")).toBe(true);
42 expect(reload).toHaveBeenCalledTimes(2);
43 });
44
@@ -51,6 +50,5 @@ describe("reloadOnceForChunkLoadError", () => {
50
51 expect(reloadOnceForChunkLoadError(blocked, reload)).toBe(false);
52 expect(reload).not.toHaveBeenCalled();
54 - expect(() => clearChunkReloadGuard(blocked)).not.toThrow();
53 });
54 });
apps/web/src/lib/chunkReloadGuard.ts
+4 -8
@@ -1,15 +1,17 @@
1 // Deploys and desktop server swaps can invalidate lazy chunks; one reload fetches the fresh index.
2 +// Keyed by build and kept across boots: a chunk loaded after boot can fail on every visit.
3 const CHUNK_RELOAD_GUARD_KEY = "t3code:chunk-load-reloaded";
4
5 export function reloadOnceForChunkLoadError(
6 getStorage: () => Storage = () => window.sessionStorage,
7 reload: () => void = () => window.location.reload(),
8 + build = import.meta.env.APP_VERSION,
9 ): boolean {
10 let alreadyReloaded: boolean;
11 try {
12 const storage = getStorage();
11 - alreadyReloaded = storage.getItem(CHUNK_RELOAD_GUARD_KEY) === "1";
12 - if (!alreadyReloaded) storage.setItem(CHUNK_RELOAD_GUARD_KEY, "1");
13 + alreadyReloaded = storage.getItem(CHUNK_RELOAD_GUARD_KEY) === build;
14 + if (!alreadyReloaded) storage.setItem(CHUNK_RELOAD_GUARD_KEY, build);
15 } catch {
16 // Without storage, the reload guard cannot prevent an infinite loop.
17 return false;
@@ -18,9 +20,3 @@ export function reloadOnceForChunkLoadError(
20 reload();
21 return true;
22 }
21 -
22 -export function clearChunkReloadGuard(getStorage: () => Storage = () => window.sessionStorage) {
23 - try {
24 - getStorage().removeItem(CHUNK_RELOAD_GUARD_KEY);
25 - } catch {}
26 -}
apps/web/src/main.test.ts new
+37
@@ -0,0 +1,37 @@
1 +import { expect, it, vi } from "vite-plus/test";
2 +
3 +vi.mock("./appInit", () => ({ startAppServices: () => ({ load: async () => {} }) }));
4 +vi.mock("./AppRoot", () => ({ AppRoot: () => null }));
5 +vi.mock("./i18n/runtime", () => ({ whenLocaleReady: async () => {} }));
6 +vi.mock("react-dom/client", () => ({ default: { createRoot: () => ({ render: () => {} }) } }));
7 +
8 +// Boots main in a fresh page that shares the tab's sessionStorage, as a reload does.
9 +async function bootThenFailLazyChunk(sessionStorage: Map<string, string>) {
10 + vi.resetModules();
11 + const reload = vi.fn().mockName("location.reload");
12 + const page = Object.assign(new EventTarget(), {
13 + sessionStorage: {
14 + getItem: (key: string) => sessionStorage.get(key) ?? null,
15 + setItem: (key: string, value: string) => sessionStorage.set(key, value),
16 + removeItem: (key: string) => sessionStorage.delete(key),
17 + },
18 + location: { reload },
19 + });
20 + vi.stubGlobal("window", page);
21 + vi.stubGlobal("document", { getElementById: () => ({}) });
22 + try {
23 + const { startup } = await import("./main");
24 + await startup;
25 + page.dispatchEvent(new Event("vite:preloadError", { cancelable: true }));
26 + return reload;
27 + } finally {
28 + vi.unstubAllGlobals();
29 + }
30 +}
31 +
32 +it("reloads only once when a lazy chunk fails after every boot", async () => {
33 + const sessionStorage = new Map<string, string>();
34 +
35 + expect(await bootThenFailLazyChunk(sessionStorage)).toHaveBeenCalledTimes(1);
36 + expect(await bootThenFailLazyChunk(sessionStorage)).not.toHaveBeenCalled();
37 +});
apps/web/src/main.tsx
+1 -4
@@ -9,16 +9,14 @@ import { isElectron } from "./env";
9 import { startAppServices } from "./appInit";
10 import { AppRoot } from "./AppRoot";
11 import { whenLocaleReady } from "./i18n/runtime";
12 -import { clearChunkReloadGuard, reloadOnceForChunkLoadError } from "./lib/chunkReloadGuard";
12 +import { reloadOnceForChunkLoadError } from "./lib/chunkReloadGuard";
13
14 const router = startAppServices();
15
16 const clerkPublishableKey = import.meta.env.VITE_CLERK_PUBLISHABLE_KEY as string | undefined;
17
18 -let chunkLoadFailed = false;
18 let reloadScheduled = false;
19 window.addEventListener("vite:preloadError", (event) => {
21 - chunkLoadFailed = true;
20 if (reloadOnceForChunkLoadError()) {
21 reloadScheduled = true;
22 event.preventDefault();
@@ -43,7 +41,6 @@ export const startup = Promise.all([
41 .then(([ManagedAuthShell]) => {
42 // Route chunk failures resolve router.load() and surface later through the error boundary.
43 if (reloadScheduled) return;
46 - if (!chunkLoadFailed) clearChunkReloadGuard();
44 ReactDOM.createRoot(document.getElementById("root") as HTMLElement).render(
45 <React.StrictMode>
46 {ManagedAuthShell && clerkPublishableKey ? (