converse/static/sw.js

63 lines
1.6 KiB
JavaScript
Raw Normal View History

// Service Worker for Egregore PWA
const CACHE_NAME = 'egregore-v8';
const OFFLINE_URL = '/offline.html';
// Assets to cache
const ASSETS = [
'/',
'/static/style.css',
'/static/app.js',
'/static/manifest.json',
'/static/icon.svg'
];
// Install event
self.addEventListener('install', (event) => {
event.waitUntil(
caches.open(CACHE_NAME).then((cache) => {
return cache.addAll(ASSETS);
})
);
self.skipWaiting();
});
// Activate event
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.clients.claim();
});
// Fetch event - network first, fallback to cache
self.addEventListener('fetch', (event) => {
// Skip non-GET requests
if (event.request.method !== 'GET') return;
// Skip API requests (we want those to always go to network)
if (event.request.url.includes('/api/')) return;
event.respondWith(
fetch(event.request)
.then((response) => {
// Clone the response and cache it
const responseClone = response.clone();
caches.open(CACHE_NAME).then((cache) => {
cache.put(event.request, responseClone);
});
return response;
})
.catch(() => {
// Fallback to cache
return caches.match(event.request);
})
);
});