-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3-sw.js
29 lines (26 loc) · 841 Bytes
/
3-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
// (A) CREATE/INSTALL CACHE
self.addEventListener("install", evt => {
self.skipWaiting();
evt.waitUntil(
caches.open("PWA")
.then(cache => cache.addAll([
"./index.html",
"./src/**/*.{jsx}",
"2-manifest.json"
//"YOUR-STYLES.css",
//"YOUR-SCRIPTS.js",
//"YOUR-IMAGES.jpg"
]))
.catch(err => console.error(err))
);
});
// (B) CLAIM CONTROL INSTANTLY
self.addEventListener("activate", evt => self.clients.claim());
// (C) LOAD FROM CACHE FIRST, FALLBACK TO NETWORK IF NOT FOUND
self.addEventListener("fetch", evt => evt.respondWith(
caches.match(evt.request).then(res => res || fetch(evt.request))
));
/* (C) LOAD WITH NETWORK FIRST, FALLBACK TO CACHE IF OFFLINE
self.addEventListener("fetch", evt => evt.respondWith(
fetch(evt.request).catch(() => caches.match(evt.request))
));*/