source: frontend/node_modules/workbox-strategies/NetworkOnly.js

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.7 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*/
8import { assert } from 'workbox-core/_private/assert.js';
9import { logger } from 'workbox-core/_private/logger.js';
10import { timeout } from 'workbox-core/_private/timeout.js';
11import { WorkboxError } from 'workbox-core/_private/WorkboxError.js';
12import { Strategy } from './Strategy.js';
13import { messages } from './utils/messages.js';
14import './_version.js';
15/**
16 * An implementation of a
17 * [network-only](https://developer.chrome.com/docs/workbox/caching-strategies-overview/#network-only)
18 * request strategy.
19 *
20 * This class is useful if you want to take advantage of any
21 * [Workbox plugins](https://developer.chrome.com/docs/workbox/using-plugins/).
22 *
23 * If the network request fails, this will throw a `WorkboxError` exception.
24 *
25 * @extends workbox-strategies.Strategy
26 * @memberof workbox-strategies
27 */
28class NetworkOnly extends Strategy {
29 /**
30 * @param {Object} [options]
31 * @param {Array<Object>} [options.plugins] [Plugins]{@link https://developers.google.com/web/tools/workbox/guides/using-plugins}
32 * to use in conjunction with this caching strategy.
33 * @param {Object} [options.fetchOptions] Values passed along to the
34 * [`init`](https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch#Parameters)
35 * of [non-navigation](https://github.com/GoogleChrome/workbox/issues/1796)
36 * `fetch()` requests made by this strategy.
37 * @param {number} [options.networkTimeoutSeconds] If set, any network requests
38 * that fail to respond within the timeout will result in a network error.
39 */
40 constructor(options = {}) {
41 super(options);
42 this._networkTimeoutSeconds = options.networkTimeoutSeconds || 0;
43 }
44 /**
45 * @private
46 * @param {Request|string} request A request to run this strategy for.
47 * @param {workbox-strategies.StrategyHandler} handler The event that
48 * triggered the request.
49 * @return {Promise<Response>}
50 */
51 async _handle(request, handler) {
52 if (process.env.NODE_ENV !== 'production') {
53 assert.isInstance(request, Request, {
54 moduleName: 'workbox-strategies',
55 className: this.constructor.name,
56 funcName: '_handle',
57 paramName: 'request',
58 });
59 }
60 let error = undefined;
61 let response;
62 try {
63 const promises = [
64 handler.fetch(request),
65 ];
66 if (this._networkTimeoutSeconds) {
67 const timeoutPromise = timeout(this._networkTimeoutSeconds * 1000);
68 promises.push(timeoutPromise);
69 }
70 response = await Promise.race(promises);
71 if (!response) {
72 throw new Error(`Timed out the network response after ` +
73 `${this._networkTimeoutSeconds} seconds.`);
74 }
75 }
76 catch (err) {
77 if (err instanceof Error) {
78 error = err;
79 }
80 }
81 if (process.env.NODE_ENV !== 'production') {
82 logger.groupCollapsed(messages.strategyStart(this.constructor.name, request));
83 if (response) {
84 logger.log(`Got response from network.`);
85 }
86 else {
87 logger.log(`Unable to get a response from the network.`);
88 }
89 messages.printFinalResponse(response);
90 logger.groupEnd();
91 }
92 if (!response) {
93 throw new WorkboxError('no-response', { url: request.url, error });
94 }
95 return response;
96 }
97}
98export { NetworkOnly };
Note: See TracBrowser for help on using the repository browser.