forked from ondras/rri
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsw.js
37 lines (31 loc) · 871 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
36
37
const CACHE_NAME = "offline";
const urlsToCache = [".", "client/client.css", "client/client.js", "img/icon.svg"];
async function precache() {
const cache = await caches.open(CACHE_NAME);
return cache.addAll(urlsToCache);
};
async function respondTo(request) {
let f = fetch(request);
const cached = await caches.match(request);
if (cached) { // try updating the cache first
try {
let response = await f;
let cache = await caches.open(CACHE_NAME);
cache.put(request, response.clone());
return response;
} catch (e) { // offline
return cached;
}
} else { // not cached, forward to network
return f;
}
};
async function onInstall(e) {
self.skipWaiting();
e.waitUntil(precache());
}
async function onFetch(e) {
e.respondWith(respondTo(e.request));
}
self.addEventListener("install", onInstall);
self.addEventListener("fetch", onFetch);