source: frontend/node_modules/workbox-strategies/src/NetworkOnly.ts

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 11 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 3.9 KB
Line 
1/*
2 Copyright 2018 Google LLC
3
4 Use of this source code is governed by an MIT-style
5 license that can be found in the LICENSE file or at
6 https://opensource.org/licenses/MIT.
7*/
8
9import {assert} from 'workbox-core/_private/assert.js';
10import {logger} from 'workbox-core/_private/logger.js';
11import {timeout} from 'workbox-core/_private/timeout.js';
12import {WorkboxError} from 'workbox-core/_private/WorkboxError.js';
13
14import {Strategy, StrategyOptions} from './Strategy.js';
15import {StrategyHandler} from './StrategyHandler.js';
16import {messages} from './utils/messages.js';
17import './_version.js';
18
19interface NetworkOnlyOptions
20 extends Omit<StrategyOptions, 'cacheName' | 'matchOptions'> {
21 networkTimeoutSeconds?: number;
22}
23
24/**
25 * An implementation of a
26 * [network-only](https://developer.chrome.com/docs/workbox/caching-strategies-overview/#network-only)
27 * request strategy.
28 *
29 * This class is useful if you want to take advantage of any
30 * [Workbox plugins](https://developer.chrome.com/docs/workbox/using-plugins/).
31 *
32 * If the network request fails, this will throw a `WorkboxError` exception.
33 *
34 * @extends workbox-strategies.Strategy
35 * @memberof workbox-strategies
36 */
37class 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 [non-navigation](https://github.com/GoogleChrome/workbox/issues/1796)
47 * `fetch()` requests made by this strategy.
48 * @param {number} [options.networkTimeoutSeconds] If set, any network requests
49 * that fail to respond within the timeout will result in a network error.
50 */
51 constructor(options: NetworkOnlyOptions = {}) {
52 super(options);
53
54 this._networkTimeoutSeconds = options.networkTimeoutSeconds || 0;
55 }
56
57 /**
58 * @private
59 * @param {Request|string} request A request to run this strategy for.
60 * @param {workbox-strategies.StrategyHandler} handler The event that
61 * triggered the request.
62 * @return {Promise<Response>}
63 */
64 async _handle(request: Request, handler: StrategyHandler): Promise<Response> {
65 if (process.env.NODE_ENV !== 'production') {
66 assert!.isInstance(request, Request, {
67 moduleName: 'workbox-strategies',
68 className: this.constructor.name,
69 funcName: '_handle',
70 paramName: 'request',
71 });
72 }
73
74 let error: Error | undefined = undefined;
75 let response: Response | undefined;
76
77 try {
78 const promises: Promise<Response | undefined>[] = [
79 handler.fetch(request),
80 ];
81
82 if (this._networkTimeoutSeconds) {
83 const timeoutPromise = timeout(
84 this._networkTimeoutSeconds * 1000,
85 ) as Promise<undefined>;
86 promises.push(timeoutPromise);
87 }
88
89 response = await Promise.race(promises);
90 if (!response) {
91 throw new Error(
92 `Timed out the network response after ` +
93 `${this._networkTimeoutSeconds} seconds.`,
94 );
95 }
96 } catch (err) {
97 if (err instanceof Error) {
98 error = err;
99 }
100 }
101
102 if (process.env.NODE_ENV !== 'production') {
103 logger.groupCollapsed(
104 messages.strategyStart(this.constructor.name, request),
105 );
106 if (response) {
107 logger.log(`Got response from network.`);
108 } else {
109 logger.log(`Unable to get a response from the network.`);
110 }
111 messages.printFinalResponse(response);
112 logger.groupEnd();
113 }
114
115 if (!response) {
116 throw new WorkboxError('no-response', {url: request.url, error});
117 }
118 return response;
119 }
120}
121
122export {NetworkOnly, NetworkOnlyOptions};
Note: See TracBrowser for help on using the repository browser.