This commit is contained in:
2026-05-06 12:43:37 +01:00
parent 000c1cd721
commit f6b7a98471
9 changed files with 2509 additions and 73 deletions

38
sw.js
View File

@@ -1,4 +1,4 @@
const CACHE_NAME = 'condopro-v1';
const CACHE_NAME = 'mycondominium-v3';
const ASSETS_TO_CACHE = [
'./',
'./index.html',
@@ -14,15 +14,43 @@ const ASSETS_TO_CACHE = [
];
self.addEventListener('install', (event) => {
self.skipWaiting(); // Force the waiting service worker to become the active service worker.
event.waitUntil(
caches.open(CACHE_NAME)
.then((cache) => cache.addAll(ASSETS_TO_CACHE))
);
});
self.addEventListener('fetch', (event) => {
event.respondWith(
caches.match(event.request)
.then((response) => response || fetch(event.request))
self.addEventListener('activate', (event) => {
event.waitUntil(clients.claim()); // Claim clients immediately so updates are visible without reloading all tabs
event.waitUntil(
caches.keys().then((cacheNames) => {
return Promise.all(
cacheNames.map((cacheName) => {
if (cacheName !== CACHE_NAME) {
return caches.delete(cacheName);
}
})
);
})
);
});
self.addEventListener('fetch', (event) => {
// Network First strategy: try network, if it fails, fallback to cache
event.respondWith(
fetch(event.request).then((networkResponse) => {
// If request is successful, update the cache
if (networkResponse && networkResponse.status === 200 && event.request.method === 'GET') {
const responseToCache = networkResponse.clone();
caches.open(CACHE_NAME).then((cache) => {
cache.put(event.request, responseToCache);
});
}
return networkResponse;
}).catch(() => {
// If network fails (offline), return cached version
return caches.match(event.request);
})
);
});