123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- import { warmStrategyCache } from './warmStrategyCache';
- import { registerRoute } from 'workbox-routing/registerRoute.js';
- import { CacheFirst } from 'workbox-strategies/CacheFirst.js';
- import { CacheableResponsePlugin } from 'workbox-cacheable-response/CacheableResponsePlugin.js';
- import { ExpirationPlugin } from 'workbox-expiration/ExpirationPlugin.js';
- import './_version.js';
- function imageCache(options = {}) {
- const defaultMatchCallback = ({ request }) => request.destination === 'image';
- const cacheName = options.cacheName || 'images';
- const matchCallback = options.matchCallback || defaultMatchCallback;
- const maxAgeSeconds = options.maxAgeSeconds || 30 * 24 * 60 * 60;
- const maxEntries = options.maxEntries || 60;
- const plugins = options.plugins || [];
- plugins.push(new CacheableResponsePlugin({
- statuses: [0, 200],
- }));
- plugins.push(new ExpirationPlugin({
- maxEntries,
- maxAgeSeconds,
- }));
- const strategy = new CacheFirst({
- cacheName,
- plugins,
- });
- registerRoute(matchCallback, strategy);
-
- if (options.warmCache) {
- warmStrategyCache({ urls: options.warmCache, strategy });
- }
- }
- export { imageCache };
|