25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 

148 satır
4.9 KiB

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