BroadcastCacheUpdate.d.ts 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. }
  7. /**
  8. * Uses the `postMessage()` API to inform any open windows/tabs when a cached
  9. * response has been updated.
  10. *
  11. * For efficiency's sake, the underlying response bodies are not compared;
  12. * only specific response headers are checked.
  13. *
  14. * @memberof module:workbox-broadcast-update
  15. */
  16. declare class BroadcastCacheUpdate {
  17. private readonly _headersToCheck;
  18. private readonly _generatePayload;
  19. /**
  20. * Construct a BroadcastCacheUpdate instance with a specific `channelName` to
  21. * broadcast messages on
  22. *
  23. * @param {Object} options
  24. * @param {Array<string>} [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.generatePayload] A function whose return value
  28. * will be used as the `payload` field in any cache update messages sent
  29. * to the window clients.
  30. */
  31. constructor({ headersToCheck, generatePayload, }?: BroadcastCacheUpdateOptions);
  32. /**
  33. * Compares two [Responses](https://developer.mozilla.org/en-US/docs/Web/API/Response)
  34. * and sends a message (via `postMessage()`) to all window clients if the
  35. * responses differ (note: neither of the Responses can be
  36. * {@link http://stackoverflow.com/questions/39109789|opaque}).
  37. *
  38. * The message that's posted has the following format (where `payload` can
  39. * be customized via the `generatePayload` option the instance is created
  40. * with):
  41. *
  42. * ```
  43. * {
  44. * type: 'CACHE_UPDATED',
  45. * meta: 'workbox-broadcast-update',
  46. * payload: {
  47. * cacheName: 'the-cache-name',
  48. * updatedURL: 'https://example.com/'
  49. * }
  50. * }
  51. * ```
  52. *
  53. * @param {Object} options
  54. * @param {Response} [options.oldResponse] Cached response to compare.
  55. * @param {Response} options.newResponse Possibly updated response to compare.
  56. * @param {Request} options.request The request.
  57. * @param {string} options.cacheName Name of the cache the responses belong
  58. * to. This is included in the broadcast message.
  59. * @param {Event} [options.event] event An optional event that triggered
  60. * this possible cache update.
  61. * @return {Promise} Resolves once the update is sent.
  62. */
  63. notifyIfUpdated(options: CacheDidUpdateCallbackParam): Promise<void>;
  64. }
  65. export { BroadcastCacheUpdate };