NetworkOnly.js 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. Copyright 2018 Google LLC
  3. Use of this source code is governed by an MIT-style
  4. license that can be found in the LICENSE file or at
  5. https://opensource.org/licenses/MIT.
  6. */
  7. import { assert } from 'workbox-core/_private/assert.js';
  8. import { logger } from 'workbox-core/_private/logger.js';
  9. import { timeout } from 'workbox-core/_private/timeout.js';
  10. import { WorkboxError } from 'workbox-core/_private/WorkboxError.js';
  11. import { Strategy } from './Strategy.js';
  12. import { messages } from './utils/messages.js';
  13. import './_version.js';
  14. /**
  15. * An implementation of a
  16. * [network-only](https://developer.chrome.com/docs/workbox/caching-strategies-overview/#network-only)
  17. * request strategy.
  18. *
  19. * This class is useful if you want to take advantage of any
  20. * [Workbox plugins](https://developer.chrome.com/docs/workbox/using-plugins/).
  21. *
  22. * If the network request fails, this will throw a `WorkboxError` exception.
  23. *
  24. * @extends workbox-strategies.Strategy
  25. * @memberof workbox-strategies
  26. */
  27. class NetworkOnly extends Strategy {
  28. /**
  29. * @param {Object} [options]
  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 {number} [options.networkTimeoutSeconds] If set, any network requests
  37. * that fail to respond within the timeout will result in a network error.
  38. */
  39. constructor(options = {}) {
  40. super(options);
  41. this._networkTimeoutSeconds = options.networkTimeoutSeconds || 0;
  42. }
  43. /**
  44. * @private
  45. * @param {Request|string} request A request to run this strategy for.
  46. * @param {workbox-strategies.StrategyHandler} handler The event that
  47. * triggered the request.
  48. * @return {Promise<Response>}
  49. */
  50. async _handle(request, handler) {
  51. if (process.env.NODE_ENV !== 'production') {
  52. assert.isInstance(request, Request, {
  53. moduleName: 'workbox-strategies',
  54. className: this.constructor.name,
  55. funcName: '_handle',
  56. paramName: 'request',
  57. });
  58. }
  59. let error = undefined;
  60. let response;
  61. try {
  62. const promises = [
  63. handler.fetch(request),
  64. ];
  65. if (this._networkTimeoutSeconds) {
  66. const timeoutPromise = timeout(this._networkTimeoutSeconds * 1000);
  67. promises.push(timeoutPromise);
  68. }
  69. response = await Promise.race(promises);
  70. if (!response) {
  71. throw new Error(`Timed out the network response after ` +
  72. `${this._networkTimeoutSeconds} seconds.`);
  73. }
  74. }
  75. catch (err) {
  76. if (err instanceof Error) {
  77. error = err;
  78. }
  79. }
  80. if (process.env.NODE_ENV !== 'production') {
  81. logger.groupCollapsed(messages.strategyStart(this.constructor.name, request));
  82. if (response) {
  83. logger.log(`Got response from network.`);
  84. }
  85. else {
  86. logger.log(`Unable to get a response from the network.`);
  87. }
  88. messages.printFinalResponse(response);
  89. logger.groupEnd();
  90. }
  91. if (!response) {
  92. throw new WorkboxError('no-response', { url: request.url, error });
  93. }
  94. return response;
  95. }
  96. }
  97. export { NetworkOnly };