diff --git a/public/boot-guard.js b/public/boot-guard.js index 8682db8..7335175 100644 --- a/public/boot-guard.js +++ b/public/boot-guard.js @@ -8,6 +8,26 @@ */ (function () { var shown = false; + + // Native WebViews can keep a service worker from an older build forever. + // That worker can serve a stale index.html whose hashed chunks no longer + // exist, producing a white/black reload loop. The native shell is served from + // https://localhost; clear its app-shell registrations before the bundle runs. + try { + var isNativeShell = location.protocol === 'https:' && location.hostname === 'localhost'; + if (isNativeShell && 'serviceWorker' in navigator) { + navigator.serviceWorker.getRegistrations().then(function (regs) { + regs.forEach(function (reg) { try { reg.unregister(); } catch (e) {} }); + }).catch(function () {}); + if (window.caches) { + window.caches.keys().then(function (keys) { + keys.filter(function (key) { return key.indexOf('skate-spots-shell-') === 0; }) + .forEach(function (key) { window.caches.delete(key).catch(function () {}); }); + }).catch(function () {}); + } + } + } catch (e) { /* best effort */ } + function paint(kind, err) { if (shown) return; shown = true; diff --git a/src/lib/appServiceWorker.ts b/src/lib/appServiceWorker.ts index 7d5d32f..ec27978 100644 --- a/src/lib/appServiceWorker.ts +++ b/src/lib/appServiceWorker.ts @@ -11,7 +11,21 @@ let started = false; */ export function registerAppServiceWorker(): void { if (started || typeof window === 'undefined' || !('serviceWorker' in navigator)) return; - if (Capacitor.isNativePlatform()) return; + if (Capacitor.isNativePlatform()) { + // The native shell must never inherit an old web app-shell worker. + if ('serviceWorker' in navigator) { + void navigator.serviceWorker.getRegistrations() + .then((regs) => regs.forEach((reg) => { try { reg.unregister(); } catch { /* ignore */ } })) + .catch(() => undefined); + } + if (typeof caches !== 'undefined') { + void caches.keys() + .then((keys) => keys.filter((key) => key.startsWith('skate-spots-shell-')) + .forEach((key) => { void caches.delete(key).catch(() => undefined); })) + .catch(() => undefined); + } + return; + } started = true; const register = () => {