forked from EntropiaFox/astranauta.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsw.js
111 lines (95 loc) · 3.17 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/**
* Dbgging notes:
* - **CTRL+F5 is unreliable**
* - spam "Clear Site Data" in DevTools
* - use "update on reload" in Service Workers DevTools section
* - sanity-check code to ensure it has updated
*/
importScripts("./js/sw-files.js");
const cacheName = /* 5ETOOLS_VERSION__OPEN */"1.110.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, responseMeta) {
responseMeta = responseMeta || {};
const path = getPath(url);
const fromCache = await caches.match(path);
if (fromCache) {
responseMeta.fromCache = true;
return fromCache;
}
let retryCount = 3;
while (true) {
let response;
try {
const controller = new AbortController();
setTimeout(() => controller.abort(), 30 * 1000);
response = await fetch(url, {signal: controller.signal});
} catch (e) {
if (retryCount-- > 0) continue;
else throw e;
}
const cache = await caches.open(cacheName);
// throttle this with `await` to ensure Firefox doesn't die under load
await cache.put(path, response.clone());
responseMeta.fromCache = false;
return response;
}
}
// All data loading (JSON, images, etc) passes through here when the service worker is active
self.addEventListener("fetch", e => {
const url = e.request.url;
const path = getPath(url);
if (!cacheableFilenames.has(path)) return;
const responseMeta = {};
e.respondWith((async () => {
const toReturn = await getOrCache(url, responseMeta);
if (responseMeta.fromCache) {
// TODO if we grabbed the result from the cache, we could here asynchronously launch a hard fetch to
// update the cache? Currently is of no relevance, as the user can simply refresh to have the service
// worker realise it is outdated and flush everything.
}
return toReturn;
})());
});
self.addEventListener("message", async evt => {
const send = (msgOut) => evt.ports[0].postMessage(msgOut);
const msg = evt.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) {
debugger
return send({type: "download-error", message: ((e.stack || "").trim()) || e.name});
}
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;
}
}
});