source: frontend/node_modules/workbox-recipes/src/offlineFallback.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.7 KB
RevLine 
[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*/
8import {setCatchHandler} from 'workbox-routing/setCatchHandler.js';
9import {matchPrecache} from 'workbox-precaching/matchPrecache.js';
10import {RouteHandler, RouteHandlerCallbackOptions} from 'workbox-core/types.js';
11
12import './_version.js';
13
14export interface OfflineFallbackOptions {
15 pageFallback?: string;
16 imageFallback?: string;
17 fontFallback?: string;
18}
19
20// Give TypeScript the correct global.
21declare let self: ServiceWorkerGlobalScope;
22
23/**
24 * 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
25 *
26 * @memberof workbox-recipes
27 *
28 * @param {Object} [options]
29 * @param {string} [options.pageFallback] Precache name to match for pag fallbacks. Defaults to offline.html
30 * @param {string} [options.imageFallback] Precache name to match for image fallbacks.
31 * @param {string} [options.fontFallback] Precache name to match for font fallbacks.
32 */
33function offlineFallback(options: OfflineFallbackOptions = {}): void {
34 const pageFallback = options.pageFallback || 'offline.html';
35 const imageFallback = options.imageFallback || false;
36 const fontFallback = options.fontFallback || false;
37
38 self.addEventListener('install', (event) => {
39 const files = [pageFallback];
40 if (imageFallback) {
41 files.push(imageFallback);
42 }
43 if (fontFallback) {
44 files.push(fontFallback);
45 }
46
47 event.waitUntil(
48 self.caches
49 .open('workbox-offline-fallbacks')
50 .then((cache) => cache.addAll(files)),
51 );
52 });
53
54 const handler: RouteHandler = async (
55 options: RouteHandlerCallbackOptions,
56 ) => {
57 const dest = options.request.destination;
58 const cache = await self.caches.open('workbox-offline-fallbacks');
59
60 if (dest === 'document') {
61 const match =
62 (await matchPrecache(pageFallback)) ||
63 (await cache.match(pageFallback));
64 return match || Response.error();
65 }
66
67 if (dest === 'image' && imageFallback !== false) {
68 const match =
69 (await matchPrecache(imageFallback)) ||
70 (await cache.match(imageFallback));
71 return match || Response.error();
72 }
73
74 if (dest === 'font' && fontFallback !== false) {
75 const match =
76 (await matchPrecache(fontFallback)) ||
77 (await cache.match(fontFallback));
78 return match || Response.error();
79 }
80
81 return Response.error();
82 };
83
84 setCatchHandler(handler);
85}
86
87export {offlineFallback};
Note: See TracBrowser for help on using the repository browser.