source: frontend/node_modules/workbox-cacheable-response/CacheableResponse.js

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 12 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 4.9 KB
Line 
1/*
2 Copyright 2018 Google LLC
3
4 Use of this source code is governed by an MIT-style
5 license that can be found in the LICENSE file or at
6 https://opensource.org/licenses/MIT.
7*/
8import { assert } from 'workbox-core/_private/assert.js';
9import { WorkboxError } from 'workbox-core/_private/WorkboxError.js';
10import { getFriendlyURL } from 'workbox-core/_private/getFriendlyURL.js';
11import { logger } from 'workbox-core/_private/logger.js';
12import './_version.js';
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 workbox-cacheable-response
20 */
21class 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 if (process.env.NODE_ENV !== 'production') {
38 if (!(config.statuses || config.headers)) {
39 throw new WorkboxError('statuses-or-headers-required', {
40 moduleName: 'workbox-cacheable-response',
41 className: 'CacheableResponse',
42 funcName: 'constructor',
43 });
44 }
45 if (config.statuses) {
46 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.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 if (process.env.NODE_ENV !== 'production') {
76 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 if (process.env.NODE_ENV !== 'production') {
93 if (!cacheable) {
94 logger.groupCollapsed(`The request for ` +
95 `'${getFriendlyURL(response.url)}' returned a response that does ` +
96 `not meet the criteria for being cached.`);
97 logger.groupCollapsed(`View cacheability criteria here.`);
98 logger.log(`Cacheable statuses: ` + JSON.stringify(this._statuses));
99 logger.log(`Cacheable headers: ` + JSON.stringify(this._headers, null, 2));
100 logger.groupEnd();
101 const logFriendlyHeaders = {};
102 response.headers.forEach((value, key) => {
103 logFriendlyHeaders[key] = value;
104 });
105 logger.groupCollapsed(`View response status and headers here.`);
106 logger.log(`Response status: ${response.status}`);
107 logger.log(`Response headers: ` + JSON.stringify(logFriendlyHeaders, null, 2));
108 logger.groupEnd();
109 logger.groupCollapsed(`View full response details here.`);
110 logger.log(response.headers);
111 logger.log(response);
112 logger.groupEnd();
113 logger.groupEnd();
114 }
115 }
116 return cacheable;
117 }
118}
119export { CacheableResponse };
Note: See TracBrowser for help on using the repository browser.