-
Notifications
You must be signed in to change notification settings - Fork 0
/
service-worker.js
64 lines (52 loc) · 1.71 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
52
53
54
55
56
57
58
59
60
61
62
63
64
/*global caches*/
/*global console*/
/*global fetch*/
var CACHE_NAME = 'SAMPLE_APP_CACHE';
var urlsToCache = [
'/',
'/index.html',
'/resources/css/bootstrap.min.css',
'/resources/css/font-awesome.min.css',
'/resources/css/site.css',
'/resources/fonts/',
'/resources/fonts/fontawesome-webfont.woff2',
'/resources/img/favicon.ico',
'/resources/img/sample-snap.jpg',
'/resources/js/app.js',
'/resources/js/bootstrap.min.js',
'/resources/js/inview.min.js',
'/resources/js/jquery.min.js',
'/resources/js/site.js'
];
this.addEventListener('install', function (event) {
'use strict';
console.info('Install event called.');
event.waitUntil(
caches.open(CACHE_NAME).then(function (cache) {
return cache.addAll(urlsToCache);
}).catch(function (error) {
console.log('Unable to install cache. Error is: ' + error);
})
);
});
this.addEventListener('fetch', function (event) {
'use strict';
console.info('Fetch event called for request: ' + event.request);
event.respondWith(
caches.match(event.request).then(function (response) {
if (response) {
return response;
}
var clonedRequest = event.request.clone();
return fetch(clonedRequest).then(function (response) {
if (!response || response.status !== 200 || response.type !== 'basic') {
return response;
}
var clonedResponse = response.clone();
caches.open(CACHE_NAME).then(function (cache) {
cache.put(event.request, clonedResponse);
});
});
})
);
});