Files

38 lines
1.1 KiB
JavaScript

// Service worker del panel de cliente: solo notificaciones push.
// Sin estrategias de cache — el panel siempre va a red.
self.addEventListener("push", (event) => {
let data = {};
try {
data = event.data ? event.data.json() : {};
} catch {
data = {};
}
const titulo = (data && data.titulo) || "Cotizador — Uriel Jareth Consulting";
const cuerpo = (data && data.cuerpo) || "";
const url = (data && data.url) || "/cliente/pagos";
event.waitUntil(
self.registration.showNotification(titulo, {
body: cuerpo,
icon: "/favicon.ico",
data: { url },
})
);
});
self.addEventListener("notificationclick", (event) => {
const data = event.notification.data || {};
const url = data.url || "/cliente/pagos";
event.notification.close();
event.waitUntil(
self.clients
.matchAll({ type: "window", includeUncontrolled: true })
.then((cs) => {
const existing = cs.find((c) => "focus" in c);
return existing
? existing.navigate(url).then(() => existing.focus())
: self.clients.openWindow(url);
})
);
});