42 lines
1.1 KiB
JavaScript
42 lines
1.1 KiB
JavaScript
const CACHE_NAME = 'timetracker-v1';
|
|
// This list should include all the essential files for your app's shell.
|
|
const filesToCache = [
|
|
'/',
|
|
'/index.html',
|
|
'/style/style.css',
|
|
'/js/main.js',
|
|
'/js/ui.js',
|
|
'/js/api.js',
|
|
'/js/utils.js',
|
|
'/images/icon-192x192.png',
|
|
'/images/icon-512x512.png'
|
|
];
|
|
|
|
// The install event runs when the service worker is first installed.
|
|
self.addEventListener('install', event => {
|
|
console.log('Service worker install event!');
|
|
event.waitUntil(
|
|
caches.open(CACHE_NAME)
|
|
.then(cache => {
|
|
console.log('Caching app shell');
|
|
return cache.addAll(filesToCache);
|
|
})
|
|
);
|
|
});
|
|
|
|
// The fetch event runs for every network request the page makes.
|
|
self.addEventListener('fetch', event => {
|
|
// We only want to handle GET requests.
|
|
if (event.request.method !== 'GET' || event.request.url.includes('/api/')) {
|
|
return;
|
|
}
|
|
|
|
event.respondWith(
|
|
caches.match(event.request)
|
|
.then(response => {
|
|
// If we have a cached version, return it.
|
|
// Otherwise, fetch it from the network.
|
|
return response || fetch(event.request);
|
|
})
|
|
);
|
|
}); |