-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathservice-worker.js
45 lines (41 loc) · 1.08 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
const cacheName = "2048PWA";
const appShellFiles = [
"./index.html",
"./style.css",
"./app.js",
"./assets/2048-192x192.png",
"./assets/2048-512x512.png",
"./assets/2048-mobile-ui.png",
"./assets/2048-og.png",
"./assets/2048-UI.png",
"./assets/animatedbg.gif",
"./assets/arrow.png",
"./assets/bg.gif",
"./assets/defaultbg.png",
"./assets/favicon.ico",
"./assets/howtobg.png",
"./assets/translate.svg",
];
const handlePWAInstall = (event) => {
event.waitUntil(
(async () => {
const cache = await caches.open(cacheName);
await cache.addAll(appShellFiles);
})()
);
};
const handleFetch = (event) => {
event.respondWith(
(async () => {
const request = await caches.match(event.request);
console.log(request);
if (request) return request;
const response = await fetch(event.request);
const cache = await caches.open(cacheName);
await cache.put(event.request, response.clone());
return response;
})()
);
};
self.addEventListener("install", handlePWAInstall);
self.addEventListener("fetch", handleFetch);