source: frontend/node_modules/workbox-recipes/src/googleFontsCache.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 {registerRoute} from 'workbox-routing/registerRoute.js';
9import {StaleWhileRevalidate} from 'workbox-strategies/StaleWhileRevalidate.js';
10import {CacheFirst} from 'workbox-strategies/CacheFirst.js';
11import {CacheableResponsePlugin} from 'workbox-cacheable-response/CacheableResponsePlugin.js';
12import {ExpirationPlugin} from 'workbox-expiration/ExpirationPlugin.js';
13
14import './_version.js';
15
16export 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 */
32function 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
64export {googleFontsCache};
Note: See TracBrowser for help on using the repository browser.