-
Notifications
You must be signed in to change notification settings - Fork 6
/
sw.js
53 lines (46 loc) · 1.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
const version = "font-Playground v1.0";
const cacheFiles = [
"/",
"./dist/script.js",
"./dist/style.css",
"./dist/favicon.svg",
"./fonts/Recursive_VF_1.078.woff2",
];
let channel = new BroadcastChannel("varfont-refresh");
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) =>
/font-playground/gi.test(e)
);
return Promise.all(
cacheNameAry.map((cacheName) => {
if (cacheName !== version) {
caches.delete(cacheName);
}
})
);
})
.then(() => clients.claim())
.then(() => channel.postMessage(`refresh`))
);
});
addEventListener("fetch", (fetchEvent) => {
const request = fetchEvent.request;
fetchEvent.respondWith(
caches.match(request).then((resFromCache) => {
if (resFromCache) return resFromCache;
return fetch(request);
})
);
});