From c9de1096e64980bc7e0b366e57e228dd4da848fa Mon Sep 17 00:00:00 2001 From: Luciano Mammino Date: Sun, 21 Jan 2024 17:48:51 +0000 Subject: [PATCH] Fixes service worker error --- public/sw.js | 33 +++++++++++++++++++++------------ src/components/Footer.astro | 11 +++++++++-- 2 files changed, 30 insertions(+), 14 deletions(-) diff --git a/public/sw.js b/public/sw.js index 36c544ba2..65766373f 100644 --- a/public/sw.js +++ b/public/sw.js @@ -1,17 +1,26 @@ // this service worker doesn't do anything intentionally. // It's just here to clear up the previously installed SW from Gatsby -// based on https://github.com/NekR/self-destroying-sw -self.addEventListener('install', (_e) => { - self.skipWaiting() +// based on https://github.com/NekR/self-destroying-sw +// and on https://web.dev/articles/service-worker-mindset +// and https://developer.chrome.com/docs/workbox/remove-buggy-service-workers/ + +self.addEventListener('install', () => { + // Skip over the "waiting" lifecycle state, to ensure that our + // new service worker is activated immediately, even if there's + // another tab open controlled by our older service worker code. + self.skipWaiting(); }) -self.addEventListener('activate', (_e) => { - self.registration - .unregister() - .then(() => self.clients.matchAll()) - .then((clients) => { - for (const client of clients) { - client.navigate(client.url) - } - }) +self.addEventListener('activate', () => { + // Optional: Get a list of all the current open windows/tabs under + // our service worker's control, and force them to reload. + // This can "unbreak" any open windows/tabs as soon as the new + // service worker activates, rather than users having to manually reload. + self.clients.matchAll({ + type: 'window' + }).then(windowClients => { + for (const windowClient of windowClients) { + windowClient.navigate(windowClient.url) + } + }) }) diff --git a/src/components/Footer.astro b/src/components/Footer.astro index 0e3282ae1..f373228d9 100644 --- a/src/components/Footer.astro +++ b/src/components/Footer.astro @@ -267,9 +267,16 @@ const today = new Date() }