| 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 { assert } from 'workbox-core/_private/assert.js';
|
|---|
| 9 | import { logger } from 'workbox-core/_private/logger.js';
|
|---|
| 10 | import { WorkboxError } from 'workbox-core/_private/WorkboxError.js';
|
|---|
| 11 | import { Strategy } from './Strategy.js';
|
|---|
| 12 | import { messages } from './utils/messages.js';
|
|---|
| 13 | import './_version.js';
|
|---|
| 14 | /**
|
|---|
| 15 | * An implementation of a [cache-first](https://developer.chrome.com/docs/workbox/caching-strategies-overview/#cache-first-falling-back-to-network)
|
|---|
| 16 | * request strategy.
|
|---|
| 17 | *
|
|---|
| 18 | * A cache first strategy is useful for assets that have been revisioned,
|
|---|
| 19 | * such as URLs like `/styles/example.a8f5f1.css`, since they
|
|---|
| 20 | * can be cached for long periods of time.
|
|---|
| 21 | *
|
|---|
| 22 | * If the network request fails, and there is no cache match, this will throw
|
|---|
| 23 | * a `WorkboxError` exception.
|
|---|
| 24 | *
|
|---|
| 25 | * @extends workbox-strategies.Strategy
|
|---|
| 26 | * @memberof workbox-strategies
|
|---|
| 27 | */
|
|---|
| 28 | class CacheFirst extends Strategy {
|
|---|
| 29 | /**
|
|---|
| 30 | * @private
|
|---|
| 31 | * @param {Request|string} request A request to run this strategy for.
|
|---|
| 32 | * @param {workbox-strategies.StrategyHandler} handler The event that
|
|---|
| 33 | * triggered the request.
|
|---|
| 34 | * @return {Promise<Response>}
|
|---|
| 35 | */
|
|---|
| 36 | async _handle(request, handler) {
|
|---|
| 37 | const logs = [];
|
|---|
| 38 | if (process.env.NODE_ENV !== 'production') {
|
|---|
| 39 | assert.isInstance(request, Request, {
|
|---|
| 40 | moduleName: 'workbox-strategies',
|
|---|
| 41 | className: this.constructor.name,
|
|---|
| 42 | funcName: 'makeRequest',
|
|---|
| 43 | paramName: 'request',
|
|---|
| 44 | });
|
|---|
| 45 | }
|
|---|
| 46 | let response = await handler.cacheMatch(request);
|
|---|
| 47 | let error = undefined;
|
|---|
| 48 | if (!response) {
|
|---|
| 49 | if (process.env.NODE_ENV !== 'production') {
|
|---|
| 50 | logs.push(`No response found in the '${this.cacheName}' cache. ` +
|
|---|
| 51 | `Will respond with a network request.`);
|
|---|
| 52 | }
|
|---|
| 53 | try {
|
|---|
| 54 | response = await handler.fetchAndCachePut(request);
|
|---|
| 55 | }
|
|---|
| 56 | catch (err) {
|
|---|
| 57 | if (err instanceof Error) {
|
|---|
| 58 | error = err;
|
|---|
| 59 | }
|
|---|
| 60 | }
|
|---|
| 61 | if (process.env.NODE_ENV !== 'production') {
|
|---|
| 62 | if (response) {
|
|---|
| 63 | logs.push(`Got response from network.`);
|
|---|
| 64 | }
|
|---|
| 65 | else {
|
|---|
| 66 | logs.push(`Unable to get a response from the network.`);
|
|---|
| 67 | }
|
|---|
| 68 | }
|
|---|
| 69 | }
|
|---|
| 70 | else {
|
|---|
| 71 | if (process.env.NODE_ENV !== 'production') {
|
|---|
| 72 | logs.push(`Found a cached response in the '${this.cacheName}' cache.`);
|
|---|
| 73 | }
|
|---|
| 74 | }
|
|---|
| 75 | if (process.env.NODE_ENV !== 'production') {
|
|---|
| 76 | logger.groupCollapsed(messages.strategyStart(this.constructor.name, request));
|
|---|
| 77 | for (const log of logs) {
|
|---|
| 78 | logger.log(log);
|
|---|
| 79 | }
|
|---|
| 80 | messages.printFinalResponse(response);
|
|---|
| 81 | logger.groupEnd();
|
|---|
| 82 | }
|
|---|
| 83 | if (!response) {
|
|---|
| 84 | throw new WorkboxError('no-response', { url: request.url, error });
|
|---|
| 85 | }
|
|---|
| 86 | return response;
|
|---|
| 87 | }
|
|---|
| 88 | }
|
|---|
| 89 | export { CacheFirst };
|
|---|