CoolFace
Apppublic

ahmedmiloudi/BioTechLabAI

sourceHugging Faceapache-2.0updated 8mo agoView on Hugging Face
1likes
sw.js88 linesDownload Raw Back to static
1const CACHE_NAME = 'drug-discovery-ai-v1';2const urlsToCache = [3  '/',4];5 6// Installation7self.addEventListener('install', (event) => {8  console.log('[SW] Installing...');9  event.waitUntil(10    caches.open(CACHE_NAME)11      .then((cache) => {12        console.log('[SW] Caching app shell');13        return cache.addAll(urlsToCache);14      })15      .catch(err => console.error('[SW] Cache failed:', err))16  );17  self.skipWaiting();18});19 20// Activation21self.addEventListener('activate', (event) => {22  console.log('[SW] Activating...');23  event.waitUntil(24    caches.keys().then((cacheNames) => {25      return Promise.all(26        cacheNames.map((cacheName) => {27          if (cacheName !== CACHE_NAME) {28            console.log('[SW] Deleting old cache:', cacheName);29            return caches.delete(cacheName);30          }31        })32      );33    })34  );35  return self.clients.claim();36});37 38// Fetch - Network First Strategy (for dynamic Streamlit app)39self.addEventListener('fetch', (event) => {40  // Skip cross-origin requests41  if (!event.request.url.startsWith(self.location.origin)) {42    return;43  }44 45  event.respondWith(46    fetch(event.request)47      .then((response) => {48        // Clone response for cache49        const responseClone = response.clone();50        51        // Only cache successful GET requests52        if (event.request.method === 'GET' && response.status === 200) {53          caches.open(CACHE_NAME).then((cache) => {54            cache.put(event.request, responseClone);55          });56        }57        58        return response;59      })60      .catch(() => {61        // If network fails, try cache62        return caches.match(event.request).then(cachedResponse => {63          if (cachedResponse) {64            return cachedResponse;65          }66          67          // Return offline page for navigation requests68          if (event.request.mode === 'navigate') {69            return caches.match('/');70          }71        });72      })73  );74});75 76// Background sync (for future offline functionality)77self.addEventListener('sync', (event) => {78  console.log('[SW] Background sync:', event.tag);79  if (event.tag === 'sync-data') {80    event.waitUntil(syncData());81  }82});83 84async function syncData() {85  // Placeholder for syncing cached analysis results86  console.log('[SW] Syncing data...');87}88