/
TestEntry
/
workers-test
Обзор
Документация
Войти
/
TestEntry
/
workers-test
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
sw.js
48 строк
1 KB
PontiyPirat
first_commit
17 апр 2025, 21:09
17 апр 2025, 21:09
aca7b57
Код
Авторство
О чём код?
const CACHE_NAME = 'sw_0_0_1'; const ASSETS_TO_CACHE = [ '/', '/index.html', '/main.js', ]; // Установка SW и кэширование ресурсов self.addEventListener('install', event => { console.log('[SW] Installing...'); event.waitUntil( caches.open(CACHE_NAME).then(cache => { console.log('[SW] Caching static assets'); return cache.addAll(ASSETS_TO_CACHE); }) ); }); // Активация и очистка старого кэша self.addEventListener('activate', event => { event.waitUntil( caches.keys().then(keys => Promise.all(keys .filter(key => key !== CACHE_NAME) .map(key => caches.delete(key)) ) ) ); }); // Перехват fetch-запросов self.addEventListener('fetch', event => { if (event.request.url.includes('httpbin.org')) { event.respondWith( caches.open('httpbin-cache').then(async cache => { const cached = await cache.match(event.request); if (cached) return cached; return fetch(event.request).then(response => { cache.put(event.request, response.clone()); return response; }).catch(() => new Response('Offline fallback') ); }) ); } });