StaleWhileRevalidate.d.ts 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import { RouteHandlerObject, RouteHandlerCallbackOptions, WorkboxPlugin } from 'workbox-core/types.js';
  2. import './_version.js';
  3. interface StaleWhileRevalidateOptions {
  4. cacheName?: string;
  5. plugins?: WorkboxPlugin[];
  6. fetchOptions?: RequestInit;
  7. matchOptions?: CacheQueryOptions;
  8. }
  9. /**
  10. * An implementation of a
  11. * [stale-while-revalidate]{@link https://developers.google.com/web/fundamentals/instant-and-offline/offline-cookbook/#stale-while-revalidate}
  12. * request strategy.
  13. *
  14. * Resources are requested from both the cache and the network in parallel.
  15. * The strategy will respond with the cached version if available, otherwise
  16. * wait for the network response. The cache is updated with the network response
  17. * with each successful request.
  18. *
  19. * By default, this strategy will cache responses with a 200 status code as
  20. * well as [opaque responses]{@link https://developers.google.com/web/tools/workbox/guides/handle-third-party-requests}.
  21. * Opaque responses are cross-origin requests where the response doesn't
  22. * support [CORS]{@link https://enable-cors.org/}.
  23. *
  24. * If the network request fails, and there is no cache match, this will throw
  25. * a `WorkboxError` exception.
  26. *
  27. * @memberof module:workbox-strategies
  28. */
  29. declare class StaleWhileRevalidate implements RouteHandlerObject {
  30. private readonly _cacheName;
  31. private readonly _plugins;
  32. private readonly _fetchOptions?;
  33. private readonly _matchOptions?;
  34. /**
  35. * @param {Object} options
  36. * @param {string} options.cacheName Cache name to store and retrieve
  37. * requests. Defaults to cache names provided by
  38. * [workbox-core]{@link module:workbox-core.cacheNames}.
  39. * @param {Array<Object>} options.plugins [Plugins]{@link https://developers.google.com/web/tools/workbox/guides/using-plugins}
  40. * to use in conjunction with this caching strategy.
  41. * @param {Object} options.fetchOptions Values passed along to the
  42. * [`init`](https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch#Parameters)
  43. * of all fetch() requests made by this strategy.
  44. * @param {Object} options.matchOptions [`CacheQueryOptions`](https://w3c.github.io/ServiceWorker/#dictdef-cachequeryoptions)
  45. */
  46. constructor(options?: StaleWhileRevalidateOptions);
  47. /**
  48. * This method will perform a request strategy and follows an API that
  49. * will work with the
  50. * [Workbox Router]{@link module:workbox-routing.Router}.
  51. *
  52. * @param {Object} options
  53. * @param {Request|string} options.request A request to run this strategy for.
  54. * @param {Event} [options.event] The event that triggered the request.
  55. * @return {Promise<Response>}
  56. */
  57. handle({ event, request }: RouteHandlerCallbackOptions): Promise<Response>;
  58. /**
  59. * @param {Object} options
  60. * @param {Request} options.request
  61. * @param {Event} [options.event]
  62. * @return {Promise<Response>}
  63. *
  64. * @private
  65. */
  66. _getFromNetwork({ request, event }: {
  67. request: Request;
  68. event?: ExtendableEvent;
  69. }): Promise<Response>;
  70. }
  71. export { StaleWhileRevalidate };