clove/email
public/sw.js
/* mailbox/public/sw.js
* Copyright (c) 2026 Clove Nytrix Doughmination Twilight
* Licensed under the DASL-1.0 Licence.
* See LICENCE.md in the project root for full licence information.
*/
// Service worker for the mailbox PWA. Its main job is Web Push: show a
// notification when the server sends one, and focus/open the inbox when the
// user taps it. Deliberately minimal — no offline caching.
self.addEventListener("install", () => {
self.skipWaiting();
});
self.addEventListener("activate", (event) => {
event.waitUntil(self.clients.claim());
});
self.addEventListener("push", (event) => {
let data = {};
try {
data = event.data ? event.data.json() : {};
} catch (_) {
data = { body: event.data ? event.data.text() : "" };
}
const title = data.title || "New email";
const options = {
body: data.body || "",
icon: "/apple-touch-icon.png",
badge: "/apple-touch-icon.png",
tag: data.tag || undefined,
data: { url: data.url || "/inbox" },
};
event.waitUntil(self.registration.showNotification(title, options));
});
self.addEventListener("notificationclick", (event) => {
event.notification.close();
const url = (event.notification.data && event.notification.data.url) || "/inbox";
event.waitUntil(
self.clients.matchAll({ type: "window", includeUncontrolled: true }).then((wins) => {
for (const win of wins) {
// Reuse an already-open tab if we have one.
if ("focus" in win) {
if ("navigate" in win) win.navigate(url).catch(() => {});
return win.focus();
}
}
if (self.clients.openWindow) return self.clients.openWindow(url);
return undefined;
})
);
});