hopefully fix some sw bugs

This commit is contained in:
willyb321
2018-10-13 09:22:16 +11:00
parent f0bf8e8ce2
commit 8f089cb1ee

View File

@@ -1,8 +1,6 @@
console.log('Hello from sw.js');
if (workbox) {
workbox.skipWaiting();
workbox.clientsClaim();
console.log('Yay! Workbox is loaded 🎉');
workbox.routing.registerRoute(
new RegExp('https://fonts.(?:googleapis|gstatic).com/(.*)'),
@@ -44,3 +42,31 @@ if (workbox) {
} else {
console.log('Boo! Workbox didn\'t load 😬');
}
(async() => {
if (event.request.mode === 'navigate' && registration.waiting) {
if ((await clients.matchAll()).length < 2) {
registration.waiting.postMessage('skipWaiting');
}
}
})();
addEventListener('message', messageEvent => {
if (messageEvent.data === 'skipWaiting') return skipWaiting();
});
addEventListener('fetch', event => {
event.respondWith(
(async() => {
if (
event.request.mode === 'navigate' &&
event.request.method === 'GET' &&
registration.waiting &&
(await clients.matchAll()).length < 2
) {
registration.waiting.postMessage('skipWaiting');
return new Response('', { headers: { Refresh: '0' } });
}
return (await caches.match(event.request)) || fetch(event.request);
})()
);
});