parent
42a78c529d
commit
d3cdcea0df
1 changed files with 36 additions and 8 deletions
|
|
@ -1,4 +1,4 @@
|
||||||
const CACHE_NAME = 'agency-cache-v3';
|
const CACHE_NAME = 'agency-cache-v4';
|
||||||
const ASSETS = [
|
const ASSETS = [
|
||||||
'/',
|
'/',
|
||||||
'/index.html',
|
'/index.html',
|
||||||
|
|
@ -7,11 +7,28 @@ const ASSETS = [
|
||||||
'/icon-512.png'
|
'/icon-512.png'
|
||||||
];
|
];
|
||||||
|
|
||||||
|
// Force immediate update to the latest SW
|
||||||
self.addEventListener('install', (event) => {
|
self.addEventListener('install', (event) => {
|
||||||
event.waitUntil(
|
event.waitUntil(
|
||||||
caches.open(CACHE_NAME).then((cache) => {
|
caches.open(CACHE_NAME).then((cache) => {
|
||||||
return cache.addAll(ASSETS);
|
return cache.addAll(ASSETS);
|
||||||
})
|
}).then(() => self.skipWaiting())
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
// Clean up old caches and take control of all clients immediately
|
||||||
|
self.addEventListener('activate', (event) => {
|
||||||
|
event.waitUntil(
|
||||||
|
caches.keys().then((cacheNames) => {
|
||||||
|
return Promise.all(
|
||||||
|
cacheNames.map((cacheName) => {
|
||||||
|
if (cacheName !== CACHE_NAME) {
|
||||||
|
console.log('Deleting old cache:', cacheName);
|
||||||
|
return caches.delete(cacheName);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
);
|
||||||
|
}).then(() => self.clients.claim())
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
@ -20,16 +37,27 @@ self.addEventListener('fetch', (event) => {
|
||||||
if (!event.request.url.startsWith('http')) return;
|
if (!event.request.url.startsWith('http')) return;
|
||||||
|
|
||||||
event.respondWith(
|
event.respondWith(
|
||||||
caches.match(event.request).then((response) => {
|
caches.match(event.request).then((cachedResponse) => {
|
||||||
// Return cached response if found, otherwise fetch from network
|
if (cachedResponse) {
|
||||||
return response || fetch(event.request).catch(error => {
|
return cachedResponse;
|
||||||
|
}
|
||||||
|
|
||||||
|
return fetch(event.request).catch((error) => {
|
||||||
// If network fetch fails and it's a navigation request, return index.html
|
// If network fetch fails and it's a navigation request, return index.html
|
||||||
if (event.request.mode === 'navigate') {
|
if (event.request.mode === 'navigate') {
|
||||||
return caches.match('/index.html');
|
return caches.match('/index.html');
|
||||||
}
|
}
|
||||||
// For assets, let the browser handle the failure normally
|
|
||||||
console.error('Fetch failed:', event.request.url, error);
|
// For assets, return a failure response instead of throwing.
|
||||||
throw error;
|
// Re-throwing (or returning a rejected promise) causes the browser to show
|
||||||
|
// the "unexpected error" interception UI.
|
||||||
|
console.warn('Fetch failed for:', event.request.url, error);
|
||||||
|
|
||||||
|
return new Response('Network error occurred', {
|
||||||
|
status: 503,
|
||||||
|
statusText: 'Service Unavailable',
|
||||||
|
headers: new Headers({ 'Content-Type': 'text/plain' })
|
||||||
|
});
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue