CacheFirst.d.ts 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import { RouteHandlerObject, RouteHandlerCallbackOptions, WorkboxPlugin } from 'workbox-core/types.js';
  2. import './_version.js';
  3. interface CacheFirstOptions {
  4. cacheName?: string;
  5. plugins?: WorkboxPlugin[];
  6. fetchOptions?: RequestInit;
  7. matchOptions?: CacheQueryOptions;
  8. }
  9. /**
  10. * An implementation of a [cache-first]{@link https://developers.google.com/web/fundamentals/instant-and-offline/offline-cookbook/#cache-falling-back-to-network}
  11. * request strategy.
  12. *
  13. * A cache first strategy is useful for assets that have been revisioned,
  14. * such as URLs like `/styles/example.a8f5f1.css`, since they
  15. * can be cached for long periods of time.
  16. *
  17. * If the network request fails, and there is no cache match, this will throw
  18. * a `WorkboxError` exception.
  19. *
  20. * @memberof module:workbox-strategies
  21. */
  22. declare class CacheFirst implements RouteHandlerObject {
  23. private readonly _cacheName;
  24. private readonly _plugins;
  25. private readonly _fetchOptions?;
  26. private readonly _matchOptions?;
  27. /**
  28. * @param {Object} options
  29. * @param {string} options.cacheName Cache name to store and retrieve
  30. * requests. Defaults to cache names provided by
  31. * [workbox-core]{@link module:workbox-core.cacheNames}.
  32. * @param {Array<Object>} options.plugins [Plugins]{@link https://developers.google.com/web/tools/workbox/guides/using-plugins}
  33. * to use in conjunction with this caching strategy.
  34. * @param {Object} options.fetchOptions Values passed along to the
  35. * [`init`](https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch#Parameters)
  36. * of all fetch() requests made by this strategy.
  37. * @param {Object} options.matchOptions [`CacheQueryOptions`](https://w3c.github.io/ServiceWorker/#dictdef-cachequeryoptions)
  38. */
  39. constructor(options?: CacheFirstOptions);
  40. /**
  41. * This method will perform a request strategy and follows an API that
  42. * will work with the
  43. * [Workbox Router]{@link module:workbox-routing.Router}.
  44. *
  45. * @param {Object} options
  46. * @param {Request|string} options.request A request to run this strategy for.
  47. * @param {Event} [options.event] The event that triggered the request.
  48. * @return {Promise<Response>}
  49. */
  50. handle({ event, request }: RouteHandlerCallbackOptions): Promise<Response>;
  51. /**
  52. * Handles the network and cache part of CacheFirst.
  53. *
  54. * @param {Request} request
  55. * @param {Event} [event]
  56. * @return {Promise<Response>}
  57. *
  58. * @private
  59. */
  60. _getFromNetwork(request: Request, event?: ExtendableEvent): Promise<any>;
  61. }
  62. export { CacheFirst };