-
Notifications
You must be signed in to change notification settings - Fork 1
/
sw.js
42 lines (34 loc) · 1.07 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
// Offline SW
const filesToCache = [
'https://lax18.github.io/WebJROTC/css/font.css',
'https://lax18.github.io/WebJROTC/css/material.indigo-red.min.css',
'https://lax18.github.io/WebJROTC/js/firebase.js',
'https://lax18.github.io/WebJROTC/js/material.min.js'
];
const staticCacheName = 'NokomisJROTC';
self.addEventListener('install', event => {
console.log('Attempting to install service worker and cache static assets');
event.waitUntil(
caches.open(staticCacheName)
.then(cache => {
return cache.addAll(filesToCache);
})
);
});
self.addEventListener('fetch', event => {
console.log('Fetch event for ', event.request.url);
event.respondWith(
caches.match(event.request)
.then(response => {
if (response) {
console.log('Found ', event.request.url, ' in cache');
return response;
}
console.log('Network request for ', event.request.url);
return fetch(event.request)
// TODO 4 - Add fetched files to the cache
}).catch(error => {
// TODO 6 - Respond with custom offline page
})
);
});