-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice-worker.js
67 lines (57 loc) · 1.41 KB
/
service-worker.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
const CACHE_NAME = "chess-clock";
let requiredContent = [
"./",
"index.html",
"style.css",
"js/main.js",
"js/chess-clock.js",
"js/chess-clock-element.js",
"js/time-selection.js",
"icons/icon.svg",
"images/cached-white-18dp.svg",
"images/delete-white-18dp.svg",
"images/fullscreen-white-18dp.svg",
"images/fullscreen_exit-white-18dp.svg",
"images/keyboard_arrow_down-white-18dp.svg",
"images/keyboard_arrow_up-white-18dp.svg",
"images/pause-white-18dp.svg",
"images/settings-white-18dp.svg",
"sound/timeout.ogg",
];
console.log("[Service Worker] running");
self.addEventListener("install", e =>
{
console.log("[Service Worker] Install");
e.waitUntil(
(async () => {
let cache = await caches.open(CACHE_NAME);
for (let url of requiredContent)
{
try
{
await cache.add(url);
}
catch (e)
{
console.warn("[Service Worker] unable to cache:", url);
}
}
})()
);
});
self.addEventListener("fetch", e => {
console.log("[Service Worker] onfetch");
e.respondWith(
(async () => {
console.log('[Service Worker] Fetching resource: ' + e.request.url);
let result = await caches.match(e.request);
if (result)
return result;
let response = await fetch(e.request);
let cache = await caches.open(CACHE_NAME);
console.log('[Service Worker] Caching new resource: ' + e.request.url);
cache.put(e.request, response.clone());
return response;
})()
);
});