Skip to content

Commit ecdc282

Browse files
fix(web): "Try again" recovers from a failed page chunk load
Browsers cache a failed dynamic import for the life of the document, so router.invalidate() re-imported the same failed URL and kept failing even after the file was back. For module-load errors, "Try again" now reloads. Fixes #10613 Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 6f21b0f commit ecdc282

3 files changed

Lines changed: 33 additions & 2 deletions

File tree

‎apps/web/src/lib/chunkReloadGuard.test.ts‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { describe, expect, it, vi } from "vite-plus/test";
22

3-
import { reloadOnceForChunkLoadError } from "./chunkReloadGuard";
3+
import { isChunkLoadError, reloadOnceForChunkLoadError } from "./chunkReloadGuard";
44

55
function createStorageStub(): Storage {
66
const store = new Map<string, string>();
@@ -52,3 +52,14 @@ describe("reloadOnceForChunkLoadError", () => {
5252
expect(reload).not.toHaveBeenCalled();
5353
});
5454
});
55+
56+
describe("isChunkLoadError", () => {
57+
it.each([
58+
["Failed to fetch dynamically imported module: t3code://app/assets/a.js", true],
59+
["error loading dynamically imported module: https://app.t3.codes/assets/a.js", true],
60+
["Importing a module script failed.", true],
61+
["Cannot read properties of undefined", false],
62+
])("%s", (message, expected) => {
63+
expect(isChunkLoadError(new TypeError(message))).toBe(expected);
64+
});
65+
});

‎apps/web/src/lib/chunkReloadGuard.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,17 @@ export function reloadOnceForChunkLoadError(
2020
reload();
2121
return true;
2222
}
23+
24+
// Browsers cache a failed dynamic import per document, so only a reload can retry it.
25+
const CHUNK_LOAD_ERROR_PREFIXES = [
26+
"Failed to fetch dynamically imported module", // Chromium
27+
"error loading dynamically imported module", // Firefox
28+
"Importing a module script failed", // Safari
29+
];
30+
31+
export function isChunkLoadError(error: unknown): boolean {
32+
const message = error instanceof Error ? error.message : undefined;
33+
return (
34+
message !== undefined && CHUNK_LOAD_ERROR_PREFIXES.some((prefix) => message.startsWith(prefix))
35+
);
36+
}

‎apps/web/src/routes/__root.tsx‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ import { primaryServerConfigAtom, primaryServerWelcomeAtom } from "../state/serv
5757
import { readProject } from "../state/entities";
5858

5959
import { getDesktopSnapShotBridge } from "../lib/desktopSnapShot";
60+
import { isChunkLoadError } from "../lib/chunkReloadGuard";
6061
import { shouldResumeSnapShotSetupOnStartup } from "../lib/snapShotSetupResume";
6162

6263
export const Route = createRootRoute({
@@ -243,7 +244,12 @@ function RootRouteErrorView({ error }: ErrorComponentProps) {
243244
<StandalonePageHeader title="Something went wrong." description={message} />
244245

245246
<div className="mt-5 flex flex-wrap gap-2">
246-
<Button size="sm" onClick={() => void router.invalidate()}>
247+
<Button
248+
size="sm"
249+
onClick={() =>
250+
isChunkLoadError(error) ? window.location.reload() : void router.invalidate()
251+
}
252+
>
247253
Try again
248254
</Button>
249255
<Button size="sm" variant="outline" onClick={() => window.location.reload()}>

0 commit comments

Comments
 (0)