| 1 | import { Strategy, StrategyOptions } from './Strategy.js';
|
|---|
| 2 | import { StrategyHandler } from './StrategyHandler.js';
|
|---|
| 3 | import './_version.js';
|
|---|
| 4 | export interface NetworkFirstOptions extends StrategyOptions {
|
|---|
| 5 | networkTimeoutSeconds?: number;
|
|---|
| 6 | }
|
|---|
| 7 | /**
|
|---|
| 8 | * An implementation of a
|
|---|
| 9 | * [network first](https://developer.chrome.com/docs/workbox/caching-strategies-overview/#network-first-falling-back-to-cache)
|
|---|
| 10 | * request strategy.
|
|---|
| 11 | *
|
|---|
| 12 | * By default, this strategy will cache responses with a 200 status code as
|
|---|
| 13 | * well as [opaque responses](https://developer.chrome.com/docs/workbox/caching-resources-during-runtime/#opaque-responses).
|
|---|
| 14 | * Opaque responses are are cross-origin requests where the response doesn't
|
|---|
| 15 | * support [CORS](https://enable-cors.org/).
|
|---|
| 16 | *
|
|---|
| 17 | * If the network request fails, and there is no cache match, this will throw
|
|---|
| 18 | * a `WorkboxError` exception.
|
|---|
| 19 | *
|
|---|
| 20 | * @extends workbox-strategies.Strategy
|
|---|
| 21 | * @memberof workbox-strategies
|
|---|
| 22 | */
|
|---|
| 23 | declare class NetworkFirst extends Strategy {
|
|---|
| 24 | private readonly _networkTimeoutSeconds;
|
|---|
| 25 | /**
|
|---|
| 26 | * @param {Object} [options]
|
|---|
| 27 | * @param {string} [options.cacheName] Cache name to store and retrieve
|
|---|
| 28 | * requests. Defaults to cache names provided by
|
|---|
| 29 | * {@link workbox-core.cacheNames}.
|
|---|
| 30 | * @param {Array<Object>} [options.plugins] [Plugins]{@link https://developers.google.com/web/tools/workbox/guides/using-plugins}
|
|---|
| 31 | * to use in conjunction with this caching strategy.
|
|---|
| 32 | * @param {Object} [options.fetchOptions] Values passed along to the
|
|---|
| 33 | * [`init`](https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch#Parameters)
|
|---|
| 34 | * of [non-navigation](https://github.com/GoogleChrome/workbox/issues/1796)
|
|---|
| 35 | * `fetch()` requests made by this strategy.
|
|---|
| 36 | * @param {Object} [options.matchOptions] [`CacheQueryOptions`](https://w3c.github.io/ServiceWorker/#dictdef-cachequeryoptions)
|
|---|
| 37 | * @param {number} [options.networkTimeoutSeconds] If set, any network requests
|
|---|
| 38 | * that fail to respond within the timeout will fallback to the cache.
|
|---|
| 39 | *
|
|---|
| 40 | * This option can be used to combat
|
|---|
| 41 | * "[lie-fi]{@link https://developers.google.com/web/fundamentals/performance/poor-connectivity/#lie-fi}"
|
|---|
| 42 | * scenarios.
|
|---|
| 43 | */
|
|---|
| 44 | constructor(options?: NetworkFirstOptions);
|
|---|
| 45 | /**
|
|---|
| 46 | * @private
|
|---|
| 47 | * @param {Request|string} request A request to run this strategy for.
|
|---|
| 48 | * @param {workbox-strategies.StrategyHandler} handler The event that
|
|---|
| 49 | * triggered the request.
|
|---|
| 50 | * @return {Promise<Response>}
|
|---|
| 51 | */
|
|---|
| 52 | _handle(request: Request, handler: StrategyHandler): Promise<Response>;
|
|---|
| 53 | /**
|
|---|
| 54 | * @param {Object} options
|
|---|
| 55 | * @param {Request} options.request
|
|---|
| 56 | * @param {Array} options.logs A reference to the logs array
|
|---|
| 57 | * @param {Event} options.event
|
|---|
| 58 | * @return {Promise<Response>}
|
|---|
| 59 | *
|
|---|
| 60 | * @private
|
|---|
| 61 | */
|
|---|
| 62 | private _getTimeoutPromise;
|
|---|
| 63 | /**
|
|---|
| 64 | * @param {Object} options
|
|---|
| 65 | * @param {number|undefined} options.timeoutId
|
|---|
| 66 | * @param {Request} options.request
|
|---|
| 67 | * @param {Array} options.logs A reference to the logs Array.
|
|---|
| 68 | * @param {Event} options.event
|
|---|
| 69 | * @return {Promise<Response>}
|
|---|
| 70 | *
|
|---|
| 71 | * @private
|
|---|
| 72 | */
|
|---|
| 73 | _getNetworkPromise({ timeoutId, request, logs, handler, }: {
|
|---|
| 74 | request: Request;
|
|---|
| 75 | logs: any[];
|
|---|
| 76 | timeoutId?: number;
|
|---|
| 77 | handler: StrategyHandler;
|
|---|
| 78 | }): Promise<Response | undefined>;
|
|---|
| 79 | }
|
|---|
| 80 | export { NetworkFirst };
|
|---|