Files
polymarket-analyst_github/sw.js
T

37 lines
1.3 KiB
JavaScript

const CACHE_NAME = "polymarket-arena-v37";
const APP_SHELL = ["/", "/index.html", "/personal.html", "/cycle-worker.js"];
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;
const url = new URL(event.request.url);
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 () => (await caches.match(event.request)) || (await caches.match("/index.html"))));
return;
}
if(url.origin === self.location.origin && !url.pathname.startsWith("/api/")) {
event.respondWith(caches.match(event.request).then(cached => cached || fetch(event.request).then(response => {
const copy = response.clone();
caches.open(CACHE_NAME).then(cache => cache.put(event.request, copy));
return response;
})));
}
});