-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsw.js
56 lines (51 loc) · 1.32 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
---
layout: null
---
const staticCacheName = 'vgscomcerveja-{{ site.time | date: "%Y-%m-%d-%H-%M" }}';
const filesToCache = [
{% for page in site.pages_to_cache %}
'{{ page }}',
{% endfor %}
{% for post in site.posts limit: 7 %}
'{{ post.url }}',
{% endfor %}
{% for asset in site.files_to_cache %}
'{{ asset }}',
{% endfor %}
];
// cache to install
this.addEventListener("install", event => {
this.skipWaiting();
event.waitUntil(
caches.open(staticCacheName)
.then(cache => {
return cache.addAll(filesToCache);
})
)
});
// clear cache on activate
this.addEventListener("activate", event => {
event.waitUntil(
caches.keys().then(cacheNames => {
return Promise.all(
cacheNames
.filter(cacheName => (cacheName.startsWith('vgscomcerveja-')))
.filter(cacheName => (cacheName !== staticCacheName))
.map(cacheName => caches.delete(cacheName))
);
})
);
});
// serve from cache
this.addEventListener("fetch", event => {
event.respondWith(
caches.match(event.request)
.then(response => {
//console.log('load cached');
return response || fetch(event.request);
})
.catch(() => {
return caches.match("/offline/index.html");
})
)
});