File size: 1,322 Bytes
b8081da
 
bb0263f
 
b8081da
bb0263f
 
 
b8081da
bb0263f
 
b8081da
bb0263f
b8081da
bb0263f
 
b8081da
 
 
 
 
 
 
 
 
 
bb0263f
b8081da
 
 
 
 
 
 
 
bb0263f
b8081da
 
 
 
 
 
 
 
bb0263f
 
 
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
const CACHE_NAME = 'quantvat-shell-v1';
const STATIC_ASSETS = [
  '/',
  '/static/css/base.css',
  '/static/icons/icon-192.png',
  'https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;800&display=swap'
];

// Pre-cache the UI shell
self.addEventListener('install', (event) => {
  event.waitUntil(
    caches.open(CACHE_NAME).then((cache) => cache.addAll(STATIC_ASSETS))
  );
  self.skipWaiting();
});

// Cleanup old caches
self.addEventListener('activate', (event) => {
  event.waitUntil(
    caches.keys().then((keys) => Promise.all(
      keys.map((k) => k !== CACHE_NAME && caches.delete(k))
    ))
  );
});

// 3. Fetch Phase: Intelligent Routing
self.addEventListener('fetch', (event) => {
  const url = new URL(event.request.url);

  // CBypass cache for API calls to ensure fresh trading data
  if (url.pathname.startsWith('/api/') || url.pathname.includes('/tasks/')) {
    return;
  }

  // Stale-While-Revalidate for UI
  event.respondWith(
    caches.open(CACHE_NAME).then((cache) => {
      return cache.match(event.request).then((response) => {
        const fetchPromise = fetch(event.request).then((networkResponse) => {
          cache.put(event.request, networkResponse.clone());
          return networkResponse;
        });
        return response || fetchPromise;
      });
    })
  );
});