106 lines
3.4 KiB
JavaScript
106 lines
3.4 KiB
JavaScript
const CACHE_NAME = "nouri-v1-0-0";
|
|
const OFFLINE_URL = "/static/pwa/offline.html";
|
|
const STATIC_ASSETS = [
|
|
"/static/css/styles.css",
|
|
"/static/js/theme.js",
|
|
"/static/js/ui.js",
|
|
"/static/js/planner.js",
|
|
"/static/js/pwa.js",
|
|
"/static/brand/pwa-180.png",
|
|
"/static/brand/pwa-192.png",
|
|
"/static/brand/pwa-512.png",
|
|
"/static/brand/pwa-maskable-512.png",
|
|
"/static/brand/pwa-badge.png",
|
|
"/static/brand/favicon.svg",
|
|
"/app.webmanifest",
|
|
OFFLINE_URL,
|
|
];
|
|
|
|
const cacheFirst = async (request) => {
|
|
const cached = await caches.match(request);
|
|
if (cached) return cached;
|
|
const response = await fetch(request);
|
|
if (response && response.ok && response.type === "basic") {
|
|
const cache = await caches.open(CACHE_NAME);
|
|
cache.put(request, response.clone());
|
|
}
|
|
return response;
|
|
};
|
|
|
|
self.addEventListener("install", (event) => {
|
|
event.waitUntil(
|
|
caches.open(CACHE_NAME).then((cache) => cache.addAll(STATIC_ASSETS)).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;
|
|
|
|
const requestUrl = new URL(event.request.url);
|
|
const isSameOrigin = requestUrl.origin === self.location.origin;
|
|
const isStaticAsset = isSameOrigin && (
|
|
requestUrl.pathname.startsWith("/static/")
|
|
|| requestUrl.pathname === "/app.webmanifest"
|
|
|| requestUrl.pathname === "/service-worker.js"
|
|
);
|
|
const isUpload = isSameOrigin && requestUrl.pathname.startsWith("/uploads/");
|
|
|
|
if (event.request.mode === "navigate") {
|
|
event.respondWith(
|
|
fetch(event.request)
|
|
.then((response) => {
|
|
const copy = response.clone();
|
|
caches.open(CACHE_NAME).then((cache) => cache.put(event.request, copy));
|
|
return response;
|
|
})
|
|
.catch(async () => {
|
|
return (await caches.match(event.request)) || caches.match(OFFLINE_URL);
|
|
})
|
|
);
|
|
return;
|
|
}
|
|
|
|
if (isStaticAsset || isUpload) {
|
|
event.respondWith(cacheFirst(event.request));
|
|
}
|
|
});
|
|
|
|
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;
|
|
})
|
|
);
|
|
});
|