UNPKG

6.88 kBJavaScriptView Raw
1this.workbox = this.workbox || {};
2this.workbox.cacheableResponse = (function (exports, assert_js, WorkboxError_js, getFriendlyURL_js, logger_js) {
3 'use strict';
4
5 try {
6 self['workbox:cacheable-response:5.1.4'] && _();
7 } catch (e) {}
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 module:workbox-cacheable-response
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_js.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_js.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_js.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_js.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_js.logger.groupCollapsed(`The request for ` + `'${getFriendlyURL_js.getFriendlyURL(response.url)}' returned a response that does ` + `not meet the criteria for being cached.`);
108 logger_js.logger.groupCollapsed(`View cacheability criteria here.`);
109 logger_js.logger.log(`Cacheable statuses: ` + JSON.stringify(this._statuses));
110 logger_js.logger.log(`Cacheable headers: ` + JSON.stringify(this._headers, null, 2));
111 logger_js.logger.groupEnd();
112 const logFriendlyHeaders = {};
113 response.headers.forEach((value, key) => {
114 logFriendlyHeaders[key] = value;
115 });
116 logger_js.logger.groupCollapsed(`View response status and headers here.`);
117 logger_js.logger.log(`Response status: ` + response.status);
118 logger_js.logger.log(`Response headers: ` + JSON.stringify(logFriendlyHeaders, null, 2));
119 logger_js.logger.groupEnd();
120 logger_js.logger.groupCollapsed(`View full response details here.`);
121 logger_js.logger.log(response.headers);
122 logger_js.logger.log(response);
123 logger_js.logger.groupEnd();
124 logger_js.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 module:workbox-cacheable-response
146 */
147
148 class CacheableResponsePlugin {
149 /**
150 * To construct a new CacheableResponsePlugin 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 /**
165 * @param {Object} options
166 * @param {Response} options.response
167 * @return {Response|null}
168 * @private
169 */
170 this.cacheWillUpdate = async ({
171 response
172 }) => {
173 if (this._cacheableResponse.isResponseCacheable(response)) {
174 return response;
175 }
176
177 return null;
178 };
179
180 this._cacheableResponse = new CacheableResponse(config);
181 }
182
183 }
184
185 exports.CacheableResponse = CacheableResponse;
186 exports.CacheableResponsePlugin = CacheableResponsePlugin;
187
188 return exports;
189
190}({}, workbox.core._private, workbox.core._private, workbox.core._private, workbox.core._private));
191