sendou.ink/public/sw-2.js
Kalle e5d12ea79c
Some checks failed
Tests and checks on push / run-checks-and-tests (push) Has been cancelled
Updates translation progress / update-translation-progress-issue (push) Has been cancelled
Try versioned service worker
2025-03-08 18:03:30 +02:00

31 lines
917 B
JavaScript

self.addEventListener("fetch", () => {
return;
});
self.addEventListener("push", (event) => {
const { title, ...options } = JSON.parse(event.data.text());
event.waitUntil(self.registration.showNotification(title, options));
});
self.addEventListener("notificationclick", (event) => {
const targetUrl = event.notification.data.url;
event.notification.close(); // Android needs explicit close.
event.waitUntil(
clients.matchAll({ type: "window" }).then((windowClients) => {
// Check if there is already a window/tab open with the target URL
for (let i = 0; i < windowClients.length; i++) {
const client = windowClients[i];
// If so, just focus it.
if (client.url === targetUrl && "focus" in client) {
return client.focus();
}
}
// If not, then open the target URL in a new window/tab.
if (clients.openWindow) {
return clients.openWindow(targetUrl);
}
}),
);
});