-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsw.js
34 lines (30 loc) · 831 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
// (A) FILES TO CACHE
const cName = "Weather",
const version = '1.01',
cFiles = [
"index.html",
"style.css",
"pulltorefresh.js",
"main.js"
];
// (B) CREATE/INSTALL CACHE
self.addEventListener("install", (evt) => {
evt.waitUntil(
caches.open(cName)
.then((cache) => { return cache.addAll(cFiles); })
.catch((err) => { console.error(err) })
);
});
// (C) CACHE STRATEGY
self.addEventListener("fetch", (evt) => {
// // (C1) LOAD FROM CACHE FIRST, FALLBACK TO NETWORK IF NOT FOUND
// evt.respondWith(
// caches.match(evt.request)
// .then((res) => { return res || fetch(evt.request); })
// );
// (C2) LOAD WITH NETWORK FIRST, FALLBACK TO CACHE IF OFFLINE
evt.respondWith(
fetch(evt.request)
.catch(() => { return caches.match(evt.request); })
);
});