From 660017edda3666a180281d96fc1d0cbb42faa6ac Mon Sep 17 00:00:00 2001 From: Nathan Lamy Date: Tue, 19 Aug 2025 22:56:00 +0200 Subject: [PATCH] feat: add service worker --- app/components/settings/notifications.tsx | 2 +- public/sw/push.js | 20 ++++++++++++++++++++ pwa.config.ts | 6 +++--- 3 files changed, 24 insertions(+), 4 deletions(-) create mode 100644 public/sw/push.js diff --git a/app/components/settings/notifications.tsx b/app/components/settings/notifications.tsx index bfeacad..9d171ba 100644 --- a/app/components/settings/notifications.tsx +++ b/app/components/settings/notifications.tsx @@ -127,7 +127,7 @@ export default function NotificationSettings() { - {!isInstalled() ? ( + {isInstalled() ? ( pushEnabled ? (
diff --git a/public/sw/push.js b/public/sw/push.js new file mode 100644 index 0000000..6d2f242 --- /dev/null +++ b/public/sw/push.js @@ -0,0 +1,20 @@ +self.addEventListener("push", onPush); + +async function onPush(event) { + if (event.data) { + const data = event.data.json(); + const { title, ...rest } = data; + + // Send the push data to the application + const clients = await self.clients.matchAll(); + clients.forEach((client) => client.postMessage(data)); + + await event.waitUntil( + self.registration.showNotification(title, { + ...rest, + }) + ); + } +} + +// self.addEventListener("notificationclick", function (event) {}); diff --git a/pwa.config.ts b/pwa.config.ts index ae6170e..1d6404a 100644 --- a/pwa.config.ts +++ b/pwa.config.ts @@ -1,7 +1,7 @@ export default { - // workbox: { - // importScripts: ["/sw/push.js"], - // }, + workbox: { + importScripts: ["/sw/push.js"], + }, includeAssets: [ "favicon.ico", "favicon-96x96.png",