fix: clear stale native app-shell service workers before boot

This commit is contained in:
SkateSpots 2026-10-30 00:20:32 +07:00
parent 6aba9b356f
commit 7719be12d9
2 changed files with 35 additions and 1 deletions

View file

@ -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;

View file

@ -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 = () => {