Files
MobileJROTC/sw.js
T
NokomisJROTC a30b57147b Update sw.js
2019-09-10 08:40:52 -04:00

44 lines
1.1 KiB
JavaScript

// Offline SW
const filesToCache = [
'sw.js',
'https://lax18.github.io/MobileJROTC/css/font.css',
'https://lax18.github.io/MobileJROTC/css/material.indigo-red.min.css',
'https://lax18.github.io/MobileJROTC/js/firebase.js',
'https://lax18.github.io/MobileJROTC/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
})
);
});