Files
Homelab-manager/web/public/sw.js
T
bobbanandClaude Sonnet 5.5 ec9c2f73f0 Dependencies beyond servers, web push, inventory auto-fill with QR labels, Windows agent health
- Dependencies: servers and integrations can depend on each other; alerts are folded under the root cause, and a failure explained by something down isn't announced
- Web push: hand-written VAPID and RFC 8291 encryption, per-device subscriptions, a fifth notification channel, service worker handlers
- Inventory: maker/model/serial from the agents fill blank fields, "Refresh from agent", QR label printing and ?asset= deep links
- Windows agent (v4) reports failed services, disk health, Storage Spaces pools and hardware identity; Linux agent (v5) reports hardware identity

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-18 18:53:30 +02:00

44 lines
1.8 KiB
JavaScript

// Exists so browsers treat the app as installable. It deliberately caches
// nothing and never calls respondWith(): every request goes to the network
// exactly as if there were no service worker. A caching worker would keep
// serving an old JS bundle after a rebuild, and this app is a live view of
// authenticated data with no useful offline mode anyway.
self.addEventListener("install", () => self.skipWaiting());
self.addEventListener("activate", (event) => event.waitUntil(self.clients.claim()));
self.addEventListener("fetch", () => {});
// Push notifications (Settings > Notifications). The message arrives encrypted for this browser only and is read here; a tap opens the app
// at the page it names, reusing a window that is already open.
self.addEventListener("push", (event) => {
let data = {};
try {
data = event.data ? event.data.json() : {};
} catch {
data = { body: event.data ? event.data.text() : "" };
}
event.waitUntil(
self.registration.showNotification(data.title || "Homelab Manager", {
body: data.body || "",
icon: "/icon-192.png",
badge: "/favicon-32.png",
tag: data.tag || undefined,
data: { url: typeof data.url === "string" && data.url.startsWith("/") ? data.url : "/alerts" },
}),
);
});
self.addEventListener("notificationclick", (event) => {
event.notification.close();
const target = new URL((event.notification.data && event.notification.data.url) || "/alerts", self.location.origin).href;
event.waitUntil(
self.clients.matchAll({ type: "window", includeUncontrolled: true }).then((windows) => {
for (const w of windows) {
if (new URL(w.url).origin === self.location.origin && "focus" in w) {
return w.focus().then(() => ("navigate" in w ? w.navigate(target) : undefined));
}
}
return self.clients.openWindow(target);
}),
);
});