workbox-recipes.dev.js
11.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
this.workbox = this.workbox || {};
this.workbox.recipes = (function (exports, registerRoute_js, StaleWhileRevalidate_js, CacheFirst_js, CacheableResponsePlugin_js, ExpirationPlugin_js, NetworkFirst_js, setCatchHandler_js, matchPrecache_js) {
'use strict';
try {
self['workbox:recipes:6.5.3'] && _();
} catch (e) {}
/*
Copyright 2020 Google LLC
Use of this source code is governed by an MIT-style
license that can be found in the LICENSE file or at
https://opensource.org/licenses/MIT.
*/
/**
* An implementation of the [Google fonts]{@link https://developers.google.com/web/tools/workbox/guides/common-recipes#google_fonts} caching recipe
*
* @memberof workbox-recipes
*
* @param {Object} [options]
* @param {string} [options.cachePrefix] Cache prefix for caching stylesheets and webfonts. Defaults to google-fonts
* @param {number} [options.maxAgeSeconds] Maximum age, in seconds, that font entries will be cached for. Defaults to 1 year
* @param {number} [options.maxEntries] Maximum number of fonts that will be cached. Defaults to 30
*/
function googleFontsCache(options = {}) {
const sheetCacheName = `${options.cachePrefix || 'google-fonts'}-stylesheets`;
const fontCacheName = `${options.cachePrefix || 'google-fonts'}-webfonts`;
const maxAgeSeconds = options.maxAgeSeconds || 60 * 60 * 24 * 365;
const maxEntries = options.maxEntries || 30; // Cache the Google Fonts stylesheets with a stale-while-revalidate strategy.
registerRoute_js.registerRoute(({
url
}) => url.origin === 'https://fonts.googleapis.com', new StaleWhileRevalidate_js.StaleWhileRevalidate({
cacheName: sheetCacheName
})); // Cache the underlying font files with a cache-first strategy for 1 year.
registerRoute_js.registerRoute(({
url
}) => url.origin === 'https://fonts.gstatic.com', new CacheFirst_js.CacheFirst({
cacheName: fontCacheName,
plugins: [new CacheableResponsePlugin_js.CacheableResponsePlugin({
statuses: [0, 200]
}), new ExpirationPlugin_js.ExpirationPlugin({
maxAgeSeconds,
maxEntries
})]
}));
}
/**
* @memberof workbox-recipes
* @param {Object} options
* @param {string[]} options.urls Paths to warm the strategy's cache with
* @param {Strategy} options.strategy Strategy to use
*/
function warmStrategyCache(options) {
self.addEventListener('install', event => {
const done = options.urls.map(path => options.strategy.handleAll({
event,
request: new Request(path)
})[1]);
event.waitUntil(Promise.all(done));
});
}
/*
Copyright 2020 Google LLC
Use of this source code is governed by an MIT-style
license that can be found in the LICENSE file or at
https://opensource.org/licenses/MIT.
*/
/**
* An implementation of the [image caching recipe]{@link https://developers.google.com/web/tools/workbox/guides/common-recipes#caching_images}
*
* @memberof workbox-recipes
*
* @param {Object} [options]
* @param {string} [options.cacheName] Name for cache. Defaults to images
* @param {RouteMatchCallback} [options.matchCallback] Workbox callback function to call to match to. Defaults to request.destination === 'image';
* @param {number} [options.maxAgeSeconds] Maximum age, in seconds, that font entries will be cached for. Defaults to 30 days
* @param {number} [options.maxEntries] Maximum number of images that will be cached. Defaults to 60
* @param {WorkboxPlugin[]} [options.plugins] Additional plugins to use for this recipe
* @param {string[]} [options.warmCache] Paths to call to use to warm this cache
*/
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_js.CacheableResponsePlugin({
statuses: [0, 200]
}));
plugins.push(new ExpirationPlugin_js.ExpirationPlugin({
maxEntries,
maxAgeSeconds
}));
const strategy = new CacheFirst_js.CacheFirst({
cacheName,
plugins
});
registerRoute_js.registerRoute(matchCallback, strategy); // Warms the cache
if (options.warmCache) {
warmStrategyCache({
urls: options.warmCache,
strategy
});
}
}
/*
Copyright 2020 Google LLC
Use of this source code is governed by an MIT-style
license that can be found in the LICENSE file or at
https://opensource.org/licenses/MIT.
*/
/**
* 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}
*
* @memberof workbox-recipes
*
* @param {Object} [options]
* @param {string} [options.cacheName] Name for cache. Defaults to static-resources
* @param {RouteMatchCallback} [options.matchCallback] Workbox callback function to call to match to. Defaults to request.destination === 'style' || request.destination === 'script' || request.destination === 'worker';
* @param {WorkboxPlugin[]} [options.plugins] Additional plugins to use for this recipe
* @param {string[]} [options.warmCache] Paths to call to use to warm this cache
*/
function staticResourceCache(options = {}) {
const defaultMatchCallback = ({
request
}) => request.destination === 'style' || request.destination === 'script' || request.destination === 'worker';
const cacheName = options.cacheName || 'static-resources';
const matchCallback = options.matchCallback || defaultMatchCallback;
const plugins = options.plugins || [];
plugins.push(new CacheableResponsePlugin_js.CacheableResponsePlugin({
statuses: [0, 200]
}));
const strategy = new StaleWhileRevalidate_js.StaleWhileRevalidate({
cacheName,
plugins
});
registerRoute_js.registerRoute(matchCallback, strategy); // Warms the cache
if (options.warmCache) {
warmStrategyCache({
urls: options.warmCache,
strategy
});
}
}
/*
Copyright 2020 Google LLC
Use of this source code is governed by an MIT-style
license that can be found in the LICENSE file or at
https://opensource.org/licenses/MIT.
*/
/**
* An implementation of a page caching recipe with a network timeout
*
* @memberof workbox-recipes
*
* @param {Object} [options]
* @param {string} [options.cacheName] Name for cache. Defaults to pages
* @param {RouteMatchCallback} [options.matchCallback] Workbox callback function to call to match to. Defaults to request.mode === 'navigate';
* @param {number} [options.networkTimoutSeconds] Maximum amount of time, in seconds, to wait on the network before falling back to cache. Defaults to 3
* @param {WorkboxPlugin[]} [options.plugins] Additional plugins to use for this recipe
* @param {string[]} [options.warmCache] Paths to call to use to warm this cache
*/
function pageCache(options = {}) {
const defaultMatchCallback = ({
request
}) => request.mode === 'navigate';
const cacheName = options.cacheName || 'pages';
const matchCallback = options.matchCallback || defaultMatchCallback;
const networkTimeoutSeconds = options.networkTimeoutSeconds || 3;
const plugins = options.plugins || [];
plugins.push(new CacheableResponsePlugin_js.CacheableResponsePlugin({
statuses: [0, 200]
}));
const strategy = new NetworkFirst_js.NetworkFirst({
networkTimeoutSeconds,
cacheName,
plugins
}); // Registers the route
registerRoute_js.registerRoute(matchCallback, strategy); // Warms the cache
if (options.warmCache) {
warmStrategyCache({
urls: options.warmCache,
strategy
});
}
}
/*
Copyright 2020 Google LLC
Use of this source code is governed by an MIT-style
license that can be found in the LICENSE file or at
https://opensource.org/licenses/MIT.
*/
/**
* An implementation of the [comprehensive fallbacks recipe]{@link https://developers.google.com/web/tools/workbox/guides/advanced-recipes#comprehensive_fallbacks}. Be sure to include the fallbacks in your precache injection
*
* @memberof workbox-recipes
*
* @param {Object} [options]
* @param {string} [options.pageFallback] Precache name to match for pag fallbacks. Defaults to offline.html
* @param {string} [options.imageFallback] Precache name to match for image fallbacks.
* @param {string} [options.fontFallback] Precache name to match for font fallbacks.
*/
function offlineFallback(options = {}) {
const pageFallback = options.pageFallback || 'offline.html';
const imageFallback = options.imageFallback || false;
const fontFallback = options.fontFallback || false;
self.addEventListener('install', event => {
const files = [pageFallback];
if (imageFallback) {
files.push(imageFallback);
}
if (fontFallback) {
files.push(fontFallback);
}
event.waitUntil(self.caches.open('workbox-offline-fallbacks').then(cache => cache.addAll(files)));
});
const handler = async options => {
const dest = options.request.destination;
const cache = await self.caches.open('workbox-offline-fallbacks');
if (dest === 'document') {
const match = (await matchPrecache_js.matchPrecache(pageFallback)) || (await cache.match(pageFallback));
return match || Response.error();
}
if (dest === 'image' && imageFallback !== false) {
const match = (await matchPrecache_js.matchPrecache(imageFallback)) || (await cache.match(imageFallback));
return match || Response.error();
}
if (dest === 'font' && fontFallback !== false) {
const match = (await matchPrecache_js.matchPrecache(fontFallback)) || (await cache.match(fontFallback));
return match || Response.error();
}
return Response.error();
};
setCatchHandler_js.setCatchHandler(handler);
}
exports.googleFontsCache = googleFontsCache;
exports.imageCache = imageCache;
exports.offlineFallback = offlineFallback;
exports.pageCache = pageCache;
exports.staticResourceCache = staticResourceCache;
exports.warmStrategyCache = warmStrategyCache;
return exports;
}({}, workbox.routing, workbox.strategies, workbox.strategies, workbox.cacheableResponse, workbox.expiration, workbox.strategies, workbox.routing, workbox.precaching));
//# sourceMappingURL=workbox-recipes.dev.js.map