forked from EntropiaFox/astranauta.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sw.js
81 lines (68 loc) · 2.08 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
importScripts("./js/sw-files.js");
const cacheName = /* 5ETOOLS_VERSION__OPEN */"1.92.1"/* 5ETOOLS_VERSION__CLOSE */;
const cacheableFilenames = new Set(filesToCache);
let isCacheRunning;
function getPath (urlOrPath) {
// Add a fake domain name to allow proper URL conversion
if (urlOrPath.startsWith("/")) urlOrPath = `https://5e.com${urlOrPath}`;
return (new URL(urlOrPath)).pathname;
}
// Installing Service Worker
self.addEventListener("install", () => {
self.skipWaiting();
});
self.addEventListener("activate", e => {
clients.claim();
// Remove any outdated caches
e.waitUntil((async () => {
const cacheNames = await caches.keys();
await Promise.all(cacheNames.filter(name => name !== cacheName).map(name => caches.delete(name)));
})());
});
async function getOrCache (url, retryCount = 0) {
const path = getPath(url);
const fromCache = await caches.match(path);
if (fromCache) return fromCache;
while (true) {
let response;
try {
response = await fetch(url);
} catch (e) {
if (retryCount-- > 0) continue;
else throw e;
}
const cache = await caches.open(cacheName);
cache.put(path, response.clone()); // don't await
return response;
}
}
self.addEventListener("fetch", e => {
const url = e.request.url;
const path = getPath(url);
if (!cacheableFilenames.has(path)) return;
e.respondWith(getOrCache(url));
});
self.addEventListener("message", async e => {
const send = (msgOut) => e.ports[0].postMessage(msgOut);
const msg = e.data;
switch (msg.type) {
case "cache-cancel":
isCacheRunning = false;
break;
case "cache-start": {
isCacheRunning = true;
for (let i = 0; i < filesToCache.length; ++i) {
if (!isCacheRunning) return send({type: "download-cancelled"});
try {
await getOrCache(filesToCache[i]);
} catch (e) {
return send({type: "download-error"});
}
if (!isCacheRunning) return send({type: "download-cancelled"});
if (i % 50) send({type: "download-progress", data: {pct: `${((i / filesToCache.length) * 100).toFixed(2)}%`}});
}
send({type: "download-progress", data: {pct: `100%`}});
break;
}
}
});