Fix: SW updatefound Listener vor reg.update() + race condition safety (SW by-v804)

This commit is contained in:
rene 2026-05-09 20:58:07 +02:00
parent fbd8f0cd8f
commit ab851d4bb1
4 changed files with 10 additions and 9 deletions

View file

@ -628,18 +628,19 @@
window.addEventListener('load', () => {
navigator.serviceWorker.register('/sw.js', { updateViaCache: 'none' })
.then(reg => {
reg.update();
// Primär: neuer SW hat aktiviert → sofort neu laden
reg.addEventListener('updatefound', () => {
const sw = reg.installing;
function _watchSW(sw) {
if (!sw) return;
sw.addEventListener('statechange', () => {
if (sw.state === 'activated') {
window.location.replace('/?_t=' + Date.now());
}
});
});
}
// Listener VOR update() registrieren — verhindert Race Condition
reg.addEventListener('updatefound', () => _watchSW(reg.installing));
// Falls SW bereits installiert (Seite wurde nach SW-Install neu geladen)
if (reg.installing) _watchSW(reg.installing);
reg.update();
})
.catch(err => console.warn('SW Registration failed:', err));
});