-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserviceworker.js
40 lines (40 loc) · 1.33 KB
/
serviceworker.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
var CACHE_VERSION = '2.7.0';
var CURRENT_CACHES = {
cache: 'phonebook-v' + CACHE_VERSION
};
self.addEventListener('activate', function (event) {
var expectedCacheNamesSet = new Set(Object.values(CURRENT_CACHES));
event.waitUntil(
caches.keys().then(function (cacheNames) {
return Promise.all(
cacheNames.map(function (cacheName) {
if (!expectedCacheNamesSet.has(cacheName)) {
return caches.delete(cacheName);
}
})
);
})
);
});
self.addEventListener('fetch', function (event) {
event.respondWith(
caches.open(CURRENT_CACHES.cache).then(function (cache) {
return cache.match(event.request).then(function (response) {
if (response) {
return response;
}
return fetch(event.request.clone()).then(function (response) {
if (
response.status == 200 &&
!response.url.includes('/api/')
) {
cache.put(event.request, response.clone());
}
return response;
});
}).catch(function (error) {
throw error;
});
})
);
});