From d1db510a5a3400a65e1f0e003c167deb4bcd2e96 Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Thu, 25 Jun 2026 21:09:28 -0400 Subject: [PATCH] Auto-reload on repeated chunk failure (#12841) * Auto-reload on repeated chunk failure * Fail safe in private browser --- app/utils/lazyWithRetry.ts | 41 ++++++++++++++++++++++++++++++++++++++ app/utils/sentry.ts | 1 + 2 files changed, 42 insertions(+) diff --git a/app/utils/lazyWithRetry.ts b/app/utils/lazyWithRetry.ts index 8bd8aa3892..6d1aee1390 100644 --- a/app/utils/lazyWithRetry.ts +++ b/app/utils/lazyWithRetry.ts @@ -32,6 +32,11 @@ function retry>( .catch((error) => { setTimeout(() => { if (retriesLeft === 1) { + // Retrying a removed chunk is futile, the only recovery is to + // reload and fetch a fresh index.html with current chunk URLs. + if (reloadIfStaleChunk(error)) { + return; + } reject(error); return; } @@ -40,3 +45,39 @@ function retry>( }); }); } + +const reloadSessionKey = "chunkReload"; + +/** + * Reloads the page when a dynamic import fails because the chunk no longer + * exists, typically after a deploy left an open tab referencing stale URLs. + * Guarded so it only reloads once per session to avoid reload loops. + * + * @param error The error thrown by the failed dynamic import. + * @returns Whether a reload was triggered. + */ +function reloadIfStaleChunk(error: unknown): boolean { + const message = error instanceof Error ? error.message : String(error); + const isChunkError = + /Failed to fetch dynamically imported module|Importing a module script failed/i.test( + message + ); + + if (!isChunkError) { + return false; + } + + try { + if (sessionStorage.getItem(reloadSessionKey)) { + return false; + } + sessionStorage.setItem(reloadSessionKey, "1"); + } catch { + // sessionStorage may be unavailable (e.g. Safari private mode). Without it + // we can't guard against a reload loop, so fail safe and don't reload. + return false; + } + + window.location.reload(); + return true; +} diff --git a/app/utils/sentry.ts b/app/utils/sentry.ts index 89afabe265..3593ffa2fe 100644 --- a/app/utils/sentry.ts +++ b/app/utils/sentry.ts @@ -36,6 +36,7 @@ export function initSentry(history: History) { tracesSampleRate: env.ENVIRONMENT === "production" ? 0.1 : 1, ignoreErrors: [ "Failed to fetch dynamically imported module", + "Importing a module script failed", "ResizeObserver loop completed with undelivered notifications", "ResizeObserver loop limit exceeded", "Object Not Found Matching Id",