CacheableResponsePlugin.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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 { CacheableResponse } from './CacheableResponse.js';
  8. import './_version.js';
  9. /**
  10. * A class implementing the `cacheWillUpdate` lifecycle callback. This makes it
  11. * easier to add in cacheability checks to requests made via Workbox's built-in
  12. * strategies.
  13. *
  14. * @memberof module:workbox-cacheable-response
  15. */
  16. class CacheableResponsePlugin {
  17. /**
  18. * To construct a new CacheableResponsePlugin instance you must provide at
  19. * least one of the `config` properties.
  20. *
  21. * If both `statuses` and `headers` are specified, then both conditions must
  22. * be met for the `Response` to be considered cacheable.
  23. *
  24. * @param {Object} config
  25. * @param {Array<number>} [config.statuses] One or more status codes that a
  26. * `Response` can have and be considered cacheable.
  27. * @param {Object<string,string>} [config.headers] A mapping of header names
  28. * and expected values that a `Response` can have and be considered cacheable.
  29. * If multiple headers are provided, only one needs to be present.
  30. */
  31. constructor(config) {
  32. /**
  33. * @param {Object} options
  34. * @param {Response} options.response
  35. * @return {Response|null}
  36. * @private
  37. */
  38. this.cacheWillUpdate = async ({ response }) => {
  39. if (this._cacheableResponse.isResponseCacheable(response)) {
  40. return response;
  41. }
  42. return null;
  43. };
  44. this._cacheableResponse = new CacheableResponse(config);
  45. }
  46. }
  47. export { CacheableResponsePlugin };