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 { getOrCreatePrecacheController } from './utils/getOrCreatePrecacheController.js';
|
9 | import './_version.js';
|
10 | /**
|
11 | * `PrecacheFallbackPlugin` allows you to specify an "offline fallback"
|
12 | * response to be used when a given strategy is unable to generate a response.
|
13 | *
|
14 | * It does this by intercepting the `handlerDidError` plugin callback
|
15 | * and returning a precached response, taking the expected revision parameter
|
16 | * into account automatically.
|
17 | *
|
18 | * Unless you explicitly pass in a `PrecacheController` instance to the
|
19 | * constructor, the default instance will be used. Generally speaking, most
|
20 | * developers will end up using the default.
|
21 | *
|
22 | * @memberof workbox-precaching
|
23 | */
|
24 | class PrecacheFallbackPlugin {
|
25 | /**
|
26 | * Constructs a new PrecacheFallbackPlugin with the associated fallbackURL.
|
27 | *
|
28 | * @param {Object} config
|
29 | * @param {string} config.fallbackURL A precached URL to use as the fallback
|
30 | * if the associated strategy can't generate a response.
|
31 | * @param {PrecacheController} [config.precacheController] An optional
|
32 | * PrecacheController instance. If not provided, the default
|
33 | * PrecacheController will be used.
|
34 | */
|
35 | constructor({ fallbackURL, precacheController, }) {
|
36 | /**
|
37 | * @return {Promise<Response>} The precache response for the fallback URL.
|
38 | *
|
39 | * @private
|
40 | */
|
41 | this.handlerDidError = () => this._precacheController.matchPrecache(this._fallbackURL);
|
42 | this._fallbackURL = fallbackURL;
|
43 | this._precacheController =
|
44 | precacheController || getOrCreatePrecacheController();
|
45 | }
|
46 | }
|
47 | export { PrecacheFallbackPlugin };
|