forked from koddsson/LGTM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice-worker.js
51 lines (46 loc) · 1.36 KB
/
service-worker.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
'use strict';
const CACHE_NAME = 'LGTM-v2';
var isImageRequest = function(request) {
return request.method === 'GET' && request.context === 'image';
};
self.addEventListener('install', function(event) {
console.log('Installing');
console.log('Adding large images to cache!');
fetch('data.json').then(function(response) {
return response.json();
}).then(function(json) {
caches.open(CACHE_NAME).then(function(cache) {
json.forEach(function(item) {
console.log(`Adding ${item.url} to cache`);
fetch(item.url).then(function(response) {
let largeUrl = item.url
.replace('.gif', 's.gif')
.replace('.png', 's.png')
.replace('.jpg', 's.jpg');
cache.put(largeUrl, response);
});
});
});
});
});
self.addEventListener('activate', function(event) {
console.log('activating');
});
self.addEventListener('fetch', function(event) {
console.log('fetching');
if (!isImageRequest(event.request)) {
return;
} else {
let cleanURL = event.request.url.substring(0, event.request.url.indexOf('?'));
event.respondWith(
caches.match(cleanURL).then(function(response) {
if (response) {
console.log('Cache hit!');
} else {
console.log('Cache miss!');
}
return response || fetch(event.request);
})
);
}
});