flowtrack/static/sw.js

30 lines
1.1 KiB
JavaScript
Raw Normal View History

// Service worker: only shows push notifications. It deliberately caches
// nothing, so the app always loads fresh from the server.
self.addEventListener("install", () => self.skipWaiting());
self.addEventListener("activate", event => event.waitUntil(self.clients.claim()));
self.addEventListener("push", event => {
let message = { title: "Flowtrack", body: "" };
try {
message = { ...message, ...event.data.json() };
} catch {
// An empty or unreadable push still has to show something on iOS.
}
event.waitUntil(self.registration.showNotification(message.title, {
body: message.body,
icon: "/icon-192.png",
badge: "/icon-192.png",
tag: message.tag,
data: { url: message.url || "/" }
}));
});
self.addEventListener("notificationclick", event => {
event.notification.close();
event.waitUntil((async () => {
const windows = await self.clients.matchAll({ type: "window", includeUncontrolled: true });
if (windows.length) return windows[0].focus();
return self.clients.openWindow(event.notification.data.url);
})());
});