-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsw.js
More file actions
47 lines (44 loc) · 1.2 KB
/
sw.js
File metadata and controls
47 lines (44 loc) · 1.2 KB
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
var CACHE_NAME = 'static-cache';
var urlsToCache = [
'/',
'/index.html',
'/assets/main.css',
'/assets/materialize.css',
'/images/Digital.jpg',
'//maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css',
'//fonts.googleapis.com/icon?family=Material+Icons'
];
self.addEventListener('install', function(event) {
event.waitUntil(
caches.open(CACHE_NAME)
.then(function(cache) {
return cache.addAll(urlsToCache);
})
);
});
self.addEventListener('fetch', function(event) {
event.respondWith(
caches.match(event.request)
.then(function(response) {
return response || fetchAndCache(event.request);
})
);
});
function fetchAndCache(url) {
return fetch(url)
.then(function(response) {
// Check if we received a valid response
if (!response.ok) {
throw Error(response.statusText);
}
return caches.open(CACHE_NAME)
.then(function(cache) {
cache.put(url, response.clone());
return response;
});
})
.catch(function(error) {
console.log('Request failed:', error);
// You could return a custom offline 404 page here
});
}