1this.workbox = this.workbox || {}; 2this.workbox.broadcastUpdate = (function (exports, assert_js, timeout_js, resultingClientExists_js, logger_js, WorkboxError_js, dontWaitFor_js) { 3 'use strict'; 4 5 // @ts-ignore 6 try { 7 self['workbox:broadcast-update:7.4.0'] && _(); 8 } catch (e) {} 9 10 /* 11 Copyright 2018 Google LLC 12 13 Use of this source code is governed by an MIT-style 14 license that can be found in the LICENSE file or at 15 https://opensource.org/licenses/MIT. 16 */ 17 /** 18 * Given two `Response's`, compares several header values to see if they are 19 * the same or not. 20 * 21 * @param {Response} firstResponse 22 * @param {Response} secondResponse 23 * @param {Array<string>} headersToCheck 24 * @return {boolean} 25 * 26 * @memberof workbox-broadcast-update 27 */ 28 const responsesAreSame = (firstResponse, secondResponse, headersToCheck) => { 29 { 30 if (!(firstResponse instanceof Response && secondResponse instanceof Response)) { 31 throw new WorkboxError_js.WorkboxError('invalid-responses-are-same-args'); 32 } 33 } 34 const atLeastOneHeaderAvailable = headersToCheck.some(header => { 35 return firstResponse.headers.has(header) && secondResponse.headers.has(header); 36 }); 37 if (!atLeastOneHeaderAvailable) { 38 { 39 logger_js.logger.warn(`Unable to determine where the response has been updated ` + `because none of the headers that would be checked are present.`); 40 logger_js.logger.debug(`Attempting to compare the following: `, firstResponse, secondResponse, headersToCheck); 41 } 42 // Just return true, indicating the that responses are the same, since we 43 // can't determine otherwise. 44 return true; 45 } 46 return headersToCheck.every(header => { 47 const headerStateComparison = firstResponse.headers.has(header) === secondResponse.headers.has(header); 48 const headerValueComparison = firstResponse.headers.get(header) === secondResponse.headers.get(header); 49 return headerStateComparison && headerValueComparison; 50 }); 51 }; 52 53 /* 54 Copyright 2018 Google LLC 55 56 Use of this source code is governed by an MIT-style 57 license that can be found in the LICENSE file or at 58 https://opensource.org/licenses/MIT. 59 */ 60 const CACHE_UPDATED_MESSAGE_TYPE = 'CACHE_UPDATED'; 61 const CACHE_UPDATED_MESSAGE_META = 'workbox-broadcast-update'; 62 const NOTIFY_ALL_CLIENTS = true; 63 const DEFAULT_HEADERS_TO_CHECK = ['content-length', 'etag', 'last-modified']; 64 65 /* 66 Copyright 2018 Google LLC 67 68 Use of this source code is governed by an MIT-style 69 license that can be found in the LICENSE file or at 70 https://opensource.org/licenses/MIT. 71 */ 72 // UA-sniff Safari: https://stackoverflow.com/questions/7944460/detect-safari-browser 73 // TODO(philipwalton): remove once this Safari bug fix has been released. 74 // https://bugs.webkit.org/show_bug.cgi?id=201169 75 const isSafari = /^((?!chrome|android).)*safari/i.test(navigator.userAgent); 76 /** 77 * Generates the default payload used in update messages. By default the 78 * payload includes the `cacheName` and `updatedURL` fields. 79 * 80 * @return Object 81 * @private 82 */ 83 function defaultPayloadGenerator(data) { 84 return { 85 cacheName: data.cacheName, 86 updatedURL: data.request.url 87 }; 88 } 89 /** 90 * Uses the `postMessage()` API to inform any open windows/tabs when a cached 91 * response has been updated. 92 * 93 * For efficiency's sake, the underlying response bodies are not compared; 94 * only specific response headers are checked. 95 * 96 * @memberof workbox-broadcast-update 97 */ 98 class BroadcastCacheUpdate { 99 /** 100 * Construct a BroadcastCacheUpdate instance with a specific `channelName` to 101 * broadcast messages on 102 * 103 * @param {Object} [options] 104 * @param {Array<string>} [options.headersToCheck=['content-length', 'etag', 'last-modified']] 105 * A list of headers that will be used to determine whether the responses 106 * differ. 107 * @param {string} [options.generatePayload] A function whose return value 108 * will be used as the `payload` field in any cache update messages sent 109 * to the window clients. 110 * @param {boolean} [options.notifyAllClients=true] If true (the default) then 111 * all open clients will receive a message. If false, then only the client 112 * that make the original request will be notified of the update. 113 */ 114 constructor({ 115 generatePayload, 116 headersToCheck, 117 notifyAllClients 118 } = {}) { 119 this._headersToCheck = headersToCheck || DEFAULT_HEADERS_TO_CHECK; 120 this._generatePayload = generatePayload || defaultPayloadGenerator; 121 this._notifyAllClients = notifyAllClients !== null && notifyAllClients !== void 0 ? notifyAllClients : NOTIFY_ALL_CLIENTS; 122 } 123 /** 124 * Compares two [Responses](https://developer.mozilla.org/en-US/docs/Web/API/Response) 125 * and sends a message (via `postMessage()`) to all window clients if the 126 * responses differ. Neither of the Responses can be 127 * [opaque](https://developer.chrome.com/docs/workbox/caching-resources-during-runtime/#opaque-responses). 128 * 129 * The message that's posted has the following format (where `payload` can 130 * be customized via the `generatePayload` option the instance is created 131 * with): 132 * 133 * ``` 134 * { 135 * type: 'CACHE_UPDATED', 136 * meta: 'workbox-broadcast-update', 137 * payload: { 138 * cacheName: 'the-cache-name', 139 * updatedURL: 'https://example.com/' 140 * } 141 * } 142 * ``` 143 * 144 * @param {Object} options 145 * @param {Response} [options.oldResponse] Cached response to compare. 146 * @param {Response} options.newResponse Possibly updated response to compare. 147 * @param {Request} options.request The request. 148 * @param {string} options.cacheName Name of the cache the responses belong 149 * to. This is included in the broadcast message. 150 * @param {Event} options.event event The event that triggered 151 * this possible cache update. 152 * @return {Promise} Resolves once the update is sent. 153 */ 154 async notifyIfUpdated(options) { 155 { 156 assert_js.assert.isType(options.cacheName, 'string', { 157 moduleName: 'workbox-broadcast-update', 158 className: 'BroadcastCacheUpdate', 159 funcName: 'notifyIfUpdated', 160 paramName: 'cacheName' 161 }); 162 assert_js.assert.isInstance(options.newResponse, Response, { 163 moduleName: 'workbox-broadcast-update', 164 className: 'BroadcastCacheUpdate', 165 funcName: 'notifyIfUpdated', 166 paramName: 'newResponse' 167 }); 168 assert_js.assert.isInstance(options.request, Request, { 169 moduleName: 'workbox-broadcast-update', 170 className: 'BroadcastCacheUpdate', 171 funcName: 'notifyIfUpdated', 172 paramName: 'request' 173 }); 174 }
175 // Without two responses there is nothing to compare. 176 if (!options.oldResponse) { 177 return; 178 } 179 if (!responsesAreSame(options.oldResponse, options.newResponse, this._headersToCheck)) { 180 { 181 logger_js.logger.log(`Newer response found (and cached) for:`, options.request.url); 182 } 183 const messageData = { 184 type: CACHE_UPDATED_MESSAGE_TYPE, 185 meta: CACHE_UPDATED_MESSAGE_META, 186 payload: this._generatePayload(options) 187 }; 188 // For navigation requests, wait until the new window client exists 189 // before sending the message 190 if (options.request.mode === 'navigate') { 191 let resultingClientId; 192 if (options.event instanceof FetchEvent) { 193 resultingClientId = options.event.resultingClientId; 194 } 195 const resultingWin = await resultingClientExists_js.resultingClientExists(resultingClientId); 196 // Safari does not currently implement postMessage buffering and 197 // there's no good way to feature detect that, so to increase the 198 // chances of the message being delivered in Safari, we add a timeout. 199 // We also do this if `resultingClientExists()` didn't return a client, 200 // which means it timed out, so it's worth waiting a bit longer. 201 if (!resultingWin || isSafari) { 202 // 3500 is chosen because (according to CrUX data) 80% of mobile 203 // websites hit the DOMContentLoaded event in less than 3.5 seconds. 204 // And presumably sites implementing service worker are on the 205 // higher end of the performance spectrum. 206 await timeout_js.timeout(3500); 207 } 208 } 209 if (this._notifyAllClients) { 210 const windows = await self.clients.matchAll({ 211 type: 'window' 212 }); 213 for (const win of windows) { 214 win.postMessage(messageData); 215 } 216 } else { 217 // See https://github.com/GoogleChrome/workbox/issues/2895 218 if (options.event instanceof FetchEvent) { 219 const client = await self.clients.get(options.event.clientId); 220 client === null || client === void 0 ? void 0 : client.postMessage(messageData); 221 } 222 } 223 } 224 } 225 } 226 227 /* 228 Copyright 2018 Google LLC 229 230 Use of this source code is governed by an MIT-style 231 license that can be found in the LICENSE file or at 232 https://opensource.org/licenses/MIT. 233 */ 234 /** 235 * This plugin will automatically broadcast a message whenever a cached response 236 * is updated. 237 * 238 * @memberof workbox-broadcast-update 239 */ 240 class BroadcastUpdatePlugin { 241 /** 242 * Construct a {@link workbox-broadcast-update.BroadcastUpdate} instance with 243 * the passed options and calls its `notifyIfUpdated` method whenever the 244 * plugin's `cacheDidUpdate` callback is invoked. 245 * 246 * @param {Object} [options] 247 * @param {Array<string>} [options.headersToCheck=['content-length', 'etag', 'last-modified']] 248 * A list of headers that will be used to determine whether the responses 249 * differ. 250 * @param {string} [options.generatePayload] A function whose return value 251 * will be used as the `payload` field in any cache update messages sent 252 * to the window clients. 253 */ 254 constructor(options) { 255 /** 256 * A "lifecycle" callback that will be triggered automatically by the 257 * `workbox-sw` and `workbox-runtime-caching` handlers when an entry is 258 * added to a cache. 259 * 260 * @private 261 * @param {Object} options The input object to this function. 262 * @param {string} options.cacheName Name of the cache being updated. 263 * @param {Response} [options.oldResponse] The previous cached value, if any. 264 * @param {Response} options.newResponse The new value in the cache. 265 * @param {Request} options.request The request that triggered the update. 266 * @param {Request} options.event The event that triggered the update. 267 */ 268 this.cacheDidUpdate = async options => { 269 dontWaitFor_js.dontWaitFor(this._broadcastUpdate.notifyIfUpdated(options)); 270 }; 271 this._broadcastUpdate = new BroadcastCacheUpdate(options); 272 } 273 } 274 275 exports.BroadcastCacheUpdate = BroadcastCacheUpdate; 276 exports.BroadcastUpdatePlugin = BroadcastUpdatePlugin; 277 exports.responsesAreSame = responsesAreSame; 278 279 return exports; 280 281})({}, workbox.core._private, workbox.core._private, workbox.core._private, workbox.core._private, workbox.core._private, workbox.core._private); 282//# sourceMappingURL=workbox-broadcast-update.dev.js.map
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.