From 20caace68aff2cf8d7c2de57b28a003b93e2aac9 Mon Sep 17 00:00:00 2001 From: Cotes Chung <11371340+cotes2020@users.noreply.github.com> Date: Wed, 8 Jun 2022 16:09:18 +0800 Subject: [PATCH] fix(PWA): avoid repeated notification of SW update --- assets/js/pwa/app.js | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/assets/js/pwa/app.js b/assets/js/pwa/app.js index fd15191d6..9eb1a03af 100644 --- a/assets/js/pwa/app.js +++ b/assets/js/pwa/app.js @@ -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 */ @@ -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); + } } } });