-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🐛 Unregistering legacy service worker for old users
- legacy version of the app kept being served as the old service worker was not handled properly
- Loading branch information
Showing
2 changed files
with
34 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: './' | ||
}) | ||
} | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.'); | ||
}); | ||
}); |