PageSourceSearch

https://codewithkamlesh.in/service-worker.js

js codewithkamlesh.in collected 2026-10-04 12:50:51 UTC 1,474 bytes, 42 lines download raw bytes

1/* CWK PWA — minimal offline shell service worker.
2   Strategy: network-first for HTML/API, cache-first for static assets.
3   Bump CACHE_VERSION to invalidate the offline cache after a deploy. */
4const CACHE_VERSION = 'cwk-v1';
5const OFFLINE_URLS = ['/favicon.png', '/manifest.json'];
6
7self.addEventListener('install', (event) => {
8  self.skipWaiting();
9  event.waitUntil(
10    caches.open(CACHE_VERSION).then((c) => c.addAll(OFFLINE_URLS)).catch(() => {})
11  );
12});
13
14self.addEventListener('activate', (event) => {
15  event.waitUntil(
16    caches.keys().then((keys) => Promise.all(keys.filter(k => k !== CACHE_VERSION).map(k => caches.delete(k))))
17  );
18  self.clients.claim();
19});
20
21self.addEventListener('fetch', (event) => {
22  const req = event.request;
23  if (req.method !== 'GET') return;
24  const url = new URL(req.url);
25  // Never cache API or auth callbacks
26  if (url.pathname.startsWith('/api/') || url.pathname.startsWith('/auth/')) return;
27  // Network-first for HTML documents
28  if (req.mode === 'navigate' || req.headers.get('accept')?.includes('text/html')) {
29    event.respondWith(
30      fetch(req).catch(() => caches.match('/favicon.png'))
31    );
32    return;
33  }
34  // Cache-first for static assets
35  event.respondWith(
36    caches.match(req).then((cached) => cached || fetch(req).then((res) => {
37      const clone = res.clone();
38      caches.open(CACHE_VERSION).then((c) => c.put(req, clone)).catch(() => {});
39      return res;
40    }).catch(() => cached))
41  );
42});

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.