Enable PWA
All checks were successful
Build and Deploy / build-and-deploy (push) Successful in 4m14s

This commit is contained in:
Antoni Nuñez Romeu
2026-05-06 16:21:51 +02:00
parent 012d4aa121
commit 5e6e3f04d3
4 changed files with 59 additions and 28 deletions

View File

@@ -1,37 +1,67 @@
const CACHE_NAME = 'time-tracker-app-v1';
const CACHE_NAME = 'time-tracker-app-v2';
const urlsToCache = [
'/',
'/static/js/bundle.js',
'/static/css/main.css',
'/index.html',
'/manifest.json',
'/favicon.ico',
'/logo_ficosa.png'
'/logo192.png',
'/logo512.png'
];
// Install event - cache essential assets
// Install event - cache essential static assets
self.addEventListener('install', (event) => {
// Skip waiting to activate immediately
self.skipWaiting();
event.waitUntil(
caches.open(CACHE_NAME)
.then((cache) => {
console.log('Opened cache');
// Use addAll for guaranteed files. If these fail, SW won't install.
// We omit JS/CSS because CRA uses hashed filenames in production.
return cache.addAll(urlsToCache);
})
);
});
// Fetch event - serve cached content when offline
// Fetch event - Stale-while-revalidate strategy for most requests
self.addEventListener('fetch', (event) => {
// Only handle GET requests
if (event.request.method !== 'GET') return;
// Skip cross-origin requests
if (!event.request.url.startsWith(self.location.origin)) return;
event.respondWith(
caches.match(event.request)
.then((response) => {
// Return cached version or fetch from network
return response || fetch(event.request);
.then((cachedResponse) => {
const fetchPromise = fetch(event.request).then((networkResponse) => {
// Dynamically cache successful GET requests
if (networkResponse && networkResponse.status === 200) {
const responseToCache = networkResponse.clone();
caches.open(CACHE_NAME)
.then((cache) => {
cache.put(event.request, responseToCache);
});
}
return networkResponse;
}).catch(() => {
// If network fails and we have no cache, return the cached index.html for navigation requests (SPA fallback)
if (event.request.mode === 'navigate') {
return caches.match('/');
}
});
return cachedResponse || fetchPromise;
})
);
});
// Activate event - clean up old caches
self.addEventListener('activate', (event) => {
// Take control of all clients immediately
event.waitUntil(self.clients.claim());
event.waitUntil(
caches.keys().then((cacheNames) => {
return Promise.all(