ExpirationPlugin.d.ts 5.0 KB

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