75 lines
2.4 KiB
JavaScript
75 lines
2.4 KiB
JavaScript
const CACHE_NAME = "nouri-v0-5-0";
|
|
const APP_SHELL = [
|
|
"/",
|
|
"/static/css/styles.css",
|
|
"/static/js/theme.js",
|
|
"/static/js/ui.js",
|
|
"/static/js/planner.js",
|
|
"/static/js/pwa.js",
|
|
"/static/brand/pwa-192.png",
|
|
"/static/brand/pwa-512.png",
|
|
"/static/brand/favicon.svg",
|
|
];
|
|
|
|
self.addEventListener("install", (event) => {
|
|
event.waitUntil(
|
|
caches.open(CACHE_NAME).then((cache) => cache.addAll(APP_SHELL)).then(() => self.skipWaiting())
|
|
);
|
|
});
|
|
|
|
self.addEventListener("activate", (event) => {
|
|
event.waitUntil(
|
|
caches.keys().then((keys) =>
|
|
Promise.all(keys.filter((key) => key !== CACHE_NAME).map((key) => caches.delete(key)))
|
|
).then(() => self.clients.claim())
|
|
);
|
|
});
|
|
|
|
self.addEventListener("fetch", (event) => {
|
|
if (event.request.method !== "GET") return;
|
|
event.respondWith(
|
|
caches.match(event.request).then((cached) => {
|
|
if (cached) return cached;
|
|
return fetch(event.request).then((response) => {
|
|
if (!response || response.status !== 200 || response.type !== "basic") {
|
|
return response;
|
|
}
|
|
const clone = response.clone();
|
|
caches.open(CACHE_NAME).then((cache) => cache.put(event.request, clone));
|
|
return response;
|
|
});
|
|
})
|
|
);
|
|
});
|
|
|
|
self.addEventListener("push", (event) => {
|
|
if (!event.data) return;
|
|
const data = event.data.json();
|
|
event.waitUntil(
|
|
self.registration.showNotification(data.title || "Nouri", {
|
|
body: data.body || "",
|
|
icon: data.icon || "/static/brand/pwa-192.png",
|
|
badge: data.badge || "/static/brand/pwa-badge.png",
|
|
data: { url: data.url || "/" },
|
|
})
|
|
);
|
|
});
|
|
|
|
self.addEventListener("notificationclick", (event) => {
|
|
event.notification.close();
|
|
const targetUrl = event.notification.data && event.notification.data.url ? event.notification.data.url : "/";
|
|
event.waitUntil(
|
|
clients.matchAll({ type: "window", includeUncontrolled: true }).then((clientList) => {
|
|
for (const client of clientList) {
|
|
if (client.url.includes(targetUrl) && "focus" in client) {
|
|
return client.focus();
|
|
}
|
|
}
|
|
if (clients.openWindow) {
|
|
return clients.openWindow(targetUrl);
|
|
}
|
|
return null;
|
|
})
|
|
);
|
|
});
|