-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsw.js
95 lines (82 loc) · 2.76 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
---
layout: null
---
var CACHE_VERSION = 1;
var CURRENT_CACHES = {
offline: '{{ site.title | slugify }}-cache-' + CACHE_VERSION
};
var OFFLINE_URL = 'offline.html';
var CACHE_URLS = [OFFLINE_URL];
{% for file in site.static_files %}
CACHE_URLS.push('{{ file.path }}');
{% endfor %}
{% for post in site.posts %}
CACHE_URLS.push('{{ post.url }}');
{% endfor %}
{% for page in site.pages %}
{% if page.permalink %}
CACHE_URLS.push('{{ page.permalink }}');
{% endif %}
{% if page.url %}
CACHE_URLS.push('{{ page.url }}');
{% endif %}
{% endfor %}
self.addEventListener('install', onInstall);
self.addEventListener('activate', onActicate);
self.addEventListener('fetch', onFetch);
function onInstall(event) {
event.waitUntil(caches.open(CURRENT_CACHES.offline).then(function(cache) {
return cache.addAll(CACHE_URLS);
}).catch(function(err) {
console.log('Error in install handler: ', err);
}));
}
function onActicate(event) {
var expectedCacheNames = Object.keys(CURRENT_CACHES).map(function(key) {
return CURRENT_CACHES[key];
});
event.waitUntil(
caches.keys().then(function(cacheNames) {
return Promise.all(
cacheNames.map(function(cacheName) {
if (expectedCacheNames.indexOf(cacheName) === -1) {
console.log('Delete out of date cache: ', cacheName);
return caches.delete(cacheName);
}
})
);
})
);
}
function onFetch(event) {
event.respondWith(
caches.open(CURRENT_CACHES.offline).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(err) {
console.log('Fetch caches error: ', err);
return caches.match(OFFLINE_URL);
});
// if (response) {
// console.log('Found response in cache: ', response);
// return response;
// }
// console.log('No response for %s found in cache. About to fetch from network...', event.request.url);
// return fetch(event.request.clone()).then(function(response) {
// console.log('Response for %s from network is: %0', event.request.url, response);
// if (response.status < 400) {
// console.log('Caching the response to: ', event.request.url);
// cache.put(event.request, response.clone());
// } else {
// console.log('The response status code is not under 400, not caching the response to', event.request.url);
// }
// return response;
// }).catch(function(error) {
// console.log('Error in fetch handler: ', error);
// })
});
})
)
}