-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice-worker.js
44 lines (42 loc) · 1.17 KB
/
service-worker.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
let CACHE_NAME = 'static-cache';
let urlsToCache = [
'index.html',
'style.css',
'assets',
'node_modules',
'script.js',
'service-worker.js',
];
self.addEventListener('install', function (event) {
console.info('Caching resources');
event.waitUntil(
caches.keys().then(function(cacheNames) {
return Promise.all(
cacheNames.filter(function(cacheName) {
// Return true if you want to remove this cache,
// but remember that caches are shared across
// the whole origin
}).map(function(cacheName) {
return caches.delete(cacheName);
})
);
})
);
});
self.addEventListener('activate', (event) => {
console.log('👷', 'activate', event);
return self.clients.claim();
});
self.addEventListener('fetch', function (event) {
// console.log('👷', 'fetch', event);
event.respondWith(
caches.open('static-cache').then(function(cache) {
return cache.match(event.request).then(function (response) {
return response || fetch(event.request).then(function(response) {
cache.put(event.request, response.clone());
return response;
});
});
})
);
});