update service worker
This commit is contained in:
parent
1bbd3b2be7
commit
dafc29b9d1
54
public/sw.js
54
public/sw.js
@ -1,11 +1,10 @@
|
||||
// A name for our cache
|
||||
const CACHE_NAME = 'timetracker-v1';
|
||||
// A name for our cache, change this every time you update the app shell
|
||||
const CACHE_NAME = 'timetracker-v2';
|
||||
|
||||
// The files that make up the "app shell" - the minimum needed to run
|
||||
// The files that make up the "app shell"
|
||||
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'
|
||||
];
|
||||
@ -15,13 +14,31 @@ self.addEventListener('install', (event) => {
|
||||
event.waitUntil(
|
||||
caches.open(CACHE_NAME)
|
||||
.then((cache) => {
|
||||
console.log('Opened cache');
|
||||
console.log('Opened cache and added app shell');
|
||||
return cache.addAll(urlsToCache);
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
// 2. Fetch: Intercept network requests.
|
||||
// 2. Activation: Clean up old caches. (NEW)
|
||||
// This event fires after install and when the new service worker takes control.
|
||||
self.addEventListener('activate', (event) => {
|
||||
event.waitUntil(
|
||||
caches.keys().then((cacheNames) => {
|
||||
return Promise.all(
|
||||
cacheNames.map((cacheName) => {
|
||||
// If a cache is not the current one, delete it.
|
||||
if (cacheName !== CACHE_NAME) {
|
||||
console.log('Deleting old cache:', cacheName);
|
||||
return caches.delete(cacheName);
|
||||
}
|
||||
})
|
||||
);
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
// 3. Fetch: Serve from cache, fallback to network, and cache new requests. (IMPROVED)
|
||||
self.addEventListener('fetch', (event) => {
|
||||
// We only want to cache GET requests
|
||||
if (event.request.method !== 'GET') {
|
||||
@ -30,13 +47,24 @@ self.addEventListener('fetch', (event) => {
|
||||
|
||||
event.respondWith(
|
||||
caches.match(event.request)
|
||||
.then((response) => {
|
||||
// If the file is in the cache, serve it.
|
||||
if (response) {
|
||||
return response;
|
||||
.then((cachedResponse) => {
|
||||
// If the response is in the cache, return it.
|
||||
if (cachedResponse) {
|
||||
return cachedResponse;
|
||||
}
|
||||
// Otherwise, fetch it from the network.
|
||||
return fetch(event.request);
|
||||
|
||||
// If not in cache, go to the network.
|
||||
return fetch(event.request).then((networkResponse) => {
|
||||
// If the network request is successful, cache it for next time.
|
||||
if (networkResponse && networkResponse.status === 200) {
|
||||
const responseToCache = networkResponse.clone(); // Clone the response
|
||||
caches.open(CACHE_NAME)
|
||||
.then((cache) => {
|
||||
cache.put(event.request, responseToCache);
|
||||
});
|
||||
}
|
||||
return networkResponse;
|
||||
});
|
||||
})
|
||||
);
|
||||
});
|
||||
});
|
||||
Loading…
x
Reference in New Issue
Block a user