From ab24bd3454f2569328cb1a02659b10d455962372 Mon Sep 17 00:00:00 2001 From: Yoga Pangestu Date: Tue, 24 Feb 2026 08:24:23 +0700 Subject: [PATCH] Fix: service worker notification URL handling to gracefully manage missing URLs. --- public/service-worker.js | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/public/service-worker.js b/public/service-worker.js index 54724e0..c1fed7a 100644 --- a/public/service-worker.js +++ b/public/service-worker.js @@ -23,7 +23,7 @@ self.addEventListener('push', (event) => { vibrate: [200, 100, 200], requireInteraction: true, data: { - url: notification.url + url: notification.url || null } }) ); @@ -32,17 +32,19 @@ self.addEventListener('push', (event) => { self.addEventListener('notificationclick', (event) => { event.notification.close(); + const url = event.notification.data && event.notification.data.url; + + if (!url) return; + event.waitUntil( clients.matchAll({ type: 'window', includeUncontrolled: true }).then((clientList) => { - // Check if there's already a tab open with this URL for (const client of clientList) { - if (client.url === event.notification.data.url && 'focus' in client) { + if (client.url === url && 'focus' in client) { return client.focus(); } } - // If not, open a new window if (clients.openWindow) { - return clients.openWindow(event.notification.data.url); + return clients.openWindow(url); } }) );