fix: Handle missing notification URLs gracefully in the service worker by adding null coalescing and an early exit condition.

This commit is contained in:
Yoga Pangestu 2026-02-24 08:29:16 +07:00
parent 8ff24135d1
commit fb31078745

View File

@ -23,7 +23,7 @@ self.addEventListener('push', (event) => {
vibrate: [200, 100, 200], vibrate: [200, 100, 200],
requireInteraction: true, requireInteraction: true,
data: { data: {
url: notification.url url: notification.url || null
} }
}) })
); );
@ -32,17 +32,20 @@ self.addEventListener('push', (event) => {
self.addEventListener('notificationclick', (event) => { self.addEventListener('notificationclick', (event) => {
event.notification.close(); event.notification.close();
const url = event.notification.data && event.notification.data.url;
if (!url) return;
event.waitUntil( event.waitUntil(
clients.matchAll({ type: 'window', includeUncontrolled: true }).then((clientList) => { clients.matchAll({ type: 'window', includeUncontrolled: true }).then((clientList) => {
// Check if there's already a tab open with this URL
for (const client of clientList) { 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(); return client.focus();
} }
} }
// If not, open a new window // If not, open a new window
if (clients.openWindow) { if (clients.openWindow) {
return clients.openWindow(event.notification.data.url); return clients.openWindow(url);
} }
}) })
); );