-
Notifications
You must be signed in to change notification settings - Fork 0
/
sw.js
35 lines (31 loc) · 931 Bytes
/
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
async function addResourcesToCache (resources) {
const cache = await caches.open("v1.0.0b1.1.2");
await cache.addAll(resources);
};
self.addEventListener("install", (event) => {
event.waitUntil(
addResourcesToCache([
"./",
"./index.html",
"./style.css",
"./app.js",
"./red-cross.svg"
])
);
});
async function putInCache (request, response) {
const cache = await caches.open("v1");
await cache.put(request, response);
};
async function cacheFirst (request) {
const responseFromCache = await caches.match(request);
if (responseFromCache) {
return responseFromCache;
}
const responseFromNetwork = await fetch(request);
putInCache(request, responseFromNetwork.clone());
return responseFromNetwork;
};
self.addEventListener("fetch", (event) => {
event.respondWith(cacheFirst(event.request));
});