NetworkOnly.js 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 { fetchWrapper } from 'workbox-core/_private/fetchWrapper.js';
  9. import { logger } from 'workbox-core/_private/logger.js';
  10. import { WorkboxError } from 'workbox-core/_private/WorkboxError.js';
  11. import { messages } from './utils/messages.js';
  12. import './_version.js';
  13. /**
  14. * An implementation of a
  15. * [network-only]{@link https://developers.google.com/web/fundamentals/instant-and-offline/offline-cookbook/#network-only}
  16. * request strategy.
  17. *
  18. * This class is useful if you want to take advantage of any
  19. * [Workbox plugins]{@link https://developers.google.com/web/tools/workbox/guides/using-plugins}.
  20. *
  21. * If the network request fails, this will throw a `WorkboxError` exception.
  22. *
  23. * @memberof module:workbox-strategies
  24. */
  25. class NetworkOnly {
  26. /**
  27. * @param {Object} options
  28. * @param {string} options.cacheName Cache name to store and retrieve
  29. * requests. Defaults to cache names provided by
  30. * [workbox-core]{@link module:workbox-core.cacheNames}.
  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 all fetch() requests made by this strategy.
  36. */
  37. constructor(options = {}) {
  38. this._plugins = options.plugins || [];
  39. this._fetchOptions = options.fetchOptions;
  40. }
  41. /**
  42. * This method will perform a request strategy and follows an API that
  43. * will work with the
  44. * [Workbox Router]{@link module:workbox-routing.Router}.
  45. *
  46. * @param {Object} options
  47. * @param {Request|string} options.request The request to run this strategy for.
  48. * @param {Event} [options.event] The event that triggered the request.
  49. * @return {Promise<Response>}
  50. */
  51. async handle({ event, request }) {
  52. if (typeof request === 'string') {
  53. request = new Request(request);
  54. }
  55. if (process.env.NODE_ENV !== 'production') {
  56. assert.isInstance(request, Request, {
  57. moduleName: 'workbox-strategies',
  58. className: 'NetworkOnly',
  59. funcName: 'handle',
  60. paramName: 'request',
  61. });
  62. }
  63. let error;
  64. let response;
  65. try {
  66. response = await fetchWrapper.fetch({
  67. request,
  68. event,
  69. fetchOptions: this._fetchOptions,
  70. plugins: this._plugins,
  71. });
  72. }
  73. catch (err) {
  74. error = err;
  75. }
  76. if (process.env.NODE_ENV !== 'production') {
  77. logger.groupCollapsed(messages.strategyStart('NetworkOnly', request));
  78. if (response) {
  79. logger.log(`Got response from network.`);
  80. }
  81. else {
  82. logger.log(`Unable to get a response from the network.`);
  83. }
  84. messages.printFinalResponse(response);
  85. logger.groupEnd();
  86. }
  87. if (!response) {
  88. throw new WorkboxError('no-response', { url: request.url, error });
  89. }
  90. return response;
  91. }
  92. }
  93. export { NetworkOnly };