Skip to content

Commit

Permalink
Fixes service worker error
Browse files Browse the repository at this point in the history
  • Loading branch information
lmammino committed Jan 21, 2024
1 parent 0cd3c3d commit c9de109
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 14 deletions.
33 changes: 21 additions & 12 deletions public/sw.js
Original file line number Diff line number Diff line change
@@ -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)
}
})
})
11 changes: 9 additions & 2 deletions src/components/Footer.astro
Original file line number Diff line number Diff line change
Expand Up @@ -267,9 +267,16 @@ const today = new Date()
}
</style>
<script>
// Registers service worker
// Registers service worker to clean up the old Gatsby one
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('/sw.js').then((reg) => reg.update())
navigator.serviceWorker
.register('/sw.js')
.then((reg) => {
if (reg.active) {
reg.update()
}
})
.catch((err) => {}) // ignore errors
}

// Initializes google tag manager for Analytics
Expand Down

0 comments on commit c9de109

Please sign in to comment.