1
0
Fork 0
mirror of https://github.com/maybe-finance/maybe.git synced 2025-08-06 05:55:21 +02:00

Update service-worker.js

Changes:
- Optional chaining to avoid runtime errors
- for...of for readability
- Defensive checks for event.data and event.notification.data


Signed-off-by: Blueray <83096078+blueraymusic@users.noreply.github.com>
This commit is contained in:
Blueray 2025-07-21 16:18:08 -05:00 committed by GitHub
parent 321a343df4
commit b62c6052c8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -24,3 +24,48 @@
// })
// )
// })
// _____--------_____
// Improve
self.addEventListener("push", async (event) => {
if (!event.data) return
try {
const { title, options } = await event.data.json()
if (!title || !options) return
event.waitUntil(
self.registration.showNotification(title, options)
)
} catch (error) {
console.error(" Failed to Parse the Json:", error)
}
})
self.addEventListener("notificationclick", (event) => {
event.notification.close()
const targetPath = event.notification?.data?.path
if (!targetPath) return
event.waitUntil(
clients.matchAll({ type: "window", includeUncontrolled: true }).then((clientList) => {
for (const client of clientList) {
const clientPath = new URL(client.url).pathname
if (clientPath === targetPath && "focus" in client) {
return client.focus()
}
}
// No matching tab found, open new one
if (clients.openWindow) {
return clients.openWindow(targetPath)
}
})
)
})