add expiry push notifications
Deploy to Cloudflare Pages / deploy (push) Successful in 1m27s

This commit is contained in:
2026-08-24 13:07:38 +00:00 Unverified
parent 76782541b0
commit e06cd700f6
24 changed files with 3001 additions and 12 deletions
+33
View File
@@ -0,0 +1,33 @@
/* global self */
self.addEventListener("push", (event) => {
let message = {
title: "HotFridge",
body: "You have food expiring soon.",
url: "/app/list",
};
try {
message = { ...message, ...JSON.parse(event.data?.text() ?? "{}") };
} catch {
// Keep the generic notification when a payload cannot be decoded.
}
event.waitUntil(
self.registration.showNotification(message.title, {
body: message.body,
tag: "near-expiry",
renotify: true,
data: { url: message.url },
}),
);
});
self.addEventListener("notificationclick", (event) => {
event.notification.close();
const url = event.notification.data?.url ?? "/app/list";
event.waitUntil(
self.clients.matchAll({ type: "window", includeUncontrolled: true }).then((clients) => {
const existing = clients.find((client) => new URL(client.url).pathname === url);
return existing ? existing.focus() : self.clients.openWindow(url);
}),
);
});