workbox-cacheable-response.dev.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. this.workbox = this.workbox || {};
  2. this.workbox.cacheableResponse = (function (exports, assert_js, WorkboxError_js, getFriendlyURL_js, logger_js) {
  3. 'use strict';
  4. try {
  5. self['workbox:cacheable-response:5.1.4'] && _();
  6. } catch (e) {}
  7. /*
  8. Copyright 2018 Google LLC
  9. Use of this source code is governed by an MIT-style
  10. license that can be found in the LICENSE file or at
  11. https://opensource.org/licenses/MIT.
  12. */
  13. /**
  14. * This class allows you to set up rules determining what
  15. * status codes and/or headers need to be present in order for a
  16. * [`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response)
  17. * to be considered cacheable.
  18. *
  19. * @memberof module:workbox-cacheable-response
  20. */
  21. class CacheableResponse {
  22. /**
  23. * To construct a new CacheableResponse instance you must provide at least
  24. * one of the `config` properties.
  25. *
  26. * If both `statuses` and `headers` are specified, then both conditions must
  27. * be met for the `Response` to be considered cacheable.
  28. *
  29. * @param {Object} config
  30. * @param {Array<number>} [config.statuses] One or more status codes that a
  31. * `Response` can have and be considered cacheable.
  32. * @param {Object<string,string>} [config.headers] A mapping of header names
  33. * and expected values that a `Response` can have and be considered cacheable.
  34. * If multiple headers are provided, only one needs to be present.
  35. */
  36. constructor(config = {}) {
  37. {
  38. if (!(config.statuses || config.headers)) {
  39. throw new WorkboxError_js.WorkboxError('statuses-or-headers-required', {
  40. moduleName: 'workbox-cacheable-response',
  41. className: 'CacheableResponse',
  42. funcName: 'constructor'
  43. });
  44. }
  45. if (config.statuses) {
  46. assert_js.assert.isArray(config.statuses, {
  47. moduleName: 'workbox-cacheable-response',
  48. className: 'CacheableResponse',
  49. funcName: 'constructor',
  50. paramName: 'config.statuses'
  51. });
  52. }
  53. if (config.headers) {
  54. assert_js.assert.isType(config.headers, 'object', {
  55. moduleName: 'workbox-cacheable-response',
  56. className: 'CacheableResponse',
  57. funcName: 'constructor',
  58. paramName: 'config.headers'
  59. });
  60. }
  61. }
  62. this._statuses = config.statuses;
  63. this._headers = config.headers;
  64. }
  65. /**
  66. * Checks a response to see whether it's cacheable or not, based on this
  67. * object's configuration.
  68. *
  69. * @param {Response} response The response whose cacheability is being
  70. * checked.
  71. * @return {boolean} `true` if the `Response` is cacheable, and `false`
  72. * otherwise.
  73. */
  74. isResponseCacheable(response) {
  75. {
  76. assert_js.assert.isInstance(response, Response, {
  77. moduleName: 'workbox-cacheable-response',
  78. className: 'CacheableResponse',
  79. funcName: 'isResponseCacheable',
  80. paramName: 'response'
  81. });
  82. }
  83. let cacheable = true;
  84. if (this._statuses) {
  85. cacheable = this._statuses.includes(response.status);
  86. }
  87. if (this._headers && cacheable) {
  88. cacheable = Object.keys(this._headers).some(headerName => {
  89. return response.headers.get(headerName) === this._headers[headerName];
  90. });
  91. }
  92. {
  93. if (!cacheable) {
  94. logger_js.logger.groupCollapsed(`The request for ` + `'${getFriendlyURL_js.getFriendlyURL(response.url)}' returned a response that does ` + `not meet the criteria for being cached.`);
  95. logger_js.logger.groupCollapsed(`View cacheability criteria here.`);
  96. logger_js.logger.log(`Cacheable statuses: ` + JSON.stringify(this._statuses));
  97. logger_js.logger.log(`Cacheable headers: ` + JSON.stringify(this._headers, null, 2));
  98. logger_js.logger.groupEnd();
  99. const logFriendlyHeaders = {};
  100. response.headers.forEach((value, key) => {
  101. logFriendlyHeaders[key] = value;
  102. });
  103. logger_js.logger.groupCollapsed(`View response status and headers here.`);
  104. logger_js.logger.log(`Response status: ` + response.status);
  105. logger_js.logger.log(`Response headers: ` + JSON.stringify(logFriendlyHeaders, null, 2));
  106. logger_js.logger.groupEnd();
  107. logger_js.logger.groupCollapsed(`View full response details here.`);
  108. logger_js.logger.log(response.headers);
  109. logger_js.logger.log(response);
  110. logger_js.logger.groupEnd();
  111. logger_js.logger.groupEnd();
  112. }
  113. }
  114. return cacheable;
  115. }
  116. }
  117. /*
  118. Copyright 2018 Google LLC
  119. Use of this source code is governed by an MIT-style
  120. license that can be found in the LICENSE file or at
  121. https://opensource.org/licenses/MIT.
  122. */
  123. /**
  124. * A class implementing the `cacheWillUpdate` lifecycle callback. This makes it
  125. * easier to add in cacheability checks to requests made via Workbox's built-in
  126. * strategies.
  127. *
  128. * @memberof module:workbox-cacheable-response
  129. */
  130. class CacheableResponsePlugin {
  131. /**
  132. * To construct a new CacheableResponsePlugin instance you must provide at
  133. * least one of the `config` properties.
  134. *
  135. * If both `statuses` and `headers` are specified, then both conditions must
  136. * be met for the `Response` to be considered cacheable.
  137. *
  138. * @param {Object} config
  139. * @param {Array<number>} [config.statuses] One or more status codes that a
  140. * `Response` can have and be considered cacheable.
  141. * @param {Object<string,string>} [config.headers] A mapping of header names
  142. * and expected values that a `Response` can have and be considered cacheable.
  143. * If multiple headers are provided, only one needs to be present.
  144. */
  145. constructor(config) {
  146. /**
  147. * @param {Object} options
  148. * @param {Response} options.response
  149. * @return {Response|null}
  150. * @private
  151. */
  152. this.cacheWillUpdate = async ({
  153. response
  154. }) => {
  155. if (this._cacheableResponse.isResponseCacheable(response)) {
  156. return response;
  157. }
  158. return null;
  159. };
  160. this._cacheableResponse = new CacheableResponse(config);
  161. }
  162. }
  163. exports.CacheableResponse = CacheableResponse;
  164. exports.CacheableResponsePlugin = CacheableResponsePlugin;
  165. return exports;
  166. }({}, workbox.core._private, workbox.core._private, workbox.core._private, workbox.core._private));
  167. //# sourceMappingURL=workbox-cacheable-response.dev.js.map