| 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 {logger} from 'workbox-core/_private/logger.js';
|
|---|
| 11 | import {WorkboxError} from 'workbox-core/_private/WorkboxError.js';
|
|---|
| 12 |
|
|---|
| 13 | import {Strategy} from './Strategy.js';
|
|---|
| 14 | import {StrategyHandler} from './StrategyHandler.js';
|
|---|
| 15 | import {messages} from './utils/messages.js';
|
|---|
| 16 | import './_version.js';
|
|---|
| 17 |
|
|---|
| 18 | /**
|
|---|
| 19 | * An implementation of a [cache-only](https://developer.chrome.com/docs/workbox/caching-strategies-overview/#cache-only)
|
|---|
| 20 | * request strategy.
|
|---|
| 21 | *
|
|---|
| 22 | * This class is useful if you want to take advantage of any
|
|---|
| 23 | * [Workbox plugins](https://developer.chrome.com/docs/workbox/using-plugins/).
|
|---|
| 24 | *
|
|---|
| 25 | * If there is no cache match, this will throw a `WorkboxError` exception.
|
|---|
| 26 | *
|
|---|
| 27 | * @extends workbox-strategies.Strategy
|
|---|
| 28 | * @memberof workbox-strategies
|
|---|
| 29 | */
|
|---|
| 30 | class CacheOnly extends Strategy {
|
|---|
| 31 | /**
|
|---|
| 32 | * @private
|
|---|
| 33 | * @param {Request|string} request A request to run this strategy for.
|
|---|
| 34 | * @param {workbox-strategies.StrategyHandler} handler The event that
|
|---|
| 35 | * triggered the request.
|
|---|
| 36 | * @return {Promise<Response>}
|
|---|
| 37 | */
|
|---|
| 38 | async _handle(request: Request, handler: StrategyHandler): Promise<Response> {
|
|---|
| 39 | if (process.env.NODE_ENV !== 'production') {
|
|---|
| 40 | assert!.isInstance(request, Request, {
|
|---|
| 41 | moduleName: 'workbox-strategies',
|
|---|
| 42 | className: this.constructor.name,
|
|---|
| 43 | funcName: 'makeRequest',
|
|---|
| 44 | paramName: 'request',
|
|---|
| 45 | });
|
|---|
| 46 | }
|
|---|
| 47 |
|
|---|
| 48 | const response = await handler.cacheMatch(request);
|
|---|
| 49 |
|
|---|
| 50 | if (process.env.NODE_ENV !== 'production') {
|
|---|
| 51 | logger.groupCollapsed(
|
|---|
| 52 | messages.strategyStart(this.constructor.name, request),
|
|---|
| 53 | );
|
|---|
| 54 | if (response) {
|
|---|
| 55 | logger.log(
|
|---|
| 56 | `Found a cached response in the '${this.cacheName}' ` + `cache.`,
|
|---|
| 57 | );
|
|---|
| 58 | messages.printFinalResponse(response);
|
|---|
| 59 | } else {
|
|---|
| 60 | logger.log(`No response found in the '${this.cacheName}' cache.`);
|
|---|
| 61 | }
|
|---|
| 62 | logger.groupEnd();
|
|---|
| 63 | }
|
|---|
| 64 |
|
|---|
| 65 | if (!response) {
|
|---|
| 66 | throw new WorkboxError('no-response', {url: request.url});
|
|---|
| 67 | }
|
|---|
| 68 | return response;
|
|---|
| 69 | }
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 | export {CacheOnly};
|
|---|