You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

100 lines
3.6 KiB

  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 {WorkboxError} from 'workbox-core/_private/WorkboxError.mjs';
  14. import {responsesAreSame} from './utils/responsesAreSame.mjs';
  15. import {broadcastUpdate} from './broadcastUpdate.mjs';
  16. import './_version.mjs';
  17. /**
  18. * Uses the [Broadcast Channel API]{@link https://developers.google.com/web/updates/2016/09/broadcastchannel}
  19. * to notify interested parties when a cached response has been updated.
  20. *
  21. * For efficiency's sake, the underlying response bodies are not compared;
  22. * only specific response headers are checked.
  23. *
  24. * @memberof workbox.broadcastUpdate
  25. */
  26. class BroadcastCacheUpdate {
  27. /**
  28. * Construct a BroadcastCacheUpdate instance with a specific `channelName` to
  29. * broadcast messages on
  30. *
  31. * @param {string} channelName The name that will be used when creating
  32. * the `BroadcastChannel`.
  33. * @param {Object} options
  34. * @param {Array<string>}
  35. * [options.headersToCheck=['content-length', 'etag', 'last-modified']] A
  36. * list of headers that will be used to determine whether the responses
  37. * differ.
  38. * @param {string} [options.source='workbox-broadcast-cache-update'] An
  39. * attribution value that indicates where the update originated.
  40. */
  41. constructor(channelName, {headersToCheck, source} = {}) {
  42. if (process.env.NODE_ENV !== 'production') {
  43. if (typeof channelName !== 'string' || channelName.length === 0) {
  44. throw new WorkboxError('channel-name-required');
  45. }
  46. }
  47. this._channelName = channelName;
  48. this._headersToCheck = headersToCheck || [
  49. 'content-length',
  50. 'etag',
  51. 'last-modified',
  52. ];
  53. this._source = source || 'workbox-broadcast-cache-update';
  54. // TODO assert typeof headersToCheck instanceof Array
  55. }
  56. /**
  57. * @return {BroadcastChannel|undefined} The BroadcastChannel instance used for
  58. * broadcasting updates, or undefined if the browser doesn't support the
  59. * Broadcast Channel API.
  60. *
  61. * @private
  62. */
  63. _getChannel() {
  64. if (('BroadcastChannel' in self) && !this._channel) {
  65. this._channel = new BroadcastChannel(this._channelName);
  66. }
  67. return this._channel;
  68. }
  69. /**
  70. * Compare two [Responses](https://developer.mozilla.org/en-US/docs/Web/API/Response)
  71. * and send a message via the
  72. * {@link https://developers.google.com/web/updates/2016/09/broadcastchannel|Broadcast Channel API}
  73. * if they differ.
  74. *
  75. * Neither of the Responses can be {@link http://stackoverflow.com/questions/39109789|opaque}.
  76. *
  77. * @param {Response} firstResponse First responses to compare.
  78. * @param {Response} secondResponse Second responses to compare.
  79. * @param {string} url The URL of the updated request.
  80. * @param {string} cacheName Name of the cache the responses belong to.
  81. * This is included in the message posted on the broadcast channel.
  82. */
  83. notifyIfUpdated(firstResponse, secondResponse, url, cacheName) {
  84. if (!responsesAreSame(
  85. firstResponse, secondResponse, this._headersToCheck)) {
  86. broadcastUpdate(this._getChannel(), cacheName, url, this._source);
  87. }
  88. }
  89. }
  90. export {BroadcastCacheUpdate};