update service worker
This commit is contained in:
parent
1bbd3b2be7
commit
dafc29b9d1
52
public/sw.js
52
public/sw.js
@ -1,11 +1,10 @@
|
|||||||
// A name for our cache
|
// A name for our cache, change this every time you update the app shell
|
||||||
const CACHE_NAME = 'timetracker-v1';
|
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 = [
|
const urlsToCache = [
|
||||||
'/',
|
'/',
|
||||||
'/index.html',
|
'/index.html',
|
||||||
// You can also cache the CDN assets if you want
|
|
||||||
'https://cdn.tailwindcss.com',
|
'https://cdn.tailwindcss.com',
|
||||||
'https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap'
|
'https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap'
|
||||||
];
|
];
|
||||||
@ -15,13 +14,31 @@ self.addEventListener('install', (event) => {
|
|||||||
event.waitUntil(
|
event.waitUntil(
|
||||||
caches.open(CACHE_NAME)
|
caches.open(CACHE_NAME)
|
||||||
.then((cache) => {
|
.then((cache) => {
|
||||||
console.log('Opened cache');
|
console.log('Opened cache and added app shell');
|
||||||
return cache.addAll(urlsToCache);
|
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) => {
|
self.addEventListener('fetch', (event) => {
|
||||||
// We only want to cache GET requests
|
// We only want to cache GET requests
|
||||||
if (event.request.method !== 'GET') {
|
if (event.request.method !== 'GET') {
|
||||||
@ -30,13 +47,24 @@ self.addEventListener('fetch', (event) => {
|
|||||||
|
|
||||||
event.respondWith(
|
event.respondWith(
|
||||||
caches.match(event.request)
|
caches.match(event.request)
|
||||||
.then((response) => {
|
.then((cachedResponse) => {
|
||||||
// If the file is in the cache, serve it.
|
// If the response is in the cache, return it.
|
||||||
if (response) {
|
if (cachedResponse) {
|
||||||
return response;
|
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