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