-
Notifications
You must be signed in to change notification settings - Fork 0
/
sw.js
49 lines (44 loc) · 1.41 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
const cacheName = 'nicobako';
self.addEventListener("install", (event) => {
event.waitUntil(
caches
.open(cacheName)
.then((cache) =>
cache.addAll([
"/",
"/index.html",
"/static/pico.min.css",
"/static/nico-bako.png",
"/pages/about_me.html",
"/pages/about_site.html",
"/pages/other_work.html",
"/pages/other_links.html",
"https://unpkg.com/[email protected]",
"https://unpkg.com/[email protected]",
]),
),
);
});
self.addEventListener("fetch", (event) => {
// Let the browser do its default thing
// for non-GET requests.
if (event.request.method !== "GET") return;
// Prevent the default, and handle the request ourselves.
event.respondWith(
(async () => {
// Try to get the response from a cache.
const cache = await caches.open(cacheName);
const cachedResponse = await cache.match(event.request);
if (cachedResponse) {
// If we found a match in the cache, return it, but also
// update the entry in the cache in the background.
event.waitUntil(cache.add(event.request));
console.log("resource found in cache");
return cachedResponse;
}
// If we didn't find a match in the cache, use the network.
console.log("resource not found in cache");
return fetch(event.request);
})(),
);
});