BroadcastCacheUpdate.d.ts 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import { CacheDidUpdateCallbackParam } from 'workbox-core/types.js';
  2. import './_version.js';
  3. export interface BroadcastCacheUpdateOptions {
  4. headersToCheck?: string[];
  5. generatePayload?: (options: CacheDidUpdateCallbackParam) => Record<string, any>;
  6. notifyAllClients?: boolean;
  7. }
  8. /**
  9. * Uses the `postMessage()` API to inform any open windows/tabs when a cached
  10. * response has been updated.
  11. *
  12. * For efficiency's sake, the underlying response bodies are not compared;
  13. * only specific response headers are checked.
  14. *
  15. * @memberof workbox-broadcast-update
  16. */
  17. declare class BroadcastCacheUpdate {
  18. private readonly _headersToCheck;
  19. private readonly _generatePayload;
  20. private readonly _notifyAllClients;
  21. /**
  22. * Construct a BroadcastCacheUpdate instance with a specific `channelName` to
  23. * broadcast messages on
  24. *
  25. * @param {Object} [options]
  26. * @param {Array<string>} [options.headersToCheck=['content-length', 'etag', 'last-modified']]
  27. * A list of headers that will be used to determine whether the responses
  28. * differ.
  29. * @param {string} [options.generatePayload] A function whose return value
  30. * will be used as the `payload` field in any cache update messages sent
  31. * to the window clients.
  32. * @param {boolean} [options.notifyAllClients=true] If true (the default) then
  33. * all open clients will receive a message. If false, then only the client
  34. * that make the original request will be notified of the update.
  35. */
  36. constructor({ generatePayload, headersToCheck, notifyAllClients, }?: BroadcastCacheUpdateOptions);
  37. /**
  38. * Compares two [Responses](https://developer.mozilla.org/en-US/docs/Web/API/Response)
  39. * and sends a message (via `postMessage()`) to all window clients if the
  40. * responses differ. Neither of the Responses can be
  41. * [opaque](https://developer.chrome.com/docs/workbox/caching-resources-during-runtime/#opaque-responses).
  42. *
  43. * The message that's posted has the following format (where `payload` can
  44. * be customized via the `generatePayload` option the instance is created
  45. * with):
  46. *
  47. * ```
  48. * {
  49. * type: 'CACHE_UPDATED',
  50. * meta: 'workbox-broadcast-update',
  51. * payload: {
  52. * cacheName: 'the-cache-name',
  53. * updatedURL: 'https://example.com/'
  54. * }
  55. * }
  56. * ```
  57. *
  58. * @param {Object} options
  59. * @param {Response} [options.oldResponse] Cached response to compare.
  60. * @param {Response} options.newResponse Possibly updated response to compare.
  61. * @param {Request} options.request The request.
  62. * @param {string} options.cacheName Name of the cache the responses belong
  63. * to. This is included in the broadcast message.
  64. * @param {Event} options.event event The event that triggered
  65. * this possible cache update.
  66. * @return {Promise} Resolves once the update is sent.
  67. */
  68. notifyIfUpdated(options: CacheDidUpdateCallbackParam): Promise<void>;
  69. }
  70. export { BroadcastCacheUpdate };