Spaces:
Running
Running
| const CACHE_NAME = "annotator-yolo-v5"; | |
| const PRECACHE = [ | |
| "./", | |
| "./index.html", | |
| "./manifest.json", | |
| "./sw.js", | |
| "./icon.svg", | |
| "./icon-192.png", | |
| "./icon-512.png", | |
| "./README.md" | |
| ]; | |
| self.addEventListener("install", (event) => { | |
| self.skipWaiting(); | |
| event.waitUntil(caches.open(CACHE_NAME).then((cache) => cache.addAll(PRECACHE))); | |
| }); | |
| self.addEventListener("activate", (event) => { | |
| event.waitUntil((async () => { | |
| const keys = await caches.keys(); | |
| await Promise.all(keys.filter((key) => key !== CACHE_NAME).map((key) => caches.delete(key))); | |
| await self.clients.claim(); | |
| })()); | |
| }); | |
| self.addEventListener("fetch", (event) => { | |
| const request = event.request; | |
| if (request.method !== "GET") return; | |
| const url = new URL(request.url); | |
| if (request.mode === "navigate") { | |
| event.respondWith((async () => { | |
| try { | |
| const fresh = await fetch(request); | |
| const cache = await caches.open(CACHE_NAME); | |
| cache.put("./index.html", fresh.clone()); | |
| return fresh; | |
| } catch { | |
| return (await caches.match(request)) || (await caches.match("./index.html")); | |
| } | |
| })()); | |
| return; | |
| } | |
| if (url.origin === location.origin) { | |
| event.respondWith((async () => { | |
| const cached = await caches.match(request); | |
| if (cached) return cached; | |
| const fresh = await fetch(request); | |
| const cache = await caches.open(CACHE_NAME); | |
| cache.put(request, fresh.clone()); | |
| return fresh; | |
| })()); | |
| } | |
| }); | |