Upload 2 new, 10 modified files from chat-switchboard-v0.7.4.zip
This commit is contained in:
76
src/sw.js
Normal file
76
src/sw.js
Normal file
@@ -0,0 +1,76 @@
|
||||
// ==========================================
|
||||
// Chat Switchboard – Service Worker
|
||||
// ==========================================
|
||||
// Caches the app shell for offline / instant load.
|
||||
// API calls always go to network (never cached).
|
||||
// Version string is injected by the frontend entrypoint.
|
||||
|
||||
const CACHE_NAME = 'switchboard-%%APP_VERSION%%';
|
||||
|
||||
// App shell files to pre-cache on install
|
||||
const SHELL_FILES = [
|
||||
'./',
|
||||
'./index.html',
|
||||
'./css/styles.css',
|
||||
'./js/debug.js',
|
||||
'./js/events.js',
|
||||
'./js/api.js',
|
||||
'./js/ui.js',
|
||||
'./js/app.js',
|
||||
'./vendor/marked.min.js',
|
||||
'./vendor/purify.min.js',
|
||||
'./favicon.svg',
|
||||
'./favicon-32.png',
|
||||
'./favicon-192.png',
|
||||
'./manifest.json',
|
||||
];
|
||||
|
||||
// Install: pre-cache the shell
|
||||
self.addEventListener('install', (event) => {
|
||||
event.waitUntil(
|
||||
caches.open(CACHE_NAME)
|
||||
.then(cache => cache.addAll(SHELL_FILES))
|
||||
.then(() => self.skipWaiting())
|
||||
);
|
||||
});
|
||||
|
||||
// Activate: purge old caches
|
||||
self.addEventListener('activate', (event) => {
|
||||
event.waitUntil(
|
||||
caches.keys().then(keys =>
|
||||
Promise.all(
|
||||
keys.filter(k => k !== CACHE_NAME)
|
||||
.map(k => caches.delete(k))
|
||||
)
|
||||
).then(() => self.clients.claim())
|
||||
);
|
||||
});
|
||||
|
||||
// Fetch: network-first for API/WS, cache-first for shell assets
|
||||
self.addEventListener('fetch', (event) => {
|
||||
const url = new URL(event.request.url);
|
||||
|
||||
// Never cache API calls, WebSocket upgrades, or branding assets
|
||||
if (url.pathname.includes('/api/') ||
|
||||
url.pathname.includes('/ws') ||
|
||||
url.pathname.includes('/branding/') ||
|
||||
event.request.method !== 'GET') {
|
||||
return;
|
||||
}
|
||||
|
||||
// Cache-first for app shell, network fallback
|
||||
event.respondWith(
|
||||
caches.match(event.request).then(cached => {
|
||||
const fetchPromise = fetch(event.request).then(response => {
|
||||
// Update cache with fresh version
|
||||
if (response.ok) {
|
||||
const clone = response.clone();
|
||||
caches.open(CACHE_NAME).then(cache => cache.put(event.request, clone));
|
||||
}
|
||||
return response;
|
||||
}).catch(() => cached); // offline fallback to cache
|
||||
|
||||
return cached || fetchPromise;
|
||||
})
|
||||
);
|
||||
});
|
||||
Reference in New Issue
Block a user