-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsw.js
57 lines (54 loc) · 1.48 KB
/
sw.js
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
let cacheName = `cache_qr_v0.3.4c`;
let cachedFiles = [
"./icons/icon192x192.png",
"./lib/qr-scanner-worker.min.js", // TODO: use second cache only for library files?
"./lib/qr-scanner.min.js",
"./lib/qrcodegen.js",
"./bundle.js",
"./favicon32x32.png",
"./index.html",
"./pwa.js",
"./site.webmanifest",
"./style.css"
];
self.addEventListener("install", (event) => {
event.waitUntil(
caches.open(cacheName)
.then((cache) => {
return cache.addAll(cachedFiles);
})
.then(() => {
return self.skipWaiting();
})
.catch((error) => {
console.log(error);
})
);
});
self.addEventListener("activate", (event) => {
event.waitUntil(
caches.keys()
.then((keyList) => {
return Promise.all(keyList.map((key) => {
if (key !== cacheName) {
return caches.delete(key);
}
}));
})
);
return self.clients.claim();
});
self.addEventListener("fetch", (event) => {
event.respondWith(
caches.match(event.request)
.then((response) => {
return response || fetch(event.request)
.then((response) => {
return caches.open(cacheName).then((cache) => {
cache.put(event.request, response.clone());
return response;
});
});
})
);
});