ExpirationPlugin.d.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import { WorkboxPlugin } from 'workbox-core/types.js';
  2. import './_version.js';
  3. /**
  4. * This plugin can be used in the Workbox APIs to regularly enforce a
  5. * limit on the age and / or the number of cached requests.
  6. *
  7. * Whenever a cached request is used or updated, this plugin will look
  8. * at the used Cache and remove any old or extra requests.
  9. *
  10. * When using `maxAgeSeconds`, requests may be used *once* after expiring
  11. * because the expiration clean up will not have occurred until *after* the
  12. * cached request has been used. If the request has a "Date" header, then
  13. * a light weight expiration check is performed and the request will not be
  14. * used immediately.
  15. *
  16. * When using `maxEntries`, the entry least-recently requested will be removed
  17. * from the cache first.
  18. *
  19. * @memberof module:workbox-expiration
  20. */
  21. declare class ExpirationPlugin implements WorkboxPlugin {
  22. private readonly _config;
  23. private readonly _maxAgeSeconds?;
  24. private _cacheExpirations;
  25. /**
  26. * @param {Object} config
  27. * @param {number} [config.maxEntries] The maximum number of entries to cache.
  28. * Entries used the least will be removed as the maximum is reached.
  29. * @param {number} [config.maxAgeSeconds] The maximum age of an entry before
  30. * it's treated as stale and removed.
  31. * @param {boolean} [config.purgeOnQuotaError] Whether to opt this cache in to
  32. * automatic deletion if the available storage quota has been exceeded.
  33. */
  34. constructor(config?: {
  35. maxEntries?: number;
  36. maxAgeSeconds?: number;
  37. purgeOnQuotaError?: boolean;
  38. });
  39. /**
  40. * A simple helper method to return a CacheExpiration instance for a given
  41. * cache name.
  42. *
  43. * @param {string} cacheName
  44. * @return {CacheExpiration}
  45. *
  46. * @private
  47. */
  48. private _getCacheExpiration;
  49. /**
  50. * A "lifecycle" callback that will be triggered automatically by the
  51. * `workbox-strategies` handlers when a `Response` is about to be returned
  52. * from a [Cache](https://developer.mozilla.org/en-US/docs/Web/API/Cache) to
  53. * the handler. It allows the `Response` to be inspected for freshness and
  54. * prevents it from being used if the `Response`'s `Date` header value is
  55. * older than the configured `maxAgeSeconds`.
  56. *
  57. * @param {Object} options
  58. * @param {string} options.cacheName Name of the cache the response is in.
  59. * @param {Response} options.cachedResponse The `Response` object that's been
  60. * read from a cache and whose freshness should be checked.
  61. * @return {Response} Either the `cachedResponse`, if it's
  62. * fresh, or `null` if the `Response` is older than `maxAgeSeconds`.
  63. *
  64. * @private
  65. */
  66. cachedResponseWillBeUsed: WorkboxPlugin['cachedResponseWillBeUsed'];
  67. /**
  68. * @param {Response} cachedResponse
  69. * @return {boolean}
  70. *
  71. * @private
  72. */
  73. private _isResponseDateFresh;
  74. /**
  75. * This method will extract the data header and parse it into a useful
  76. * value.
  77. *
  78. * @param {Response} cachedResponse
  79. * @return {number|null}
  80. *
  81. * @private
  82. */
  83. private _getDateHeaderTimestamp;
  84. /**
  85. * A "lifecycle" callback that will be triggered automatically by the
  86. * `workbox-strategies` handlers when an entry is added to a cache.
  87. *
  88. * @param {Object} options
  89. * @param {string} options.cacheName Name of the cache that was updated.
  90. * @param {string} options.request The Request for the cached entry.
  91. *
  92. * @private
  93. */
  94. cacheDidUpdate: WorkboxPlugin['cacheDidUpdate'];
  95. /**
  96. * This is a helper method that performs two operations:
  97. *
  98. * - Deletes *all* the underlying Cache instances associated with this plugin
  99. * instance, by calling caches.delete() on your behalf.
  100. * - Deletes the metadata from IndexedDB used to keep track of expiration
  101. * details for each Cache instance.
  102. *
  103. * When using cache expiration, calling this method is preferable to calling
  104. * `caches.delete()` directly, since this will ensure that the IndexedDB
  105. * metadata is also cleanly removed and open IndexedDB instances are deleted.
  106. *
  107. * Note that if you're *not* using cache expiration for a given cache, calling
  108. * `caches.delete()` and passing in the cache's name should be sufficient.
  109. * There is no Workbox-specific method needed for cleanup in that case.
  110. */
  111. deleteCacheAndMetadata(): Promise<void>;
  112. }
  113. export { ExpirationPlugin };