25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

141 lines
4.6 KiB

  1. /*
  2. Copyright 2018 Google Inc. All Rights Reserved.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. import {cacheNames} from 'workbox-core/_private/cacheNames.mjs';
  14. import {fetchWrapper} from 'workbox-core/_private/fetchWrapper.mjs';
  15. import {assert} from 'workbox-core/_private/assert.mjs';
  16. import {logger} from 'workbox-core/_private/logger.mjs';
  17. import messages from './utils/messages.mjs';
  18. import './_version.mjs';
  19. /**
  20. * An implementation of a
  21. * [network-only]{@link https://developers.google.com/web/fundamentals/instant-and-offline/offline-cookbook/#network-only}
  22. * request strategy.
  23. *
  24. * This class is useful if you want to take advantage of any [Workbox plugins]{@link https://developers.google.com/web/tools/workbox/guides/using-plugins}.
  25. *
  26. * @memberof workbox.strategies
  27. */
  28. class NetworkOnly {
  29. /**
  30. * @param {Object} options
  31. * @param {string} options.cacheName Cache name to store and retrieve
  32. * requests. Defaults to cache names provided by
  33. * [workbox-core]{@link workbox.core.cacheNames}.
  34. * @param {Array<Object>} options.plugins [Plugins]{@link https://developers.google.com/web/tools/workbox/guides/using-plugins}
  35. * to use in conjunction with this caching strategy.
  36. * @param {Object} options.fetchOptions Values passed along to the
  37. * [`init`](https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch#Parameters)
  38. * of all fetch() requests made by this strategy.
  39. */
  40. constructor(options = {}) {
  41. this._cacheName = cacheNames.getRuntimeName(options.cacheName);
  42. this._plugins = options.plugins || [];
  43. this._fetchOptions = options.fetchOptions || null;
  44. }
  45. /**
  46. * This method will perform a request strategy and follows an API that
  47. * will work with the
  48. * [Workbox Router]{@link workbox.routing.Router}.
  49. *
  50. * @param {Object} options
  51. * @param {FetchEvent} options.event The fetch event to run this strategy
  52. * against.
  53. * @return {Promise<Response>}
  54. */
  55. async handle({event}) {
  56. if (process.env.NODE_ENV !== 'production') {
  57. assert.isInstance(event, FetchEvent, {
  58. moduleName: 'workbox-strategies',
  59. className: 'NetworkOnly',
  60. funcName: 'handle',
  61. paramName: 'event',
  62. });
  63. }
  64. return this.makeRequest({
  65. event,
  66. request: event.request,
  67. });
  68. }
  69. /**
  70. * This method can be used to perform a make a standalone request outside the
  71. * context of the [Workbox Router]{@link workbox.routing.Router}.
  72. *
  73. * See "[Advanced Recipes](https://developers.google.com/web/tools/workbox/guides/advanced-recipes#make-requests)"
  74. * for more usage information.
  75. *
  76. * @param {Object} options
  77. * @param {Request|string} options.request Either a
  78. * [`Request`]{@link https://developer.mozilla.org/en-US/docs/Web/API/Request}
  79. * object, or a string URL, corresponding to the request to be made.
  80. * @param {FetchEvent} [options.event] If provided, `event.waitUntil()` will
  81. * be called automatically to extend the service worker's lifetime.
  82. * @return {Promise<Response>}
  83. */
  84. async makeRequest({event, request}) {
  85. if (typeof request === 'string') {
  86. request = new Request(request);
  87. }
  88. if (process.env.NODE_ENV !== 'production') {
  89. assert.isInstance(request, Request, {
  90. moduleName: 'workbox-strategies',
  91. className: 'NetworkOnly',
  92. funcName: 'handle',
  93. paramName: 'request',
  94. });
  95. }
  96. let error;
  97. let response;
  98. try {
  99. response = await fetchWrapper.fetch({
  100. request,
  101. event,
  102. fetchOptions: this._fetchOptions,
  103. plugins: this._plugins,
  104. });
  105. } catch (err) {
  106. error = err;
  107. }
  108. if (process.env.NODE_ENV !== 'production') {
  109. logger.groupCollapsed(
  110. messages.strategyStart('NetworkOnly', request));
  111. if (response) {
  112. logger.log(`Got response from network.`);
  113. } else {
  114. logger.log(`Unable to get a response from the network.`);
  115. }
  116. messages.printFinalResponse(response);
  117. logger.groupEnd();
  118. }
  119. // If there was an error thrown, re-throw it to ensure the Routers
  120. // catch handler is triggered.
  121. if (error) {
  122. throw error;
  123. }
  124. return response;
  125. }
  126. }
  127. export {NetworkOnly};