BroadcastUpdatePlugin.js 2.2 KB

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