CacheOnly.js 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 { cacheNames } from 'workbox-core/_private/cacheNames.js';
  9. import { cacheWrapper } from 'workbox-core/_private/cacheWrapper.js';
  10. import { logger } from 'workbox-core/_private/logger.js';
  11. import { WorkboxError } from 'workbox-core/_private/WorkboxError.js';
  12. import { messages } from './utils/messages.js';
  13. import './_version.js';
  14. /**
  15. * An implementation of a
  16. * [cache-only]{@link https://developers.google.com/web/fundamentals/instant-and-offline/offline-cookbook/#cache-only}
  17. * request strategy.
  18. *
  19. * This class is useful if you want to take advantage of any
  20. * [Workbox plugins]{@link https://developers.google.com/web/tools/workbox/guides/using-plugins}.
  21. *
  22. * If there is no cache match, this will throw a `WorkboxError` exception.
  23. *
  24. * @memberof module:workbox-strategies
  25. */
  26. class CacheOnly {
  27. /**
  28. * @param {Object} options
  29. * @param {string} options.cacheName Cache name to store and retrieve
  30. * requests. Defaults to cache names provided by
  31. * [workbox-core]{@link module:workbox-core.cacheNames}.
  32. * @param {Array<Object>} options.plugins [Plugins]{@link https://developers.google.com/web/tools/workbox/guides/using-plugins}
  33. * to use in conjunction with this caching strategy.
  34. * @param {Object} options.matchOptions [`CacheQueryOptions`](https://w3c.github.io/ServiceWorker/#dictdef-cachequeryoptions)
  35. */
  36. constructor(options = {}) {
  37. this._cacheName = cacheNames.getRuntimeName(options.cacheName);
  38. this._plugins = options.plugins || [];
  39. this._matchOptions = options.matchOptions;
  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 A 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: 'CacheOnly',
  59. funcName: 'makeRequest',
  60. paramName: 'request',
  61. });
  62. }
  63. const response = await cacheWrapper.match({
  64. cacheName: this._cacheName,
  65. request,
  66. event,
  67. matchOptions: this._matchOptions,
  68. plugins: this._plugins,
  69. });
  70. if (process.env.NODE_ENV !== 'production') {
  71. logger.groupCollapsed(messages.strategyStart('CacheOnly', request));
  72. if (response) {
  73. logger.log(`Found a cached response in the '${this._cacheName}'` +
  74. ` cache.`);
  75. messages.printFinalResponse(response);
  76. }
  77. else {
  78. logger.log(`No response found in the '${this._cacheName}' cache.`);
  79. }
  80. logger.groupEnd();
  81. }
  82. if (!response) {
  83. throw new WorkboxError('no-response', { url: request.url });
  84. }
  85. return response;
  86. }
  87. }
  88. export { CacheOnly };