diff --git a/sw_scripts/additional-scripts.js b/sw_scripts/additional-scripts.js index 1067606ff..07e5d2d20 100644 --- a/sw_scripts/additional-scripts.js +++ b/sw_scripts/additional-scripts.js @@ -112,6 +112,25 @@ self.addEventListener("message", (event) => { logConsoleAndDb("Service worker posted a message."); }); +self.addEventListener("notificationclick", (event) => { + logConsoleAndDb("Notification got clicked.", event); + event.notification.close(); + // from https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerGlobalScope/notificationclick_event + // ... though I don't see any benefit over just "clients.openWindow" + event.waitUntil( + clients + .matchAll({ + type: "window", + }) + .then((clientList) => { + for (const client of clientList) { + if (client.url === "/" && "focus" in client) return client.focus(); + } + if (clients.openWindow) return clients.openWindow("/"); + }), + ); +}); + self.addEventListener("fetch", (event) => { logConsoleAndDb("Service worker got fetch event.", event); });