Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

há 3 anos
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 {cacheWrapper} from 'workbox-core/_private/cacheWrapper.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. * [cache-only]{@link https://developers.google.com/web/fundamentals/instant-and-offline/offline-cookbook/#cache-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 CacheOnly {
  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.matchOptions [`CacheQueryOptions`](https://w3c.github.io/ServiceWorker/#dictdef-cachequeryoptions)
  37. */
  38. constructor(options = {}) {
  39. this._cacheName = cacheNames.getRuntimeName(options.cacheName);
  40. this._plugins = options.plugins || [];
  41. this._matchOptions = options.matchOptions || null;
  42. }
  43. /**
  44. * This method will perform a request strategy and follows an API that
  45. * will work with the
  46. * [Workbox Router]{@link workbox.routing.Router}.
  47. *
  48. * @param {Object} options
  49. * @param {FetchEvent} options.event The fetch event to run this strategy
  50. * against.
  51. * @return {Promise<Response>}
  52. */
  53. async handle({event}) {
  54. if (process.env.NODE_ENV !== 'production') {
  55. assert.isInstance(event, FetchEvent, {
  56. moduleName: 'workbox-strategies',
  57. className: 'CacheOnly',
  58. funcName: 'handle',
  59. paramName: 'event',
  60. });
  61. }
  62. return this.makeRequest({
  63. event,
  64. request: event.request,
  65. });
  66. }
  67. /**
  68. * This method can be used to perform a make a standalone request outside the
  69. * context of the [Workbox Router]{@link workbox.routing.Router}.
  70. *
  71. * See "[Advanced Recipes](https://developers.google.com/web/tools/workbox/guides/advanced-recipes#make-requests)"
  72. * for more usage information.
  73. *
  74. * @param {Object} options
  75. * @param {Request|string} options.request Either a
  76. * [`Request`]{@link https://developer.mozilla.org/en-US/docs/Web/API/Request}
  77. * object, or a string URL, corresponding to the request to be made.
  78. * @param {FetchEvent} [options.event] If provided, `event.waitUntil()` will
  79. * be called automatically to extend the service worker's lifetime.
  80. * @return {Promise<Response>}
  81. */
  82. async makeRequest({event, request}) {
  83. if (typeof request === 'string') {
  84. request = new Request(request);
  85. }
  86. if (process.env.NODE_ENV !== 'production') {
  87. assert.isInstance(request, Request, {
  88. moduleName: 'workbox-strategies',
  89. className: 'CacheOnly',
  90. funcName: 'makeRequest',
  91. paramName: 'request',
  92. });
  93. }
  94. const response = await cacheWrapper.match({
  95. cacheName: this._cacheName,
  96. request,
  97. event,
  98. matchOptions: this._matchOptions,
  99. plugins: this._plugins,
  100. });
  101. if (process.env.NODE_ENV !== 'production') {
  102. logger.groupCollapsed(
  103. messages.strategyStart('CacheOnly', request));
  104. if (response) {
  105. logger.log(`Found a cached response in the '${this._cacheName}'` +
  106. ` cache.`);
  107. messages.printFinalResponse(response);
  108. } else {
  109. logger.log(`No response found in the '${this._cacheName}' cache.`);
  110. }
  111. logger.groupEnd();
  112. }
  113. return response;
  114. }
  115. }
  116. export {CacheOnly};