Rebranded for Thunderbird Portal (THBD): numbering prefix EL->TH, all "ELMI"/"ЭЛМИ" UI strings genericized, ELMI's real contract templates and company legal/bank details removed (not applicable to this company), fresh VAPID keypair and encryption key, fresh single-migration schema history. Same architecture as elmi-portal: Web Push notifications, unread-chat app badge, cookie-session auth. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Gh2UXUQUVBWroWEnn1FLFG
54 lines
1.9 KiB
JavaScript
54 lines
1.9 KiB
JavaScript
// Web Push service worker. Deliberately minimal — no offline caching, no
|
|
// asset interception. Its only job is to keep receiving push events even
|
|
// when the page's own tab is frozen or closed, and turn them into an OS
|
|
// notification.
|
|
|
|
self.addEventListener("push", (event) => {
|
|
let payload = { title: "Thunderbird Portal", body: "", url: "/inquiries" };
|
|
try {
|
|
if (event.data) payload = { ...payload, ...event.data.json() };
|
|
} catch {
|
|
// malformed payload — fall back to the generic notification above
|
|
}
|
|
|
|
event.waitUntil(
|
|
(async () => {
|
|
await self.registration.showNotification(payload.title, {
|
|
body: payload.body,
|
|
icon: "/icon-192.png",
|
|
data: { url: payload.url },
|
|
});
|
|
|
|
// badgeCount is only present on payloads that affect the unread chat
|
|
// count — other event types omit it entirely so the badge is left
|
|
// alone. Android/desktop only; iOS Safari has no Badging API (the
|
|
// calls just don't exist there, so this is a no-op, not an error).
|
|
if (typeof payload.badgeCount === "number" && "setAppBadge" in self.navigator) {
|
|
if (payload.badgeCount > 0) {
|
|
await self.navigator.setAppBadge(payload.badgeCount);
|
|
} else {
|
|
await self.navigator.clearAppBadge();
|
|
}
|
|
}
|
|
})(),
|
|
);
|
|
});
|
|
|
|
self.addEventListener("notificationclick", (event) => {
|
|
event.notification.close();
|
|
const url = event.notification.data && event.notification.data.url ? event.notification.data.url : "/inquiries";
|
|
|
|
event.waitUntil(
|
|
(async () => {
|
|
const clientsList = await self.clients.matchAll({ type: "window", includeUncontrolled: true });
|
|
const existing = clientsList.find((c) => "focus" in c);
|
|
if (existing) {
|
|
await existing.navigate(url);
|
|
await existing.focus();
|
|
return;
|
|
}
|
|
await self.clients.openWindow(url);
|
|
})(),
|
|
);
|
|
});
|