UNPKG

17 kBJavaScriptView Raw
1this.workbox = this.workbox || {};
2this.workbox.broadcastUpdate = (function (exports, assert_mjs, getFriendlyURL_mjs, logger_mjs, Deferred_mjs, WorkboxError_mjs) {
3 'use strict';
4
5 try {
6 self['workbox:broadcast-update:4.3.0'] && _();
7 } catch (e) {} // eslint-disable-line
8
9 /*
10 Copyright 2018 Google LLC
11
12 Use of this source code is governed by an MIT-style
13 license that can be found in the LICENSE file or at
14 https://opensource.org/licenses/MIT.
15 */
16 /**
17 * Given two `Response's`, compares several header values to see if they are
18 * the same or not.
19 *
20 * @param {Response} firstResponse
21 * @param {Response} secondResponse
22 * @param {Array<string>} headersToCheck
23 * @return {boolean}
24 *
25 * @memberof workbox.broadcastUpdate
26 * @private
27 */
28
29 const responsesAreSame = (firstResponse, secondResponse, headersToCheck) => {
30 {
31 if (!(firstResponse instanceof Response && secondResponse instanceof Response)) {
32 throw new WorkboxError_mjs.WorkboxError('invalid-responses-are-same-args');
33 }
34 }
35
36 const atLeastOneHeaderAvailable = headersToCheck.some(header => {
37 return firstResponse.headers.has(header) && secondResponse.headers.has(header);
38 });
39
40 if (!atLeastOneHeaderAvailable) {
41 {
42 logger_mjs.logger.warn(`Unable to determine where the response has been updated ` + `because none of the headers that would be checked are present.`);
43 logger_mjs.logger.debug(`Attempting to compare the following: `, firstResponse, secondResponse, headersToCheck);
44 } // Just return true, indicating the that responses are the same, since we
45 // can't determine otherwise.
46
47
48 return true;
49 }
50
51 return headersToCheck.every(header => {
52 const headerStateComparison = firstResponse.headers.has(header) === secondResponse.headers.has(header);
53 const headerValueComparison = firstResponse.headers.get(header) === secondResponse.headers.get(header);
54 return headerStateComparison && headerValueComparison;
55 });
56 };
57
58 /*
59 Copyright 2018 Google LLC
60
61 Use of this source code is governed by an MIT-style
62 license that can be found in the LICENSE file or at
63 https://opensource.org/licenses/MIT.
64 */
65 const CACHE_UPDATED_MESSAGE_TYPE = 'CACHE_UPDATED';
66 const CACHE_UPDATED_MESSAGE_META = 'workbox-broadcast-update';
67 const DEFAULT_BROADCAST_CHANNEL_NAME = 'workbox';
68 const DEFAULT_DEFER_NOTIFICATION_TIMEOUT = 10000;
69 const DEFAULT_HEADERS_TO_CHECK = ['content-length', 'etag', 'last-modified'];
70
71 /*
72 Copyright 2018 Google LLC
73
74 Use of this source code is governed by an MIT-style
75 license that can be found in the LICENSE file or at
76 https://opensource.org/licenses/MIT.
77 */
78 /**
79 * You would not normally call this method directly; it's called automatically
80 * by an instance of the {@link BroadcastCacheUpdate} class. It's exposed here
81 * for the benefit of developers who would rather not use the full
82 * `BroadcastCacheUpdate` implementation.
83 *
84 * Calling this will dispatch a message on the provided
85 * {@link https://developers.google.com/web/updates/2016/09/broadcastchannel|Broadcast Channel}
86 * to notify interested subscribers about a change to a cached resource.
87 *
88 * The message that's posted has a formation inspired by the
89 * [Flux standard action](https://github.com/acdlite/flux-standard-action#introduction)
90 * format like so:
91 *
92 * ```
93 * {
94 * type: 'CACHE_UPDATED',
95 * meta: 'workbox-broadcast-update',
96 * payload: {
97 * cacheName: 'the-cache-name',
98 * updatedURL: 'https://example.com/'
99 * }
100 * }
101 * ```
102 *
103 * (Usage of [Flux](https://facebook.github.io/flux/) itself is not at
104 * all required.)
105 *
106 * @param {Object} options
107 * @param {string} options.cacheName The name of the cache in which the updated
108 * `Response` was stored.
109 * @param {string} options.url The URL associated with the updated `Response`.
110 * @param {BroadcastChannel} [options.channel] The `BroadcastChannel` to use.
111 * If no channel is set or the browser doesn't support the BroadcastChannel
112 * api, then an attempt will be made to `postMessage` each window client.
113 *
114 * @memberof workbox.broadcastUpdate
115 */
116
117 const broadcastUpdate = async ({
118 channel,
119 cacheName,
120 url
121 }) => {
122 {
123 assert_mjs.assert.isType(cacheName, 'string', {
124 moduleName: 'workbox-broadcast-update',
125 className: '~',
126 funcName: 'broadcastUpdate',
127 paramName: 'cacheName'
128 });
129 assert_mjs.assert.isType(url, 'string', {
130 moduleName: 'workbox-broadcast-update',
131 className: '~',
132 funcName: 'broadcastUpdate',
133 paramName: 'url'
134 });
135 }
136
137 const data = {
138 type: CACHE_UPDATED_MESSAGE_TYPE,
139 meta: CACHE_UPDATED_MESSAGE_META,
140 payload: {
141 cacheName: cacheName,
142 updatedURL: url
143 }
144 };
145
146 if (channel) {
147 channel.postMessage(data);
148 } else {
149 const windows = await clients.matchAll({
150 type: 'window'
151 });
152
153 for (const win of windows) {
154 win.postMessage(data);
155 }
156 }
157 };
158
159 /*
160 Copyright 2018 Google LLC
161
162 Use of this source code is governed by an MIT-style
163 license that can be found in the LICENSE file or at
164 https://opensource.org/licenses/MIT.
165 */
166 /**
167 * Uses the [Broadcast Channel API]{@link https://developers.google.com/web/updates/2016/09/broadcastchannel}
168 * to notify interested parties when a cached response has been updated.
169 * In browsers that do not support the Broadcast Channel API, the instance
170 * falls back to sending the update via `postMessage()` to all window clients.
171 *
172 * For efficiency's sake, the underlying response bodies are not compared;
173 * only specific response headers are checked.
174 *
175 * @memberof workbox.broadcastUpdate
176 */
177
178 class BroadcastCacheUpdate {
179 /**
180 * Construct a BroadcastCacheUpdate instance with a specific `channelName` to
181 * broadcast messages on
182 *
183 * @param {Object} options
184 * @param {Array<string>}
185 * [options.headersToCheck=['content-length', 'etag', 'last-modified']]
186 * A list of headers that will be used to determine whether the responses
187 * differ.
188 * @param {string} [options.channelName='workbox'] The name that will be used
189 *. when creating the `BroadcastChannel`, which defaults to 'workbox' (the
190 * channel name used by the `workbox-window` package).
191 * @param {string} [options.deferNoticationTimeout=10000] The amount of time
192 * to wait for a ready message from the window on navigation requests
193 * before sending the update.
194 */
195 constructor({
196 headersToCheck,
197 channelName,
198 deferNoticationTimeout
199 } = {}) {
200 this._headersToCheck = headersToCheck || DEFAULT_HEADERS_TO_CHECK;
201 this._channelName = channelName || DEFAULT_BROADCAST_CHANNEL_NAME;
202 this._deferNoticationTimeout = deferNoticationTimeout || DEFAULT_DEFER_NOTIFICATION_TIMEOUT;
203
204 {
205 assert_mjs.assert.isType(this._channelName, 'string', {
206 moduleName: 'workbox-broadcast-update',
207 className: 'BroadcastCacheUpdate',
208 funcName: 'constructor',
209 paramName: 'channelName'
210 });
211 assert_mjs.assert.isArray(this._headersToCheck, {
212 moduleName: 'workbox-broadcast-update',
213 className: 'BroadcastCacheUpdate',
214 funcName: 'constructor',
215 paramName: 'headersToCheck'
216 });
217 }
218
219 this._initWindowReadyDeferreds();
220 }
221 /**
222 * Compare two [Responses](https://developer.mozilla.org/en-US/docs/Web/API/Response)
223 * and send a message via the
224 * {@link https://developers.google.com/web/updates/2016/09/broadcastchannel|Broadcast Channel API}
225 * if they differ.
226 *
227 * Neither of the Responses can be {@link http://stackoverflow.com/questions/39109789|opaque}.
228 *
229 * @param {Object} options
230 * @param {Response} options.oldResponse Cached response to compare.
231 * @param {Response} options.newResponse Possibly updated response to compare.
232 * @param {string} options.url The URL of the request.
233 * @param {string} options.cacheName Name of the cache the responses belong
234 * to. This is included in the broadcast message.
235 * @param {Event} [options.event] event An optional event that triggered
236 * this possible cache update.
237 * @return {Promise} Resolves once the update is sent.
238 */
239
240
241 notifyIfUpdated({
242 oldResponse,
243 newResponse,
244 url,
245 cacheName,
246 event
247 }) {
248 if (!responsesAreSame(oldResponse, newResponse, this._headersToCheck)) {
249 {
250 logger_mjs.logger.log(`Newer response found (and cached) for:`, url);
251 }
252
253 const sendUpdate = async () => {
254 // In the case of a navigation request, the requesting page will likely
255 // not have loaded its JavaScript in time to recevied the update
256 // notification, so we defer it until ready (or we timeout waiting).
257 if (event && event.request && event.request.mode === 'navigate') {
258 {
259 logger_mjs.logger.debug(`Original request was a navigation request, ` + `waiting for a ready message from the window`, event.request);
260 }
261
262 await this._windowReadyOrTimeout(event);
263 }
264
265 await this._broadcastUpdate({
266 channel: this._getChannel(),
267 cacheName,
268 url
269 });
270 }; // Send the update and ensure the SW stays alive until it's sent.
271
272
273 const done = sendUpdate();
274
275 if (event) {
276 try {
277 event.waitUntil(done);
278 } catch (error) {
279 {
280 logger_mjs.logger.warn(`Unable to ensure service worker stays alive ` + `when broadcasting cache update for ` + `${getFriendlyURL_mjs.getFriendlyURL(event.request.url)}'.`);
281 }
282 }
283 }
284
285 return done;
286 }
287 }
288 /**
289 * NOTE: this is exposed on the instance primarily so it can be spied on
290 * in tests.
291 *
292 * @param {Object} opts
293 * @private
294 */
295
296
297 async _broadcastUpdate(opts) {
298 await broadcastUpdate(opts);
299 }
300 /**
301 * @return {BroadcastChannel|undefined} The BroadcastChannel instance used for
302 * broadcasting updates, or undefined if the browser doesn't support the
303 * Broadcast Channel API.
304 *
305 * @private
306 */
307
308
309 _getChannel() {
310 if ('BroadcastChannel' in self && !this._channel) {
311 this._channel = new BroadcastChannel(this._channelName);
312 }
313
314 return this._channel;
315 }
316 /**
317 * Waits for a message from the window indicating that it's capable of
318 * receiving broadcasts. By default, this will only wait for the amount of
319 * time specified via the `deferNoticationTimeout` option.
320 *
321 * @param {Event} event The navigation fetch event.
322 * @return {Promise}
323 * @private
324 */
325
326
327 _windowReadyOrTimeout(event) {
328 if (!this._navigationEventsDeferreds.has(event)) {
329 const deferred = new Deferred_mjs.Deferred(); // Set the deferred on the `_navigationEventsDeferreds` map so it will
330 // be resolved when the next ready message event comes.
331
332 this._navigationEventsDeferreds.set(event, deferred); // But don't wait too long for the message since it may never come.
333
334
335 const timeout = setTimeout(() => {
336 {
337 logger_mjs.logger.debug(`Timed out after ${this._deferNoticationTimeout}` + `ms waiting for message from window`);
338 }
339
340 deferred.resolve();
341 }, this._deferNoticationTimeout); // Ensure the timeout is cleared if the deferred promise is resolved.
342
343 deferred.promise.then(() => clearTimeout(timeout));
344 }
345
346 return this._navigationEventsDeferreds.get(event).promise;
347 }
348 /**
349 * Creates a mapping between navigation fetch events and deferreds, and adds
350 * a listener for message events from the window. When message events arrive,
351 * all deferreds in the mapping are resolved.
352 *
353 * Note: it would be easier if we could only resolve the deferred of
354 * navigation fetch event whose client ID matched the source ID of the
355 * message event, but currently client IDs are not exposed on navigation
356 * fetch events: https://www.chromestatus.com/feature/4846038800138240
357 *
358 * @private
359 */
360
361
362 _initWindowReadyDeferreds() {
363 // A mapping between navigation events and their deferreds.
364 this._navigationEventsDeferreds = new Map(); // The message listener needs to be added in the initial run of the
365 // service worker, but since we don't actually need to be listening for
366 // messages until the cache updates, we only invoke the callback if set.
367
368 self.addEventListener('message', event => {
369 if (event.data.type === 'WINDOW_READY' && event.data.meta === 'workbox-window' && this._navigationEventsDeferreds.size > 0) {
370 {
371 logger_mjs.logger.debug(`Received WINDOW_READY event: `, event);
372 } // Resolve any pending deferreds.
373
374
375 for (const deferred of this._navigationEventsDeferreds.values()) {
376 deferred.resolve();
377 }
378
379 this._navigationEventsDeferreds.clear();
380 }
381 });
382 }
383
384 }
385
386 /*
387 Copyright 2018 Google LLC
388
389 Use of this source code is governed by an MIT-style
390 license that can be found in the LICENSE file or at
391 https://opensource.org/licenses/MIT.
392 */
393 /**
394 * This plugin will automatically broadcast a message whenever a cached response
395 * is updated.
396 *
397 * @memberof workbox.broadcastUpdate
398 */
399
400 class Plugin {
401 /**
402 * Construct a BroadcastCacheUpdate instance with the passed options and
403 * calls its `notifyIfUpdated()` method whenever the plugin's
404 * `cacheDidUpdate` callback is invoked.
405 *
406 * @param {Object} options
407 * @param {Array<string>}
408 * [options.headersToCheck=['content-length', 'etag', 'last-modified']]
409 * A list of headers that will be used to determine whether the responses
410 * differ.
411 * @param {string} [options.channelName='workbox'] The name that will be used
412 *. when creating the `BroadcastChannel`, which defaults to 'workbox' (the
413 * channel name used by the `workbox-window` package).
414 * @param {string} [options.deferNoticationTimeout=10000] The amount of time
415 * to wait for a ready message from the window on navigation requests
416 * before sending the update.
417 */
418 constructor(options) {
419 this._broadcastUpdate = new BroadcastCacheUpdate(options);
420 }
421 /**
422 * A "lifecycle" callback that will be triggered automatically by the
423 * `workbox-sw` and `workbox-runtime-caching` handlers when an entry is
424 * added to a cache.
425 *
426 * @private
427 * @param {Object} options The input object to this function.
428 * @param {string} options.cacheName Name of the cache being updated.
429 * @param {Response} [options.oldResponse] The previous cached value, if any.
430 * @param {Response} options.newResponse The new value in the cache.
431 * @param {Request} options.request The request that triggered the udpate.
432 * @param {Request} [options.event] The event that triggered the update.
433 */
434
435
436 cacheDidUpdate({
437 cacheName,
438 oldResponse,
439 newResponse,
440 request,
441 event
442 }) {
443 {
444 assert_mjs.assert.isType(cacheName, 'string', {
445 moduleName: 'workbox-broadcast-update',
446 className: 'Plugin',
447 funcName: 'cacheDidUpdate',
448 paramName: 'cacheName'
449 });
450 assert_mjs.assert.isInstance(newResponse, Response, {
451 moduleName: 'workbox-broadcast-update',
452 className: 'Plugin',
453 funcName: 'cacheDidUpdate',
454 paramName: 'newResponse'
455 });
456 assert_mjs.assert.isInstance(request, Request, {
457 moduleName: 'workbox-broadcast-update',
458 className: 'Plugin',
459 funcName: 'cacheDidUpdate',
460 paramName: 'request'
461 });
462 }
463
464 if (!oldResponse) {
465 // Without a two responses there is nothing to compare.
466 return;
467 }
468
469 this._broadcastUpdate.notifyIfUpdated({
470 cacheName,
471 oldResponse,
472 newResponse,
473 event,
474 url: request.url
475 });
476 }
477
478 }
479
480 /*
481 Copyright 2018 Google LLC
482
483 Use of this source code is governed by an MIT-style
484 license that can be found in the LICENSE file or at
485 https://opensource.org/licenses/MIT.
486 */
487
488 exports.BroadcastCacheUpdate = BroadcastCacheUpdate;
489 exports.Plugin = Plugin;
490 exports.broadcastUpdate = broadcastUpdate;
491 exports.responsesAreSame = responsesAreSame;
492
493 return exports;
494
495}({}, workbox.core._private, workbox.core._private, workbox.core._private, workbox.core._private, workbox.core._private));
496