-
Notifications
You must be signed in to change notification settings - Fork 0
/
sw.js
65 lines (60 loc) · 1.53 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
64
65
// Establish a cache name
const cacheName = "CHHSClockCache_Oct2024_v1";
const cachedItems = [
"/index.html",
"/main.js",
"/main.css",
"/images/favicon-32.png",
"/images/favicon-16.png",
"/sw.js",
];
var debugLogs = false;
function log() {
if (!debugLogs) return;
console.log.apply(this, arguments);
}
self.addEventListener("install", (event) => {
log("Installing...");
event.waitUntil(caches.open(cacheName));
});
// remove cached items when new cache exists
self.addEventListener("activate", (e) => {
log("Activating...");
e.waitUntil(
caches.keys().then((keyList) => {
log(keyList);
return Promise.all(
keyList.map((key) => {
if (key === cacheName) {
return;
}
return caches.delete(key);
}),
);
}),
);
});
// Network first, cache fallback strategy
self.addEventListener("fetch", (event) => {
var parsedUrl = new URL(event.request.url).pathname;
if (parsedUrl === "/") parsedUrl = "/index.html";
// Check if this is one of our cached URLs
if (cachedItems.includes(parsedUrl)) {
// Open the cache
event.respondWith(caches.open(cacheName).then((cache) => {
// Go to the network first
return fetch(event.request.url).then((fetchedResponse) => {
log("Network first! " + parsedUrl)
cache.put(event.request, fetchedResponse.clone());
return fetchedResponse;
}).catch(() => {
// If the network is unavailable, get
log("From the cache: " + parsedUrl);
return cache.match(event.request.url);
});
}));
} else {
log("Not on the list: " + parsedUrl);
return;
}
});