1 | this.workbox = this.workbox || {};
|
2 | this.workbox.cacheableResponse = (function (exports,WorkboxError_mjs,assert_mjs,getFriendlyURL_mjs,logger_mjs) {
|
3 | ;
|
4 |
|
5 | try {
|
6 | self.workbox.v['workbox:cacheable-response:3.6.2'] = 1;
|
7 | } catch (e) {} // eslint-disable-line
|
8 |
|
9 | /*
|
10 | Copyright 2017 Google Inc.
|
11 |
|
12 | Licensed under the Apache License, Version 2.0 (the "License");
|
13 | you may not use this file except in compliance with the License.
|
14 | You may obtain a copy of the License at
|
15 |
|
16 | https://www.apache.org/licenses/LICENSE-2.0
|
17 |
|
18 | Unless required by applicable law or agreed to in writing, software
|
19 | distributed under the License is distributed on an "AS IS" BASIS,
|
20 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
21 | See the License for the specific language governing permissions and
|
22 | limitations under the License.
|
23 | */
|
24 |
|
25 | /**
|
26 | * This class allows you to set up rules determining what
|
27 | * status codes and/or headers need to be present in order for a
|
28 | * [`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response)
|
29 | * to be considered cacheable.
|
30 | *
|
31 | * @memberof workbox.cacheableResponse
|
32 | */
|
33 | class CacheableResponse {
|
34 | /**
|
35 | * To construct a new CacheableResponse instance you must provide at least
|
36 | * one of the `config` properties.
|
37 | *
|
38 | * If both `statuses` and `headers` are specified, then both conditions must
|
39 | * be met for the `Response` to be considered cacheable.
|
40 | *
|
41 | * @param {Object} config
|
42 | * @param {Array<number>} [config.statuses] One or more status codes that a
|
43 | * `Response` can have and be considered cacheable.
|
44 | * @param {Object<string,string>} [config.headers] A mapping of header names
|
45 | * and expected values that a `Response` can have and be considered cacheable.
|
46 | * If multiple headers are provided, only one needs to be present.
|
47 | */
|
48 | constructor(config = {}) {
|
49 | {
|
50 | if (!(config.statuses || config.headers)) {
|
51 | throw new WorkboxError_mjs.WorkboxError('statuses-or-headers-required', {
|
52 | moduleName: 'workbox-cacheable-response',
|
53 | className: 'CacheableResponse',
|
54 | funcName: 'constructor'
|
55 | });
|
56 | }
|
57 |
|
58 | if (config.statuses) {
|
59 | assert_mjs.assert.isArray(config.statuses, {
|
60 | moduleName: 'workbox-cacheable-response',
|
61 | className: 'CacheableResponse',
|
62 | funcName: 'constructor',
|
63 | paramName: 'config.statuses'
|
64 | });
|
65 | }
|
66 |
|
67 | if (config.headers) {
|
68 | assert_mjs.assert.isType(config.headers, 'object', {
|
69 | moduleName: 'workbox-cacheable-response',
|
70 | className: 'CacheableResponse',
|
71 | funcName: 'constructor',
|
72 | paramName: 'config.headers'
|
73 | });
|
74 | }
|
75 | }
|
76 |
|
77 | this._statuses = config.statuses;
|
78 | this._headers = config.headers;
|
79 | }
|
80 |
|
81 | /**
|
82 | * Checks a response to see whether it's cacheable or not, based on this
|
83 | * object's configuration.
|
84 | *
|
85 | * @param {Response} response The response whose cacheability is being
|
86 | * checked.
|
87 | * @return {boolean} `true` if the `Response` is cacheable, and `false`
|
88 | * otherwise.
|
89 | */
|
90 | isResponseCacheable(response) {
|
91 | {
|
92 | assert_mjs.assert.isInstance(response, Response, {
|
93 | moduleName: 'workbox-cacheable-response',
|
94 | className: 'CacheableResponse',
|
95 | funcName: 'isResponseCacheable',
|
96 | paramName: 'response'
|
97 | });
|
98 | }
|
99 |
|
100 | let cacheable = true;
|
101 |
|
102 | if (this._statuses) {
|
103 | cacheable = this._statuses.includes(response.status);
|
104 | }
|
105 |
|
106 | if (this._headers && cacheable) {
|
107 | cacheable = Object.keys(this._headers).some(headerName => {
|
108 | return response.headers.get(headerName) === this._headers[headerName];
|
109 | });
|
110 | }
|
111 |
|
112 | {
|
113 | if (!cacheable) {
|
114 | logger_mjs.logger.groupCollapsed(`The request for ` + `'${getFriendlyURL_mjs.getFriendlyURL(response.url)}' returned a response that does ` + `not meet the criteria for being cached.`);
|
115 |
|
116 | logger_mjs.logger.groupCollapsed(`View cacheability criteria here.`);
|
117 | logger_mjs.logger.unprefixed.log(`Cacheable statuses: ` + JSON.stringify(this._statuses));
|
118 | logger_mjs.logger.unprefixed.log(`Cacheable headers: ` + JSON.stringify(this._headers, null, 2));
|
119 | logger_mjs.logger.groupEnd();
|
120 |
|
121 | const logFriendlyHeaders = {};
|
122 | response.headers.forEach((value, key) => {
|
123 | logFriendlyHeaders[key] = value;
|
124 | });
|
125 |
|
126 | logger_mjs.logger.groupCollapsed(`View response status and headers here.`);
|
127 | logger_mjs.logger.unprefixed.log(`Response status: ` + response.status);
|
128 | logger_mjs.logger.unprefixed.log(`Response headers: ` + JSON.stringify(logFriendlyHeaders, null, 2));
|
129 | logger_mjs.logger.groupEnd();
|
130 |
|
131 | logger_mjs.logger.groupCollapsed(`View full response details here.`);
|
132 | logger_mjs.logger.unprefixed.log(response.headers);
|
133 | logger_mjs.logger.unprefixed.log(response);
|
134 | logger_mjs.logger.groupEnd();
|
135 |
|
136 | logger_mjs.logger.groupEnd();
|
137 | }
|
138 | }
|
139 |
|
140 | return cacheable;
|
141 | }
|
142 | }
|
143 |
|
144 | /*
|
145 | Copyright 2016 Google Inc. All Rights Reserved.
|
146 | Licensed under the Apache License, Version 2.0 (the "License");
|
147 | you may not use this file except in compliance with the License.
|
148 | You may obtain a copy of the License at
|
149 | http://www.apache.org/licenses/LICENSE-2.0
|
150 | Unless required by applicable law or agreed to in writing, software
|
151 | distributed under the License is distributed on an "AS IS" BASIS,
|
152 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
153 | See the License for the specific language governing permissions and
|
154 | limitations under the License.
|
155 | */
|
156 |
|
157 | /**
|
158 | * A class implementing the `cacheWillUpdate` lifecycle callback. This makes it
|
159 | * easier to add in cacheability checks to requests made via Workbox's built-in
|
160 | * strategies.
|
161 | *
|
162 | * @memberof workbox.cacheableResponse
|
163 | */
|
164 | class Plugin {
|
165 | /**
|
166 | * To construct a new cacheable response Plugin instance you must provide at
|
167 | * least one of the `config` properties.
|
168 | *
|
169 | * If both `statuses` and `headers` are specified, then both conditions must
|
170 | * be met for the `Response` to be considered cacheable.
|
171 | *
|
172 | * @param {Object} config
|
173 | * @param {Array<number>} [config.statuses] One or more status codes that a
|
174 | * `Response` can have and be considered cacheable.
|
175 | * @param {Object<string,string>} [config.headers] A mapping of header names
|
176 | * and expected values that a `Response` can have and be considered cacheable.
|
177 | * If multiple headers are provided, only one needs to be present.
|
178 | */
|
179 | constructor(config) {
|
180 | this._cacheableResponse = new CacheableResponse(config);
|
181 | }
|
182 |
|
183 | /**
|
184 | * @param {Object} options
|
185 | * @param {Response} options.response
|
186 | * @return {boolean}
|
187 | * @private
|
188 | */
|
189 | cacheWillUpdate({ response }) {
|
190 | if (this._cacheableResponse.isResponseCacheable(response)) {
|
191 | return response;
|
192 | }
|
193 | return null;
|
194 | }
|
195 | }
|
196 |
|
197 | /*
|
198 | Copyright 2017 Google Inc.
|
199 |
|
200 | Licensed under the Apache License, Version 2.0 (the "License");
|
201 | you may not use this file except in compliance with the License.
|
202 | You may obtain a copy of the License at
|
203 |
|
204 | https://www.apache.org/licenses/LICENSE-2.0
|
205 |
|
206 | Unless required by applicable law or agreed to in writing, software
|
207 | distributed under the License is distributed on an "AS IS" BASIS,
|
208 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
209 | See the License for the specific language governing permissions and
|
210 | limitations under the License.
|
211 | */
|
212 |
|
213 | /*
|
214 | Copyright 2017 Google Inc.
|
215 |
|
216 | Licensed under the Apache License, Version 2.0 (the "License");
|
217 | you may not use this file except in compliance with the License.
|
218 | You may obtain a copy of the License at
|
219 |
|
220 | https://www.apache.org/licenses/LICENSE-2.0
|
221 |
|
222 | Unless required by applicable law or agreed to in writing, software
|
223 | distributed under the License is distributed on an "AS IS" BASIS,
|
224 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
225 | See the License for the specific language governing permissions and
|
226 | limitations under the License.
|
227 | */
|
228 |
|
229 | exports.CacheableResponse = CacheableResponse;
|
230 | exports.Plugin = Plugin;
|
231 |
|
232 | return exports;
|
233 |
|
234 | }({},workbox.core._private,workbox.core._private,workbox.core._private,workbox.core._private));
|
235 |
|
236 | //# sourceMappingURL=workbox-cacheable-response.dev.js.map
|