-
Notifications
You must be signed in to change notification settings - Fork 22
/
sw.js
49 lines (48 loc) · 1.42 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
var CACHE_NAME = 'morpheus';
var CACHED_FILES = [
'css/morpheus-latest.min.css',
'fonts/FontAwesome.otf',
'fonts/fontawesome-webfont.eot',
'fonts/fontawesome-webfont.svg',
'fonts/fontawesome-webfont.ttf',
'fonts/fontawesome-webfont.woff',
'fonts/fontawesome-webfont.woff2',
'js/morpheus-external-latest.min.js',
'js/morpheus-latest.min.js',
'/',
'index.html',
'favicon.ico'
];
self.addEventListener('install', function (event) {
event.waitUntil(
caches.open(CACHE_NAME).then(function (cache) {
cache.addAll(CACHED_FILES);
})
);
});
self.addEventListener('fetch', function (event) {
event.respondWith(
caches.open(CACHE_NAME).then(function (cache) {
return cache.match(event.request).then(function (cachedResponse) {
if (cachedResponse) {
// try to get an updated version
return fetch(event.request.clone()).then(function (response) {
cache.put(event.request, response.clone());
return response;
}).catch(function () {
return cachedResponse;
})
} else {
return fetch(event.request).then(function (response) {
return response;
});
}
}).catch(function (error) {
// This catch() will handle exceptions that arise from the match() or fetch() operations.
// Note that a HTTP error response (e.g. 404) will NOT trigger an exception.
// It will return a normal response object that has the appropriate error code set.
throw error;
});
})
);
});