-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsw.js
100 lines (88 loc) · 2.64 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
---
layout: null
---
var APP_PREFIX = 'alexdiliberto-cache-';
var VERSION = 'v29';
var CACHE_NAME = APP_PREFIX + VERSION;
var urlsToCache = [];
// Cache posts
// Limits the number of posts that gets cached to 3
// Reads a piece of front-matter in each post that directs the second loop to the folder where the assets are held
// {% for post in site.posts limit:3 %}
// urlsToCache.push("{{ post.url }}");
// {% for asset in post.assets %}
// {% for file in site.static_files %}
// {% if file.path contains asset %}
// urlsToCache.push("{{ file.path }}");
// {% endif %}
// {% endfor %}
// {% endfor %}
// {% endfor %}
{% for post in site.posts limit:3 %}
urlsToCache.push("{{ post.url }}");
{% endfor %}
// Cache pages
{% for page in site.html_pages %}
{% unless page.url contains '/talks' %}
{% unless page.url contains '/node_modules' %}
urlsToCache.push("{{ page.url }}");
{% endunless %}
{% endunless %}
{% endfor %}
// Cache assets
{% for file in site.static_files %}
{% unless file.path contains '/talks' %}
{% unless file.path contains '/node_modules' %}
{% if file.path contains '/img' or file.extname == '.js' or file.extname == '.png' %}
urlsToCache.push("{{ file.path }}")
{% endif %}
{% endunless %}
{% endunless %}
{% endfor %}
//console.log('urlsToCache', urlsToCache);
self.addEventListener('install', function(event) {
self.skipWaiting();
event.waitUntil(
caches.open(CACHE_NAME).then(function(cache) {
return cache.addAll(urlsToCache);
})
);
});
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) {
if (event.request.url.indexOf('chrome-extension') !== -1) {
return response;
}
cache.put(event.request, response.clone());
return response;
});
});
}).catch(function() {
return caches.match('/offline/');
})
);
});
self.addEventListener('activate', function(event) {
var cacheWhitelist = [CACHE_NAME];
event.waitUntil(
caches.keys().then(function(cacheNames) {
return Promise.all(
cacheNames.map(function(cacheName) {
if (cacheWhitelist.indexOf(cacheName) === -1) {
return caches.delete(cacheName);
}
})
);
})
);
});
self.addEventListener('message', function(event) {
if (event.data.action === 'skipWaiting') {
self.skipWaiting().then(function() {
window.location.reload();
});
}
});