deregister sw when update is ready to go to force activating on iOS

This commit is contained in:
Bruno Windels 2020-10-15 15:59:15 +02:00
parent 6b15e7e3ba
commit 3354d601d1

View File

@ -31,19 +31,19 @@
</script> </script>
<script id="service-worker" type="module"> <script id="service-worker" type="module">
if('serviceWorker' in navigator) { if('serviceWorker' in navigator) {
navigator.serviceWorker.register('sw.js') navigator.serviceWorker.register('sw.js').then(function(registration) {
.then(function(registration) {
console.log("Service Worker registered"); console.log("Service Worker registered");
function tryActivateUpdate() {
if (registration.waiting && registration.active) {
registration.unregister();
console.log("Service Worker unregistered");
}
}
tryActivateUpdate();
registration.onupdatefound = function() { registration.onupdatefound = function() {
const newWorker = registration.installing; const newWorker = registration.installing;
alert("update found!");
newWorker.onstatechange = function() { newWorker.onstatechange = function() {
if (newWorker.state === "installed") { tryActivateUpdate();
alert("new worker is now on state " + newWorker.state);
// if (confirm("update installed! shall I skip waiting?")) {
// newWorker.skipWaiting();
// }
}
} }
}; };
}); });