import { Serwist, NetworkFirst, ExpirationPlugin } from "serwist"; import { defaultCache } from "@serwist/next/worker"; const serwist = new Serwist({ precacheEntries: self.__SW_MANIFEST, skipWaiting: true, clientsClaim: true, navigationPreload: true, runtimeCaching: [ ...defaultCache, { matcher: ({ url }) => url.pathname.startsWith("/api/"), handler: new NetworkFirst({ cacheName: "api-cache", plugins: [ new ExpirationPlugin({ maxEntries: 50, maxAgeSeconds: 60 * 60, }), ], }), }, ], fallbacks: { entries: [ { url: "/~offline", matcher({ request }) { return request.destination === "document"; }, }, ], }, }); serwist.addEventListeners(); self.addEventListener("push", (event) => { console.log("[Service Worker] Push received:", event); let data = {}; if (event.data) { try { data = event.data.json(); } catch { data = { title: "Notification", body: event.data.text() }; } } const title = data.title || "Default title"; const options = { body: data.body || "Default body", icon: data.icon || "/logo.png", badge: "/logo.png", vibrate: [100, 50, 100], }; event.waitUntil(self.registration.showNotification(title, options)); }); self.addEventListener("notificationclick", (event) => { event.notification.close(); event.waitUntil(clients.openWindow("/")); });