Sessions 5-7a: 955 tests, deployment ready
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
/// <reference lib="webworker" />
|
||||
/// <reference types="@serwist/next/typings" />
|
||||
|
||||
import { defaultCache } from '@serwist/next/worker';
|
||||
import { Serwist } from 'serwist';
|
||||
|
||||
declare const self: ServiceWorkerGlobalScope & {
|
||||
__SW_MANIFEST: (string | { url: string; revision: string | null })[];
|
||||
};
|
||||
|
||||
const serwist = new Serwist({
|
||||
precacheEntries: self.__SW_MANIFEST,
|
||||
skipWaiting: true,
|
||||
clientsClaim: true,
|
||||
navigationPreload: true,
|
||||
runtimeCaching: defaultCache,
|
||||
});
|
||||
|
||||
serwist.addEventListeners();
|
||||
|
||||
// Web Push handler — fires when the push service delivers a notification.
|
||||
// Pushes are emitted server-side by src/services/distribution/webPush.js.
|
||||
self.addEventListener('push', (event) => {
|
||||
if (!event.data) return;
|
||||
let payload: { title?: string; body?: string; icon?: string; url?: string };
|
||||
try {
|
||||
payload = event.data.json();
|
||||
} catch {
|
||||
payload = { title: 'VYNDR', body: event.data.text() };
|
||||
}
|
||||
const { title = 'VYNDR', body = '', icon = '/icons/icon-192.png', url = '/' } = payload;
|
||||
event.waitUntil(
|
||||
self.registration.showNotification(title, {
|
||||
body,
|
||||
icon,
|
||||
badge: '/icons/icon-192.png',
|
||||
data: { url },
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
self.addEventListener('notificationclick', (event) => {
|
||||
event.notification.close();
|
||||
const url = (event.notification.data as { url?: string } | undefined)?.url ?? '/';
|
||||
event.waitUntil(
|
||||
self.clients.matchAll({ type: 'window', includeUncontrolled: true }).then((clients) => {
|
||||
const existing = clients.find((c) => c.url.endsWith(url));
|
||||
if (existing) return existing.focus();
|
||||
return self.clients.openWindow(url);
|
||||
})
|
||||
);
|
||||
});
|
||||
Reference in New Issue
Block a user