// Service Worker for NeuroMorph Gallery PWA const CACHE_NAME = 'neuromorph-gallery-v1.0.0'; const urlsToCache = [ '/', '/style.css', '/script.js', '/components/navbar.js', '/components/footer.js', '/components/artwork-card.js', '/gallery.html', '/upload.html', '/manifest.json' ]; // Install event self.addEventListener('install', (event) => { event.waitUntil( caches.open(CACHE_NAME) .then((cache) => { return cache.addAll(urlsToCache); }) ); }); // Activate event self.addEventListener('activate', (event) => { event.waitUntil( caches.keys().then((cacheNames) => { return Promise.all( cacheNames.map((cacheName) => { if (cacheName !== CACHE_NAME) { return caches.delete(cacheName); } }) ); }); // Fetch event self.addEventListener('fetch', (event) => { event.respondWith( caches.match(event.request) .then((response) => { // Return cached version or fetch from network return response || fetch(event.request); }) ); }); // Background sync for offline uploads self.addEventListener('sync', (event) => { if (event.tag === 'background-upload') { event.waitUntil(doBackgroundUpload()); } }); async function doBackgroundUpload() { // Handle background uploads when connection is restored const db = await openDB(); const pendingUploads = await db.getAll('pendingUploads'); for (const upload of pendingUploads) { try { await fetch('/api/upload', { method: 'POST', body: upload.data }); await db.delete('pendingUploads', upload.id); } catch (error) { console.error('Background upload failed:', error); } } } // IndexedDB for offline storage function openDB() { return new Promise((resolve, reject) => { const request = indexedDB.open('NeuroMorphGallery', 1); request.onerror = () => reject(request.error); request.onsuccess = () => resolve(request.result); request.onupgradeneeded = (event) => { const db = event.target.result; // Create object store for artworks if (!db.objectStoreNames.contains('artworks')) { const store = db.createObjectStore('artworks', { keyPath: 'id' }); store.createIndex('title', 'title', { unique: false }); store.createIndex('artist', 'artist.name', { unique: false }); }; }); }