123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- import { assert } from 'workbox-core/_private/assert.js';
- import { timeout } from 'workbox-core/_private/timeout.js';
- import { resultingClientExists } from 'workbox-core/_private/resultingClientExists.js';
- import { logger } from 'workbox-core/_private/logger.js';
- import { responsesAreSame } from './responsesAreSame.js';
- import { CACHE_UPDATED_MESSAGE_TYPE, CACHE_UPDATED_MESSAGE_META, DEFAULT_HEADERS_TO_CHECK } from './utils/constants.js';
- import './_version.js';
- const isSafari = /^((?!chrome|android).)*safari/i.test(navigator.userAgent);
- function defaultPayloadGenerator(data) {
- return {
- cacheName: data.cacheName,
- updatedURL: data.request.url,
- };
- }
- class BroadcastCacheUpdate {
-
- constructor({ headersToCheck, generatePayload, } = {}) {
- this._headersToCheck = headersToCheck || DEFAULT_HEADERS_TO_CHECK;
- this._generatePayload = generatePayload || defaultPayloadGenerator;
- }
-
- async notifyIfUpdated(options) {
- if (process.env.NODE_ENV !== 'production') {
- assert.isType(options.cacheName, 'string', {
- moduleName: 'workbox-broadcast-update',
- className: 'BroadcastCacheUpdate',
- funcName: 'notifyIfUpdated',
- paramName: 'cacheName',
- });
- assert.isInstance(options.newResponse, Response, {
- moduleName: 'workbox-broadcast-update',
- className: 'BroadcastCacheUpdate',
- funcName: 'notifyIfUpdated',
- paramName: 'newResponse',
- });
- 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)) {
- if (process.env.NODE_ENV !== 'production') {
- 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(resultingClientId);
-
-
-
-
-
- if (!resultingWin || isSafari) {
-
-
-
-
- await timeout(3500);
- }
- }
- const windows = await self.clients.matchAll({ type: 'window' });
- for (const win of windows) {
- win.postMessage(messageData);
- }
- }
- }
- }
- export { BroadcastCacheUpdate };
|