Plugin.mjs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 {assert} from 'workbox-core/_private/assert.mjs';
  8. import {BroadcastCacheUpdate} from './BroadcastCacheUpdate.mjs';
  9. import './_version.mjs';
  10. /**
  11. * This plugin will automatically broadcast a message whenever a cached response
  12. * is updated.
  13. *
  14. * @memberof workbox.broadcastUpdate
  15. */
  16. class Plugin {
  17. /**
  18. * Construct a BroadcastCacheUpdate instance with the passed options and
  19. * calls its `notifyIfUpdated()` method whenever the plugin's
  20. * `cacheDidUpdate` callback is invoked.
  21. *
  22. * @param {Object} options
  23. * @param {Array<string>}
  24. * [options.headersToCheck=['content-length', 'etag', 'last-modified']]
  25. * A list of headers that will be used to determine whether the responses
  26. * differ.
  27. * @param {string} [options.channelName='workbox'] The name that will be used
  28. *. when creating the `BroadcastChannel`, which defaults to 'workbox' (the
  29. * channel name used by the `workbox-window` package).
  30. * @param {string} [options.deferNoticationTimeout=10000] The amount of time
  31. * to wait for a ready message from the window on navigation requests
  32. * before sending the update.
  33. */
  34. constructor(options) {
  35. this._broadcastUpdate = new BroadcastCacheUpdate(options);
  36. }
  37. /**
  38. * A "lifecycle" callback that will be triggered automatically by the
  39. * `workbox-sw` and `workbox-runtime-caching` handlers when an entry is
  40. * added to a cache.
  41. *
  42. * @private
  43. * @param {Object} options The input object to this function.
  44. * @param {string} options.cacheName Name of the cache being updated.
  45. * @param {Response} [options.oldResponse] The previous cached value, if any.
  46. * @param {Response} options.newResponse The new value in the cache.
  47. * @param {Request} options.request The request that triggered the udpate.
  48. * @param {Request} [options.event] The event that triggered the update.
  49. */
  50. cacheDidUpdate({cacheName, oldResponse, newResponse, request, event}) {
  51. if (process.env.NODE_ENV !== 'production') {
  52. assert.isType(cacheName, 'string', {
  53. moduleName: 'workbox-broadcast-update',
  54. className: 'Plugin',
  55. funcName: 'cacheDidUpdate',
  56. paramName: 'cacheName',
  57. });
  58. assert.isInstance(newResponse, Response, {
  59. moduleName: 'workbox-broadcast-update',
  60. className: 'Plugin',
  61. funcName: 'cacheDidUpdate',
  62. paramName: 'newResponse',
  63. });
  64. assert.isInstance(request, Request, {
  65. moduleName: 'workbox-broadcast-update',
  66. className: 'Plugin',
  67. funcName: 'cacheDidUpdate',
  68. paramName: 'request',
  69. });
  70. }
  71. if (!oldResponse) {
  72. // Without a two responses there is nothing to compare.
  73. return;
  74. }
  75. this._broadcastUpdate.notifyIfUpdated({
  76. cacheName,
  77. oldResponse,
  78. newResponse,
  79. event,
  80. url: request.url,
  81. });
  82. }
  83. }
  84. export {Plugin};