-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache_app_v1.js
More file actions
36 lines (33 loc) · 946 Bytes
/
Copy pathcache_app_v1.js
File metadata and controls
36 lines (33 loc) · 946 Bytes
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
/* eslint-disable no-restricted-globals */
const cacheName = 'cache_v1';
// Install
self.addEventListener('install', (ev) => {
console.log('ServiceWorker Installed');
});
// Activate // cleanUp
self.addEventListener('activate', (ev) => {
ev.waitUntil(
caches.keys().then((cacheNames) => Promise.all(
cacheNames.map((cache) => {
if (cache !== cacheName) {
return caches.delete(cache);
}
}),
)),
);
});
// fetch
self.addEventListener('fetch', (ev) => {
ev.respondWith(
fetch(ev.request)
.then((res) => {
const resClone = res.clone();
caches
.open(cacheName)
.then((cache) => {
cache.put(ev.request, resClone);
});
return res;
}).catch((err) => caches.match(ev.request).then((res) => res)),
);
});