PageSourceSearch

https://www.liarumantscha.ch/service-worker.js

js liarumantscha.ch collected 2026-09-24 15:25:07 UTC 4,446 bytes, 103 lines download raw bytes

1/*
2 Copyright 2015 Google Inc. All Rights Reserved.
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 http://www.apache.org/licenses/LICENSE-2.0
7 Unless required by applicable law or agreed to in writing, software
8 distributed under the License is distributed on an "AS IS" BASIS,
9 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 See the License for the specific language governing permissions and
11 limitations under the License.
12*/
13
14'use strict';
15importScripts('/service-worker-cache-polyfill.js');
16
17// Incrementing CACHE_VERSION will kick off the install event and force previously cached
18// resources to be cached again.
19const CACHE_VERSION = 1;
20let CURRENT_CACHES = {
21  offline: 'offline-v' + CACHE_VERSION
22};
23const OFFLINE_URL = '/offline.html';
24
25function createCacheBustedRequest(url) {
26  let request = new Request(url, {cache: 'reload'});
27  // See https://fetch.spec.whatwg.org/#concept-request-mode
28  // This is not yet supported in Chrome as of M48, so we need to explicitly check to see
29  // if the cache: 'reload' option had any effect.
30  if ('cache' in request) {
31    return request;
32  }
33
34  // If {cache: 'reload'} didn't have any effect, append a cache-busting URL parameter instead.
35  let bustedUrl = new URL(url, self.location.href);
36  bustedUrl.search += (bustedUrl.search ? '&' : '') + 'cachebust=' + Date.now();
37  return new Request(bustedUrl);
38}
39
40self.addEventListener('install', event => {
41  event.waitUntil(
42      // We can't use cache.add() here, since we want OFFLINE_URL to be the cache key, but
43      // the actual URL we end up requesting might include a cache-busting parameter.
44      fetch(createCacheBustedRequest(OFFLINE_URL)).then(function(response) {
45        return caches.open(CURRENT_CACHES.offline).then(function(cache) {
46          return cache.put(OFFLINE_URL, response);
47        });
48      })
49  );
50});
51
52self.addEventListener('activate', event => {
53  // Delete all caches that aren't named in CURRENT_CACHES.
54  // While there is only one cache in this example, the same logic will handle the case where
55  // there are multiple versioned caches.
56  let expectedCacheNames = Object.keys(CURRENT_CACHES).map(function(key) {
57    return CURRENT_CACHES[key];
58  });
59
60  event.waitUntil(
61      caches.keys().then(cacheNames => {
62        return Promise.all(
63            cacheNames.map(cacheName => {
64              if (expectedCacheNames.indexOf(cacheName) === -1) {
65                // If this cache name isn't present in the array of "expected" cache names,
66                // then delete it.
67                console.log('Deleting out of date cache:', cacheName);
68                return caches.delete(cacheName);
69              }
70            })
71        );
72      })
73  );
74});
75
76self.addEventListener('fetch', event => {
77  // We only want to call event.respondWith() if this is a navigation request
78  // for an HTML page.
79  // request.mode of 'navigate' is unfortunately not supported in Chrome
80  // versions older than 49, so we need to include a less precise fallback,
81  // which checks for a GET request with an Accept: text/html header.
82  if (event.request.mode === 'navigate' ||
83      (event.request.method === 'GET' &&
84          event.request.headers.get('accept').includes('text/html'))) {
85    console.log('Handling fetch event for', event.request.url);
86    event.respondWith(
87        fetch(event.request).catch(error => {
88          // The catch is only triggered if fetch() throws an exception, which will most likely
89          // happen due to the server being unreachable.
90          // If fetch() returns a valid HTTP response with an response code in the 4xx or 5xx
91          // range, the catch() will NOT be called. If you need custom handling for 4xx or 5xx
92          // errors, see https://github.com/GoogleChrome/samples/tree/gh-pages/service-worker/fallback-response
93          console.log('Fetch failed; returning offline page instead.', error);
94          return caches.match(OFFLINE_URL);
95        })
96    );
97  }
98
99  // If our if() condition is false, then this fetch handler won't intercept the request.
100  // If there are any other fetch handlers registered, they will get a chance to call
101  // event.respondWith(). If no fetch handlers call event.respondWith(), the request will be
102  // handled by the browser as if there were no service worker involvement.
103});

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.