PageSourceSearch

https://thomashunter.name/service-worker.js

js thomashunter.name collected 2026-09-25 02:48:18 UTC 2,385 bytes, 78 lines download raw bytes

1/**
2 * Unlike most Service Workers, this one always attempts to download a file
3 * from the network. Only when network access fails do we fallback to using
4 * the cache. When a request succeeds we always update the cache with the new
5 * version. If a request fails and the result isn't in the cache then we
6 * display an Offline page.
7 */
8const CACHE = 'content-v1';
9const OFFLINE = '/offline';
10
11const AUTO_CACHE = [
12  OFFLINE,
13  '/',
14  '/service-worker.js',
15  '/manifest.json',
16  '/styles/main.css',
17  '/scripts/main.js',
18  '/scripts/highlight.js',
19  '/favicon.ico',
20  '/images/icon-152.png',
21  '/avatar.jpg',
22  '/scripts/countly.min.js',
23];
24
25self.addEventListener('install', event => {
26  event.waitUntil(
27    caches.open(CACHE)
28      .then(cache => cache.addAll(AUTO_CACHE))
29      .then(self.skipWaiting())
30  );
31});
32
33self.addEventListener('activate', event => {
34  event.waitUntil(
35    caches.keys().then(cacheNames => {
36      return cacheNames.filter(cacheName => CACHE !== cacheName);
37    }).then(unusedCaches => {
38      console.log('DESTROYING CACHE', unusedCaches.join(','));
39      return Promise.all(unusedCaches.map(unusedCache => {
40        return caches.delete(unusedCache);
41      }));
42    }).then(() => self.clients.claim())
43  );
44});
45
46self.addEventListener('fetch', event => {
47  if (!event.request.url.startsWith(self.location.origin) || event.request.method !== 'GET') {
48    // External request, or POST, ignore
49    return void event.respondWith(fetch(event.request));
50  }
51
52  event.respondWith(
53    // Always try to download from server first
54    fetch(event.request).then(response => {
55      // When a download is successful cache the result
56      caches.open(CACHE).then(cache => {
57        cache.put(event.request, response)
58      });
59      // And of course display it
60      return response.clone();
61    }).catch((_err) => {
62      // A failure probably means network access issues
63      // See if we have a cached version
64      return caches.match(event.request).then(cachedResponse => {
65        if (cachedResponse) {
66          // We did have a cached version, display it
67          return cachedResponse;
68        }
69
70        // We did not have a cached version, display offline page
71        return caches.open(CACHE).then((cache) => {
72          const offlineRequest = new Request(OFFLINE);
73          return cache.match(offlineRequest);
74        });
75      });
76    })
77  );
78});

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.