Skip to content

Commit

Permalink
🐛 Unregistering legacy service worker for old users
Browse files Browse the repository at this point in the history
- legacy version of the app kept being served as the old service worker was not handled properly
  • Loading branch information
misavojte committed Jan 8, 2024
1 parent ab3a9b6 commit a000021
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
14 changes: 14 additions & 0 deletions static/registerSW.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
Solely for unregistering the old service worker used in the legacy version of the app.
This is necessary because the user may have the old service worker cached, and if so,
it would prevent the new service worker from registering.
*/

if ('serviceWorker'in navigator) {
window.addEventListener('load', ()=>{
navigator.serviceWorker.register('./sw.js', {
scope: './'
})
}
)
}
20 changes: 20 additions & 0 deletions static/sw.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
Solely for unregistering the old service worker used in the legacy version of the app.
This is necessary because the user may have the old service worker cached, and if so,
it would prevent the new service worker from registering.
*/

self.addEventListener('activate', event => {
event.waitUntil(
caches.keys().then(cacheNames => {
return Promise.all(
cacheNames.map(cacheName => {
return caches.delete(cacheName);
})
);
}).then(() => self.clients.claim())
);
self.registration.unregister().then(() => {
console.log('Service Worker unregistered.');
});
});

0 comments on commit a000021

Please sign in to comment.