-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsw.js
More file actions
52 lines (48 loc) · 1.66 KB
/
sw.js
File metadata and controls
52 lines (48 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
const CACHE_NAME = 'kreasys-v2';
const STATIC_ASSETS = [
'/',
'/index.html',
'/css/styles.css',
'/js/core/state.js',
'/js/core/vfs.js',
'/js/core/ai.js',
'/js/core/telegram.js',
'/js/ui/app.js',
'/manifest.json'
];
// Install: pre-cache all static assets
self.addEventListener('install', event => {
event.waitUntil(
caches.open(CACHE_NAME).then(cache => cache.addAll(STATIC_ASSETS))
);
self.skipWaiting();
});
// Activate: clean up 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)))
)
);
self.clients.claim();
});
// Fetch: serve from cache first, fall back to network
self.addEventListener('fetch', event => {
// Skip non-GET and cross-origin API requests (LLM, Telegram)
if (event.request.method !== 'GET') return;
if (event.request.url.includes('api.telegram.org')) return;
if (event.request.url.includes('openrouter.ai') || event.request.url.includes('groq.com') || event.request.url.includes('nvidia.com')) return;
event.respondWith(
caches.match(event.request).then(cached => {
if (cached) return cached;
return fetch(event.request).then(resp => {
// Cache successful same-origin responses
if (resp && resp.status === 200 && resp.type === 'basic') {
const cloned = resp.clone();
caches.open(CACHE_NAME).then(c => c.put(event.request, cloned));
}
return resp;
}).catch(() => cached);
})
);
});