-
Notifications
You must be signed in to change notification settings - Fork 0
/
sw.js
67 lines (59 loc) · 1.93 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
const STATIC_CACHE_NAME = 'restaurant-static-v6';
const IMG_CACHE_NAME = 'restaurant-img-v2';
const allCaches = [STATIC_CACHE_NAME/*, IMG_CACHE_NAME*/];
self.addEventListener('install', e => {
e.waitUntil(
caches.open(STATIC_CACHE_NAME).then(cache => {
return cache.addAll([
'index.html',
'restaurant.html',
'dist/js/main.js',
'dist/js/dbhelper.js',
'dist/js/restaurant_info.js',
'dist/css/styles.css'
]);
})
);
});
self.addEventListener('activate', event => {
console.log('activated');
event.waitUntil(
caches.keys().then(cacheNames => {
return Promise.all(
cacheNames.filter(cacheName => {
return cacheName.startsWith('restaurant-') &&
!allCaches.includes(cacheName);
}).map(cacheName => {
return caches.delete(cacheName);
})
);
})
);
});
self.addEventListener('fetch', e => {
const { request } = e;
let { url } = request;
if (url.indexOf('/restaurant.html?id=') !== -1) {
url = 'restaurant.html';
}
if (new URL(url).pathname === '/')
url = 'index.html';
if (url.startsWith('https://maps')) {
request.mode = 'cors';
request.headers = new Headers({
'Access-Control-Allow-Origin':'*'
});
}
e.respondWith(
caches.open(STATIC_CACHE_NAME).then(cache => {
return cache.match(url)
.then(res => {
if (res) return res;
return fetch(request.url).then(networkResponse => {
cache.put(url, networkResponse.clone());
return networkResponse;
})
.catch(err => console.error(err, url))
})
}))
});