-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserviceworker.js
executable file
·55 lines (45 loc) · 1.36 KB
/
serviceworker.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
const CACHE = 'cache_portv1';
const filestoCache = [
'/logo/favicon.png',
'/logo/logo.png',
'/vendor/jquery/jquery.min.js',
'/vendor/bootstrap/js/bootstrap.bundle.min.js',
'/vendor/jquery.easing/jquery.easing.min.js',
'/tilt.js',
]
self.addEventListener('install', function(event) {
console.log('The service worker is being installed.');
self.skipWaiting();
event.waitUntil(
caches.open(CACHE).then(function(cache) {
return cache.addAll(filestoCache);
})
);
});
self.addEventListener('activate', (e) => {
self.skipWaiting();
e.waitUntil(
caches.keys().then((keyList) => {
return Promise.all(keyList.map((key) => {
if(key !== CACHE) {
return caches.delete(key);
}
}));
})
);
});
self.addEventListener('fetch', function(event) {
event.respondWith(
caches.match(event.request).then(response => {
if (response) {
console.log('Found ', event.request.url, ' in cache');
return response;
}
console.log('Network request for ', event.request.url);
return fetch(event.request)
}).catch(function(err) {
// Fallback to cache
console.log("Oh Snap :" + err);
})
);
});