first commit
This commit is contained in:
55
web/sw.js
Normal file
55
web/sw.js
Normal file
@@ -0,0 +1,55 @@
|
||||
const CACHE_NAME = "drivers-garage-v1";
|
||||
const APP_SHELL = [
|
||||
"/",
|
||||
"/static/app.js",
|
||||
"/static/styles.css",
|
||||
"/static/icon.svg",
|
||||
"/manifest.webmanifest",
|
||||
];
|
||||
|
||||
self.addEventListener("install", (event) => {
|
||||
event.waitUntil(caches.open(CACHE_NAME).then((cache) => cache.addAll(APP_SHELL)));
|
||||
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)))),
|
||||
);
|
||||
self.clients.claim();
|
||||
});
|
||||
|
||||
self.addEventListener("fetch", (event) => {
|
||||
const { request } = event;
|
||||
if (request.method !== "GET") return;
|
||||
if (new URL(request.url).pathname.startsWith("/api/")) return;
|
||||
event.respondWith(
|
||||
fetch(request)
|
||||
.then((response) => {
|
||||
const copy = response.clone();
|
||||
caches.open(CACHE_NAME).then((cache) => cache.put(request, copy));
|
||||
return response;
|
||||
})
|
||||
.catch(() => caches.match(request).then((cached) => cached || caches.match("/"))),
|
||||
);
|
||||
});
|
||||
|
||||
self.addEventListener("push", (event) => {
|
||||
const data = event.data?.json?.() || {};
|
||||
event.waitUntil(
|
||||
self.registration.showNotification(data.title || "Гараж", {
|
||||
body: data.body || "Пора обновить данные по автомобилю.",
|
||||
icon: "/static/icon.svg",
|
||||
badge: "/static/icon.svg",
|
||||
tag: data.tag || "drivers-garage-reminder",
|
||||
data: data.url || "/",
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
self.addEventListener("notificationclick", (event) => {
|
||||
event.notification.close();
|
||||
event.waitUntil(clients.openWindow(event.notification.data || "/"));
|
||||
});
|
||||
Reference in New Issue
Block a user