feat: PWA support — manifest, service worker, icons, public auth exemption
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
75
cortex/static/sw.js
Normal file
75
cortex/static/sw.js
Normal file
@@ -0,0 +1,75 @@
|
||||
const CACHE = 'cortex-v1';
|
||||
|
||||
const PRECACHE = [
|
||||
'/static/style.css',
|
||||
'/static/app.js',
|
||||
'/static/marked.min.js',
|
||||
'/static/icon-192.png',
|
||||
'/static/icon-512.png',
|
||||
'/static/icon.svg',
|
||||
'/static/manifest.json',
|
||||
];
|
||||
|
||||
self.addEventListener('install', evt => {
|
||||
evt.waitUntil(
|
||||
caches.open(CACHE)
|
||||
.then(c => c.addAll(PRECACHE))
|
||||
.then(() => self.skipWaiting())
|
||||
);
|
||||
});
|
||||
|
||||
self.addEventListener('activate', evt => {
|
||||
evt.waitUntil(
|
||||
caches.keys()
|
||||
.then(keys => Promise.all(
|
||||
keys.filter(k => k !== CACHE).map(k => caches.delete(k))
|
||||
))
|
||||
.then(() => self.clients.claim())
|
||||
);
|
||||
});
|
||||
|
||||
self.addEventListener('fetch', evt => {
|
||||
const url = new URL(evt.request.url);
|
||||
|
||||
// Only handle same-origin GETs
|
||||
if (evt.request.method !== 'GET' || url.origin !== self.location.origin) return;
|
||||
|
||||
// Never intercept streaming or API calls
|
||||
if (
|
||||
url.pathname.startsWith('/chat') ||
|
||||
url.pathname.startsWith('/orchestrate') ||
|
||||
url.pathname.startsWith('/api/') ||
|
||||
url.pathname.startsWith('/distill') ||
|
||||
url.pathname.startsWith('/webhook') ||
|
||||
url.pathname.startsWith('/auth/')
|
||||
) return;
|
||||
|
||||
// Static assets — cache first, refresh in background (stale-while-revalidate)
|
||||
if (url.pathname.startsWith('/static/')) {
|
||||
evt.respondWith(
|
||||
caches.open(CACHE).then(cache =>
|
||||
cache.match(evt.request).then(cached => {
|
||||
const network = fetch(evt.request).then(resp => {
|
||||
if (resp.ok) cache.put(evt.request, resp.clone());
|
||||
return resp;
|
||||
});
|
||||
return cached || network;
|
||||
})
|
||||
)
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
// HTML pages — network first, cached shell fallback
|
||||
evt.respondWith(
|
||||
fetch(evt.request)
|
||||
.then(resp => {
|
||||
if (resp.ok) {
|
||||
const clone = resp.clone();
|
||||
caches.open(CACHE).then(c => c.put(evt.request, clone));
|
||||
}
|
||||
return resp;
|
||||
})
|
||||
.catch(() => caches.match(evt.request))
|
||||
);
|
||||
});
|
||||
Reference in New Issue
Block a user