Files
loki/public/sw.js
T
Georgy Butaev e30c842869 feat(desktop) + fix(design,lint): project selection + auto-sync + Sync to Web (ck_* token); canonical injectIntoTab + real appendEvent bridge.dispatch/worker.started in Fleet Runner runtime (replaces fake in-memory); hardened zellij.ts (session resolution + -- write-chars); ui-public-download-* + --public-accent (zero design violations); full cockpit->fleetcrown rebrand in desktop/scripts/docs/thoughts/marketing/home; update desktop README + docs/desktop-app.md with capabilities + gaps (in-memory vs real log)
- desktop build clean, tsc clean, lint 0, home/ 89/89
- advances desktop-2/3: dispatches now produce durable events in ~/.fleetcrown/events.jsonl with proper runId/sentinel so stop-hooks and web control plane treat them as first-class
- still no embedded watcher (idle via co-running home/watcher.ts for now)

Refs: desktop-app.md phases, Cockpit.roadmap T1/T2, prior landing 2d98638
2026-06-02 14:54:51 +02:00

57 lines
1.8 KiB
JavaScript

/**
* FleetCrown service worker — handles Web Push events.
*
* Scope: root ("/") so notifications fire regardless of which app page is open
* (or none — that's the point of background push).
*
* Two events:
* - "push" — show the OS notification using the payload body
* - "notificationclick" — bring an open tab to focus, or open one at the URL
*/
self.addEventListener("install", (event) => {
event.waitUntil(self.skipWaiting());
});
self.addEventListener("activate", (event) => {
event.waitUntil(self.clients.claim());
});
self.addEventListener("push", (event) => {
let data = { title: "FleetCrown", body: "Agent update", url: "/control", tag: "fleetcrown" };
if (event.data) {
try { data = { ...data, ...event.data.json() }; }
catch { data.body = event.data.text() || data.body; }
}
event.waitUntil(
self.registration.showNotification(data.title, {
body: data.body,
tag: data.tag,
data: { url: data.url },
// Coalesce same-tag notifications instead of stacking; the user only
// needs to see the latest state for a given tab.
renotify: false,
}),
);
});
self.addEventListener("notificationclick", (event) => {
const url = event.notification.data?.url || "/control";
event.notification.close();
event.waitUntil((async () => {
const all = await self.clients.matchAll({ type: "window", includeUncontrolled: true });
// Focus a FleetCrown tab if one's already open; navigate it to the target.
for (const client of all) {
try {
const u = new URL(client.url);
if (u.origin === self.location.origin) {
await client.focus();
if ("navigate" in client) await client.navigate(url);
return;
}
} catch { /* ignore */ }
}
await self.clients.openWindow(url);
})());
});