CacheableResponse.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 { WorkboxError } from 'workbox-core/_private/WorkboxError.js';
  9. import { getFriendlyURL } from 'workbox-core/_private/getFriendlyURL.js';
  10. import { logger } from 'workbox-core/_private/logger.js';
  11. import './_version.js';
  12. /**
  13. * This class allows you to set up rules determining what
  14. * status codes and/or headers need to be present in order for a
  15. * [`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response)
  16. * to be considered cacheable.
  17. *
  18. * @memberof module:workbox-cacheable-response
  19. */
  20. class CacheableResponse {
  21. /**
  22. * To construct a new CacheableResponse instance you must provide at least
  23. * one of the `config` properties.
  24. *
  25. * If both `statuses` and `headers` are specified, then both conditions must
  26. * be met for the `Response` to be considered cacheable.
  27. *
  28. * @param {Object} config
  29. * @param {Array<number>} [config.statuses] One or more status codes that a
  30. * `Response` can have and be considered cacheable.
  31. * @param {Object<string,string>} [config.headers] A mapping of header names
  32. * and expected values that a `Response` can have and be considered cacheable.
  33. * If multiple headers are provided, only one needs to be present.
  34. */
  35. constructor(config = {}) {
  36. if (process.env.NODE_ENV !== 'production') {
  37. if (!(config.statuses || config.headers)) {
  38. throw new WorkboxError('statuses-or-headers-required', {
  39. moduleName: 'workbox-cacheable-response',
  40. className: 'CacheableResponse',
  41. funcName: 'constructor',
  42. });
  43. }
  44. if (config.statuses) {
  45. assert.isArray(config.statuses, {
  46. moduleName: 'workbox-cacheable-response',
  47. className: 'CacheableResponse',
  48. funcName: 'constructor',
  49. paramName: 'config.statuses',
  50. });
  51. }
  52. if (config.headers) {
  53. assert.isType(config.headers, 'object', {
  54. moduleName: 'workbox-cacheable-response',
  55. className: 'CacheableResponse',
  56. funcName: 'constructor',
  57. paramName: 'config.headers',
  58. });
  59. }
  60. }
  61. this._statuses = config.statuses;
  62. this._headers = config.headers;
  63. }
  64. /**
  65. * Checks a response to see whether it's cacheable or not, based on this
  66. * object's configuration.
  67. *
  68. * @param {Response} response The response whose cacheability is being
  69. * checked.
  70. * @return {boolean} `true` if the `Response` is cacheable, and `false`
  71. * otherwise.
  72. */
  73. isResponseCacheable(response) {
  74. if (process.env.NODE_ENV !== 'production') {
  75. assert.isInstance(response, Response, {
  76. moduleName: 'workbox-cacheable-response',
  77. className: 'CacheableResponse',
  78. funcName: 'isResponseCacheable',
  79. paramName: 'response',
  80. });
  81. }
  82. let cacheable = true;
  83. if (this._statuses) {
  84. cacheable = this._statuses.includes(response.status);
  85. }
  86. if (this._headers && cacheable) {
  87. cacheable = Object.keys(this._headers).some((headerName) => {
  88. return response.headers.get(headerName) === this._headers[headerName];
  89. });
  90. }
  91. if (process.env.NODE_ENV !== 'production') {
  92. if (!cacheable) {
  93. logger.groupCollapsed(`The request for ` +
  94. `'${getFriendlyURL(response.url)}' returned a response that does ` +
  95. `not meet the criteria for being cached.`);
  96. logger.groupCollapsed(`View cacheability criteria here.`);
  97. logger.log(`Cacheable statuses: ` +
  98. JSON.stringify(this._statuses));
  99. logger.log(`Cacheable headers: ` +
  100. JSON.stringify(this._headers, null, 2));
  101. logger.groupEnd();
  102. const logFriendlyHeaders = {};
  103. response.headers.forEach((value, key) => {
  104. logFriendlyHeaders[key] = value;
  105. });
  106. logger.groupCollapsed(`View response status and headers here.`);
  107. logger.log(`Response status: ` + response.status);
  108. logger.log(`Response headers: ` +
  109. JSON.stringify(logFriendlyHeaders, null, 2));
  110. logger.groupEnd();
  111. logger.groupCollapsed(`View full response details here.`);
  112. logger.log(response.headers);
  113. logger.log(response);
  114. logger.groupEnd();
  115. logger.groupEnd();
  116. }
  117. }
  118. return cacheable;
  119. }
  120. }
  121. export { CacheableResponse };