| [9af201e] | 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 | */
|
|---|
| 8 | import {warmStrategyCache} from './warmStrategyCache';
|
|---|
| 9 | import {registerRoute} from 'workbox-routing/registerRoute.js';
|
|---|
| 10 | import {StaleWhileRevalidate} from 'workbox-strategies/StaleWhileRevalidate.js';
|
|---|
| 11 | import {CacheableResponsePlugin} from 'workbox-cacheable-response/CacheableResponsePlugin.js';
|
|---|
| 12 | import {
|
|---|
| 13 | RouteMatchCallback,
|
|---|
| 14 | RouteMatchCallbackOptions,
|
|---|
| 15 | WorkboxPlugin,
|
|---|
| 16 | } from 'workbox-core/types.js';
|
|---|
| 17 |
|
|---|
| 18 | import './_version.js';
|
|---|
| 19 |
|
|---|
| 20 | export interface StaticResourceOptions {
|
|---|
| 21 | cacheName?: string;
|
|---|
| 22 | matchCallback?: RouteMatchCallback;
|
|---|
| 23 | plugins?: Array<WorkboxPlugin>;
|
|---|
| 24 | warmCache?: Array<string>;
|
|---|
| 25 | }
|
|---|
| 26 |
|
|---|
| 27 | /**
|
|---|
| 28 | * An implementation of the [CSS and JavaScript files recipe]{@link https://developers.google.com/web/tools/workbox/guides/common-recipes#cache_css_and_javascript_files}
|
|---|
| 29 | *
|
|---|
| 30 | * @memberof workbox-recipes
|
|---|
| 31 | *
|
|---|
| 32 | * @param {Object} [options]
|
|---|
| 33 | * @param {string} [options.cacheName] Name for cache. Defaults to static-resources
|
|---|
| 34 | * @param {RouteMatchCallback} [options.matchCallback] Workbox callback function to call to match to. Defaults to request.destination === 'style' || request.destination === 'script' || request.destination === 'worker';
|
|---|
| 35 | * @param {WorkboxPlugin[]} [options.plugins] Additional plugins to use for this recipe
|
|---|
| 36 | * @param {string[]} [options.warmCache] Paths to call to use to warm this cache
|
|---|
| 37 | */
|
|---|
| 38 | function staticResourceCache(options: StaticResourceOptions = {}): void {
|
|---|
| 39 | const defaultMatchCallback = ({request}: RouteMatchCallbackOptions) =>
|
|---|
| 40 | request.destination === 'style' ||
|
|---|
| 41 | request.destination === 'script' ||
|
|---|
| 42 | request.destination === 'worker';
|
|---|
| 43 |
|
|---|
| 44 | const cacheName = options.cacheName || 'static-resources';
|
|---|
| 45 | const matchCallback = options.matchCallback || defaultMatchCallback;
|
|---|
| 46 | const plugins = options.plugins || [];
|
|---|
| 47 | plugins.push(
|
|---|
| 48 | new CacheableResponsePlugin({
|
|---|
| 49 | statuses: [0, 200],
|
|---|
| 50 | }),
|
|---|
| 51 | );
|
|---|
| 52 |
|
|---|
| 53 | const strategy = new StaleWhileRevalidate({
|
|---|
| 54 | cacheName,
|
|---|
| 55 | plugins,
|
|---|
| 56 | });
|
|---|
| 57 |
|
|---|
| 58 | registerRoute(matchCallback, strategy);
|
|---|
| 59 |
|
|---|
| 60 | // Warms the cache
|
|---|
| 61 | if (options.warmCache) {
|
|---|
| 62 | warmStrategyCache({urls: options.warmCache, strategy});
|
|---|
| 63 | }
|
|---|
| 64 | }
|
|---|
| 65 |
|
|---|
| 66 | export {staticResourceCache};
|
|---|