-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservice-worker.js
More file actions
91 lines (81 loc) · 2.74 KB
/
Copy pathservice-worker.js
File metadata and controls
91 lines (81 loc) · 2.74 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
const CACHE_NAME = 'thay-ai-core-v4';
const CDN_CACHE_NAME = 'thay-ai-cdn-v2';
// Assets that must be pre-cached immediately
const PRECACHE_ASSETS = [
'/',
'/index.html',
'/manifest.json'
];
self.addEventListener('install', (event) => {
event.waitUntil(
caches.open(CACHE_NAME).then((cache) => {
return cache.addAll(PRECACHE_ASSETS);
})
);
self.skipWaiting();
});
self.addEventListener('activate', (event) => {
event.waitUntil(
caches.keys().then((cacheNames) => {
return Promise.all(
cacheNames.map((cacheName) => {
// Clean up old versions but keep the CDN cache (it's expensive to redownload)
if (cacheName !== CACHE_NAME && cacheName !== CDN_CACHE_NAME) {
return caches.delete(cacheName);
}
})
);
})
);
self.clients.claim();
});
self.addEventListener('fetch', (event) => {
if (event.request.method !== 'GET') return;
const url = new URL(event.request.url);
// --- STRATEGY 1: CACHE FIRST (For Immutable Libraries & Assets) ---
if (
url.hostname.includes('aistudiocdn.com') ||
url.hostname.includes('cdn.tailwindcss.com') ||
url.hostname.includes('esm.sh') ||
url.hostname.includes('fonts.googleapis.com') ||
url.hostname.includes('gstatic.com') ||
url.hostname.includes('cdn.jsdelivr.net')
) {
event.respondWith(
caches.open(CDN_CACHE_NAME).then(async (cache) => {
const cachedResponse = await cache.match(event.request);
if (cachedResponse) {
return cachedResponse;
}
try {
const networkResponse = await fetch(event.request);
if (networkResponse && networkResponse.status === 200) {
cache.put(event.request, networkResponse.clone());
}
return networkResponse;
} catch (e) {
console.warn("CDN Fetch failed:", e);
return new Response('', { status: 408, statusText: 'Request Timeout' });
}
})
);
return;
}
// --- STRATEGY 2: STALE-WHILE-REVALIDATE (For App Shell & JS Chunks) ---
// This covers index.html and the Vite-generated JS chunks (e.g. assets/index-*.js)
// This ensures that after the first load, the app logic is available offline.
event.respondWith(
caches.open(CACHE_NAME).then(async (cache) => {
const cachedResponse = await cache.match(event.request);
const fetchPromise = fetch(event.request).then((networkResponse) => {
if (networkResponse && networkResponse.status === 200) {
cache.put(event.request, networkResponse.clone());
}
return networkResponse;
}).catch((e) => {
// console.log("Network fail, using cache only", e);
});
return cachedResponse || fetchPromise;
})
);
});