| 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 | */
|
|---|
| 8 |
|
|---|
| 9 | import {assert} from 'workbox-core/_private/assert.js';
|
|---|
| 10 | import {WorkboxError} from 'workbox-core/_private/WorkboxError.js';
|
|---|
| 11 | import {getFriendlyURL} from 'workbox-core/_private/getFriendlyURL.js';
|
|---|
| 12 | import {logger} from 'workbox-core/_private/logger.js';
|
|---|
| 13 | import './_version.js';
|
|---|
| 14 |
|
|---|
| 15 | export interface CacheableResponseOptions {
|
|---|
| 16 | statuses?: number[];
|
|---|
| 17 | headers?: {[headerName: string]: string};
|
|---|
| 18 | }
|
|---|
| 19 |
|
|---|
| 20 | /**
|
|---|
| 21 | * This class allows you to set up rules determining what
|
|---|
| 22 | * status codes and/or headers need to be present in order for a
|
|---|
| 23 | * [`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response)
|
|---|
| 24 | * to be considered cacheable.
|
|---|
| 25 | *
|
|---|
| 26 | * @memberof workbox-cacheable-response
|
|---|
| 27 | */
|
|---|
| 28 | class CacheableResponse {
|
|---|
| 29 | private readonly _statuses?: CacheableResponseOptions['statuses'];
|
|---|
| 30 | private readonly _headers?: CacheableResponseOptions['headers'];
|
|---|
| 31 |
|
|---|
| 32 | /**
|
|---|
| 33 | * To construct a new CacheableResponse instance you must provide at least
|
|---|
| 34 | * one of the `config` properties.
|
|---|
| 35 | *
|
|---|
| 36 | * If both `statuses` and `headers` are specified, then both conditions must
|
|---|
| 37 | * be met for the `Response` to be considered cacheable.
|
|---|
| 38 | *
|
|---|
| 39 | * @param {Object} config
|
|---|
| 40 | * @param {Array<number>} [config.statuses] One or more status codes that a
|
|---|
| 41 | * `Response` can have and be considered cacheable.
|
|---|
| 42 | * @param {Object<string,string>} [config.headers] A mapping of header names
|
|---|
| 43 | * and expected values that a `Response` can have and be considered cacheable.
|
|---|
| 44 | * If multiple headers are provided, only one needs to be present.
|
|---|
| 45 | */
|
|---|
| 46 | constructor(config: CacheableResponseOptions = {}) {
|
|---|
| 47 | if (process.env.NODE_ENV !== 'production') {
|
|---|
| 48 | if (!(config.statuses || config.headers)) {
|
|---|
| 49 | throw new WorkboxError('statuses-or-headers-required', {
|
|---|
| 50 | moduleName: 'workbox-cacheable-response',
|
|---|
| 51 | className: 'CacheableResponse',
|
|---|
| 52 | funcName: 'constructor',
|
|---|
| 53 | });
|
|---|
| 54 | }
|
|---|
| 55 |
|
|---|
| 56 | if (config.statuses) {
|
|---|
| 57 | assert!.isArray(config.statuses, {
|
|---|
| 58 | moduleName: 'workbox-cacheable-response',
|
|---|
| 59 | className: 'CacheableResponse',
|
|---|
| 60 | funcName: 'constructor',
|
|---|
| 61 | paramName: 'config.statuses',
|
|---|
| 62 | });
|
|---|
| 63 | }
|
|---|
| 64 |
|
|---|
| 65 | if (config.headers) {
|
|---|
| 66 | assert!.isType(config.headers, 'object', {
|
|---|
| 67 | moduleName: 'workbox-cacheable-response',
|
|---|
| 68 | className: 'CacheableResponse',
|
|---|
| 69 | funcName: 'constructor',
|
|---|
| 70 | paramName: 'config.headers',
|
|---|
| 71 | });
|
|---|
| 72 | }
|
|---|
| 73 | }
|
|---|
| 74 |
|
|---|
| 75 | this._statuses = config.statuses;
|
|---|
| 76 | this._headers = config.headers;
|
|---|
| 77 | }
|
|---|
| 78 |
|
|---|
| 79 | /**
|
|---|
| 80 | * Checks a response to see whether it's cacheable or not, based on this
|
|---|
| 81 | * object's configuration.
|
|---|
| 82 | *
|
|---|
| 83 | * @param {Response} response The response whose cacheability is being
|
|---|
| 84 | * checked.
|
|---|
| 85 | * @return {boolean} `true` if the `Response` is cacheable, and `false`
|
|---|
| 86 | * otherwise.
|
|---|
| 87 | */
|
|---|
| 88 | isResponseCacheable(response: Response): boolean {
|
|---|
| 89 | if (process.env.NODE_ENV !== 'production') {
|
|---|
| 90 | assert!.isInstance(response, Response, {
|
|---|
| 91 | moduleName: 'workbox-cacheable-response',
|
|---|
| 92 | className: 'CacheableResponse',
|
|---|
| 93 | funcName: 'isResponseCacheable',
|
|---|
| 94 | paramName: 'response',
|
|---|
| 95 | });
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 | let cacheable = true;
|
|---|
| 99 |
|
|---|
| 100 | if (this._statuses) {
|
|---|
| 101 | cacheable = this._statuses.includes(response.status);
|
|---|
| 102 | }
|
|---|
| 103 |
|
|---|
| 104 | if (this._headers && cacheable) {
|
|---|
| 105 | cacheable = Object.keys(this._headers).some((headerName) => {
|
|---|
| 106 | return response.headers.get(headerName) === this._headers![headerName];
|
|---|
| 107 | });
|
|---|
| 108 | }
|
|---|
| 109 |
|
|---|
| 110 | if (process.env.NODE_ENV !== 'production') {
|
|---|
| 111 | if (!cacheable) {
|
|---|
| 112 | logger.groupCollapsed(
|
|---|
| 113 | `The request for ` +
|
|---|
| 114 | `'${getFriendlyURL(response.url)}' returned a response that does ` +
|
|---|
| 115 | `not meet the criteria for being cached.`,
|
|---|
| 116 | );
|
|---|
| 117 |
|
|---|
| 118 | logger.groupCollapsed(`View cacheability criteria here.`);
|
|---|
| 119 | logger.log(`Cacheable statuses: ` + JSON.stringify(this._statuses));
|
|---|
| 120 | logger.log(
|
|---|
| 121 | `Cacheable headers: ` + JSON.stringify(this._headers, null, 2),
|
|---|
| 122 | );
|
|---|
| 123 | logger.groupEnd();
|
|---|
| 124 |
|
|---|
| 125 | const logFriendlyHeaders: {[key: string]: string} = {};
|
|---|
| 126 | response.headers.forEach((value, key) => {
|
|---|
| 127 | logFriendlyHeaders[key] = value;
|
|---|
| 128 | });
|
|---|
| 129 |
|
|---|
| 130 | logger.groupCollapsed(`View response status and headers here.`);
|
|---|
| 131 | logger.log(`Response status: ${response.status}`);
|
|---|
| 132 | logger.log(
|
|---|
| 133 | `Response headers: ` + JSON.stringify(logFriendlyHeaders, null, 2),
|
|---|
| 134 | );
|
|---|
| 135 | logger.groupEnd();
|
|---|
| 136 |
|
|---|
| 137 | logger.groupCollapsed(`View full response details here.`);
|
|---|
| 138 | logger.log(response.headers);
|
|---|
| 139 | logger.log(response);
|
|---|
| 140 | logger.groupEnd();
|
|---|
| 141 |
|
|---|
| 142 | logger.groupEnd();
|
|---|
| 143 | }
|
|---|
| 144 | }
|
|---|
| 145 |
|
|---|
| 146 | return cacheable;
|
|---|
| 147 | }
|
|---|
| 148 | }
|
|---|
| 149 |
|
|---|
| 150 | export {CacheableResponse};
|
|---|