8
8
9
9
import { assert } from 'workbox-core/_private/assert.js' ;
10
10
import { logger } from 'workbox-core/_private/logger.js' ;
11
+ import { timeout } from 'workbox-core/_private/timeout.js' ;
11
12
import { WorkboxError } from 'workbox-core/_private/WorkboxError.js' ;
12
13
13
- import { Strategy } from './Strategy.js' ;
14
+ import { Strategy , StrategyOptions } from './Strategy.js' ;
14
15
import { StrategyHandler } from './StrategyHandler.js' ;
15
16
import { messages } from './utils/messages.js' ;
16
17
import './_version.js' ;
17
18
18
19
20
+ interface NetworkOnlyOptions extends Omit < StrategyOptions , 'cacheName' | 'matchOptions' > {
21
+ networkTimeoutSeconds ?: number ;
22
+ }
23
+
19
24
/**
20
25
* An implementation of a
21
26
* [network-only]{@link https://developers.google.com/web/fundamentals/instant-and-offline/offline-cookbook/#network-only}
@@ -30,6 +35,24 @@ import './_version.js';
30
35
* @memberof module:workbox-strategies
31
36
*/
32
37
class NetworkOnly extends Strategy {
38
+ private readonly _networkTimeoutSeconds : number ;
39
+
40
+ /**
41
+ * @param {Object } [options]
42
+ * @param {Array<Object> } [options.plugins] [Plugins]{@link https://developers.google.com/web/tools/workbox/guides/using-plugins}
43
+ * to use in conjunction with this caching strategy.
44
+ * @param {Object } [options.fetchOptions] Values passed along to the
45
+ * [`init`](https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch#Parameters)
46
+ * of all fetch() requests made by this strategy.
47
+ * @param {number } [options.networkTimeoutSeconds] If set, any network requests
48
+ * that fail to respond within the timeout will result in a network error.
49
+ */
50
+ constructor ( options : NetworkOnlyOptions = { } ) {
51
+ super ( options ) ;
52
+
53
+ this . _networkTimeoutSeconds = options . networkTimeoutSeconds || 0 ;
54
+ }
55
+
33
56
/**
34
57
* @private
35
58
* @param {Request|string } request A request to run this strategy for.
@@ -42,15 +65,27 @@ class NetworkOnly extends Strategy {
42
65
assert ! . isInstance ( request , Request , {
43
66
moduleName : 'workbox-strategies' ,
44
67
className : this . constructor . name ,
45
- funcName : 'handle ' ,
68
+ funcName : '_handle ' ,
46
69
paramName : 'request' ,
47
70
} ) ;
48
71
}
49
72
50
- let error ;
51
- let response ;
73
+ let error : Error | undefined = undefined ;
74
+ let response : Response | undefined ;
75
+
52
76
try {
53
- response = await handler . fetch ( request ) ;
77
+ const promises : Promise < Response | undefined > [ ] = [ handler . fetch ( request ) ] ;
78
+
79
+ if ( this . _networkTimeoutSeconds ) {
80
+ const timeoutPromise = timeout ( this . _networkTimeoutSeconds * 1000 ) as Promise < undefined > ;
81
+ promises . push ( timeoutPromise ) ;
82
+ }
83
+
84
+ response = await Promise . race ( promises ) ;
85
+ if ( ! response ) {
86
+ throw new Error ( `Timed out the network response after ` +
87
+ `${ this . _networkTimeoutSeconds } seconds.` ) ;
88
+ }
54
89
} catch ( err ) {
55
90
error = err ;
56
91
}
@@ -74,4 +109,4 @@ class NetworkOnly extends Strategy {
74
109
}
75
110
}
76
111
77
- export { NetworkOnly } ;
112
+ export { NetworkOnly , NetworkOnlyOptions } ;
0 commit comments