1 | this.workbox = this.workbox || {};
|
2 | this.workbox.core = (function (exports) {
|
3 | 'use strict';
|
4 |
|
5 | try {
|
6 | self['workbox:core:4.1.1'] && _();
|
7 | } catch (e) {}
|
8 |
|
9 | |
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | const logger = (() => {
|
16 | let inGroup = false;
|
17 | const methodToColorMap = {
|
18 | debug: `#7f8c8d`,
|
19 |
|
20 | log: `#2ecc71`,
|
21 |
|
22 | warn: `#f39c12`,
|
23 |
|
24 | error: `#c0392b`,
|
25 |
|
26 | groupCollapsed: `#3498db`,
|
27 |
|
28 | groupEnd: null
|
29 |
|
30 | };
|
31 |
|
32 | const print = function (method, args) {
|
33 | if (method === 'groupCollapsed') {
|
34 |
|
35 |
|
36 | if (/^((?!chrome|android).)*safari/i.test(navigator.userAgent)) {
|
37 | console[method](...args);
|
38 | return;
|
39 | }
|
40 | }
|
41 |
|
42 | const styles = [`background: ${methodToColorMap[method]}`, `border-radius: 0.5em`, `color: white`, `font-weight: bold`, `padding: 2px 0.5em`];
|
43 |
|
44 | const logPrefix = inGroup ? [] : ['%cworkbox', styles.join(';')];
|
45 | console[method](...logPrefix, ...args);
|
46 |
|
47 | if (method === 'groupCollapsed') {
|
48 | inGroup = true;
|
49 | }
|
50 |
|
51 | if (method === 'groupEnd') {
|
52 | inGroup = false;
|
53 | }
|
54 | };
|
55 |
|
56 | const api = {};
|
57 |
|
58 | for (const method of Object.keys(methodToColorMap)) {
|
59 | api[method] = (...args) => {
|
60 | print(method, args);
|
61 | };
|
62 | }
|
63 |
|
64 | return api;
|
65 | })();
|
66 |
|
67 | |
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 | const messages = {
|
75 | 'invalid-value': ({
|
76 | paramName,
|
77 | validValueDescription,
|
78 | value
|
79 | }) => {
|
80 | if (!paramName || !validValueDescription) {
|
81 | throw new Error(`Unexpected input to 'invalid-value' error.`);
|
82 | }
|
83 |
|
84 | return `The '${paramName}' parameter was given a value with an ` + `unexpected value. ${validValueDescription} Received a value of ` + `${JSON.stringify(value)}.`;
|
85 | },
|
86 | 'not-in-sw': ({
|
87 | moduleName
|
88 | }) => {
|
89 | if (!moduleName) {
|
90 | throw new Error(`Unexpected input to 'not-in-sw' error.`);
|
91 | }
|
92 |
|
93 | return `The '${moduleName}' must be used in a service worker.`;
|
94 | },
|
95 | 'not-an-array': ({
|
96 | moduleName,
|
97 | className,
|
98 | funcName,
|
99 | paramName
|
100 | }) => {
|
101 | if (!moduleName || !className || !funcName || !paramName) {
|
102 | throw new Error(`Unexpected input to 'not-an-array' error.`);
|
103 | }
|
104 |
|
105 | return `The parameter '${paramName}' passed into ` + `'${moduleName}.${className}.${funcName}()' must be an array.`;
|
106 | },
|
107 | 'incorrect-type': ({
|
108 | expectedType,
|
109 | paramName,
|
110 | moduleName,
|
111 | className,
|
112 | funcName
|
113 | }) => {
|
114 | if (!expectedType || !paramName || !moduleName || !funcName) {
|
115 | throw new Error(`Unexpected input to 'incorrect-type' error.`);
|
116 | }
|
117 |
|
118 | return `The parameter '${paramName}' passed into ` + `'${moduleName}.${className ? className + '.' : ''}` + `${funcName}()' must be of type ${expectedType}.`;
|
119 | },
|
120 | 'incorrect-class': ({
|
121 | expectedClass,
|
122 | paramName,
|
123 | moduleName,
|
124 | className,
|
125 | funcName,
|
126 | isReturnValueProblem
|
127 | }) => {
|
128 | if (!expectedClass || !moduleName || !funcName) {
|
129 | throw new Error(`Unexpected input to 'incorrect-class' error.`);
|
130 | }
|
131 |
|
132 | if (isReturnValueProblem) {
|
133 | return `The return value from ` + `'${moduleName}.${className ? className + '.' : ''}${funcName}()' ` + `must be an instance of class ${expectedClass.name}.`;
|
134 | }
|
135 |
|
136 | return `The parameter '${paramName}' passed into ` + `'${moduleName}.${className ? className + '.' : ''}${funcName}()' ` + `must be an instance of class ${expectedClass.name}.`;
|
137 | },
|
138 | 'missing-a-method': ({
|
139 | expectedMethod,
|
140 | paramName,
|
141 | moduleName,
|
142 | className,
|
143 | funcName
|
144 | }) => {
|
145 | if (!expectedMethod || !paramName || !moduleName || !className || !funcName) {
|
146 | throw new Error(`Unexpected input to 'missing-a-method' error.`);
|
147 | }
|
148 |
|
149 | return `${moduleName}.${className}.${funcName}() expected the ` + `'${paramName}' parameter to expose a '${expectedMethod}' method.`;
|
150 | },
|
151 | 'add-to-cache-list-unexpected-type': ({
|
152 | entry
|
153 | }) => {
|
154 | return `An unexpected entry was passed to ` + `'workbox-precaching.PrecacheController.addToCacheList()' The entry ` + `'${JSON.stringify(entry)}' isn't supported. You must supply an array of ` + `strings with one or more characters, objects with a url property or ` + `Request objects.`;
|
155 | },
|
156 | 'add-to-cache-list-conflicting-entries': ({
|
157 | firstEntry,
|
158 | secondEntry
|
159 | }) => {
|
160 | if (!firstEntry || !secondEntry) {
|
161 | throw new Error(`Unexpected input to ` + `'add-to-cache-list-duplicate-entries' error.`);
|
162 | }
|
163 |
|
164 | return `Two of the entries passed to ` + `'workbox-precaching.PrecacheController.addToCacheList()' had the URL ` + `${firstEntry._entryId} but different revision details. Workbox is ` + `is unable to cache and version the asset correctly. Please remove one ` + `of the entries.`;
|
165 | },
|
166 | 'plugin-error-request-will-fetch': ({
|
167 | thrownError
|
168 | }) => {
|
169 | if (!thrownError) {
|
170 | throw new Error(`Unexpected input to ` + `'plugin-error-request-will-fetch', error.`);
|
171 | }
|
172 |
|
173 | return `An error was thrown by a plugins 'requestWillFetch()' method. ` + `The thrown error message was: '${thrownError.message}'.`;
|
174 | },
|
175 | 'invalid-cache-name': ({
|
176 | cacheNameId,
|
177 | value
|
178 | }) => {
|
179 | if (!cacheNameId) {
|
180 | throw new Error(`Expected a 'cacheNameId' for error 'invalid-cache-name'`);
|
181 | }
|
182 |
|
183 | return `You must provide a name containing at least one character for ` + `setCacheDeatils({${cacheNameId}: '...'}). Received a value of ` + `'${JSON.stringify(value)}'`;
|
184 | },
|
185 | 'unregister-route-but-not-found-with-method': ({
|
186 | method
|
187 | }) => {
|
188 | if (!method) {
|
189 | throw new Error(`Unexpected input to ` + `'unregister-route-but-not-found-with-method' error.`);
|
190 | }
|
191 |
|
192 | return `The route you're trying to unregister was not previously ` + `registered for the method type '${method}'.`;
|
193 | },
|
194 | 'unregister-route-route-not-registered': () => {
|
195 | return `The route you're trying to unregister was not previously ` + `registered.`;
|
196 | },
|
197 | 'queue-replay-failed': ({
|
198 | name
|
199 | }) => {
|
200 | return `Replaying the background sync queue '${name}' failed.`;
|
201 | },
|
202 | 'duplicate-queue-name': ({
|
203 | name
|
204 | }) => {
|
205 | return `The Queue name '${name}' is already being used. ` + `All instances of backgroundSync.Queue must be given unique names.`;
|
206 | },
|
207 | 'expired-test-without-max-age': ({
|
208 | methodName,
|
209 | paramName
|
210 | }) => {
|
211 | return `The '${methodName}()' method can only be used when the ` + `'${paramName}' is used in the constructor.`;
|
212 | },
|
213 | 'unsupported-route-type': ({
|
214 | moduleName,
|
215 | className,
|
216 | funcName,
|
217 | paramName
|
218 | }) => {
|
219 | return `The supplied '${paramName}' parameter was an unsupported type. ` + `Please check the docs for ${moduleName}.${className}.${funcName} for ` + `valid input types.`;
|
220 | },
|
221 | 'not-array-of-class': ({
|
222 | value,
|
223 | expectedClass,
|
224 | moduleName,
|
225 | className,
|
226 | funcName,
|
227 | paramName
|
228 | }) => {
|
229 | return `The supplied '${paramName}' parameter must be an array of ` + `'${expectedClass}' objects. Received '${JSON.stringify(value)},'. ` + `Please check the call to ${moduleName}.${className}.${funcName}() ` + `to fix the issue.`;
|
230 | },
|
231 | 'max-entries-or-age-required': ({
|
232 | moduleName,
|
233 | className,
|
234 | funcName
|
235 | }) => {
|
236 | return `You must define either config.maxEntries or config.maxAgeSeconds` + `in ${moduleName}.${className}.${funcName}`;
|
237 | },
|
238 | 'statuses-or-headers-required': ({
|
239 | moduleName,
|
240 | className,
|
241 | funcName
|
242 | }) => {
|
243 | return `You must define either config.statuses or config.headers` + `in ${moduleName}.${className}.${funcName}`;
|
244 | },
|
245 | 'invalid-string': ({
|
246 | moduleName,
|
247 | className,
|
248 | funcName,
|
249 | paramName
|
250 | }) => {
|
251 | if (!paramName || !moduleName || !funcName) {
|
252 | throw new Error(`Unexpected input to 'invalid-string' error.`);
|
253 | }
|
254 |
|
255 | return `When using strings, the '${paramName}' parameter must start with ` + `'http' (for cross-origin matches) or '/' (for same-origin matches). ` + `Please see the docs for ${moduleName}.${funcName}() for ` + `more info.`;
|
256 | },
|
257 | 'channel-name-required': () => {
|
258 | return `You must provide a channelName to construct a ` + `BroadcastCacheUpdate instance.`;
|
259 | },
|
260 | 'invalid-responses-are-same-args': () => {
|
261 | return `The arguments passed into responsesAreSame() appear to be ` + `invalid. Please ensure valid Responses are used.`;
|
262 | },
|
263 | 'expire-custom-caches-only': () => {
|
264 | return `You must provide a 'cacheName' property when using the ` + `expiration plugin with a runtime caching strategy.`;
|
265 | },
|
266 | 'unit-must-be-bytes': ({
|
267 | normalizedRangeHeader
|
268 | }) => {
|
269 | if (!normalizedRangeHeader) {
|
270 | throw new Error(`Unexpected input to 'unit-must-be-bytes' error.`);
|
271 | }
|
272 |
|
273 | return `The 'unit' portion of the Range header must be set to 'bytes'. ` + `The Range header provided was "${normalizedRangeHeader}"`;
|
274 | },
|
275 | 'single-range-only': ({
|
276 | normalizedRangeHeader
|
277 | }) => {
|
278 | if (!normalizedRangeHeader) {
|
279 | throw new Error(`Unexpected input to 'single-range-only' error.`);
|
280 | }
|
281 |
|
282 | return `Multiple ranges are not supported. Please use a single start ` + `value, and optional end value. The Range header provided was ` + `"${normalizedRangeHeader}"`;
|
283 | },
|
284 | 'invalid-range-values': ({
|
285 | normalizedRangeHeader
|
286 | }) => {
|
287 | if (!normalizedRangeHeader) {
|
288 | throw new Error(`Unexpected input to 'invalid-range-values' error.`);
|
289 | }
|
290 |
|
291 | return `The Range header is missing both start and end values. At least ` + `one of those values is needed. The Range header provided was ` + `"${normalizedRangeHeader}"`;
|
292 | },
|
293 | 'no-range-header': () => {
|
294 | return `No Range header was found in the Request provided.`;
|
295 | },
|
296 | 'range-not-satisfiable': ({
|
297 | size,
|
298 | start,
|
299 | end
|
300 | }) => {
|
301 | return `The start (${start}) and end (${end}) values in the Range are ` + `not satisfiable by the cached response, which is ${size} bytes.`;
|
302 | },
|
303 | 'attempt-to-cache-non-get-request': ({
|
304 | url,
|
305 | method
|
306 | }) => {
|
307 | return `Unable to cache '${url}' because it is a '${method}' request and ` + `only 'GET' requests can be cached.`;
|
308 | },
|
309 | 'cache-put-with-no-response': ({
|
310 | url
|
311 | }) => {
|
312 | return `There was an attempt to cache '${url}' but the response was not ` + `defined.`;
|
313 | },
|
314 | 'no-response': ({
|
315 | url,
|
316 | error
|
317 | }) => {
|
318 | let message = `The strategy could not generate a response for '${url}'.`;
|
319 |
|
320 | if (error) {
|
321 | message += ` The underlying error is ${error}.`;
|
322 | }
|
323 |
|
324 | return message;
|
325 | },
|
326 | 'bad-precaching-response': ({
|
327 | url,
|
328 | status
|
329 | }) => {
|
330 | return `The precaching request for '${url}' failed with an HTTP ` + `status of ${status}.`;
|
331 | }
|
332 | };
|
333 |
|
334 | |
335 |
|
336 |
|
337 |
|
338 |
|
339 |
|
340 |
|
341 |
|
342 | const generatorFunction = (code, ...args) => {
|
343 | const message = messages[code];
|
344 |
|
345 | if (!message) {
|
346 | throw new Error(`Unable to find message for code '${code}'.`);
|
347 | }
|
348 |
|
349 | return message(...args);
|
350 | };
|
351 |
|
352 | const messageGenerator = generatorFunction;
|
353 |
|
354 | |
355 |
|
356 |
|
357 |
|
358 |
|
359 |
|
360 |
|
361 | |
362 |
|
363 |
|
364 |
|
365 |
|
366 |
|
367 |
|
368 |
|
369 |
|
370 |
|
371 | class WorkboxError extends Error {
|
372 | |
373 |
|
374 |
|
375 |
|
376 |
|
377 |
|
378 |
|
379 |
|
380 | constructor(errorCode, details) {
|
381 | let message = messageGenerator(errorCode, details);
|
382 | super(message);
|
383 | this.name = errorCode;
|
384 | this.details = details;
|
385 | }
|
386 |
|
387 | }
|
388 |
|
389 | |
390 |
|
391 |
|
392 |
|
393 |
|
394 |
|
395 |
|
396 | |
397 |
|
398 |
|
399 |
|
400 | const isSWEnv = moduleName => {
|
401 | if (!('ServiceWorkerGlobalScope' in self)) {
|
402 | throw new WorkboxError('not-in-sw', {
|
403 | moduleName
|
404 | });
|
405 | }
|
406 | };
|
407 | |
408 |
|
409 |
|
410 |
|
411 |
|
412 |
|
413 |
|
414 |
|
415 | const isArray = (value, {
|
416 | moduleName,
|
417 | className,
|
418 | funcName,
|
419 | paramName
|
420 | }) => {
|
421 | if (!Array.isArray(value)) {
|
422 | throw new WorkboxError('not-an-array', {
|
423 | moduleName,
|
424 | className,
|
425 | funcName,
|
426 | paramName
|
427 | });
|
428 | }
|
429 | };
|
430 |
|
431 | const hasMethod = (object, expectedMethod, {
|
432 | moduleName,
|
433 | className,
|
434 | funcName,
|
435 | paramName
|
436 | }) => {
|
437 | const type = typeof object[expectedMethod];
|
438 |
|
439 | if (type !== 'function') {
|
440 | throw new WorkboxError('missing-a-method', {
|
441 | paramName,
|
442 | expectedMethod,
|
443 | moduleName,
|
444 | className,
|
445 | funcName
|
446 | });
|
447 | }
|
448 | };
|
449 |
|
450 | const isType = (object, expectedType, {
|
451 | moduleName,
|
452 | className,
|
453 | funcName,
|
454 | paramName
|
455 | }) => {
|
456 | if (typeof object !== expectedType) {
|
457 | throw new WorkboxError('incorrect-type', {
|
458 | paramName,
|
459 | expectedType,
|
460 | moduleName,
|
461 | className,
|
462 | funcName
|
463 | });
|
464 | }
|
465 | };
|
466 |
|
467 | const isInstance = (object, expectedClass, {
|
468 | moduleName,
|
469 | className,
|
470 | funcName,
|
471 | paramName,
|
472 | isReturnValueProblem
|
473 | }) => {
|
474 | if (!(object instanceof expectedClass)) {
|
475 | throw new WorkboxError('incorrect-class', {
|
476 | paramName,
|
477 | expectedClass,
|
478 | moduleName,
|
479 | className,
|
480 | funcName,
|
481 | isReturnValueProblem
|
482 | });
|
483 | }
|
484 | };
|
485 |
|
486 | const isOneOf = (value, validValues, {
|
487 | paramName
|
488 | }) => {
|
489 | if (!validValues.includes(value)) {
|
490 | throw new WorkboxError('invalid-value', {
|
491 | paramName,
|
492 | value,
|
493 | validValueDescription: `Valid values are ${JSON.stringify(validValues)}.`
|
494 | });
|
495 | }
|
496 | };
|
497 |
|
498 | const isArrayOfClass = (value, expectedClass, {
|
499 | moduleName,
|
500 | className,
|
501 | funcName,
|
502 | paramName
|
503 | }) => {
|
504 | const error = new WorkboxError('not-array-of-class', {
|
505 | value,
|
506 | expectedClass,
|
507 | moduleName,
|
508 | className,
|
509 | funcName,
|
510 | paramName
|
511 | });
|
512 |
|
513 | if (!Array.isArray(value)) {
|
514 | throw error;
|
515 | }
|
516 |
|
517 | for (let item of value) {
|
518 | if (!(item instanceof expectedClass)) {
|
519 | throw error;
|
520 | }
|
521 | }
|
522 | };
|
523 |
|
524 | const finalAssertExports = {
|
525 | hasMethod,
|
526 | isArray,
|
527 | isInstance,
|
528 | isOneOf,
|
529 | isSWEnv,
|
530 | isType,
|
531 | isArrayOfClass
|
532 | };
|
533 |
|
534 | |
535 |
|
536 |
|
537 |
|
538 |
|
539 |
|
540 |
|
541 | const callbacks = new Set();
|
542 | |
543 |
|
544 |
|
545 |
|
546 |
|
547 |
|
548 |
|
549 |
|
550 | function registerQuotaErrorCallback(callback) {
|
551 | {
|
552 | finalAssertExports.isType(callback, 'function', {
|
553 | moduleName: 'workbox-core',
|
554 | funcName: 'register',
|
555 | paramName: 'callback'
|
556 | });
|
557 | }
|
558 |
|
559 | callbacks.add(callback);
|
560 |
|
561 | {
|
562 | logger.log('Registered a callback to respond to quota errors.', callback);
|
563 | }
|
564 | }
|
565 | |
566 |
|
567 |
|
568 |
|
569 |
|
570 |
|
571 |
|
572 |
|
573 |
|
574 | async function executeQuotaErrorCallbacks() {
|
575 | {
|
576 | logger.log(`About to run ${callbacks.size} callbacks to clean up caches.`);
|
577 | }
|
578 |
|
579 | for (const callback of callbacks) {
|
580 | await callback();
|
581 |
|
582 | {
|
583 | logger.log(callback, 'is complete.');
|
584 | }
|
585 | }
|
586 |
|
587 | {
|
588 | logger.log('Finished running callbacks.');
|
589 | }
|
590 | }
|
591 |
|
592 | |
593 |
|
594 |
|
595 |
|
596 |
|
597 |
|
598 |
|
599 |
|
600 | |
601 |
|
602 |
|
603 |
|
604 |
|
605 |
|
606 |
|
607 | |
608 |
|
609 |
|
610 |
|
611 |
|
612 |
|
613 |
|
614 |
|
615 | class DBWrapper {
|
616 | |
617 |
|
618 |
|
619 |
|
620 |
|
621 |
|
622 |
|
623 |
|
624 | constructor(name, version, {
|
625 | onupgradeneeded,
|
626 | onversionchange = this._onversionchange
|
627 | } = {}) {
|
628 | this._name = name;
|
629 | this._version = version;
|
630 | this._onupgradeneeded = onupgradeneeded;
|
631 | this._onversionchange = onversionchange;
|
632 |
|
633 | this._db = null;
|
634 | }
|
635 | |
636 |
|
637 |
|
638 |
|
639 |
|
640 | get db() {
|
641 | return this._db;
|
642 | }
|
643 | |
644 |
|
645 |
|
646 |
|
647 |
|
648 |
|
649 |
|
650 |
|
651 | async open() {
|
652 | if (this._db) return;
|
653 | this._db = await new Promise((resolve, reject) => {
|
654 |
|
655 |
|
656 |
|
657 |
|
658 |
|
659 | let openRequestTimedOut = false;
|
660 | setTimeout(() => {
|
661 | openRequestTimedOut = true;
|
662 | reject(new Error('The open request was blocked and timed out'));
|
663 | }, this.OPEN_TIMEOUT);
|
664 | const openRequest = indexedDB.open(this._name, this._version);
|
665 |
|
666 | openRequest.onerror = () => reject(openRequest.error);
|
667 |
|
668 | openRequest.onupgradeneeded = evt => {
|
669 | if (openRequestTimedOut) {
|
670 | openRequest.transaction.abort();
|
671 | evt.target.result.close();
|
672 | } else if (this._onupgradeneeded) {
|
673 | this._onupgradeneeded(evt);
|
674 | }
|
675 | };
|
676 |
|
677 | openRequest.onsuccess = ({
|
678 | target
|
679 | }) => {
|
680 | const db = target.result;
|
681 |
|
682 | if (openRequestTimedOut) {
|
683 | db.close();
|
684 | } else {
|
685 | db.onversionchange = this._onversionchange.bind(this);
|
686 | resolve(db);
|
687 | }
|
688 | };
|
689 | });
|
690 | return this;
|
691 | }
|
692 | |
693 |
|
694 |
|
695 |
|
696 |
|
697 |
|
698 |
|
699 |
|
700 |
|
701 |
|
702 | async getKey(storeName, query) {
|
703 | return (await this.getAllKeys(storeName, query, 1))[0];
|
704 | }
|
705 | |
706 |
|
707 |
|
708 |
|
709 |
|
710 |
|
711 |
|
712 |
|
713 |
|
714 |
|
715 |
|
716 | async getAll(storeName, query, count) {
|
717 | return await this.getAllMatching(storeName, {
|
718 | query,
|
719 | count
|
720 | });
|
721 | }
|
722 | |
723 |
|
724 |
|
725 |
|
726 |
|
727 |
|
728 |
|
729 |
|
730 |
|
731 |
|
732 |
|
733 | async getAllKeys(storeName, query, count) {
|
734 | return (await this.getAllMatching(storeName, {
|
735 | query,
|
736 | count,
|
737 | includeKeys: true
|
738 | })).map(({
|
739 | key
|
740 | }) => key);
|
741 | }
|
742 | |
743 |
|
744 |
|
745 |
|
746 |
|
747 |
|
748 |
|
749 |
|
750 |
|
751 |
|
752 |
|
753 |
|
754 |
|
755 |
|
756 |
|
757 |
|
758 |
|
759 |
|
760 | async getAllMatching(storeName, {
|
761 | index,
|
762 | query = null,
|
763 |
|
764 | direction = 'next',
|
765 | count,
|
766 | includeKeys
|
767 | } = {}) {
|
768 | return await this.transaction([storeName], 'readonly', (txn, done) => {
|
769 | const store = txn.objectStore(storeName);
|
770 | const target = index ? store.index(index) : store;
|
771 | const results = [];
|
772 |
|
773 | target.openCursor(query, direction).onsuccess = ({
|
774 | target
|
775 | }) => {
|
776 | const cursor = target.result;
|
777 |
|
778 | if (cursor) {
|
779 | const {
|
780 | primaryKey,
|
781 | key,
|
782 | value
|
783 | } = cursor;
|
784 | results.push(includeKeys ? {
|
785 | primaryKey,
|
786 | key,
|
787 | value
|
788 | } : value);
|
789 |
|
790 | if (count && results.length >= count) {
|
791 | done(results);
|
792 | } else {
|
793 | cursor.continue();
|
794 | }
|
795 | } else {
|
796 | done(results);
|
797 | }
|
798 | };
|
799 | });
|
800 | }
|
801 | |
802 |
|
803 |
|
804 |
|
805 |
|
806 |
|
807 |
|
808 |
|
809 |
|
810 |
|
811 |
|
812 |
|
813 |
|
814 |
|
815 |
|
816 |
|
817 |
|
818 |
|
819 | async transaction(storeNames, type, callback) {
|
820 | await this.open();
|
821 | return await new Promise((resolve, reject) => {
|
822 | const txn = this._db.transaction(storeNames, type);
|
823 |
|
824 | txn.onabort = ({
|
825 | target
|
826 | }) => reject(target.error);
|
827 |
|
828 | txn.oncomplete = () => resolve();
|
829 |
|
830 | callback(txn, value => resolve(value));
|
831 | });
|
832 | }
|
833 | |
834 |
|
835 |
|
836 |
|
837 |
|
838 |
|
839 |
|
840 |
|
841 |
|
842 |
|
843 |
|
844 | async _call(method, storeName, type, ...args) {
|
845 | const callback = (txn, done) => {
|
846 | txn.objectStore(storeName)[method](...args).onsuccess = ({
|
847 | target
|
848 | }) => {
|
849 | done(target.result);
|
850 | };
|
851 | };
|
852 |
|
853 | return await this.transaction([storeName], type, callback);
|
854 | }
|
855 | |
856 |
|
857 |
|
858 |
|
859 |
|
860 |
|
861 | _onversionchange() {
|
862 | this.close();
|
863 | }
|
864 | |
865 |
|
866 |
|
867 |
|
868 |
|
869 |
|
870 |
|
871 |
|
872 |
|
873 |
|
874 |
|
875 |
|
876 |
|
877 | close() {
|
878 | if (this._db) {
|
879 | this._db.close();
|
880 |
|
881 | this._db = null;
|
882 | }
|
883 | }
|
884 |
|
885 | }
|
886 |
|
887 |
|
888 | DBWrapper.prototype.OPEN_TIMEOUT = 2000;
|
889 |
|
890 | const methodsToWrap = {
|
891 | 'readonly': ['get', 'count', 'getKey', 'getAll', 'getAllKeys'],
|
892 | 'readwrite': ['add', 'put', 'clear', 'delete']
|
893 | };
|
894 |
|
895 | for (const [mode, methods] of Object.entries(methodsToWrap)) {
|
896 | for (const method of methods) {
|
897 | if (method in IDBObjectStore.prototype) {
|
898 |
|
899 | DBWrapper.prototype[method] = async function (storeName, ...args) {
|
900 | return await this._call(method, storeName, mode, ...args);
|
901 | };
|
902 | }
|
903 | }
|
904 | }
|
905 |
|
906 | |
907 |
|
908 |
|
909 |
|
910 |
|
911 |
|
912 |
|
913 | |
914 |
|
915 |
|
916 |
|
917 |
|
918 |
|
919 |
|
920 |
|
921 |
|
922 |
|
923 | const deleteDatabase = async name => {
|
924 | await new Promise((resolve, reject) => {
|
925 | const request = indexedDB.deleteDatabase(name);
|
926 |
|
927 | request.onerror = ({
|
928 | target
|
929 | }) => {
|
930 | reject(target.error);
|
931 | };
|
932 |
|
933 | request.onblocked = () => {
|
934 | reject(new Error('Delete blocked'));
|
935 | };
|
936 |
|
937 | request.onsuccess = () => {
|
938 | resolve();
|
939 | };
|
940 | });
|
941 | };
|
942 |
|
943 | |
944 |
|
945 |
|
946 |
|
947 |
|
948 |
|
949 |
|
950 | const _cacheNameDetails = {
|
951 | googleAnalytics: 'googleAnalytics',
|
952 | precache: 'precache-v2',
|
953 | prefix: 'workbox',
|
954 | runtime: 'runtime',
|
955 | suffix: self.registration.scope
|
956 | };
|
957 |
|
958 | const _createCacheName = cacheName => {
|
959 | return [_cacheNameDetails.prefix, cacheName, _cacheNameDetails.suffix].filter(value => value.length > 0).join('-');
|
960 | };
|
961 |
|
962 | const cacheNames = {
|
963 | updateDetails: details => {
|
964 | Object.keys(_cacheNameDetails).forEach(key => {
|
965 | if (typeof details[key] !== 'undefined') {
|
966 | _cacheNameDetails[key] = details[key];
|
967 | }
|
968 | });
|
969 | },
|
970 | getGoogleAnalyticsName: userCacheName => {
|
971 | return userCacheName || _createCacheName(_cacheNameDetails.googleAnalytics);
|
972 | },
|
973 | getPrecacheName: userCacheName => {
|
974 | return userCacheName || _createCacheName(_cacheNameDetails.precache);
|
975 | },
|
976 | getRuntimeName: userCacheName => {
|
977 | return userCacheName || _createCacheName(_cacheNameDetails.runtime);
|
978 | }
|
979 | };
|
980 |
|
981 | |
982 |
|
983 |
|
984 |
|
985 |
|
986 |
|
987 |
|
988 | const pluginEvents = {
|
989 | CACHE_DID_UPDATE: 'cacheDidUpdate',
|
990 | CACHE_WILL_UPDATE: 'cacheWillUpdate',
|
991 | CACHED_RESPONSE_WILL_BE_USED: 'cachedResponseWillBeUsed',
|
992 | FETCH_DID_FAIL: 'fetchDidFail',
|
993 | FETCH_DID_SUCCEED: 'fetchDidSucceed',
|
994 | REQUEST_WILL_FETCH: 'requestWillFetch'
|
995 | };
|
996 |
|
997 | |
998 |
|
999 |
|
1000 |
|
1001 |
|
1002 |
|
1003 |
|
1004 | const pluginUtils = {
|
1005 | filter: (plugins, callbackName) => {
|
1006 | return plugins.filter(plugin => callbackName in plugin);
|
1007 | }
|
1008 | };
|
1009 |
|
1010 | |
1011 |
|
1012 |
|
1013 |
|
1014 |
|
1015 |
|
1016 |
|
1017 |
|
1018 | const getFriendlyURL = url => {
|
1019 | const urlObj = new URL(url, location);
|
1020 |
|
1021 | if (urlObj.origin === location.origin) {
|
1022 | return urlObj.pathname;
|
1023 | }
|
1024 |
|
1025 | return urlObj.href;
|
1026 | };
|
1027 |
|
1028 | |
1029 |
|
1030 |
|
1031 |
|
1032 |
|
1033 |
|
1034 |
|
1035 | |
1036 |
|
1037 |
|
1038 |
|
1039 |
|
1040 |
|
1041 |
|
1042 |
|
1043 |
|
1044 |
|
1045 |
|
1046 |
|
1047 |
|
1048 |
|
1049 |
|
1050 |
|
1051 |
|
1052 |
|
1053 | const putWrapper = async ({
|
1054 | cacheName,
|
1055 | request,
|
1056 | response,
|
1057 | event,
|
1058 | plugins = [],
|
1059 | matchOptions
|
1060 | } = {}) => {
|
1061 | if (!response) {
|
1062 | {
|
1063 | logger.error(`Cannot cache non-existent response for ` + `'${getFriendlyURL(request.url)}'.`);
|
1064 | }
|
1065 |
|
1066 | throw new WorkboxError('cache-put-with-no-response', {
|
1067 | url: getFriendlyURL(request.url)
|
1068 | });
|
1069 | }
|
1070 |
|
1071 | let responseToCache = await _isResponseSafeToCache({
|
1072 | request,
|
1073 | response,
|
1074 | event,
|
1075 | plugins
|
1076 | });
|
1077 |
|
1078 | if (!responseToCache) {
|
1079 | {
|
1080 | logger.debug(`Response '${getFriendlyURL(request.url)}' will not be ` + `cached.`, responseToCache);
|
1081 | }
|
1082 |
|
1083 | return;
|
1084 | }
|
1085 |
|
1086 | {
|
1087 | if (responseToCache.method && responseToCache.method !== 'GET') {
|
1088 | throw new WorkboxError('attempt-to-cache-non-get-request', {
|
1089 | url: getFriendlyURL(request.url),
|
1090 | method: responseToCache.method
|
1091 | });
|
1092 | }
|
1093 | }
|
1094 |
|
1095 | const cache = await caches.open(cacheName);
|
1096 | const updatePlugins = pluginUtils.filter(plugins, pluginEvents.CACHE_DID_UPDATE);
|
1097 | let oldResponse = updatePlugins.length > 0 ? await matchWrapper({
|
1098 | cacheName,
|
1099 | request,
|
1100 | matchOptions
|
1101 | }) : null;
|
1102 |
|
1103 | {
|
1104 | logger.debug(`Updating the '${cacheName}' cache with a new Response for ` + `${getFriendlyURL(request.url)}.`);
|
1105 | }
|
1106 |
|
1107 | try {
|
1108 | await cache.put(request, responseToCache);
|
1109 | } catch (error) {
|
1110 |
|
1111 | if (error.name === 'QuotaExceededError') {
|
1112 | await executeQuotaErrorCallbacks();
|
1113 | }
|
1114 |
|
1115 | throw error;
|
1116 | }
|
1117 |
|
1118 | for (let plugin of updatePlugins) {
|
1119 | await plugin[pluginEvents.CACHE_DID_UPDATE].call(plugin, {
|
1120 | cacheName,
|
1121 | request,
|
1122 | event,
|
1123 | oldResponse,
|
1124 | newResponse: responseToCache
|
1125 | });
|
1126 | }
|
1127 | };
|
1128 | |
1129 |
|
1130 |
|
1131 |
|
1132 |
|
1133 |
|
1134 |
|
1135 |
|
1136 |
|
1137 |
|
1138 |
|
1139 |
|
1140 |
|
1141 |
|
1142 |
|
1143 |
|
1144 |
|
1145 | const matchWrapper = async ({
|
1146 | cacheName,
|
1147 | request,
|
1148 | event,
|
1149 | matchOptions,
|
1150 | plugins = []
|
1151 | }) => {
|
1152 | const cache = await caches.open(cacheName);
|
1153 | let cachedResponse = await cache.match(request, matchOptions);
|
1154 |
|
1155 | {
|
1156 | if (cachedResponse) {
|
1157 | logger.debug(`Found a cached response in '${cacheName}'.`);
|
1158 | } else {
|
1159 | logger.debug(`No cached response found in '${cacheName}'.`);
|
1160 | }
|
1161 | }
|
1162 |
|
1163 | for (const plugin of plugins) {
|
1164 | if (pluginEvents.CACHED_RESPONSE_WILL_BE_USED in plugin) {
|
1165 | cachedResponse = await plugin[pluginEvents.CACHED_RESPONSE_WILL_BE_USED].call(plugin, {
|
1166 | cacheName,
|
1167 | request,
|
1168 | event,
|
1169 | matchOptions,
|
1170 | cachedResponse
|
1171 | });
|
1172 |
|
1173 | {
|
1174 | if (cachedResponse) {
|
1175 | finalAssertExports.isInstance(cachedResponse, Response, {
|
1176 | moduleName: 'Plugin',
|
1177 | funcName: pluginEvents.CACHED_RESPONSE_WILL_BE_USED,
|
1178 | isReturnValueProblem: true
|
1179 | });
|
1180 | }
|
1181 | }
|
1182 | }
|
1183 | }
|
1184 |
|
1185 | return cachedResponse;
|
1186 | };
|
1187 | |
1188 |
|
1189 |
|
1190 |
|
1191 |
|
1192 |
|
1193 |
|
1194 |
|
1195 |
|
1196 |
|
1197 |
|
1198 |
|
1199 |
|
1200 |
|
1201 |
|
1202 |
|
1203 | const _isResponseSafeToCache = async ({
|
1204 | request,
|
1205 | response,
|
1206 | event,
|
1207 | plugins
|
1208 | }) => {
|
1209 | let responseToCache = response;
|
1210 | let pluginsUsed = false;
|
1211 |
|
1212 | for (let plugin of plugins) {
|
1213 | if (pluginEvents.CACHE_WILL_UPDATE in plugin) {
|
1214 | pluginsUsed = true;
|
1215 | responseToCache = await plugin[pluginEvents.CACHE_WILL_UPDATE].call(plugin, {
|
1216 | request,
|
1217 | response: responseToCache,
|
1218 | event
|
1219 | });
|
1220 |
|
1221 | {
|
1222 | if (responseToCache) {
|
1223 | finalAssertExports.isInstance(responseToCache, Response, {
|
1224 | moduleName: 'Plugin',
|
1225 | funcName: pluginEvents.CACHE_WILL_UPDATE,
|
1226 | isReturnValueProblem: true
|
1227 | });
|
1228 | }
|
1229 | }
|
1230 |
|
1231 | if (!responseToCache) {
|
1232 | break;
|
1233 | }
|
1234 | }
|
1235 | }
|
1236 |
|
1237 | if (!pluginsUsed) {
|
1238 | {
|
1239 | if (!responseToCache.status === 200) {
|
1240 | if (responseToCache.status === 0) {
|
1241 | logger.warn(`The response for '${request.url}' is an opaque ` + `response. The caching strategy that you're using will not ` + `cache opaque responses by default.`);
|
1242 | } else {
|
1243 | logger.debug(`The response for '${request.url}' returned ` + `a status code of '${response.status}' and won't be cached as a ` + `result.`);
|
1244 | }
|
1245 | }
|
1246 | }
|
1247 |
|
1248 | responseToCache = responseToCache.status === 200 ? responseToCache : null;
|
1249 | }
|
1250 |
|
1251 | return responseToCache ? responseToCache : null;
|
1252 | };
|
1253 |
|
1254 | const cacheWrapper = {
|
1255 | put: putWrapper,
|
1256 | match: matchWrapper
|
1257 | };
|
1258 |
|
1259 | |
1260 |
|
1261 |
|
1262 |
|
1263 |
|
1264 |
|
1265 |
|
1266 | |
1267 |
|
1268 |
|
1269 |
|
1270 |
|
1271 |
|
1272 |
|
1273 |
|
1274 |
|
1275 |
|
1276 |
|
1277 |
|
1278 |
|
1279 |
|
1280 |
|
1281 |
|
1282 | const wrappedFetch = async ({
|
1283 | request,
|
1284 | fetchOptions,
|
1285 | event,
|
1286 | plugins = []
|
1287 | }) => {
|
1288 |
|
1289 |
|
1290 |
|
1291 | if (event && event.preloadResponse) {
|
1292 | const possiblePreloadResponse = await event.preloadResponse;
|
1293 |
|
1294 | if (possiblePreloadResponse) {
|
1295 | {
|
1296 | logger.log(`Using a preloaded navigation response for ` + `'${getFriendlyURL(request.url)}'`);
|
1297 | }
|
1298 |
|
1299 | return possiblePreloadResponse;
|
1300 | }
|
1301 | }
|
1302 |
|
1303 | if (typeof request === 'string') {
|
1304 | request = new Request(request);
|
1305 | }
|
1306 |
|
1307 | {
|
1308 | finalAssertExports.isInstance(request, Request, {
|
1309 | paramName: request,
|
1310 | expectedClass: 'Request',
|
1311 | moduleName: 'workbox-core',
|
1312 | className: 'fetchWrapper',
|
1313 | funcName: 'wrappedFetch'
|
1314 | });
|
1315 | }
|
1316 |
|
1317 | const failedFetchPlugins = pluginUtils.filter(plugins, pluginEvents.FETCH_DID_FAIL);
|
1318 |
|
1319 |
|
1320 |
|
1321 | const originalRequest = failedFetchPlugins.length > 0 ? request.clone() : null;
|
1322 |
|
1323 | try {
|
1324 | for (let plugin of plugins) {
|
1325 | if (pluginEvents.REQUEST_WILL_FETCH in plugin) {
|
1326 | request = await plugin[pluginEvents.REQUEST_WILL_FETCH].call(plugin, {
|
1327 | request: request.clone(),
|
1328 | event
|
1329 | });
|
1330 |
|
1331 | {
|
1332 | if (request) {
|
1333 | finalAssertExports.isInstance(request, Request, {
|
1334 | moduleName: 'Plugin',
|
1335 | funcName: pluginEvents.CACHED_RESPONSE_WILL_BE_USED,
|
1336 | isReturnValueProblem: true
|
1337 | });
|
1338 | }
|
1339 | }
|
1340 | }
|
1341 | }
|
1342 | } catch (err) {
|
1343 | throw new WorkboxError('plugin-error-request-will-fetch', {
|
1344 | thrownError: err
|
1345 | });
|
1346 | }
|
1347 |
|
1348 |
|
1349 |
|
1350 |
|
1351 | let pluginFilteredRequest = request.clone();
|
1352 |
|
1353 | try {
|
1354 | let fetchResponse;
|
1355 |
|
1356 | if (request.mode === 'navigate') {
|
1357 | fetchResponse = await fetch(request);
|
1358 | } else {
|
1359 | fetchResponse = await fetch(request, fetchOptions);
|
1360 | }
|
1361 |
|
1362 | {
|
1363 | logger.debug(`Network request for ` + `'${getFriendlyURL(request.url)}' returned a response with ` + `status '${fetchResponse.status}'.`);
|
1364 | }
|
1365 |
|
1366 | for (const plugin of plugins) {
|
1367 | if (pluginEvents.FETCH_DID_SUCCEED in plugin) {
|
1368 | fetchResponse = await plugin[pluginEvents.FETCH_DID_SUCCEED].call(plugin, {
|
1369 | event,
|
1370 | request: pluginFilteredRequest,
|
1371 | response: fetchResponse
|
1372 | });
|
1373 |
|
1374 | {
|
1375 | if (fetchResponse) {
|
1376 | finalAssertExports.isInstance(fetchResponse, Response, {
|
1377 | moduleName: 'Plugin',
|
1378 | funcName: pluginEvents.FETCH_DID_SUCCEED,
|
1379 | isReturnValueProblem: true
|
1380 | });
|
1381 | }
|
1382 | }
|
1383 | }
|
1384 | }
|
1385 |
|
1386 | return fetchResponse;
|
1387 | } catch (error) {
|
1388 | {
|
1389 | logger.error(`Network request for ` + `'${getFriendlyURL(request.url)}' threw an error.`, error);
|
1390 | }
|
1391 |
|
1392 | for (const plugin of failedFetchPlugins) {
|
1393 | await plugin[pluginEvents.FETCH_DID_FAIL].call(plugin, {
|
1394 | error,
|
1395 | event,
|
1396 | originalRequest: originalRequest.clone(),
|
1397 | request: pluginFilteredRequest.clone()
|
1398 | });
|
1399 | }
|
1400 |
|
1401 | throw error;
|
1402 | }
|
1403 | };
|
1404 |
|
1405 | const fetchWrapper = {
|
1406 | fetch: wrappedFetch
|
1407 | };
|
1408 |
|
1409 | |
1410 |
|
1411 |
|
1412 |
|
1413 |
|
1414 |
|
1415 |
|
1416 |
|
1417 | var _private = Object.freeze({
|
1418 | DBWrapper: DBWrapper,
|
1419 | deleteDatabase: deleteDatabase,
|
1420 | WorkboxError: WorkboxError,
|
1421 | assert: finalAssertExports,
|
1422 | cacheNames: cacheNames,
|
1423 | cacheWrapper: cacheWrapper,
|
1424 | fetchWrapper: fetchWrapper,
|
1425 | getFriendlyURL: getFriendlyURL,
|
1426 | logger: logger
|
1427 | });
|
1428 |
|
1429 | |
1430 |
|
1431 |
|
1432 |
|
1433 |
|
1434 |
|
1435 |
|
1436 | |
1437 |
|
1438 |
|
1439 |
|
1440 |
|
1441 |
|
1442 |
|
1443 | const clientsClaim = () => {
|
1444 | addEventListener('activate', () => clients.claim());
|
1445 | };
|
1446 |
|
1447 | |
1448 |
|
1449 |
|
1450 |
|
1451 |
|
1452 |
|
1453 |
|
1454 | |
1455 |
|
1456 |
|
1457 |
|
1458 |
|
1459 |
|
1460 |
|
1461 |
|
1462 |
|
1463 |
|
1464 |
|
1465 |
|
1466 |
|
1467 | const cacheNames$1 = {
|
1468 | get googleAnalytics() {
|
1469 | return cacheNames.getGoogleAnalyticsName();
|
1470 | },
|
1471 |
|
1472 | get precache() {
|
1473 | return cacheNames.getPrecacheName();
|
1474 | },
|
1475 |
|
1476 | get runtime() {
|
1477 | return cacheNames.getRuntimeName();
|
1478 | }
|
1479 |
|
1480 | };
|
1481 |
|
1482 | |
1483 |
|
1484 |
|
1485 |
|
1486 |
|
1487 |
|
1488 |
|
1489 | |
1490 |
|
1491 |
|
1492 |
|
1493 |
|
1494 |
|
1495 |
|
1496 |
|
1497 |
|
1498 |
|
1499 |
|
1500 |
|
1501 |
|
1502 |
|
1503 |
|
1504 |
|
1505 |
|
1506 |
|
1507 | const setCacheNameDetails = details => {
|
1508 | {
|
1509 | Object.keys(details).forEach(key => {
|
1510 | finalAssertExports.isType(details[key], 'string', {
|
1511 | moduleName: 'workbox-core',
|
1512 | funcName: 'setCacheNameDetails',
|
1513 | paramName: `details.${key}`
|
1514 | });
|
1515 | });
|
1516 |
|
1517 | if ('precache' in details && details.precache.length === 0) {
|
1518 | throw new WorkboxError('invalid-cache-name', {
|
1519 | cacheNameId: 'precache',
|
1520 | value: details.precache
|
1521 | });
|
1522 | }
|
1523 |
|
1524 | if ('runtime' in details && details.runtime.length === 0) {
|
1525 | throw new WorkboxError('invalid-cache-name', {
|
1526 | cacheNameId: 'runtime',
|
1527 | value: details.runtime
|
1528 | });
|
1529 | }
|
1530 |
|
1531 | if ('googleAnalytics' in details && details.googleAnalytics.length === 0) {
|
1532 | throw new WorkboxError('invalid-cache-name', {
|
1533 | cacheNameId: 'googleAnalytics',
|
1534 | value: details.googleAnalytics
|
1535 | });
|
1536 | }
|
1537 | }
|
1538 |
|
1539 | cacheNames.updateDetails(details);
|
1540 | };
|
1541 |
|
1542 | |
1543 |
|
1544 |
|
1545 |
|
1546 |
|
1547 |
|
1548 |
|
1549 | |
1550 |
|
1551 |
|
1552 |
|
1553 |
|
1554 |
|
1555 |
|
1556 | const skipWaiting = () => {
|
1557 |
|
1558 |
|
1559 | addEventListener('install', () => self.skipWaiting());
|
1560 | };
|
1561 |
|
1562 | |
1563 |
|
1564 |
|
1565 |
|
1566 |
|
1567 |
|
1568 |
|
1569 |
|
1570 | try {
|
1571 | self.workbox.v = self.workbox.v || {};
|
1572 | } catch (errer) {}
|
1573 |
|
1574 | exports._private = _private;
|
1575 | exports.clientsClaim = clientsClaim;
|
1576 | exports.cacheNames = cacheNames$1;
|
1577 | exports.registerQuotaErrorCallback = registerQuotaErrorCallback;
|
1578 | exports.setCacheNameDetails = setCacheNameDetails;
|
1579 | exports.skipWaiting = skipWaiting;
|
1580 |
|
1581 | return exports;
|
1582 |
|
1583 | }({}));
|
1584 |
|