From 6e0d7d2bbc49c2883d68a725c785ec789af2b437 Mon Sep 17 00:00:00 2001 From: Julien Calixte Date: Sun, 12 Jul 2020 22:41:55 +0200 Subject: [PATCH] :sparkles: (notificaiton) dismiss notification when the app is updated --- src/registerServiceWorker.ts | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/registerServiceWorker.ts b/src/registerServiceWorker.ts index 9190c3e..32b8afc 100644 --- a/src/registerServiceWorker.ts +++ b/src/registerServiceWorker.ts @@ -6,6 +6,12 @@ import { setNotificationInstance, notify } from './utils/notification' if (process.env.NODE_ENV === 'production') { register(`${process.env.BASE_URL}service-worker.js`, { ready(sw) { + sw.getNotifications().then((notifs) => { + const newVersions = notifs.filter( + (notif) => notif.tag === 'new-version' + ) + newVersions.forEach((notif) => notif.close()) + }) console.log('App is being served from cache by a service worker') setNotificationInstance((title, options?) => sw.showNotification(title, options) @@ -26,7 +32,8 @@ if (process.env.NODE_ENV === 'production') { sw.showNotification(title, options) ) notify('An update is available!', { - body: `Reload the app to install the update.` + body: `Reload the app to install the update.`, + tag: 'new-version' }) }, offline() {