| const CACHE_NAME = "vomebook-search-v1"; |
|
|
| const PRECACHE_URLS = [ |
| "/", |
| "/static/style.css", |
| "/static/app.js", |
| "/manifest.json" |
| ]; |
|
|
| |
| self.addEventListener("install", (event) => { |
| event.waitUntil( |
| caches.open(CACHE_NAME).then((cache) => { |
| return cache.addAll(PRECACHE_URLS).catch((err) => { |
| console.warn("[SW] precache partial failure:", err); |
| }); |
| }).then(() => self.skipWaiting()) |
| ); |
| }); |
|
|
| |
| self.addEventListener("activate", (event) => { |
| event.waitUntil( |
| caches.keys().then((keys) => { |
| return Promise.all( |
| keys.filter((k) => k !== CACHE_NAME).map((k) => caches.delete(k)) |
| ); |
| }).then(() => self.clients.claim()) |
| ); |
| }); |
|
|
| |
| self.addEventListener("fetch", (event) => { |
| const url = new URL(event.request.url); |
|
|
| |
| if (url.hostname !== self.location.hostname) { |
| return; |
| } |
|
|
| |
| if (url.pathname.startsWith("/api/")) { |
| return; |
| } |
|
|
| |
| if (url.pathname.startsWith("/txt/")) { |
| return; |
| } |
|
|
| |
| event.respondWith( |
| caches.match(event.request).then((cached) => { |
| const fetchPromise = fetch(event.request).then((response) => { |
| if (response.ok && response.status !== 206) { |
| const clone = response.clone(); |
| caches.open(CACHE_NAME).then((cache) => { |
| cache.put(event.request, clone); |
| }); |
| } |
| return response; |
| }).catch(() => cached); |
| return cached || fetchPromise; |
| }) |
| ); |
| }); |
|
|