PageSourceSearch

https://steinway.co.uk/service-worker.js

js steinway.co.uk collected 2026-09-24 23:12:20 UTC 4,704 bytes, 163 lines download raw bytes

1var version = 'v2.0.7:';
2
3var theme_path = 'wp-content/themes/steinway/';
4
5var offlineFundamentals = [
6];
7
8//Add core website files to cache during serviceworker installation
9var updateStaticCache = function () {
10  return caches.open(version + 'fundamentals').then(function (cache) {
11    return Promise.all(offlineFundamentals.map(function (value) {
12      var request = new Request(value);
13      var url = new URL(request.url);
14      if (url.origin != location.origin) {
15        request = new Request(value, { mode: 'no-cors' });
16      }
17      return fetch(request).then(function (response) {
18        var cachedCopy = response.clone();
19        return cache.put(request, cachedCopy);
20
21      });
22    }))
23  })
24};
25
26//Clear caches with a different version number
27var clearOldCaches = function () {
28  return caches.keys().then(function (keys) {
29    return Promise.all(
30      keys
31        .filter(function (key) {
32          return key.indexOf(version) != 0;
33        })
34        .map(function (key) {
35          return caches.delete(key);
36        })
37    );
38  })
39}
40
41/*
42	limits the cache
43	If cache has more than maxItems then it removes the first item in the cache
44*/
45var limitCache = function (cache, maxItems) {
46  cache.keys().then(function (items) {
47    if (items.length > maxItems) {
48      cache.delete(items[0]);
49    }
50  })
51}
52
53
54/*
55	trims the cache
56	If cache has more than maxItems then it removes the excess items starting from the beginning
57*/
58var trimCache = function (cacheName, maxItems) {
59  caches.open(cacheName)
60    .then(function (cache) {
61      cache.keys()
62        .then(function (keys) {
63          if (keys.length > maxItems) {
64            cache.delete(keys[0])
65              .then(trimCache(cacheName, maxItems));
66          }
67        });
68    });
69};
70
71
72//When the service worker is first added to a computer
73self.addEventListener("install", function (event) {
74  event.waitUntil(updateStaticCache()
75    .then(function () {
76      return self.skipWaiting();
77    })
78  );
79})
80
81self.addEventListener("message", function (event) {
82  var data = event.data;
83
84  //Send this command whenever many files are downloaded (ex: a page load)
85  if (data.command == "trimCache") {
86    trimCache(version + "pages", 25);
87    trimCache(version + "images", 10);
88    trimCache(version + "assets", 30);
89  }
90});
91
92//Service worker handles networking
93self.addEventListener("fetch", function (event) {
94  let online = navigator.onLine;
95  if (!online) {
96
97
98    //Fetch from network and cache
99    var fetchFromNetwork = function (response) {
100      var cacheCopy = response.clone();
101      if (event.request.headers.get('Accept').indexOf('text/html') != -1) {
102        caches.open(version + 'pages').then(function (cache) {
103          cache.put(event.request, cacheCopy).then(function () {
104            limitCache(cache, 25);
105          })
106        });
107      } else if (event.request.headers.get('Accept').indexOf('image') != -1) {
108        caches.open(version + 'images').then(function (cache) {
109          cache.put(event.request, cacheCopy).then(function () {
110            limitCache(cache, 10);
111          });
112        });
113      } else {
114        caches.open(version + 'assets').then(function add(cache) {
115          cache.put(event.request, cacheCopy);
116        });
117      }
118
119      return response;
120    }
121
122    //Fetch from network failed
123    var fallback = function () {
124      if (event.request.headers.get('Accept').indexOf('text/html') != -1) {
125        return caches.match(event.request).then(function (response) {
126          return response;
127        })
128      }
129    }
130
131    //This service worker won't touch the admin area and preview pages
132    if (event.request.url.match(/wp-admin/) || event.request.url.match(/preview=true/)) {
133      return;
134    }
135
136    //This service worker won't touch non-get requests
137    if (event.request.method != 'GET') {
138      return;
139    }
140
141    //For HTML requests, look for file in network, then cache if network fails.
142    if (event.request.headers.get('Accept').indexOf('text/html') != -1) {
143      event.respondWith(fetch(event.request).then(fetchFromNetwork, fallback));
144      return;
145    }
146
147    //For non-HTML requests, look for file in cache, then network if no cache exists.
148    event.respondWith(
149      caches.match(event.request).then(function (cached) {
150        return cached || fetch(event.request).then(fetchFromNetwork, fallback);
151      })
152    )
153  }
154});
155
156//After the install event
157self.addEventListener("activate", function (event) {
158  event.waitUntil(clearOldCaches()
159    .then(function () {
160      return self.clients.claim();
161    })
162  );
163});

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.