-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice-worker.js
59 lines (53 loc) · 1.83 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
'use strict';
const CACHE_VERSION = 0.1;
const OFFLINE_CACHE = `meteo.fvg-v${ CACHE_VERSION }`;
self.addEventListener('install', event => {
event.waitUntil(
caches.open(OFFLINE_CACHE).then(cache => {
return cache.addAll([
'img/symbols-sprite.png',
'img/ui-sprite.svg',
'css/fonts/roboto-regular.ttf',
'css/fonts/roboto-light.ttf',
'css/fonts/roboto-medium.ttf',
'css/fonts/roboto-bold.ttf',
]);
})
);
});
self.addEventListener('activate', event => {
event.waitUntil(
caches.keys().then(keys => {
return Promise.all(
keys.map(key => {
if (key != OFFLINE_CACHE) {
return caches.delete(key);
}
})
);
})
);
});
self.addEventListener('fetch', event => {
if (event.request.mode == 'navigate') {
console.log(`Handling fetch event for ${ event.request.url }`);
event.respondWith(
fetch(event.request).catch(exception => {
// The `catch` is only triggered if `fetch()` throws an exception,
// which most likely happens due to the server being unreachable.
console.error( 'Fetch failed; returning offline page instead.', exception);
// return caches.open(OFFLINE_CACHE).then(cache => {
// return cache.match(OFFLINE_URL);
// });
})
);
} else {
// It’s not a request for an HTML document, but rather for a CSS or SVG
// file or whatever…
event.respondWith(
caches.match(event.request).then(response => {
return response || fetch(event.request);
})
);
}
});