-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
sw.js
54 lines (51 loc) · 1.34 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
---
layout: null
---
var CACHE_NAME = "pixyll2";
self.addEventListener("install", function(e) {
e.waitUntil(
caches.open(CACHE_NAME).then(function(cache) {
return cache.addAll([
"{{ '/css/pixyll.css' | relative_url }}?{{ site.time | date: '%Y%m%d%H%M' }}",
"{{ '/' | relative_url }}"
]);
})
);
});
self.addEventListener("activate", function(e) {
e.waitUntil(
caches.keys().then(function(names) {
return Promise.all(
names.map(function(name) {
if (name != CACHE_NAME) {
return caches.delete(name);
}
})
);
})
);
return clients.claim();
});
addEventListener("fetch", function(e) {
e.respondWith(
caches.match(e.request).then(function(response) {
return response || fetch(e.request).then(function(response) {
var clonedResponse = response.clone();
var hosts = [
"https://fonts.googleapis.com",
"https://fonts.gstatic.com",
"https://maxcdn.bootstrapcdn.com",
"https://cdnjs.cloudflare.com"
];
hosts.map(function(host) {
if (e.request.url.indexOf(host) === 0) {
caches.open(CACHE_NAME).then(function(cache) {
cache.put(e.request, clonedResponse);
});
}
});
return response;
});
})
);
});