| 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 | import { CacheableResponse, } from './CacheableResponse.js';
|
|---|
| 9 | import './_version.js';
|
|---|
| 10 | /**
|
|---|
| 11 | * A class implementing the `cacheWillUpdate` lifecycle callback. This makes it
|
|---|
| 12 | * easier to add in cacheability checks to requests made via Workbox's built-in
|
|---|
| 13 | * strategies.
|
|---|
| 14 | *
|
|---|
| 15 | * @memberof workbox-cacheable-response
|
|---|
| 16 | */
|
|---|
| 17 | class CacheableResponsePlugin {
|
|---|
| 18 | /**
|
|---|
| 19 | * To construct a new CacheableResponsePlugin instance you must provide at
|
|---|
| 20 | * least one of the `config` properties.
|
|---|
| 21 | *
|
|---|
| 22 | * If both `statuses` and `headers` are specified, then both conditions must
|
|---|
| 23 | * be met for the `Response` to be considered cacheable.
|
|---|
| 24 | *
|
|---|
| 25 | * @param {Object} config
|
|---|
| 26 | * @param {Array<number>} [config.statuses] One or more status codes that a
|
|---|
| 27 | * `Response` can have and be considered cacheable.
|
|---|
| 28 | * @param {Object<string,string>} [config.headers] A mapping of header names
|
|---|
| 29 | * and expected values that a `Response` can have and be considered cacheable.
|
|---|
| 30 | * If multiple headers are provided, only one needs to be present.
|
|---|
| 31 | */
|
|---|
| 32 | constructor(config) {
|
|---|
| 33 | /**
|
|---|
| 34 | * @param {Object} options
|
|---|
| 35 | * @param {Response} options.response
|
|---|
| 36 | * @return {Response|null}
|
|---|
| 37 | * @private
|
|---|
| 38 | */
|
|---|
| 39 | this.cacheWillUpdate = async ({ response }) => {
|
|---|
| 40 | if (this._cacheableResponse.isResponseCacheable(response)) {
|
|---|
| 41 | return response;
|
|---|
| 42 | }
|
|---|
| 43 | return null;
|
|---|
| 44 | };
|
|---|
| 45 | this._cacheableResponse = new CacheableResponse(config);
|
|---|
| 46 | }
|
|---|
| 47 | }
|
|---|
| 48 | export { CacheableResponsePlugin };
|
|---|