56 lines
1.5 KiB
JavaScript
56 lines
1.5 KiB
JavaScript
const CACHE_NAME = "facereg-v1";
|
|
const ASSETS = [
|
|
"https://cdn.jsdelivr.net/npm/@mdi/font@7.4.47/css/materialdesignicons.min.css",
|
|
"https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700;800&display=swap",
|
|
"https://cdn.jsdelivr.net/npm/face-api.js@0.22.2/dist/face_api.min.js",
|
|
];
|
|
|
|
self.addEventListener("install", (event) => {
|
|
event.waitUntil(
|
|
caches.open(CACHE_NAME).then((cache) => cache.addAll(ASSETS)),
|
|
);
|
|
self.skipWaiting();
|
|
});
|
|
|
|
self.addEventListener("activate", (event) => {
|
|
event.waitUntil(
|
|
caches
|
|
.keys()
|
|
.then((keys) =>
|
|
Promise.all(
|
|
keys.filter((k) => k !== CACHE_NAME).map((k) => caches.delete(k)),
|
|
),
|
|
),
|
|
);
|
|
self.clients.claim();
|
|
});
|
|
|
|
self.addEventListener("fetch", (event) => {
|
|
// Skip non-GET and API requests
|
|
if (event.request.method !== "GET") return;
|
|
const url = new URL(event.request.url);
|
|
if (
|
|
url.pathname.includes("save_foto.php") ||
|
|
url.pathname.includes("validate_foto_token.php")
|
|
)
|
|
return;
|
|
|
|
event.respondWith(
|
|
caches.match(event.request).then((cached) => {
|
|
return (
|
|
cached ||
|
|
fetch(event.request).then((response) => {
|
|
// Cache CDN resources
|
|
if (event.request.url.startsWith("https://cdn.")) {
|
|
const clone = response.clone();
|
|
caches
|
|
.open(CACHE_NAME)
|
|
.then((cache) => cache.put(event.request, clone));
|
|
}
|
|
return response;
|
|
})
|
|
);
|
|
}),
|
|
);
|
|
});
|