-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservice-worker.js
More file actions
32 lines (29 loc) · 923 Bytes
/
Copy pathservice-worker.js
File metadata and controls
32 lines (29 loc) · 923 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
var CACHE_NAME = 'promo-cache-v1';
var FILES_TO_CACHE = [
'/promo-site/index.html',
'/promo-site/assets/css/styles.css',
'/promo-site/assets/js/script.js',
'/promo-site/manifest.json',
];
self.addEventListener('install', function(e){
e.waitUntil((async ()=>{
const cache = await caches.open(CACHE_NAME);
await cache.addAll(FILES_TO_CACHE);
})());
self.skipWaiting();
});
self.addEventListener('activate', function(e){
e.waitUntil(self.clients.claim());
});
self.addEventListener('fetch', function(e){
e.respondWith((async ()=>{
const cache = await caches.open(CACHE_NAME);
const cachedResponse = await cache.match(e.request);
if (cachedResponse) return cachedResponse;
const networkResponse = await fetch(e.request);
if (networkResponse && networkResponse.status === 200) {
cache.put(e.request, networkResponse.clone());
}
return networkResponse;
})());
});