-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsw.js
56 lines (42 loc) · 1.12 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
var echat_v = "p-e-4104-37";
var assets = [
/*
"/?mnps=welcomer-pl",
"/svc=aet",
"/favicon.svg",
"/manifest.webmanifest"
*/
"/"
];
self.addEventListener("install", async event => {
const cache = await caches.open(echat_v);
cache.addAll(assets);
});
self.addEventListener("fetch", event => {
const req = event.request;
const url = new URL(req.url);
if (url.origin === location.origin) {
event.respondWith(cacheFirst(req));
} else {
event.respondWith(networkFirst(req));
}
});
self.addEventListener('push', event => {
console.log('Push message', event);
// event.waitUntl(fetch())
});
async function cacheFirst(req) {
const cachedResponse = await caches.match(req);
return cachedResponse || fetch(req);
}
async function networkFirst(req) {
const cache = await caches.open("dynamic-content");
try {
const res = await fetch(req);
cache.put(req, res.clone());
return res;
} catch (err) {
const cachedResponse = await cache.match(req);
return cachedResponse || caches.match("./fallback.json");
}
}