-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservice-worker.js
More file actions
93 lines (84 loc) · 2.31 KB
/
service-worker.js
File metadata and controls
93 lines (84 loc) · 2.31 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
const cacheName = "gamifydev-v0.72";
self.addEventListener("install", (event) => {
// if there is a new service worker, skip waiting
self.skipWaiting();
event.waitUntil(
caches.open(cacheName).then((cache) => {
return cache.addAll([
"/",
"/index.html",
"/css/main.css",
"/index.js",
"/js/tys.js",
"/js/utils.js",
"/js/about.js",
"/js/contact.js",
"/js/quiz.js",
"/js/external/dexie.js",
"/js/tailwind.min.js",
"/js/storage.js",
"/js/modules.js",
"/data/questions.json",
"/data/app.json",
"/data/notes/history_of_the_web.md",
"/data/notes/exercises.json",
// // '/images/icon.png'
]);
})
);
});
self.addEventListener("activate", (event) => {
event.waitUntil(
caches.keys().then((cacheNames) => {
return Promise.all(
cacheNames
.filter((name) => name !== cacheName)
.map((name) => {
return caches.delete(name);
})
);
})
);
});
// self.addEventListener("fetch", (event) => {
// event.respondWith(
// caches.open(cacheName).then(async (cache) => {
// return cache.match(event.request).then((response) => {
// return response || fetch(event.request);
// });
// })
// );
// });
function isCacheable(request) {
const url = new URL(request.url);
return !url.pathname.endsWith(".json");
}
async function cacheFirstWithRefresh(request) {
const fetchResponsePromise = fetch(request).then(async (networkResponse) => {
if (networkResponse.ok) {
const cache = await caches.open(cacheName);
if (
request.url.startsWith("chrome-extension") ||
request.url.includes("extension") ||
!(request.url.indexOf("http") === 0)
) {
return;
}
cache.put(request, networkResponse.clone());
}
return networkResponse;
});
// if (
// request.url.startsWith("chrome-extension") ||
// request.url.includes("extension") ||
// !(request.url.indexOf("http") === 0)
// ) {
// return;
// }
return (await caches.match(request)) || (await fetchResponsePromise);
}
self.addEventListener("fetch", (event) => {
if (isCacheable(event.request)) {
event.respondWith(cacheFirstWithRefresh(event.request));
}
});