source: frontend/node_modules/workbox-recipes/src/pageCache.ts

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

Fix frontend appearance

  • Property mode set to 100644
File size: 2.2 KB
Line 
1/*
2 Copyright 2020 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*/
8import {warmStrategyCache} from './warmStrategyCache';
9import {registerRoute} from 'workbox-routing/registerRoute.js';
10import {NetworkFirst} from 'workbox-strategies/NetworkFirst.js';
11import {CacheableResponsePlugin} from 'workbox-cacheable-response/CacheableResponsePlugin.js';
12import {
13 RouteMatchCallback,
14 RouteMatchCallbackOptions,
15 WorkboxPlugin,
16} from 'workbox-core/types.js';
17
18import './_version.js';
19
20export interface PageCacheOptions {
21 cacheName?: string;
22 matchCallback?: RouteMatchCallback;
23 networkTimeoutSeconds?: number;
24 plugins?: Array<WorkboxPlugin>;
25 warmCache?: Array<string>;
26}
27
28/**
29 * An implementation of a page caching recipe with a network timeout
30 *
31 * @memberof workbox-recipes
32 *
33 * @param {Object} [options]
34 * @param {string} [options.cacheName] Name for cache. Defaults to pages
35 * @param {RouteMatchCallback} [options.matchCallback] Workbox callback function to call to match to. Defaults to request.mode === 'navigate';
36 * @param {number} [options.networkTimoutSeconds] Maximum amount of time, in seconds, to wait on the network before falling back to cache. Defaults to 3
37 * @param {WorkboxPlugin[]} [options.plugins] Additional plugins to use for this recipe
38 * @param {string[]} [options.warmCache] Paths to call to use to warm this cache
39 */
40function pageCache(options: PageCacheOptions = {}): void {
41 const defaultMatchCallback = ({request}: RouteMatchCallbackOptions) =>
42 request.mode === 'navigate';
43
44 const cacheName = options.cacheName || 'pages';
45 const matchCallback = options.matchCallback || defaultMatchCallback;
46 const networkTimeoutSeconds = options.networkTimeoutSeconds || 3;
47 const plugins = options.plugins || [];
48 plugins.push(
49 new CacheableResponsePlugin({
50 statuses: [0, 200],
51 }),
52 );
53
54 const strategy = new NetworkFirst({
55 networkTimeoutSeconds,
56 cacheName,
57 plugins,
58 });
59
60 // Registers the route
61 registerRoute(matchCallback, strategy);
62
63 // Warms the cache
64 if (options.warmCache) {
65 warmStrategyCache({urls: options.warmCache, strategy});
66 }
67}
68
69export {pageCache};
Note: See TracBrowser for help on using the repository browser.