fix : service worker qui causait un écran blanc après mise à jour

- SW v3 : network-first sur index.html (plus de cache périmé)
- Assets versionnés (hash) : cache-first
- index.html : auto-update du SW + rechargement quand une nouvelle version prend le contrôle
- Résout le 'Unable to open asset URL' (référence à un JS/CSS qui n'existe plus)
This commit is contained in:
Mathis 2026-10-04 20:45:05 +02:00
parent 3ecd57119f
commit bf22f65f91
2 changed files with 58 additions and 14 deletions

View file

@ -33,7 +33,25 @@
<script>
if ('serviceWorker' in navigator) {
window.addEventListener('load', () => {
navigator.serviceWorker.register('/sw.js').catch(() => {})
navigator.serviceWorker.register('/sw.js').then((reg) => {
// Si une nouvelle version du SW est disponible, on l'active.
reg.addEventListener('updatefound', () => {
const nw = reg.installing
if (!nw) return
nw.addEventListener('statechange', () => {
if (nw.state === 'installed' && navigator.serviceWorker.controller) {
nw.postMessage({ type: 'SKIP_WAITING' })
}
})
})
// Quand un nouveau SW prend le contrôle, on recharge pour servir la nouvelle version.
let refreshing = false
navigator.serviceWorker.addEventListener('controllerchange', () => {
if (refreshing) return
refreshing = true
window.location.reload()
})
}).catch(() => {})
})
}
</script>