-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
service-worker.js
268 lines (224 loc) · 12.7 KB
/
service-worker.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/**
* Copyright 2016 Google Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// DO NOT EDIT THIS GENERATED OUTPUT DIRECTLY!
// This file should be overwritten as part of your build process.
// If you need to extend the behavior of the generated service worker, the best approach is to write
// additional code and include it using the importScripts option:
// https://github.com/GoogleChrome/sw-precache#importscripts-arraystring
//
// Alternatively, it's possible to make changes to the underlying template file and then use that as the
// new base for generating output, via the templateFilePath option:
// https://github.com/GoogleChrome/sw-precache#templatefilepath-string
//
// If you go that route, make sure that whenever you update your sw-precache dependency, you reconcile any
// changes made to this original template file with your modified copy.
// This generated service worker JavaScript will precache your site's resources.
// The code needs to be saved in a .js file at the top-level of your site, and registered
// from your pages in order to be used. See
// https://github.com/googlechrome/sw-precache/blob/master/demo/app/js/service-worker-registration.js
// for an example of how you can register this script and handle various service worker events.
/* eslint-env worker, serviceworker */
/* eslint-disable indent, no-unused-vars, no-multiple-empty-lines, max-nested-callbacks, space-before-function-paren, quotes, comma-spacing */
'use strict';
var precacheConfig = [["404.html","6cada695bb64ba7e46ac116c3f4c307b"],["README.md","96b6f6cf864bbf05f6cc626fdb418eae"],["assets/img/buymeacoffee.png","812e9287a8bc839df980e69f6385a5d7"],["assets/img/logo.png","850f3fa3f85806c2ff8aab035eae14ef"],["assets/img/main.png","249b27c73ba528e17642106c3c387754"],["assets/sounds/alert-long-break.mp3","fccc8bd60ad8ae2489545e87ec2ef937"],["assets/sounds/alert-short-break.mp3","b2e90a5ac307e3f64a106ee2b5e8ea04"],["assets/sounds/alert-work.mp3","a7d88fe90699ba3a6e2c33c0174088d9"],["assets/sounds/background_music/Campfire.mp3","57ddd74c6c98fae4de6143709700c537"],["assets/sounds/background_music/Forest.mp3","1e3b89d8ce029fc6db7daba32974abe1"],["assets/sounds/background_music/Ocean.mp3","2662f6397eb8a6027cc99e3022c78703"],["assets/sounds/background_music/Rain.mp3","6a34e8650915f2ee4b323a46d5ae37f8"],["assets/sounds/background_music/Windy_Desert.mp3","c17af3595b10f6a746b6fdb6a5bc6a33"],["assets/sounds/button-click.mp3","b46a69aca80db4354cca20ae543097f8"],["assets/sounds/notification-bell.mp3","0bb9efd3c64b5622bf3010b241f18f45"],["assets/sounds/tick.mp3","f2fa02a014a82f5d6672a6fcc3f5b222"],["css/styles.css","7bf3a4d85deb7916ba3ef0d90544f62d"],["index.html","db3ff6b2e6607807ffca41bcd4d00ed1"],["js/app.js","e6fd0cf34b4946aa3b8e02d8f5d9d63f"],["manifest.json","f8bc975a2061c3d90dcb0387a5c3552e"],["public/android-icon-144x144.png","ef0f722d1e0370eae406c14faea88d1b"],["public/android-icon-192x192.png","2e4f03d5b6d04882533f7d34644fdd0b"],["public/android-icon-36x36.png","1e9ce2762a7345b8cc4ccb78f6c53032"],["public/android-icon-48x48.png","3d1e3949e28727d244b38b784caa2c3c"],["public/android-icon-72x72.png","2fa359a05b8a268279485dc2e52fa4da"],["public/android-icon-96x96.png","f130d0441c5030f0a7890851df93aaf5"],["public/apple-icon-114x114.png","32b5d70e691b668532ace5a35a619cbb"],["public/apple-icon-120x120.png","b73503eaf9eb5829f3da0fe7a07cab9d"],["public/apple-icon-144x144.png","ef0f722d1e0370eae406c14faea88d1b"],["public/apple-icon-152x152.png","fd80b8c70b6cae744237981df7de85b7"],["public/apple-icon-180x180.png","f0035858db72719ba23ae6a787b71a15"],["public/apple-icon-57x57.png","fd3994de1dfc2a7d90a084ee5f9843ef"],["public/apple-icon-60x60.png","218f136008d91227e7649bb4f5cc5c5d"],["public/apple-icon-72x72.png","2fa359a05b8a268279485dc2e52fa4da"],["public/apple-icon-76x76.png","6dc8fe91dbfbb86556b5253a4c4fff6f"],["public/apple-icon-precomposed.png","fa71c5cc07aed9bdac5a04deac717cc2"],["public/apple-icon.png","fa71c5cc07aed9bdac5a04deac717cc2"],["public/browserconfig.xml","653d077300a12f09a69caeea7a8947f8"],["public/favicon-16x16.png","e93e44ba227703147b200a46ed1fba4a"],["public/favicon-32x32.png","947f052466427ae794f9eed5dc2069fa"],["public/favicon-96x96.png","f130d0441c5030f0a7890851df93aaf5"],["public/favicon.ico","2c08baa42398796be1ce81708c882c87"],["public/manifest.json","b58fcfa7628c9205cb11a1b2c3e8f99a"],["public/ms-icon-144x144.png","ef0f722d1e0370eae406c14faea88d1b"],["public/ms-icon-150x150.png","587a7718b23f30410095f1e67a3ac592"],["public/ms-icon-310x310.png","2fde5026374085333be5bbd4611ee880"],["public/ms-icon-70x70.png","ce104be2d147c74f5e9ac478f66aad36"]];
var cacheName = 'sw-precache-v3-sw-precache-' + (self.registration ? self.registration.scope : '');
var ignoreUrlParametersMatching = [/^utm_/];
var addDirectoryIndex = function(originalUrl, index) {
var url = new URL(originalUrl);
if (url.pathname.slice(-1) === '/') {
url.pathname += index;
}
return url.toString();
};
var cleanResponse = function(originalResponse) {
// If this is not a redirected response, then we don't have to do anything.
if (!originalResponse.redirected) {
return Promise.resolve(originalResponse);
}
// Firefox 50 and below doesn't support the Response.body stream, so we may
// need to read the entire body to memory as a Blob.
var bodyPromise = 'body' in originalResponse ?
Promise.resolve(originalResponse.body) :
originalResponse.blob();
return bodyPromise.then(function(body) {
// new Response() is happy when passed either a stream or a Blob.
return new Response(body, {
headers: originalResponse.headers,
status: originalResponse.status,
statusText: originalResponse.statusText
});
});
};
var createCacheKey = function(originalUrl, paramName, paramValue,
dontCacheBustUrlsMatching) {
// Create a new URL object to avoid modifying originalUrl.
var url = new URL(originalUrl);
// If dontCacheBustUrlsMatching is not set, or if we don't have a match,
// then add in the extra cache-busting URL parameter.
if (!dontCacheBustUrlsMatching ||
!(url.pathname.match(dontCacheBustUrlsMatching))) {
url.search += (url.search ? '&' : '') +
encodeURIComponent(paramName) + '=' + encodeURIComponent(paramValue);
}
return url.toString();
};
var isPathWhitelisted = function(whitelist, absoluteUrlString) {
// If the whitelist is empty, then consider all URLs to be whitelisted.
if (whitelist.length === 0) {
return true;
}
// Otherwise compare each path regex to the path of the URL passed in.
var path = (new URL(absoluteUrlString)).pathname;
return whitelist.some(function(whitelistedPathRegex) {
return path.match(whitelistedPathRegex);
});
};
var stripIgnoredUrlParameters = function(originalUrl,
ignoreUrlParametersMatching) {
var url = new URL(originalUrl);
// Remove the hash; see https://github.com/GoogleChrome/sw-precache/issues/290
url.hash = '';
url.search = url.search.slice(1) // Exclude initial '?'
.split('&') // Split into an array of 'key=value' strings
.map(function(kv) {
return kv.split('='); // Split each 'key=value' string into a [key, value] array
})
.filter(function(kv) {
return ignoreUrlParametersMatching.every(function(ignoredRegex) {
return !ignoredRegex.test(kv[0]); // Return true iff the key doesn't match any of the regexes.
});
})
.map(function(kv) {
return kv.join('='); // Join each [key, value] array into a 'key=value' string
})
.join('&'); // Join the array of 'key=value' strings into a string with '&' in between each
return url.toString();
};
var hashParamName = '_sw-precache';
var urlsToCacheKeys = new Map(
precacheConfig.map(function(item) {
var relativeUrl = item[0];
var hash = item[1];
var absoluteUrl = new URL(relativeUrl, self.location);
var cacheKey = createCacheKey(absoluteUrl, hashParamName, hash, false);
return [absoluteUrl.toString(), cacheKey];
})
);
function setOfCachedUrls(cache) {
return cache.keys().then(function(requests) {
return requests.map(function(request) {
return request.url;
});
}).then(function(urls) {
return new Set(urls);
});
}
self.addEventListener('install', function(event) {
event.waitUntil(
caches.open(cacheName).then(function(cache) {
return setOfCachedUrls(cache).then(function(cachedUrls) {
return Promise.all(
Array.from(urlsToCacheKeys.values()).map(function(cacheKey) {
// If we don't have a key matching url in the cache already, add it.
if (!cachedUrls.has(cacheKey)) {
var request = new Request(cacheKey, {credentials: 'same-origin'});
return fetch(request).then(function(response) {
// Bail out of installation unless we get back a 200 OK for
// every request.
if (!response.ok) {
throw new Error('Request for ' + cacheKey + ' returned a ' +
'response with status ' + response.status);
}
return cleanResponse(response).then(function(responseToCache) {
return cache.put(cacheKey, responseToCache);
});
});
}
})
);
});
}).then(function() {
// Force the SW to transition from installing -> active state
return self.skipWaiting();
})
);
});
self.addEventListener('activate', function(event) {
var setOfExpectedUrls = new Set(urlsToCacheKeys.values());
event.waitUntil(
caches.open(cacheName).then(function(cache) {
return cache.keys().then(function(existingRequests) {
return Promise.all(
existingRequests.map(function(existingRequest) {
if (!setOfExpectedUrls.has(existingRequest.url)) {
return cache.delete(existingRequest);
}
})
);
});
}).then(function() {
return self.clients.claim();
})
);
});
self.addEventListener('fetch', function(event) {
if (event.request.method === 'GET') {
// Should we call event.respondWith() inside this fetch event handler?
// This needs to be determined synchronously, which will give other fetch
// handlers a chance to handle the request if need be.
var shouldRespond;
// First, remove all the ignored parameters and hash fragment, and see if we
// have that URL in our cache. If so, great! shouldRespond will be true.
var url = stripIgnoredUrlParameters(event.request.url, ignoreUrlParametersMatching);
shouldRespond = urlsToCacheKeys.has(url);
// If shouldRespond is false, check again, this time with 'index.html'
// (or whatever the directoryIndex option is set to) at the end.
var directoryIndex = 'index.html';
if (!shouldRespond && directoryIndex) {
url = addDirectoryIndex(url, directoryIndex);
shouldRespond = urlsToCacheKeys.has(url);
}
// If shouldRespond is still false, check to see if this is a navigation
// request, and if so, whether the URL matches navigateFallbackWhitelist.
var navigateFallback = '';
if (!shouldRespond &&
navigateFallback &&
(event.request.mode === 'navigate') &&
isPathWhitelisted([], event.request.url)) {
url = new URL(navigateFallback, self.location).toString();
shouldRespond = urlsToCacheKeys.has(url);
}
// If shouldRespond was set to true at any point, then call
// event.respondWith(), using the appropriate cache key.
if (shouldRespond) {
event.respondWith(
caches.open(cacheName).then(function(cache) {
return cache.match(urlsToCacheKeys.get(url)).then(function(response) {
if (response) {
return response;
}
throw Error('The cached response that was expected is missing.');
});
}).catch(function(e) {
// Fall back to just fetch()ing the request if some unexpected error
// prevented the cached response from being valid.
console.warn('Couldn\'t serve response for "%s" from cache: %O', event.request.url, e);
return fetch(event.request);
})
);
}
}
});