| [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 {registerRoute} from 'workbox-routing/registerRoute.js';
|
|---|
| 9 | import {StaleWhileRevalidate} from 'workbox-strategies/StaleWhileRevalidate.js';
|
|---|
| 10 | import {CacheFirst} from 'workbox-strategies/CacheFirst.js';
|
|---|
| 11 | import {CacheableResponsePlugin} from 'workbox-cacheable-response/CacheableResponsePlugin.js';
|
|---|
| 12 | import {ExpirationPlugin} from 'workbox-expiration/ExpirationPlugin.js';
|
|---|
| 13 |
|
|---|
| 14 | import './_version.js';
|
|---|
| 15 |
|
|---|
| 16 | export interface GoogleFontCacheOptions {
|
|---|
| 17 | cachePrefix?: string;
|
|---|
| 18 | maxAgeSeconds?: number;
|
|---|
| 19 | maxEntries?: number;
|
|---|
| 20 | }
|
|---|
| 21 |
|
|---|
| 22 | /**
|
|---|
| 23 | * An implementation of the [Google fonts]{@link https://developers.google.com/web/tools/workbox/guides/common-recipes#google_fonts} caching recipe
|
|---|
| 24 | *
|
|---|
| 25 | * @memberof workbox-recipes
|
|---|
| 26 | *
|
|---|
| 27 | * @param {Object} [options]
|
|---|
| 28 | * @param {string} [options.cachePrefix] Cache prefix for caching stylesheets and webfonts. Defaults to google-fonts
|
|---|
| 29 | * @param {number} [options.maxAgeSeconds] Maximum age, in seconds, that font entries will be cached for. Defaults to 1 year
|
|---|
| 30 | * @param {number} [options.maxEntries] Maximum number of fonts that will be cached. Defaults to 30
|
|---|
| 31 | */
|
|---|
| 32 | function googleFontsCache(options: GoogleFontCacheOptions = {}): void {
|
|---|
| 33 | const sheetCacheName = `${options.cachePrefix || 'google-fonts'}-stylesheets`;
|
|---|
| 34 | const fontCacheName = `${options.cachePrefix || 'google-fonts'}-webfonts`;
|
|---|
| 35 | const maxAgeSeconds = options.maxAgeSeconds || 60 * 60 * 24 * 365;
|
|---|
| 36 | const maxEntries = options.maxEntries || 30;
|
|---|
| 37 |
|
|---|
| 38 | // Cache the Google Fonts stylesheets with a stale-while-revalidate strategy.
|
|---|
| 39 | registerRoute(
|
|---|
| 40 | ({url}) => url.origin === 'https://fonts.googleapis.com',
|
|---|
| 41 | new StaleWhileRevalidate({
|
|---|
| 42 | cacheName: sheetCacheName,
|
|---|
| 43 | }),
|
|---|
| 44 | );
|
|---|
| 45 |
|
|---|
| 46 | // Cache the underlying font files with a cache-first strategy for 1 year.
|
|---|
| 47 | registerRoute(
|
|---|
| 48 | ({url}) => url.origin === 'https://fonts.gstatic.com',
|
|---|
| 49 | new CacheFirst({
|
|---|
| 50 | cacheName: fontCacheName,
|
|---|
| 51 | plugins: [
|
|---|
| 52 | new CacheableResponsePlugin({
|
|---|
| 53 | statuses: [0, 200],
|
|---|
| 54 | }),
|
|---|
| 55 | new ExpirationPlugin({
|
|---|
| 56 | maxAgeSeconds,
|
|---|
| 57 | maxEntries,
|
|---|
| 58 | }),
|
|---|
| 59 | ],
|
|---|
| 60 | }),
|
|---|
| 61 | );
|
|---|
| 62 | }
|
|---|
| 63 |
|
|---|
| 64 | export {googleFontsCache};
|
|---|