// ===== Main App Cache ===== const CACHE_NAME = 'trust-clinic-v1'; const ASSETS_TO_CACHE = [ '/', '/wp-content/uploads/pwa-icons/icon-192.png', '/wp-content/uploads/pwa-icons/icon-512.png' ]; // نصب سرویس ورکر و کش کردن فایلهای اولیه self.addEventListener('install', (event) => { event.waitUntil( caches.open(CACHE_NAME).then((cache) => { console.log('Caching essential assets'); return cache.addAll(ASSETS_TO_CACHE); }) ); self.skipWaiting(); }); // فعالسازی و پاکسازی کشهای قدیمی self.addEventListener('activate', (event) => { event.waitUntil( caches.keys().then((cacheNames) => { return Promise.all( cacheNames.map((cache) => { if (cache !== CACHE_NAME && cache !== 'trust-static-v2') { return caches.delete(cache); } }) ); }) ); self.clients.claim(); }); // مدیریت درخواستها (برای تایید نصب الزامی است) self.addEventListener('fetch', (event) => { event.respondWith( caches.match(event.request).then((response) => { return response || fetch(event.request); }).catch(() => {}) ); }); // ===== Extra Browser Cache Layer (No Conflict) ===== const STATIC_CACHE = 'trust-static-v2'; // تغییر داده شد self.addEventListener('fetch', (event) => { const req = event.request; if (req.method !== 'GET') return; const url = req.url; if ( url.includes('/wp-content/') || url.endsWith('.css') || url.endsWith('.js') || url.match(/\.(png|jpg|jpeg|webp|svg|gif|woff2?)$/) ) { event.respondWith( caches.match(req).then(cached => { if (cached) return cached; return fetch(req).then(response => { return caches.open(STATIC_CACHE).then(cache => { cache.put(req, response.clone()); return response; }); }); }) ); } });