-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsw.js
87 lines (82 loc) · 2.45 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
//Establish version number of cache to remove outdated caches during an update
const cacheVersion = 'v1';
//Assets to cache for offline use
const cacheAssets = [
'/',
'/index.html',
'/restaurant.html',
'/manifest.json',
'/css/styles.css',
'/js/idb.js',
'/js/dbhelper.js',
'/js/main.js',
'/js/restaurant_info.js',
'/js/lazysizes.js',
'/js/register.js',
'/img/1_large.webp',
'/img/2_large.webp',
'/img/3_large.webp',
'/img/4_large.webp',
'/img/5_large.webp',
'/img/6_large.webp',
'/img/7_large.webp',
'/img/8_large.webp',
'/img/9_large.webp',
'/img/10_large.webp',
'/img/1_small.webp',
'/img/2_small.webp',
'/img/3_small.webp',
'/img/4_small.webp',
'/img/5_small.webp',
'/img/6_small.webp',
'/img/7_small.webp',
'/img/8_small.webp',
'/img/9_small.webp',
'/img/10_small.webp',
'/img/icon-192.png',
'/img/icon-512.png',
'/img/favicon.ico'
];
//Installs a service worker and caches assets with current cache version as its name.
self.addEventListener('install', event => {
event.waitUntil(
caches.open(`${cacheVersion}-restaurant`).then(cache => {
return cache.addAll(cacheAssets);
})
);
console.log('Installed service worker and cached assets');
});
/* Updates the service worker with a newer version (if available in a waiting state). Activate fires once older service worker no longer controls current pages. Older cache(s) is also deleted. */
self.addEventListener('activate', event => {
event.waitUntil(caches.keys().then(cacheNames => {
return Promise.all(cacheNames.filter(cacheName => {
return !cacheName.startsWith(cacheVersion);
}).map(cacheName => {
return caches.delete(cacheName);
}));
}));
console.log('Deleted old cache and activated new service worker');
});
/* Fetches assets from the cache the service worker created if a matching response is found. If not, fetches assets from the network and adds these new asset requests to the cache. */
self.addEventListener('fetch', event => {
event.respondWith(
caches.match(event.request).then(response => {
if(response) {
return response;
}
else {
const fetchRequest = event.request.clone();
return fetch(fetchRequest).then(response => {
if(!response || response.status !== 200 || response.type !== 'basic') {
return response;
}
const responseToCache = response.clone();
caches.open(`${cacheVersion}-restaurant`).then(cache => {
cache.put(fetchRequest, responseToCache);
});
return response;
});
}
})
);
});