Enhance task synchronization and logging; update service worker caching strategy

This commit is contained in:
2025-01-31 18:59:16 +01:00
parent 2a7005f53c
commit b277b1f8f0
3 changed files with 45 additions and 19 deletions

View File

@@ -2,9 +2,14 @@ const CACHE_NAME = 'org-todo-pwa-cache-v1';
const urlsToCache = [
'/',
'/index.html',
'/style.css',
'/app.js',
'/manifest.json'
'/css/style.css',
'/js/auth.js',
'/js/tasks.js',
'/js/tags.js',
'/js/utils.js',
'/js/main.js',
'https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css',
'https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/js/materialize.min.js'
];
self.addEventListener('install', event => {
@@ -14,22 +19,21 @@ self.addEventListener('install', event => {
return cache.addAll(urlsToCache);
})
);
self.skipWaiting(); // Force the waiting service worker to become the active service worker
});
self.addEventListener('activate', event => {
const cacheWhitelist = [CACHE_NAME];
event.waitUntil(
caches.keys().then(cacheNames => {
return Promise.all(
cacheNames.map(cacheName => {
if (cacheName !== CACHE_NAME) {
if (!cacheWhitelist.includes(cacheName)) {
return caches.delete(cacheName);
}
})
);
})
);
self.clients.claim(); // Take control of all clients immediately
});
self.addEventListener('fetch', event => {
@@ -39,13 +43,19 @@ self.addEventListener('fetch', event => {
if (response) {
return response;
}
return fetch(event.request);
return fetch(event.request).then(
response => {
if (!response || response.status !== 200 || response.type !== 'basic') {
return response;
}
const responseToCache = response.clone();
caches.open(CACHE_NAME)
.then(cache => {
cache.put(event.request, responseToCache);
});
return response;
}
);
})
);
});
self.addEventListener('message', event => {
if (event.data === 'skipWaiting') {
self.skipWaiting();
}
});