-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathservice-worker.js
More file actions
66 lines (60 loc) · 1.59 KB
/
service-worker.js
File metadata and controls
66 lines (60 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
const APP_VERSION = "3";
const CACHE_NAME = `vault-cache-v${APP_VERSION}`;
const FILES_TO_CACHE = [
"/index.html",
"/styles.css",
"/js/app.js",
"/js/pdf.min.js",
"/js/pdf.worker.min.js",
"/manifest.json",
"/icons/192.png",
"/icons/512.png",
"/icons/maskable.png",
"/icons/favicon.ico"
];
// Install: cache everything
self.addEventListener("install", (event) => {
event.waitUntil(
caches.open(CACHE_NAME).then((cache) => cache.addAll(FILES_TO_CACHE))
);
self.skipWaiting();
});
// Activate: clean up old caches
self.addEventListener("activate", (event) => {
event.waitUntil(
caches.keys().then((keyList) =>
Promise.all(
keyList.map((key) => {
if (key !== CACHE_NAME) return caches.delete(key);
})
)
)
);
self.clients.claim();
});
// Fetch: cache-first for offline
self.addEventListener("fetch", (event) => {
if (event.request.method !== "GET") return;
event.respondWith(
caches.match(event.request, { ignoreSearch: true }).then((cached) => {
if (cached) return cached;
return fetch(event.request)
.then((response) => {
const clone = response.clone();
caches.open(CACHE_NAME).then((cache) => {
cache.put(event.request, clone);
});
return response;
})
.catch(() => {
if (event.request.mode === "navigate") {
return caches.match("/index.html");
}
});
})
);
});
// Force update activation from app.js
self.addEventListener("message", (event) => {
if (event.data === "SKIP_WAITING") self.skipWaiting();
});