-
Notifications
You must be signed in to change notification settings - Fork 0
/
sw.js
45 lines (35 loc) · 1.16 KB
/
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
38
39
40
41
42
43
44
45
/**
References:
1. https://developers.google.com/web/fundamentals/primers/service-workers
2. https://developers.google.com/web/fundamentals/instant-and-offline/offline-cookbook
3. https://googlechrome.github.io/samples/service-worker/
4. https://developer.mozilla.org/en-US/docs/Web/API/Service_Worker_API/Using_Service_Workers
5. https://serviceworke.rs/
6. https://www.youtube.com/watch?v=baSiSIyTGSk
*/
const RUNTIME = 'runtime';
self.skipWaiting();
self.addEventListener('fetch', (event) => {
// Do nothing if not the same origin
if (!event.request.url.startsWith(self.location.origin)) {
return;
}
// Network first strategy
event.respondWith(
(async () => {
const cache = await caches.open(RUNTIME);
try {
const networkResponse = await fetch(event.request);
// Save/Update cache if network response is ok
if (networkResponse && networkResponse.status === 200) {
cache.put(event.request, networkResponse.clone());
}
return networkResponse;
} catch (e) {
const cachedResponse = await cache.match(event.request);
return cachedResponse;
}
})()
);
});
;