Skip to content

Commit

Permalink
fix(PWA): avoid repeated notification of SW update
Browse files Browse the repository at this point in the history
  • Loading branch information
cotes2020 authored and shihtiy-tw committed Nov 26, 2022
1 parent 236d635 commit 61b46c2
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions assets/js/pwa/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,10 @@ if ('serviceWorker' in navigator) {
.then(registration => {
if (registration) {
registration.addEventListener('updatefound', () => {
/* console.log('updatefound'); */
let serviceWorker = registration.installing;

serviceWorker.addEventListener('statechange', () => {
/* console.log(`statechange -> ${serviceWorker.state}`); */
if (serviceWorker.state === 'installed') {
/* console.log('installed'); */
if (navigator.serviceWorker.controller) {
$notification.toast('show');
/* in case the user ignores the notification */
Expand All @@ -39,9 +36,14 @@ if ('serviceWorker' in navigator) {
$notification.toast('hide');
});

/* there's a new Service Worker waiting to be activated */
if (localStorage.getItem(keyWaiting)) {
$notification.toast('show');
if (registration.waiting) {
/* there's a new Service Worker waiting to be activated */
$notification.toast('show');
} else {
/* closed all open pages after receiving notification */
localStorage.removeItem(keyWaiting);
}
}
}
});
Expand Down

0 comments on commit 61b46c2

Please sign in to comment.