forked from YouCount/youcount.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsw.js
42 lines (42 loc) · 1.03 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
var CACHE_NAME = 'youcount-cache-1.7';
var urlsToCache = [
'/index.html',
'/images/social.png',
'/js/script.js'
];
self.addEventListener('install', function (event) {
event.waitUntil(
caches.open(CACHE_NAME)
.then(function (cache) {
return cache.addAll(urlsToCache);
})
);
});
self.addEventListener('activate', function (event) {
event.waitUntil(
caches.keys().then(function (cacheNames) {
return Promise.all(
cacheNames.filter(function (cacheName) {
return cacheName != CACHE_NAME;
}).map(function (cacheName) {
return caches.delete(cacheName);
})
);
})
);
});
self.addEventListener('fetch', function (event) {
try {
event.respondWith(
caches.match(event.request)
.then(function (response) {
if (response) {
return response;
}
return fetch(event.request);
})
);
} catch (err) {
stat.error('Service Worker failed to fetch!')
}
});