49 lines
No EOL
1.1 KiB
JavaScript
49 lines
No EOL
1.1 KiB
JavaScript
/// <reference lib="webworker" />
|
|
|
|
declare const self: ServiceWorkerGlobalType;
|
|
|
|
const CACHE_NAME = "tia-v1";
|
|
const STATIC_ASSETS = [
|
|
"/",
|
|
"/manifest.json",
|
|
];
|
|
|
|
self.addEventListener("install", (event) => {
|
|
event.waitUntil(
|
|
caches.open(CACHE_NAME).then((cache) => cache.addAll(STATIC_ASSETS))
|
|
);
|
|
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) => {
|
|
// Skip non-GET requests
|
|
if (event.request.method !== "GET") return;
|
|
|
|
// Skip API requests
|
|
if (event.request.url.includes("/api/")) return;
|
|
|
|
event.respondWith(
|
|
caches.match(event.request).then((cached) => {
|
|
const networked = fetch(event.request)
|
|
.then((response) => {
|
|
const clone = response.clone();
|
|
caches.open(CACHE_NAME).then((cache) => cache.put(event.request, clone));
|
|
return response;
|
|
})
|
|
.catch(() => cached);
|
|
|
|
return cached || networked;
|
|
})
|
|
);
|
|
});
|
|
|
|
export {}; |