-
Notifications
You must be signed in to change notification settings - Fork 2
/
sw.js
88 lines (80 loc) · 2.19 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
const CACHE_NAME = "sw:i:12"
self.addEventListener('activate', async e => {
e.waitUntil((async () => {
const keys = await caches.keys();
keys.forEach(async i => {
if (i.startsWith("sw:") && i !== CACHE_NAME)
await caches.delete(i);
});
})());
});
self.addEventListener('fetch', async e => {
e.respondWith((async () => {
const url = new URL(e.request.url);
let nohint = true,
uquery = false,
cors = false,
name = e.request;
if (url.hostname !== self.location.hostname) {
cors = true;
}
if (URL.prototype.hasOwnProperty("searchParams")) {
hc = url.searchParams.get("hc");
if (hc != null) {
nohint = false;
switch (hc) {
case "uquery":
uquery = true;
break;
}
const cors_hint = url.searchParams.get("cors")
if (cors_hint != null) {
cors = (cors_hint == "1");
}
const uid = url.searchParams.get("uid");
if (uid != null) {
url.searchParams.delete("uid");
name = "/_virtual/" + uid + url.search;
} else if (cors) {
const cors_uid = url.searchParams.get("cors_uid");
if (cors_uid != null) {
url.searchParams.delete("cors_uid");
name = "/_virtual/_cors/" + cors_uid + url.search;
}
}
}
}
if (nohint &&
!url.pathname.startsWith("/assets/") &&
!url.pathname.startsWith("/styles/") &&
!url.pathname.startsWith("/icons/"))
return fetch(e.request.clone());
const cache = await caches.open(CACHE_NAME);
let resp = await cache.match(name);
if (!resp) {
if (uquery) {
const oldresp = await cache.matchAll(name, {"ignoreSearch": true});
if (oldresp != null && oldresp.length > 0) {
console.log('sw: deleting old cache', name);
await cache.delete(name, {"ignoreSearch": true});
/*oldresp.forEach((el, index, arr) => {
console.log('sw: deleting the response to', el.url);
cache.delete(el.url);
});*/
}
}
let req;
if (cors) {
req = new Request(e.request.url, {mode: 'cors'});
} else {
req = e.request.clone();
}
resp = await fetch(req);
if (resp.status < 400) {
console.log('sw: caching the response to', e.request.url);
await cache.put(name, resp.clone());
}
}
return resp;
})());
});