-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsw.js
63 lines (58 loc) · 1.39 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
var cacheName = "TicTacToeV28";
var appShellFiles = [
".",
"index.html",
"gamelogic.js",
"style.css",
"confetti.browser.min.js",
"poppins.woff2",
"icons/favicon/favicon.ico",
"icons/android/android-launchericon-512-512.png",
];
self.addEventListener("install", (e) => {
console.log("[Service Worker] Install");
e.waitUntil(
caches.open(cacheName).then((cache) => {
console.log("[Service Worker] Caching all: app shell and content");
return cache.addAll(appShellFiles);
})
);
});
self.addEventListener("activate", (e) => {
e.waitUntil(
caches.keys().then((keyList) => {
return Promise.all(
keyList.map((key) => {
if (key !== cacheName) {
return caches.delete(key);
}
})
);
})
);
});
self.addEventListener("fetch", (e) => {
e.respondWith(
caches.match(e.request).then((r) => {
console.log("[Service Worker] Fetching resource: " + e.request.url);
return (
r ||
fetch(e.request).then((response) => {
return caches.open(cacheName).then((cache) => {
console.log(
"[Service Worker] Caching new resource: " +
e.request.url
);
cache.put(e.request, response.clone());
return response;
});
})
);
})
);
});
self.addEventListener("message", function (event) {
if (event.data.action === "skipWaiting") {
self.skipWaiting();
}
});