1/** 2 * TrueGaming service worker. 3 * 4 * Deliberately conservative: this is a news site, so pages are always fetched 5 * from the network and never served stale. The worker exists to 6 * 1. satisfy the PWA install criteria (a fetch handler in the site's scope) 7 * 2. cache the icon/font/static assets that never change per request 8 * 3. show a friendly offline page instead of the browser error 9 * 10 * It does not interfere with OneSignal's worker, which registers under its own 11 * plugin scope. 12 * 13 * Added 2026-08-12. 14 */ 15'use strict'; 16 17const VERSION = 'tg-v1'; 18const STATIC_CACHE = `${VERSION}-static`; 19const OFFLINE_URL = '/home/offline.html'; 20 21const PRECACHE = [ 22 OFFLINE_URL, 23 '/home/wp-content/themes/TrueGaming25/style/img/app-icons/any/icon-192.png', 24]; 25 26self.addEventListener('install', event => { 27 event.waitUntil( 28 caches.open(STATIC_CACHE) 29 .then(cache => cache.addAll(PRECACHE)) 30 .then(() => self.skipWaiting()) 31 .catch(() => self.skipWaiting()) 32 ); 33}); 34 35self.addEventListener('activate', event => { 36 event.waitUntil( 37 caches.keys() 38 .then(keys => Promise.all( 39 keys.filter(k => !k.startsWith(VERSION)).map(k => caches.delete(k)) 40 )) 41 .then(() => self.clients.claim()) 42 ); 43}); 44 45self.addEventListener('fetch', event => { 46 const req = event.request; 47 48 // Only handle same-origin GETs. Anything else (POST, admin, cross-origin, 49 // analytics) goes straight to the network untouched. 50 if (req.method !== 'GET' || new URL(req.url).origin !== self.location.origin) { 51 return; 52 } 53 if (req.url.includes('/wp-admin') || req.url.includes('/wp-login')) { 54 return; 55 } 56 57 // Static assets: serve from cache, refresh in the background. 58 if (/\.(png|jpe?g|gif|webp|svg|ico|woff2?|css|js)$/i.test(new URL(req.url).pathname)) { 59 event.respondWith( 60 caches.match(req).then(hit => { 61 const network = fetch(req).then(res => { 62 if (res && res.status === 200) { 63 const copy = res.clone(); 64 caches.open(STATIC_CACHE).then(c => c.put(req, copy)); 65 } 66 return res; 67 }).catch(() => hit); 68 return hit || network; 69 }) 70 ); 71 return; 72 } 73 74 // Pages: always network. Fall back to the offline page only on failure. 75 if (req.mode === 'navigate') { 76 event.respondWith( 77 fetch(req).catch(() => caches.match(OFFLINE_URL)) 78 ); 79 } 80});
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.