Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

broadcastUpdate.mjs 3.5 KiB

il y a 3 ans
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. Copyright 2016 Google Inc. All Rights Reserved.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. import {assert} from 'workbox-core/_private/assert.mjs';
  14. import {logger} from 'workbox-core/_private/logger.mjs';
  15. import messageTypes from './messageTypes.mjs';
  16. import './_version.mjs';
  17. /**
  18. * You would not normally call this method directly; it's called automatically
  19. * by an instance of the {@link BroadcastCacheUpdate} class. It's exposed here
  20. * for the benefit of developers who would rather not use the full
  21. * `BroadcastCacheUpdate` implementation.
  22. *
  23. * Calling this will dispatch a message on the provided
  24. * {@link https://developers.google.com/web/updates/2016/09/broadcastchannel|Broadcast Channel}
  25. * to notify interested subscribers about a change to a cached resource.
  26. *
  27. * The message that's posted has a formation inspired by the
  28. * [Flux standard action](https://github.com/acdlite/flux-standard-action#introduction)
  29. * format like so:
  30. *
  31. * ```
  32. * {
  33. * type: 'CACHE_UPDATED',
  34. * meta: 'workbox-broadcast-cache-update',
  35. * payload: {
  36. * cacheName: 'the-cache-name',
  37. * updatedUrl: 'https://example.com/'
  38. * }
  39. * }
  40. * ```
  41. *
  42. * (Usage of [Flux](https://facebook.github.io/flux/) itself is not at
  43. * all required.)
  44. *
  45. * @param {BroadcastChannel} channel The `BroadcastChannel` to use.
  46. * @param {string} cacheName The name of the cache in which the updated
  47. * `Response` was stored.
  48. * @param {string} url The URL associated with the updated `Response`.
  49. * @param {string} source A string identifying this library as the source
  50. * of the update message.
  51. *
  52. * @memberof workbox.broadcastUpdate
  53. */
  54. const broadcastUpdate = (channel, cacheName, url, source) => {
  55. // There are browsers which support service workers but don't support the
  56. // Broadcast Channel API.
  57. // See https://github.com/GoogleChrome/workbox/issues/1304
  58. if (!(('BroadcastChannel' in self) && channel)) {
  59. if (process.env.NODE_ENV !== 'production') {
  60. logger.debug(`${url} was updated, but the Broadcast Channel API is not ` +
  61. `available in the current browser.`);
  62. }
  63. return;
  64. }
  65. if (process.env.NODE_ENV !== 'production') {
  66. assert.isInstance(channel, BroadcastChannel, {
  67. moduleName: 'workbox-broadcast-cache-update',
  68. className: '~',
  69. funcName: 'broadcastUpdate',
  70. paramName: 'channel',
  71. });
  72. assert.isType(cacheName, 'string', {
  73. moduleName: 'workbox-broadcast-cache-update',
  74. className: '~',
  75. funcName: 'broadcastUpdate',
  76. paramName: 'cacheName',
  77. });
  78. assert.isType(url, 'string', {
  79. moduleName: 'workbox-broadcast-cache-update',
  80. className: '~',
  81. funcName: 'broadcastUpdate',
  82. paramName: 'url',
  83. });
  84. assert.isType(source, 'string', {
  85. moduleName: 'workbox-broadcast-cache-update',
  86. className: '~',
  87. funcName: 'broadcastUpdate',
  88. paramName: 'source',
  89. });
  90. }
  91. channel.postMessage({
  92. type: messageTypes.CACHE_UPDATED,
  93. meta: source,
  94. payload: {
  95. cacheName: cacheName,
  96. updatedUrl: url,
  97. },
  98. });
  99. };
  100. export {broadcastUpdate};