-
Notifications
You must be signed in to change notification settings - Fork 0
/
sw.js
61 lines (54 loc) · 1.3 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
const version = "p5 v2.0";
const cacheFiles = [
"./",
"./dist/app.css",
"./dist/style.css",
"./dist/app.js",
"./dist/editor.worker.js",
"./dist/ts.worker.js",
"./dist/codicon-4OFKM6SH.ttf",
"./dist/favicon.ico",
];
let channel = null;
try {
channel = new BroadcastChannel("p5-refresh");
} catch (err) {
console.log("-");
}
addEventListener("install", (installEvent) => {
skipWaiting();
installEvent.waitUntil(
caches.open(version).then((GlyphsCache) => {
return GlyphsCache.addAll(cacheFiles);
})
);
});
addEventListener("activate", (activateEvent) => {
activateEvent.waitUntil(
caches
.keys()
.then((cacheNames) => {
let cacheNameAry = cacheNames.filter((e) => /p5/gi.test(e));
return Promise.all(
cacheNameAry.map((cacheName) => {
if (cacheName !== version) {
caches.delete(cacheName);
}
})
);
})
.then(() => clients.claim())
.then(() => {
if (channel) channel.postMessage(`refresh`);
})
);
});
addEventListener("fetch", (fetchEvent) => {
const request = fetchEvent.request;
fetchEvent.respondWith(
caches.match(request).then((resFromCache) => {
if (resFromCache) return resFromCache;
return fetch(request);
})
);
});