| [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 { setCatchHandler } from 'workbox-routing/setCatchHandler.js';
|
|---|
| 9 | import { matchPrecache } from 'workbox-precaching/matchPrecache.js';
|
|---|
| 10 | import './_version.js';
|
|---|
| 11 | /**
|
|---|
| 12 | * 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
|
|---|
| 13 | *
|
|---|
| 14 | * @memberof workbox-recipes
|
|---|
| 15 | *
|
|---|
| 16 | * @param {Object} [options]
|
|---|
| 17 | * @param {string} [options.pageFallback] Precache name to match for pag fallbacks. Defaults to offline.html
|
|---|
| 18 | * @param {string} [options.imageFallback] Precache name to match for image fallbacks.
|
|---|
| 19 | * @param {string} [options.fontFallback] Precache name to match for font fallbacks.
|
|---|
| 20 | */
|
|---|
| 21 | function offlineFallback(options = {}) {
|
|---|
| 22 | const pageFallback = options.pageFallback || 'offline.html';
|
|---|
| 23 | const imageFallback = options.imageFallback || false;
|
|---|
| 24 | const fontFallback = options.fontFallback || false;
|
|---|
| 25 | self.addEventListener('install', (event) => {
|
|---|
| 26 | const files = [pageFallback];
|
|---|
| 27 | if (imageFallback) {
|
|---|
| 28 | files.push(imageFallback);
|
|---|
| 29 | }
|
|---|
| 30 | if (fontFallback) {
|
|---|
| 31 | files.push(fontFallback);
|
|---|
| 32 | }
|
|---|
| 33 | event.waitUntil(self.caches
|
|---|
| 34 | .open('workbox-offline-fallbacks')
|
|---|
| 35 | .then((cache) => cache.addAll(files)));
|
|---|
| 36 | });
|
|---|
| 37 | const handler = async (options) => {
|
|---|
| 38 | const dest = options.request.destination;
|
|---|
| 39 | const cache = await self.caches.open('workbox-offline-fallbacks');
|
|---|
| 40 | if (dest === 'document') {
|
|---|
| 41 | const match = (await matchPrecache(pageFallback)) ||
|
|---|
| 42 | (await cache.match(pageFallback));
|
|---|
| 43 | return match || Response.error();
|
|---|
| 44 | }
|
|---|
| 45 | if (dest === 'image' && imageFallback !== false) {
|
|---|
| 46 | const match = (await matchPrecache(imageFallback)) ||
|
|---|
| 47 | (await cache.match(imageFallback));
|
|---|
| 48 | return match || Response.error();
|
|---|
| 49 | }
|
|---|
| 50 | if (dest === 'font' && fontFallback !== false) {
|
|---|
| 51 | const match = (await matchPrecache(fontFallback)) ||
|
|---|
| 52 | (await cache.match(fontFallback));
|
|---|
| 53 | return match || Response.error();
|
|---|
| 54 | }
|
|---|
| 55 | return Response.error();
|
|---|
| 56 | };
|
|---|
| 57 | setCatchHandler(handler);
|
|---|
| 58 | }
|
|---|
| 59 | export { offlineFallback };
|
|---|