PrecacheController.d.ts 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. import { RouteHandlerCallback } from 'workbox-core/types.js';
  2. import { WorkboxPlugin } from 'workbox-core/types.js';
  3. import { PrecacheEntry } from './_types.js';
  4. import './_version.js';
  5. /**
  6. * Performs efficient precaching of assets.
  7. *
  8. * @memberof module:workbox-precaching
  9. */
  10. declare class PrecacheController {
  11. private readonly _cacheName;
  12. private readonly _urlsToCacheKeys;
  13. private readonly _urlsToCacheModes;
  14. private readonly _cacheKeysToIntegrities;
  15. /**
  16. * Create a new PrecacheController.
  17. *
  18. * @param {string} [cacheName] An optional name for the cache, to override
  19. * the default precache name.
  20. */
  21. constructor(cacheName?: string);
  22. /**
  23. * This method will add items to the precache list, removing duplicates
  24. * and ensuring the information is valid.
  25. *
  26. * @param {
  27. * Array<module:workbox-precaching.PrecacheController.PrecacheEntry|string>
  28. * } entries Array of entries to precache.
  29. */
  30. addToCacheList(entries: Array<PrecacheEntry | string>): void;
  31. /**
  32. * Precaches new and updated assets. Call this method from the service worker
  33. * install event.
  34. *
  35. * @param {Object} options
  36. * @param {Event} [options.event] The install event (if needed).
  37. * @param {Array<Object>} [options.plugins] Plugins to be used for fetching
  38. * and caching during install.
  39. * @return {Promise<module:workbox-precaching.InstallResult>}
  40. */
  41. install({ event, plugins }?: {
  42. event?: ExtendableEvent;
  43. plugins?: WorkboxPlugin[];
  44. }): Promise<{
  45. updatedURLs: string[];
  46. notUpdatedURLs: string[];
  47. }>;
  48. /**
  49. * Deletes assets that are no longer present in the current precache manifest.
  50. * Call this method from the service worker activate event.
  51. *
  52. * @return {Promise<module:workbox-precaching.CleanupResult>}
  53. */
  54. activate(): Promise<{
  55. deletedURLs: string[];
  56. }>;
  57. /**
  58. * Requests the entry and saves it to the cache if the response is valid.
  59. * By default, any response with a status code of less than 400 (including
  60. * opaque responses) is considered valid.
  61. *
  62. * If you need to use custom criteria to determine what's valid and what
  63. * isn't, then pass in an item in `options.plugins` that implements the
  64. * `cacheWillUpdate()` lifecycle event.
  65. *
  66. * @private
  67. * @param {Object} options
  68. * @param {string} options.cacheKey The string to use a cache key.
  69. * @param {string} options.url The URL to fetch and cache.
  70. * @param {string} [options.cacheMode] The cache mode for the network request.
  71. * @param {Event} [options.event] The install event (if passed).
  72. * @param {Array<Object>} [options.plugins] An array of plugins to apply to
  73. * fetch and caching.
  74. * @param {string} [options.integrity] The value to use for the `integrity`
  75. * field when making the request.
  76. */
  77. _addURLToCache({ cacheKey, url, cacheMode, event, plugins, integrity }: {
  78. cacheKey: string;
  79. url: string;
  80. cacheMode: "reload" | "default" | "no-store" | "no-cache" | "force-cache" | "only-if-cached" | undefined;
  81. event?: ExtendableEvent;
  82. plugins?: WorkboxPlugin[];
  83. integrity?: string;
  84. }): Promise<void>;
  85. /**
  86. * Returns a mapping of a precached URL to the corresponding cache key, taking
  87. * into account the revision information for the URL.
  88. *
  89. * @return {Map<string, string>} A URL to cache key mapping.
  90. */
  91. getURLsToCacheKeys(): Map<string, string>;
  92. /**
  93. * Returns a list of all the URLs that have been precached by the current
  94. * service worker.
  95. *
  96. * @return {Array<string>} The precached URLs.
  97. */
  98. getCachedURLs(): string[];
  99. /**
  100. * Returns the cache key used for storing a given URL. If that URL is
  101. * unversioned, like `/index.html', then the cache key will be the original
  102. * URL with a search parameter appended to it.
  103. *
  104. * @param {string} url A URL whose cache key you want to look up.
  105. * @return {string} The versioned URL that corresponds to a cache key
  106. * for the original URL, or undefined if that URL isn't precached.
  107. */
  108. getCacheKeyForURL(url: string): string | undefined;
  109. /**
  110. * This acts as a drop-in replacement for [`cache.match()`](https://developer.mozilla.org/en-US/docs/Web/API/Cache/match)
  111. * with the following differences:
  112. *
  113. * - It knows what the name of the precache is, and only checks in that cache.
  114. * - It allows you to pass in an "original" URL without versioning parameters,
  115. * and it will automatically look up the correct cache key for the currently
  116. * active revision of that URL.
  117. *
  118. * E.g., `matchPrecache('index.html')` will find the correct precached
  119. * response for the currently active service worker, even if the actual cache
  120. * key is `'/index.html?__WB_REVISION__=1234abcd'`.
  121. *
  122. * @param {string|Request} request The key (without revisioning parameters)
  123. * to look up in the precache.
  124. * @return {Promise<Response|undefined>}
  125. */
  126. matchPrecache(request: string | Request): Promise<Response | undefined>;
  127. /**
  128. * Returns a function that can be used within a
  129. * {@link module:workbox-routing.Route} that will find a response for the
  130. * incoming request against the precache.
  131. *
  132. * If for an unexpected reason there is a cache miss for the request,
  133. * this will fall back to retrieving the `Response` via `fetch()` when
  134. * `fallbackToNetwork` is `true`.
  135. *
  136. * @param {boolean} [fallbackToNetwork=true] Whether to attempt to get the
  137. * response from the network if there's a precache miss.
  138. * @return {module:workbox-routing~handlerCallback}
  139. */
  140. createHandler(fallbackToNetwork?: boolean): RouteHandlerCallback;
  141. /**
  142. * Returns a function that looks up `url` in the precache (taking into
  143. * account revision information), and returns the corresponding `Response`.
  144. *
  145. * If for an unexpected reason there is a cache miss when looking up `url`,
  146. * this will fall back to retrieving the `Response` via `fetch()` when
  147. * `fallbackToNetwork` is `true`.
  148. *
  149. * @param {string} url The precached URL which will be used to lookup the
  150. * `Response`.
  151. * @param {boolean} [fallbackToNetwork=true] Whether to attempt to get the
  152. * response from the network if there's a precache miss.
  153. * @return {module:workbox-routing~handlerCallback}
  154. */
  155. createHandlerBoundToURL(url: string, fallbackToNetwork?: boolean): RouteHandlerCallback;
  156. }
  157. export { PrecacheController };