Файл: new_top/sw.js
Строк: 63
<?php
/* Top Rating System — service worker */
const CACHE = 'top-pwa-v1';
const PRECACHE = [
'/',
'/assets/css/style.css',
'/manifest.webmanifest',
'/assets/icons/icon-192.png',
'/assets/icons/icon-512.png'
];
self.addEventListener('install', (event) => {
event.waitUntil(
caches.open(CACHE).then((cache) => cache.addAll(PRECACHE)).then(() => self.skipWaiting())
);
});
self.addEventListener('activate', (event) => {
event.waitUntil(
caches.keys().then((keys) =>
Promise.all(keys.filter((k) => k !== CACHE).map((k) => caches.delete(k)))
).then(() => self.clients.claim())
);
});
self.addEventListener('fetch', (event) => {
const req = event.request;
if (req.method !== 'GET') return;
const url = new URL(req.url);
if (url.origin !== self.location.origin) return;
// Network-first for HTML / PHP routes; cache-first for static assets
const isStatic = /.(css|js|png|gif|jpg|jpeg|webp|svg|woff2?|ttf|webmanifest)$/i.test(url.pathname)
|| url.pathname.startsWith('/assets/');
if (isStatic) {
event.respondWith(
caches.match(req).then((cached) => {
const fetchPromise = fetch(req).then((res) => {
if (res && res.ok) {
const copy = res.clone();
caches.open(CACHE).then((c) => c.put(req, copy));
}
return res;
}).catch(() => cached);
return cached || fetchPromise;
})
);
return;
}
event.respondWith(
fetch(req).then((res) => {
return res;
}).catch(() => caches.match('/') || caches.match(req))
);
});
?>