update sw
This commit is contained in:
parent
8a984a11f7
commit
bba5c51f34
68
public/sw.js
68
public/sw.js
@ -1,34 +1,40 @@
|
|||||||
// A name for our cache, change this every time you update the app shell
|
const CACHE_NAME = 'timetracker-v3'; // Update this if you change anything!
|
||||||
const CACHE_NAME = 'timetracker-v2';
|
|
||||||
|
|
||||||
// The files that make up the "app shell"
|
// Cache only local assets here. Avoid opaque external resources in addAll().
|
||||||
const urlsToCache = [
|
const urlsToCache = [
|
||||||
'/',
|
'/',
|
||||||
'/index.html',
|
'/index.html',
|
||||||
|
'/manifest.json'
|
||||||
|
// Add local JS/CSS files here if you have them, e.g., '/main.js', '/styles.css'
|
||||||
];
|
];
|
||||||
|
|
||||||
// 1. Installation: Open the cache and add the app shell files.
|
// Install: Cache core files
|
||||||
self.addEventListener('install', (event) => {
|
self.addEventListener('install', (event) => {
|
||||||
|
console.log('[SW] Install event');
|
||||||
|
|
||||||
event.waitUntil(
|
event.waitUntil(
|
||||||
caches.open(CACHE_NAME)
|
caches.open(CACHE_NAME)
|
||||||
.then((cache) => {
|
.then(cache => {
|
||||||
console.log('Opened cache and added app shell');
|
console.log('[SW] Caching app shell:', urlsToCache);
|
||||||
return cache.addAll(urlsToCache);
|
return cache.addAll(urlsToCache);
|
||||||
})
|
})
|
||||||
|
.catch(err => {
|
||||||
|
console.error('[SW] Install cache error:', err);
|
||||||
|
})
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
// 2. Activation: Clean up old caches. (NEW)
|
// Activate: Clean up old caches
|
||||||
// This event fires after install and when the new service worker takes control.
|
|
||||||
self.addEventListener('activate', (event) => {
|
self.addEventListener('activate', (event) => {
|
||||||
|
console.log('[SW] Activate event');
|
||||||
|
|
||||||
event.waitUntil(
|
event.waitUntil(
|
||||||
caches.keys().then((cacheNames) => {
|
caches.keys().then(cacheNames => {
|
||||||
return Promise.all(
|
return Promise.all(
|
||||||
cacheNames.map((cacheName) => {
|
cacheNames.map(name => {
|
||||||
// If a cache is not the current one, delete it.
|
if (name !== CACHE_NAME) {
|
||||||
if (cacheName !== CACHE_NAME) {
|
console.log('[SW] Deleting old cache:', name);
|
||||||
console.log('Deleting old cache:', cacheName);
|
return caches.delete(name);
|
||||||
return caches.delete(cacheName);
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
@ -36,32 +42,38 @@ self.addEventListener('activate', (event) => {
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
// 3. Fetch: Serve from cache, fallback to network, and cache new requests. (IMPROVED)
|
// Fetch: Cache-first strategy with dynamic caching
|
||||||
self.addEventListener('fetch', (event) => {
|
self.addEventListener('fetch', (event) => {
|
||||||
// We only want to cache GET requests
|
if (event.request.method !== 'GET') return;
|
||||||
if (event.request.method !== 'GET') {
|
|
||||||
return;
|
console.log('[SW] Fetch request:', event.request.url);
|
||||||
}
|
|
||||||
|
|
||||||
event.respondWith(
|
event.respondWith(
|
||||||
caches.match(event.request)
|
caches.match(event.request)
|
||||||
.then((cachedResponse) => {
|
.then(cachedResponse => {
|
||||||
// If the response is in the cache, return it.
|
|
||||||
if (cachedResponse) {
|
if (cachedResponse) {
|
||||||
|
console.log('[SW] Serving from cache:', event.request.url);
|
||||||
return cachedResponse;
|
return cachedResponse;
|
||||||
}
|
}
|
||||||
|
|
||||||
// If not in cache, go to the network.
|
return fetch(event.request)
|
||||||
return fetch(event.request).then((networkResponse) => {
|
.then(networkResponse => {
|
||||||
// If the network request is successful, cache it for next time.
|
if (
|
||||||
if (networkResponse && networkResponse.status === 200) {
|
networkResponse &&
|
||||||
const responseToCache = networkResponse.clone(); // Clone the response
|
networkResponse.status === 200 &&
|
||||||
caches.open(CACHE_NAME)
|
networkResponse.type !== 'opaque'
|
||||||
.then((cache) => {
|
) {
|
||||||
|
const responseToCache = networkResponse.clone();
|
||||||
|
caches.open(CACHE_NAME).then(cache => {
|
||||||
|
console.log('[SW] Dynamically caching:', event.request.url);
|
||||||
cache.put(event.request, responseToCache);
|
cache.put(event.request, responseToCache);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
return networkResponse;
|
return networkResponse;
|
||||||
|
})
|
||||||
|
.catch(err => {
|
||||||
|
console.error('[SW] Network request failed:', event.request.url, err);
|
||||||
|
// Optionally, return a fallback page or image here
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user