-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsw.js
27 lines (26 loc) · 876 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
self.addEventListener("install", e => {
e.waitUntil(
caches.open("static").then(cache => {
console.log("Caching all resourcesssss")
return cache.addAll(["./", "index.css", "index.js", "icons/icon-512x512.png", "icons/icon-384x384.png", "icons/icon-256x256.png", "icons/icon-192x192.png", "manifest.json", "favicon.ico"])
})
)
})
// below employs reliance on network first / cache later approach.
self.addEventListener("fetch", e => {
if (navigator.onLine) {
// If browser is online, fetch from internet
console.log("fetching from server")
e.respondWith(fetch(e.request));
} else {
// If browser is offline, try to fetch from cache
e.respondWith(
caches.match(e.request).then(response => {
if (response) {
console.log('serving from cachee');
return response;
}
})
);
}
});