Repository navigation
Expand file tree
/
Copy pathsw.js
More file actions
71 lines (62 loc) · 1.63 KB
/
Copy pathsw.js
File metadata and controls
71 lines (62 loc) · 1.63 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
---
layout: null
---
var urlsToCache = [
'/',
'/css/main.css'
];
{% for file in site.static_files %}
{% if file.path contains '/js' or file.path contains '/images' %}
urlsToCache.push("{{ file.path }}");
{% endif %}
{% endfor %}
// Cache posts
{% for post in site.posts limit:5 %}
urlsToCache.push("{{ post.url }}");
{% endfor %}
// Cache pages
{% for page in site.html_pages %}
urlsToCache.push("{{ page.url }}");
{% endfor %}
var CACHE_NAME = 'ion-book-cache-v2';
self.addEventListener('install', function(event) {
self.skipWaiting();
// Perform install steps
event.waitUntil(
caches.open(CACHE_NAME)
.then(function(cache) {
console.log('Opened cache');
return cache.addAll(urlsToCache);
})
)
});
self.addEventListener('activate', event => {
// delete any caches that aren't in expectedCaches
// which will get rid of static-v1
var expectedCaches = []; // white-list
event.waitUntil(
caches.keys().then(keys => Promise.all(
keys.map(key => {
if (!expectedCaches.includes(key)) {
return caches.delete(key);
}
})
)).then(() => {
console.log('V2 now ready to handle fetches!');
})
);
});
self.addEventListener('fetch', function(event) {
event.respondWith(
caches.open(CACHE_NAME).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;
});
});
}).catch(function() {
console.log('Error un request.');
})
);
});