forked from alexandtheweb/uxbrighton-prototype
-
Notifications
You must be signed in to change notification settings - Fork 4
/
sw.js
42 lines (37 loc) · 1.23 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
/**
* IMPORTANT: This script must be in the root directory, otherwise it will not work.
var date = new Date();
var timestamp = date.getTime();
var cachePrefix = 'uxbri';
var cacheVersion = timestamp + '0';
var cacheID = cachePrefix + cacheVersion;
self.addEventListener('activate', function(event) {
event.waitUntil(
caches.keys().then(function(cacheNames) {
return Promise.all(
cacheNames.filter(function(cacheName) {
return cacheName.startsWith(cachePrefix);
}).map(function(cacheName) {
console.log('Deleting cache: ' + cacheName);
return caches.delete(cacheName);
})
);
})
);
});
self.addEventListener('fetch', function(event) {
if (!event.request.url.startsWith('http')) {
return;
}
event.respondWith(
caches.open(cacheID).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;
});
});
})
);
});
*/