CheddarCrisp
·
2020-03-14
sw.js
1import {registerRoute,setDefaultHandler} from 'workbox-routing';
2import {CacheFirst, StaleWhileRevalidate, NetworkFirst} from 'workbox-strategies';
3import {CacheableResponsePlugin} from 'workbox-cacheable-response';
4import {ExpirationPlugin} from 'workbox-expiration';
5import {BroadcastUpdatePlugin} from 'workbox-broadcast-update';
6
7// Cache the Google Fonts stylesheets with a stale-while-revalidate strategy.
8registerRoute(
9 /^https:\/\/fonts\.googleapis\.com/,
10 new StaleWhileRevalidate({
11 cacheName: 'google-fonts-stylesheets',
12 })
13);
14
15// Cache the underlying font files with a cache-first strategy for 1 year.
16registerRoute(
17 /^https:\/\/fonts\.gstatic\.com/,
18 new CacheFirst({
19 cacheName: 'google-fonts-webfonts',
20 plugins: [
21 new CacheableResponsePlugin({
22 statuses: [0, 200],
23 }),
24 new ExpirationPlugin({
25 maxAgeSeconds: 60 * 60 * 24 * 365,
26 maxEntries: 30,
27 }),
28 ],
29 })
30);
31
32registerRoute(
33 /\.(?:js|css|png)$/,
34 new StaleWhileRevalidate({
35 cacheName: 'static-resources',
36 plugins: [
37 new BroadcastUpdatePlugin(),
38 ],
39 })
40 );
41
42setDefaultHandler(new NetworkFirst());