source: frontend/node_modules/workbox-strategies/src/CacheFirst.ts

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

Fix frontend appearance

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