|
|
|
|
|
|
|
|
|
|
|
|
|
|
const CACHE_NAME = 'ChatMG-v2'; |
|
|
const urlsToCache = [ |
|
|
'/', |
|
|
'/chat', |
|
|
'/static/css/style.css', |
|
|
'/static/css/chat/style.css', |
|
|
'/static/css/sidebar.css', |
|
|
'/static/js/chat.js', |
|
|
'/static/images/mg.svg', |
|
|
'/static/images/icons/mg-48.png', |
|
|
'/static/images/icons/mg-72.png', |
|
|
'/static/images/icons/mg-96.png', |
|
|
'/static/images/icons/mg-128.png', |
|
|
'/static/images/icons/mg-192.png', |
|
|
'/static/images/icons/mg-256.png', |
|
|
'/static/images/icons/mg-384.png', |
|
|
'/static/images/icons/mg-512.png', |
|
|
'https://raw.githubusercontent.com/4gels/icons/refs/heads/main/splash-screen.png', |
|
|
'https://raw.githubusercontent.com/4gels/icons/refs/heads/main/splash-screen-750x1334.png', |
|
|
'/static/images/settings-icon.svg', |
|
|
'/static/images/swipe-hint.svg' |
|
|
]; |
|
|
|
|
|
|
|
|
self.addEventListener('install', (event) => { |
|
|
event.waitUntil( |
|
|
caches.open(CACHE_NAME).then((cache) => { |
|
|
console.log('Opened cache for install'); |
|
|
return cache.addAll(urlsToCache); |
|
|
}) |
|
|
); |
|
|
}); |
|
|
|
|
|
|
|
|
self.addEventListener('activate', (event) => { |
|
|
event.waitUntil( |
|
|
caches.keys().then((cacheNames) => { |
|
|
return Promise.all( |
|
|
cacheNames.filter((name) => name !== CACHE_NAME).map((name) => caches.delete(name)) |
|
|
); |
|
|
}) |
|
|
); |
|
|
}); |
|
|
|
|
|
|
|
|
self.addEventListener('fetch', (event) => { |
|
|
const { url } = event.request; |
|
|
const isAPI = url.includes('/api/'); |
|
|
const isStatic = url.includes('/static/'); |
|
|
|
|
|
if (isStatic || isAPI) { |
|
|
event.respondWith( |
|
|
caches.match(event.request).then((response) => { |
|
|
if (response) { |
|
|
|
|
|
event.waitUntil(updateCache(event.request)); |
|
|
return response; |
|
|
} |
|
|
return fetch(event.request).then((fetchResponse) => { |
|
|
|
|
|
if (fetchResponse.ok) { |
|
|
return caches.open(CACHE_NAME).then((cache) => { |
|
|
cache.put(event.request, fetchResponse.clone()); |
|
|
return fetchResponse; |
|
|
}); |
|
|
} |
|
|
return fetchResponse; |
|
|
}); |
|
|
}).catch(() => { |
|
|
|
|
|
if (isAPI) { |
|
|
return new Response(JSON.stringify({ error: 'Offline mode - Please check your connection' }), { |
|
|
status: 503, |
|
|
headers: { 'Content-Type': 'application/json' } |
|
|
}); |
|
|
} |
|
|
return caches.match(event.request); |
|
|
}) |
|
|
); |
|
|
} else { |
|
|
|
|
|
event.respondWith( |
|
|
fetch(event.request).catch(() => caches.match(event.request)) |
|
|
); |
|
|
} |
|
|
}); |
|
|
|
|
|
|
|
|
async function updateCache(request) { |
|
|
try { |
|
|
const response = await fetch(request); |
|
|
if (response.ok) { |
|
|
const cache = await caches.open(CACHE_NAME); |
|
|
await cache.put(request, response.clone()); |
|
|
} |
|
|
} catch (error) { |
|
|
console.log('Cache update failed:', error); |
|
|
} |
|
|
} |