From df252e097a128a1bbd146e516d564d97a18e3ec1 Mon Sep 17 00:00:00 2001 From: Silberengel Date: Sun, 29 Mar 2026 19:56:11 +0200 Subject: [PATCH] fix stale tabs --- src/main.tsx | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/src/main.tsx b/src/main.tsx index ec07a7d2..d25dcbe7 100644 --- a/src/main.tsx +++ b/src/main.tsx @@ -12,6 +12,38 @@ import { ErrorBoundary } from './components/ErrorBoundary.tsx' import storage from './services/local-storage.service' import { restoreSessionFeedSnapshotsAfterHardRefresh } from './services/session-feed-snapshot.service' +/** + * After a deploy, hashed chunks from the previous build are removed. A tab that still runs old JS + * (HTTP cache, or a service worker that just dropped the old precache) can hit 404 on import(). + * One reload usually picks up the new index.html and asset graph. + */ +function installStaleBuildChunkRecovery() { + if (typeof window === 'undefined') return + + const isChunkLoadFailure = (msg: string) => + msg.includes('Failed to fetch dynamically imported module') || + msg.includes('error loading dynamically imported module') || + msg.includes('Importing a module script failed') + + window.addEventListener('unhandledrejection', (event) => { + const r = event.reason + const msg = + typeof r === 'string' ? r : r instanceof Error ? r.message : String(r ?? '') + if (!isChunkLoadFailure(msg)) return + event.preventDefault() + try { + const key = 'jumble:stale-chunk-reload' + if (sessionStorage.getItem(key)) return + sessionStorage.setItem(key, '1') + } catch { + return + } + window.location.reload() + }) +} + +installStaleBuildChunkRecovery() + declare global { interface Window { __RUNTIME_CONFIG__?: { NIP66_MONITOR_NPUB?: string; DESKTOP_DOWNLOAD_URL?: string }