43 lines
1.1 KiB
JavaScript

// A name for our cache
const CACHE_NAME = 'timetracker-v1';
// The files that make up the "app shell" - the minimum needed to run
const urlsToCache = [
'/',
'/index.html',
// You can also cache the CDN assets if you want
'https://cdn.tailwindcss.com',
'https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap'
];
// 1. Installation: Open the cache and add the app shell files.
self.addEventListener('install', (event) => {
event.waitUntil(
caches.open(CACHE_NAME)
.then((cache) => {
console.log('Opened cache');
return cache.addAll(urlsToCache);
})
);
});
// 2. Fetch: Intercept network requests.
self.addEventListener('fetch', (event) => {
// We only want to cache GET requests
if (event.request.method !== 'GET') {
return;
}
event.respondWith(
caches.match(event.request)
.then((response) => {
// If the file is in the cache, serve it.
if (response) {
return response;
}
// Otherwise, fetch it from the network.
return fetch(event.request);
})
);
});