broadcastUpdate.mjs 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 {CACHE_UPDATED_MESSAGE_TYPE, CACHE_UPDATED_MESSAGE_META}
  9. from './utils/constants.mjs';
  10. import './_version.mjs';
  11. /**
  12. * You would not normally call this method directly; it's called automatically
  13. * by an instance of the {@link BroadcastCacheUpdate} class. It's exposed here
  14. * for the benefit of developers who would rather not use the full
  15. * `BroadcastCacheUpdate` implementation.
  16. *
  17. * Calling this will dispatch a message on the provided
  18. * {@link https://developers.google.com/web/updates/2016/09/broadcastchannel|Broadcast Channel}
  19. * to notify interested subscribers about a change to a cached resource.
  20. *
  21. * The message that's posted has a formation inspired by the
  22. * [Flux standard action](https://github.com/acdlite/flux-standard-action#introduction)
  23. * format like so:
  24. *
  25. * ```
  26. * {
  27. * type: 'CACHE_UPDATED',
  28. * meta: 'workbox-broadcast-update',
  29. * payload: {
  30. * cacheName: 'the-cache-name',
  31. * updatedURL: 'https://example.com/'
  32. * }
  33. * }
  34. * ```
  35. *
  36. * (Usage of [Flux](https://facebook.github.io/flux/) itself is not at
  37. * all required.)
  38. *
  39. * @param {Object} options
  40. * @param {string} options.cacheName The name of the cache in which the updated
  41. * `Response` was stored.
  42. * @param {string} options.url The URL associated with the updated `Response`.
  43. * @param {BroadcastChannel} [options.channel] The `BroadcastChannel` to use.
  44. * If no channel is set or the browser doesn't support the BroadcastChannel
  45. * api, then an attempt will be made to `postMessage` each window client.
  46. *
  47. * @memberof workbox.broadcastUpdate
  48. */
  49. const broadcastUpdate = async ({channel, cacheName, url}) => {
  50. if (process.env.NODE_ENV !== 'production') {
  51. assert.isType(cacheName, 'string', {
  52. moduleName: 'workbox-broadcast-update',
  53. className: '~',
  54. funcName: 'broadcastUpdate',
  55. paramName: 'cacheName',
  56. });
  57. assert.isType(url, 'string', {
  58. moduleName: 'workbox-broadcast-update',
  59. className: '~',
  60. funcName: 'broadcastUpdate',
  61. paramName: 'url',
  62. });
  63. }
  64. const data = {
  65. type: CACHE_UPDATED_MESSAGE_TYPE,
  66. meta: CACHE_UPDATED_MESSAGE_META,
  67. payload: {
  68. cacheName: cacheName,
  69. updatedURL: url,
  70. },
  71. };
  72. if (channel) {
  73. channel.postMessage(data);
  74. } else {
  75. const windows = await clients.matchAll({type: 'window'});
  76. for (const win of windows) {
  77. win.postMessage(data);
  78. }
  79. }
  80. };
  81. export {broadcastUpdate};