UNPKG

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