| [9af201e] | 1 | /*
|
|---|
| 2 | Copyright 2018 Google LLC
|
|---|
| 3 |
|
|---|
| 4 | Use of this source code is governed by an MIT-style
|
|---|
| 5 | license that can be found in the LICENSE file or at
|
|---|
| 6 | https://opensource.org/licenses/MIT.
|
|---|
| 7 | */
|
|---|
| 8 |
|
|---|
| 9 | import {dontWaitFor} from 'workbox-core/_private/dontWaitFor.js';
|
|---|
| 10 | import {WorkboxPlugin} from 'workbox-core/types.js';
|
|---|
| 11 |
|
|---|
| 12 | import {
|
|---|
| 13 | BroadcastCacheUpdate,
|
|---|
| 14 | BroadcastCacheUpdateOptions,
|
|---|
| 15 | } from './BroadcastCacheUpdate.js';
|
|---|
| 16 |
|
|---|
| 17 | import './_version.js';
|
|---|
| 18 |
|
|---|
| 19 | /**
|
|---|
| 20 | * This plugin will automatically broadcast a message whenever a cached response
|
|---|
| 21 | * is updated.
|
|---|
| 22 | *
|
|---|
| 23 | * @memberof workbox-broadcast-update
|
|---|
| 24 | */
|
|---|
| 25 | class BroadcastUpdatePlugin implements WorkboxPlugin {
|
|---|
| 26 | private readonly _broadcastUpdate: BroadcastCacheUpdate;
|
|---|
| 27 |
|
|---|
| 28 | /**
|
|---|
| 29 | * Construct a {@link workbox-broadcast-update.BroadcastUpdate} instance with
|
|---|
| 30 | * the passed options and calls its `notifyIfUpdated` method whenever the
|
|---|
| 31 | * plugin's `cacheDidUpdate` callback is invoked.
|
|---|
| 32 | *
|
|---|
| 33 | * @param {Object} [options]
|
|---|
| 34 | * @param {Array<string>} [options.headersToCheck=['content-length', 'etag', 'last-modified']]
|
|---|
| 35 | * A list of headers that will be used to determine whether the responses
|
|---|
| 36 | * differ.
|
|---|
| 37 | * @param {string} [options.generatePayload] A function whose return value
|
|---|
| 38 | * will be used as the `payload` field in any cache update messages sent
|
|---|
| 39 | * to the window clients.
|
|---|
| 40 | */
|
|---|
| 41 | constructor(options?: BroadcastCacheUpdateOptions) {
|
|---|
| 42 | this._broadcastUpdate = new BroadcastCacheUpdate(options);
|
|---|
| 43 | }
|
|---|
| 44 |
|
|---|
| 45 | /**
|
|---|
| 46 | * A "lifecycle" callback that will be triggered automatically by the
|
|---|
| 47 | * `workbox-sw` and `workbox-runtime-caching` handlers when an entry is
|
|---|
| 48 | * added to a cache.
|
|---|
| 49 | *
|
|---|
| 50 | * @private
|
|---|
| 51 | * @param {Object} options The input object to this function.
|
|---|
| 52 | * @param {string} options.cacheName Name of the cache being updated.
|
|---|
| 53 | * @param {Response} [options.oldResponse] The previous cached value, if any.
|
|---|
| 54 | * @param {Response} options.newResponse The new value in the cache.
|
|---|
| 55 | * @param {Request} options.request The request that triggered the update.
|
|---|
| 56 | * @param {Request} options.event The event that triggered the update.
|
|---|
| 57 | */
|
|---|
| 58 | cacheDidUpdate: WorkboxPlugin['cacheDidUpdate'] = async (options) => {
|
|---|
| 59 | dontWaitFor(this._broadcastUpdate.notifyIfUpdated(options));
|
|---|
| 60 | };
|
|---|
| 61 | }
|
|---|
| 62 |
|
|---|
| 63 | export {BroadcastUpdatePlugin};
|
|---|