Я бы хотел протестировать несколько состояний моего сервисного работника, но мое событие onstatechange
равно нулю:
if (navigator.serviceWorker.controller) {
console.log("[PWA] Active Service Worker found, no need to register");
} else {
// Register the ServiceWorker
navigator.serviceWorker
.register("{{ path('sw.js')|raw }}", { scope: "{{ path('homepage')|raw }}" })
.then(reg => {
if (!reg.installing) {
return;
}
const worker = reg.installing;
const { state } = worker;
console.log(worker);
worker.addEventListener("onstatechange", () => {
console.log('testtt');
switch (true) {
case state === "installing":
console.log("[PWA] Service Worker is installing...");
break;
case state === "installed":
console.log("[PWA] Service Worker Install successful");
break;
case state === "redundant":
console.log("[PWA] Service Worker Install failed");
break;
case state === "activated" && !navigator.serviceWorker.controller:
console.log('[PWA] Service Worker is offline')
// Show an offline style
// showOfflineToast();
break;
}
});
})
.catch(error => console.log(`[PWA] Error during service worker registration : ${error}`));
}
Если я зарегистрирую свой state
, это будет только installing
текущий статус.