123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302 |
- this.workbox = this.workbox || {};
- this.workbox.broadcastUpdate = (function (exports, assert_js, timeout_js, resultingClientExists_js, logger_js, WorkboxError_js, dontWaitFor_js) {
- 'use strict';
- try {
- self['workbox:broadcast-update:6.6.0'] && _();
- } catch (e) {}
-
-
- const responsesAreSame = (firstResponse, secondResponse, headersToCheck) => {
- {
- if (!(firstResponse instanceof Response && secondResponse instanceof Response)) {
- throw new WorkboxError_js.WorkboxError('invalid-responses-are-same-args');
- }
- }
- const atLeastOneHeaderAvailable = headersToCheck.some(header => {
- return firstResponse.headers.has(header) && secondResponse.headers.has(header);
- });
- if (!atLeastOneHeaderAvailable) {
- {
- logger_js.logger.warn(`Unable to determine where the response has been updated ` + `because none of the headers that would be checked are present.`);
- logger_js.logger.debug(`Attempting to compare the following: `, firstResponse, secondResponse, headersToCheck);
- }
-
- return true;
- }
- return headersToCheck.every(header => {
- const headerStateComparison = firstResponse.headers.has(header) === secondResponse.headers.has(header);
- const headerValueComparison = firstResponse.headers.get(header) === secondResponse.headers.get(header);
- return headerStateComparison && headerValueComparison;
- });
- };
-
- const CACHE_UPDATED_MESSAGE_TYPE = 'CACHE_UPDATED';
- const CACHE_UPDATED_MESSAGE_META = 'workbox-broadcast-update';
- const NOTIFY_ALL_CLIENTS = true;
- const DEFAULT_HEADERS_TO_CHECK = ['content-length', 'etag', 'last-modified'];
-
-
-
- const isSafari = /^((?!chrome|android).)*safari/i.test(navigator.userAgent);
-
- function defaultPayloadGenerator(data) {
- return {
- cacheName: data.cacheName,
- updatedURL: data.request.url
- };
- }
-
- class BroadcastCacheUpdate {
-
- constructor({
- generatePayload,
- headersToCheck,
- notifyAllClients
- } = {}) {
- this._headersToCheck = headersToCheck || DEFAULT_HEADERS_TO_CHECK;
- this._generatePayload = generatePayload || defaultPayloadGenerator;
- this._notifyAllClients = notifyAllClients !== null && notifyAllClients !== void 0 ? notifyAllClients : NOTIFY_ALL_CLIENTS;
- }
-
- async notifyIfUpdated(options) {
- {
- assert_js.assert.isType(options.cacheName, 'string', {
- moduleName: 'workbox-broadcast-update',
- className: 'BroadcastCacheUpdate',
- funcName: 'notifyIfUpdated',
- paramName: 'cacheName'
- });
- assert_js.assert.isInstance(options.newResponse, Response, {
- moduleName: 'workbox-broadcast-update',
- className: 'BroadcastCacheUpdate',
- funcName: 'notifyIfUpdated',
- paramName: 'newResponse'
- });
- assert_js.assert.isInstance(options.request, Request, {
- moduleName: 'workbox-broadcast-update',
- className: 'BroadcastCacheUpdate',
- funcName: 'notifyIfUpdated',
- paramName: 'request'
- });
- }
- if (!options.oldResponse) {
- return;
- }
- if (!responsesAreSame(options.oldResponse, options.newResponse, this._headersToCheck)) {
- {
- logger_js.logger.log(`Newer response found (and cached) for:`, options.request.url);
- }
- const messageData = {
- type: CACHE_UPDATED_MESSAGE_TYPE,
- meta: CACHE_UPDATED_MESSAGE_META,
- payload: this._generatePayload(options)
- };
-
- if (options.request.mode === 'navigate') {
- let resultingClientId;
- if (options.event instanceof FetchEvent) {
- resultingClientId = options.event.resultingClientId;
- }
- const resultingWin = await resultingClientExists_js.resultingClientExists(resultingClientId);
-
-
-
-
- if (!resultingWin || isSafari) {
-
-
-
-
- await timeout_js.timeout(3500);
- }
- }
- if (this._notifyAllClients) {
- const windows = await self.clients.matchAll({
- type: 'window'
- });
- for (const win of windows) {
- win.postMessage(messageData);
- }
- } else {
-
- if (options.event instanceof FetchEvent) {
- const client = await self.clients.get(options.event.clientId);
- client === null || client === void 0 ? void 0 : client.postMessage(messageData);
- }
- }
- }
- }
- }
-
-
- class BroadcastUpdatePlugin {
-
- constructor(options) {
-
- this.cacheDidUpdate = async options => {
- dontWaitFor_js.dontWaitFor(this._broadcastUpdate.notifyIfUpdated(options));
- };
- this._broadcastUpdate = new BroadcastCacheUpdate(options);
- }
- }
- exports.BroadcastCacheUpdate = BroadcastCacheUpdate;
- exports.BroadcastUpdatePlugin = BroadcastUpdatePlugin;
- exports.responsesAreSame = responsesAreSame;
- return exports;
- }({}, workbox.core._private, workbox.core._private, workbox.core._private, workbox.core._private, workbox.core._private, workbox.core._private));
|