Spaces:
Running
Running
| const CACHE_NAME = 'teplostil-cache-v3'; // Обновите версию при изменении файлов! | |
| const URLS_TO_CACHE = [ | |
| '/', | |
| '/index.html', | |
| '/style.css', | |
| '/icon.png', | |
| '/icon-512x512.png', | |
| '/manifest.json' | |
| ]; | |
| self.addEventListener('install', (event) => { | |
| event.waitUntil( | |
| caches.open(CACHE_NAME).then((cache) => { | |
| console.log('Opened cache'); | |
| return cache.addAll(URLS_TO_CACHE); | |
| }) | |
| ); | |
| self.skipWaiting(); // Применяет новый SW сразу | |
| }); | |
| self.addEventListener('activate', (event) => { | |
| event.waitUntil( | |
| caches.keys().then((cacheNames) => { | |
| return Promise.all( | |
| cacheNames.map((cacheName) => { | |
| if (cacheName !== CACHE_NAME) { | |
| console.log('Deleting old cache:', cacheName); | |
| return caches.delete(cacheName); | |
| } | |
| }) | |
| ); | |
| }) | |
| ); | |
| self.clients.claim(); // Обновляет SW без перезапуска | |
| }); | |
| self.addEventListener('fetch', (event) => { | |
| if (event.request.mode === 'navigate') { | |
| event.respondWith( | |
| caches.match('/index.html').then((response) => { | |
| return response || fetch(event.request); | |
| }) | |
| ); | |
| } else { | |
| event.respondWith( | |
| caches.match(event.request).then((response) => { | |
| return response || fetch(event.request); | |
| }) | |
| ); | |
| } | |
| }); |