From e5868837d53b444e7b1f06d3fb54375203d5af43 Mon Sep 17 00:00:00 2001 From: Udit Takkar <53316345+Udit-takkar@users.noreply.github.com> Date: Wed, 28 May 2025 04:42:25 +0530 Subject: [PATCH] perf: service worker lifecycle method (#21532) Co-authored-by: Alex van Andel --- .../components/notification-sound-handler.tsx | 5 ++++ apps/web/public/service-worker.js | 24 +++++++++---------- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/apps/web/components/notification-sound-handler.tsx b/apps/web/components/notification-sound-handler.tsx index 8a57daa552..4730439687 100644 --- a/apps/web/components/notification-sound-handler.tsx +++ b/apps/web/components/notification-sound-handler.tsx @@ -91,6 +91,11 @@ export function NotificationSoundHandler() { // Don't automatically initialize - wait for user interaction return () => { stopSound(); + if (audioContextRef.current && audioContextRef.current.state !== "closed") { + audioContextRef.current.close(); + } + audioContextRef.current = null; + audioBufferRef.current = null; }; }, []); diff --git a/apps/web/public/service-worker.js b/apps/web/public/service-worker.js index 4d3759d858..81e62f0f08 100644 --- a/apps/web/public/service-worker.js +++ b/apps/web/public/service-worker.js @@ -25,18 +25,7 @@ self.addEventListener("push", async (event) => { }); existingNotifications.forEach((notification) => { - const options = { - body: notification.body, - icon: notification.icon, - badge: notification.badge, - data: notification.data, - silent: notification.silent, - vibrate: notification.vibrate, - requireInteraction: notification.requireInteraction, - tag: notification.tag, - }; - - self.registration.showNotification(notification.title, options); + notification.close(); }); // Special handling for instant meetings @@ -87,6 +76,8 @@ self.addEventListener("notificationclick", (event) => { }); }); }; + + event.waitUntil(stopSound()); } if (!event.action) { @@ -111,4 +102,13 @@ self.addEventListener('install', (event) => { self.addEventListener('activate', (event) => { console.log('Service Worker activated.'); + + event.waitUntil( + (async () => { + const notifications = await self.registration.getNotifications(); + notifications.forEach(notification => notification.close()); + + return self.clients.claim(); + })() + ); });