From 574798d01992eb91519a65d51dec294616120e21 Mon Sep 17 00:00:00 2001 From: fly_mc Date: Fri, 25 Oct 2024 21:03:08 +0800 Subject: [PATCH] temp: try unregister sw --- .../frontend/src/scripts/initialize-sw.ts | 33 ++++++++++++++----- packages/sw/src/sw.ts | 6 ++-- 2 files changed, 28 insertions(+), 11 deletions(-) diff --git a/packages/frontend/src/scripts/initialize-sw.ts b/packages/frontend/src/scripts/initialize-sw.ts index 867ebf19e..d080095e1 100644 --- a/packages/frontend/src/scripts/initialize-sw.ts +++ b/packages/frontend/src/scripts/initialize-sw.ts @@ -6,13 +6,30 @@ import { lang } from '@@/js/config.js'; export async function initializeSw() { - if (!('serviceWorker' in navigator)) return; + if (!('serviceWorker' in navigator)) return; - navigator.serviceWorker.register('/sw.js', { scope: '/', type: 'classic' }); - navigator.serviceWorker.ready.then(registration => { - registration.active?.postMessage({ - msg: 'initialize', - lang, - }); - }); + try { + const registrations = await navigator.serviceWorker.getRegistrations(); + await Promise.all( + registrations.map(registration => registration.unregister()) + ); + + console.info('Successfully unregistered old service worker(s)'); + + const registration = await navigator.serviceWorker.register('/sw.js', { + scope: '/', + type: 'classic' + }); + + await navigator.serviceWorker.ready; + + registration.active?.postMessage({ + msg: 'initialize', + lang, + }); + + console.info('Successfully registered and initialized new service worker'); + } catch (error) { + console.error('Service worker registration/initialization failed:', error); + } } diff --git a/packages/sw/src/sw.ts b/packages/sw/src/sw.ts index af933f14e..59fc7d8d5 100644 --- a/packages/sw/src/sw.ts +++ b/packages/sw/src/sw.ts @@ -134,9 +134,9 @@ globalThis.addEventListener('notificationclick', (ev: ServiceWorkerGlobalScopeEv case 'showFollowRequests': client = await swos.openClient('push', '/my/follow-requests', loginId); break; - case 'edited': - if ('note' in data.body) client = await swos.openPost({ reply: data.body.note }, loginId); - break; + case 'edited': + if ('note' in data.body) client = await swos.openPost({ reply: data.body.note }, loginId); + break; default: switch (data.body.type) { case 'receiveFollowRequest':