NetworkFirst.d.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import { RouteHandlerObject, RouteHandlerCallbackOptions, WorkboxPlugin } from 'workbox-core/types.js';
  2. import './_version.js';
  3. interface NetworkFirstOptions {
  4. cacheName?: string;
  5. plugins?: WorkboxPlugin[];
  6. fetchOptions?: RequestInit;
  7. matchOptions?: CacheQueryOptions;
  8. networkTimeoutSeconds?: number;
  9. }
  10. /**
  11. * An implementation of a
  12. * [network first]{@link https://developers.google.com/web/fundamentals/instant-and-offline/offline-cookbook/#network-falling-back-to-cache}
  13. * request strategy.
  14. *
  15. * By default, this strategy will cache responses with a 200 status code as
  16. * well as [opaque responses]{@link https://developers.google.com/web/tools/workbox/guides/handle-third-party-requests}.
  17. * Opaque responses are are cross-origin requests where the response doesn't
  18. * support [CORS]{@link https://enable-cors.org/}.
  19. *
  20. * If the network request fails, and there is no cache match, this will throw
  21. * a `WorkboxError` exception.
  22. *
  23. * @memberof module:workbox-strategies
  24. */
  25. declare class NetworkFirst implements RouteHandlerObject {
  26. private readonly _cacheName;
  27. private readonly _plugins;
  28. private readonly _fetchOptions?;
  29. private readonly _matchOptions?;
  30. private readonly _networkTimeoutSeconds;
  31. /**
  32. * @param {Object} options
  33. * @param {string} options.cacheName Cache name to store and retrieve
  34. * requests. Defaults to cache names provided by
  35. * [workbox-core]{@link module:workbox-core.cacheNames}.
  36. * @param {Array<Object>} options.plugins [Plugins]{@link https://developers.google.com/web/tools/workbox/guides/using-plugins}
  37. * to use in conjunction with this caching strategy.
  38. * @param {Object} options.fetchOptions Values passed along to the
  39. * [`init`](https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch#Parameters)
  40. * of all fetch() requests made by this strategy.
  41. * @param {Object} options.matchOptions [`CacheQueryOptions`](https://w3c.github.io/ServiceWorker/#dictdef-cachequeryoptions)
  42. * @param {number} options.networkTimeoutSeconds If set, any network requests
  43. * that fail to respond within the timeout will fallback to the cache.
  44. *
  45. * This option can be used to combat
  46. * "[lie-fi]{@link https://developers.google.com/web/fundamentals/performance/poor-connectivity/#lie-fi}"
  47. * scenarios.
  48. */
  49. constructor(options?: NetworkFirstOptions);
  50. /**
  51. * This method will perform a request strategy and follows an API that
  52. * will work with the
  53. * [Workbox Router]{@link module:workbox-routing.Router}.
  54. *
  55. * @param {Object} options
  56. * @param {Request|string} options.request A request to run this strategy for.
  57. * @param {Event} [options.event] The event that triggered the request.
  58. * @return {Promise<Response>}
  59. */
  60. handle({ event, request }: RouteHandlerCallbackOptions): Promise<Response>;
  61. /**
  62. * @param {Object} options
  63. * @param {Request} options.request
  64. * @param {Array} options.logs A reference to the logs array
  65. * @param {Event} [options.event]
  66. * @return {Promise<Response>}
  67. *
  68. * @private
  69. */
  70. private _getTimeoutPromise;
  71. /**
  72. * @param {Object} options
  73. * @param {number|undefined} options.timeoutId
  74. * @param {Request} options.request
  75. * @param {Array} options.logs A reference to the logs Array.
  76. * @param {Event} [options.event]
  77. * @return {Promise<Response>}
  78. *
  79. * @private
  80. */
  81. _getNetworkPromise({ timeoutId, request, logs, event }: {
  82. request: Request;
  83. logs: any[];
  84. timeoutId?: number;
  85. event?: ExtendableEvent;
  86. }): Promise<Response>;
  87. /**
  88. * Used if the network timeouts or fails to make the request.
  89. *
  90. * @param {Object} options
  91. * @param {Request} request The request to match in the cache
  92. * @param {Event} [options.event]
  93. * @return {Promise<Object>}
  94. *
  95. * @private
  96. */
  97. private _respondFromCache;
  98. }
  99. export { NetworkFirst };