Plugin.mjs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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.mjs';
  8. import './_version.mjs';
  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 workbox.cacheableResponse
  15. */
  16. class Plugin {
  17. /**
  18. * To construct a new cacheable response Plugin 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. this._cacheableResponse = new CacheableResponse(config);
  33. }
  34. /**
  35. * @param {Object} options
  36. * @param {Response} options.response
  37. * @return {boolean}
  38. * @private
  39. */
  40. cacheWillUpdate({response}) {
  41. if (this._cacheableResponse.isResponseCacheable(response)) {
  42. return response;
  43. }
  44. return null;
  45. }
  46. }
  47. export {Plugin};