@serwist/precaching 9.0.0-preview.2 → 9.0.0-preview.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,21 +1,31 @@
1
- import type { SerwistPlugin } from "@serwist/core";
1
+ import type { HandlerDidErrorCallbackParam, SerwistPlugin } from "@serwist/core";
2
2
  import type { PrecacheController } from "./PrecacheController.js";
3
- interface PrecacheFallbackPluginOptions {
3
+ export interface PrecacheFallbackEntry {
4
4
  /**
5
- * A precached URL to use as the fallback
5
+ * A function that checks whether the fallback entry can be used
6
+ * for a request.
7
+ */
8
+ matcher: (param: HandlerDidErrorCallbackParam) => boolean;
9
+ /**
10
+ * A precached URL to be used as a fallback.
11
+ */
12
+ url: string;
13
+ }
14
+ export interface PrecacheFallbackPluginOptions {
15
+ /**
16
+ * Precached URLs to use as the fallback
6
17
  * if the associated strategy can't generate a response.
7
18
  */
8
- fallbackURL: string;
19
+ fallbackUrls: (string | PrecacheFallbackEntry)[];
9
20
  /**
10
- * An optional
11
- * PrecacheController instance. If not provided, the default
12
- * PrecacheController will be used.
21
+ * An optional `PrecacheController` instance. If not provided, the default
22
+ * `PrecacheController` will be used.
13
23
  */
14
24
  precacheController?: PrecacheController;
15
25
  }
16
26
  /**
17
- * `PrecacheFallbackPlugin` allows you to specify an "offline fallback"
18
- * response to be used when a given strategy is unable to generate a response.
27
+ * `PrecacheFallbackPlugin` allows you to specify offline fallbacks
28
+ * to be used when a given strategy is unable to generate a response.
19
29
  *
20
30
  * It does this by intercepting the `handlerDidError` plugin callback
21
31
  * and returning a precached response, taking the expected revision parameter
@@ -25,20 +35,20 @@ interface PrecacheFallbackPluginOptions {
25
35
  * constructor, the default instance will be used. Generally speaking, most
26
36
  * developers will end up using the default.
27
37
  */
28
- declare class PrecacheFallbackPlugin implements SerwistPlugin {
29
- private readonly _fallbackURL;
38
+ export declare class PrecacheFallbackPlugin implements SerwistPlugin {
39
+ private readonly _fallbackUrls;
30
40
  private readonly _precacheController;
31
41
  /**
32
- * Constructs a new PrecacheFallbackPlugin with the associated fallbackURL.
42
+ * Constructs a new `PrecacheFallbackPlugin` with the associated `fallbackUrls`.
33
43
  *
34
44
  * @param config
35
45
  */
36
- constructor({ fallbackURL, precacheController }: PrecacheFallbackPluginOptions);
46
+ constructor({ fallbackUrls, precacheController }: PrecacheFallbackPluginOptions);
37
47
  /**
38
- * @returns The precache response for the fallback URL.
48
+ * @returns The precache response for one of the fallback URLs, or `undefined` if
49
+ * nothing satisfies the conditions.
39
50
  * @private
40
51
  */
41
- handlerDidError: SerwistPlugin["handlerDidError"];
52
+ handlerDidError(param: HandlerDidErrorCallbackParam): Promise<Response | undefined>;
42
53
  }
43
- export { PrecacheFallbackPlugin };
44
54
  //# sourceMappingURL=PrecacheFallbackPlugin.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"PrecacheFallbackPlugin.d.ts","sourceRoot":"","sources":["../src/PrecacheFallbackPlugin.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC;AAEnD,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,yBAAyB,CAAC;AAGlE,UAAU,6BAA6B;IACrC;;;OAGG;IACH,WAAW,EAAE,MAAM,CAAC;IACpB;;;;OAIG;IACH,kBAAkB,CAAC,EAAE,kBAAkB,CAAC;CACzC;AAED;;;;;;;;;;;GAWG;AACH,cAAM,sBAAuB,YAAW,aAAa;IACnD,OAAO,CAAC,QAAQ,CAAC,YAAY,CAAS;IACtC,OAAO,CAAC,QAAQ,CAAC,mBAAmB,CAAqB;IAEzD;;;;OAIG;gBACS,EAAE,WAAW,EAAE,kBAAkB,EAAE,EAAE,6BAA6B;IAK9E;;;OAGG;IACH,eAAe,EAAE,aAAa,CAAC,iBAAiB,CAAC,CAAmE;CACrH;AAED,OAAO,EAAE,sBAAsB,EAAE,CAAC"}
1
+ {"version":3,"file":"PrecacheFallbackPlugin.d.ts","sourceRoot":"","sources":["../src/PrecacheFallbackPlugin.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAAE,4BAA4B,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC;AAEjF,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,yBAAyB,CAAC;AAGlE,MAAM,WAAW,qBAAqB;IACpC;;;OAGG;IACH,OAAO,EAAE,CAAC,KAAK,EAAE,4BAA4B,KAAK,OAAO,CAAC;IAC1D;;OAEG;IACH,GAAG,EAAE,MAAM,CAAC;CACb;AAED,MAAM,WAAW,6BAA6B;IAC5C;;;OAGG;IACH,YAAY,EAAE,CAAC,MAAM,GAAG,qBAAqB,CAAC,EAAE,CAAC;IACjD;;;OAGG;IACH,kBAAkB,CAAC,EAAE,kBAAkB,CAAC;CACzC;AAED;;;;;;;;;;;GAWG;AACH,qBAAa,sBAAuB,YAAW,aAAa;IAC1D,OAAO,CAAC,QAAQ,CAAC,aAAa,CAAqC;IACnE,OAAO,CAAC,QAAQ,CAAC,mBAAmB,CAAqB;IAEzD;;;;OAIG;gBACS,EAAE,YAAY,EAAE,kBAAkB,EAAE,EAAE,6BAA6B;IAK/E;;;;OAIG;IACG,eAAe,CAAC,KAAK,EAAE,4BAA4B;CAgB1D"}
package/dist/index.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import { PrecacheController } from "./PrecacheController.js";
2
2
  import { PrecacheFallbackPlugin } from "./PrecacheFallbackPlugin.js";
3
+ import type { PrecacheFallbackEntry, PrecacheFallbackPluginOptions } from "./PrecacheFallbackPlugin.js";
3
4
  import { PrecacheRoute } from "./PrecacheRoute.js";
4
5
  import { PrecacheStrategy } from "./PrecacheStrategy.js";
5
6
  import { addPlugins } from "./addPlugins.js";
@@ -21,5 +22,6 @@ import { precacheAndRoute } from "./precacheAndRoute.js";
21
22
  * interface.
22
23
  */
23
24
  export { addPlugins, addRoute, cleanupOutdatedCaches, createHandlerBoundToURL, getCacheKeyForURL, matchPrecache, precache, precacheAndRoute, PrecacheController, PrecacheFallbackPlugin, PrecacheRoute, PrecacheStrategy, };
24
- export * from "./_types.js";
25
+ export type * from "./_types.js";
26
+ export type { PrecacheFallbackPluginOptions, PrecacheFallbackEntry };
25
27
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAQA,OAAO,EAAE,kBAAkB,EAAE,MAAM,yBAAyB,CAAC;AAC7D,OAAO,EAAE,sBAAsB,EAAE,MAAM,6BAA6B,CAAC;AACrE,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AACnD,OAAO,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AACzD,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAC7C,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AACzC,OAAO,EAAE,qBAAqB,EAAE,MAAM,4BAA4B,CAAC;AACnE,OAAO,EAAE,uBAAuB,EAAE,MAAM,8BAA8B,CAAC;AACvE,OAAO,EAAE,iBAAiB,EAAE,MAAM,wBAAwB,CAAC;AAC3D,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AACnD,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AACzC,OAAO,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AAEzD;;;;;;;;;GASG;AAEH,OAAO,EACL,UAAU,EACV,QAAQ,EACR,qBAAqB,EACrB,uBAAuB,EACvB,iBAAiB,EACjB,aAAa,EACb,QAAQ,EACR,gBAAgB,EAChB,kBAAkB,EAClB,sBAAsB,EACtB,aAAa,EACb,gBAAgB,GACjB,CAAC;AAEF,cAAc,aAAa,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAQA,OAAO,EAAE,kBAAkB,EAAE,MAAM,yBAAyB,CAAC;AAC7D,OAAO,EAAE,sBAAsB,EAAE,MAAM,6BAA6B,CAAC;AACrE,OAAO,KAAK,EAAE,qBAAqB,EAAE,6BAA6B,EAAE,MAAM,6BAA6B,CAAC;AACxG,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AACnD,OAAO,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AACzD,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAC7C,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AACzC,OAAO,EAAE,qBAAqB,EAAE,MAAM,4BAA4B,CAAC;AACnE,OAAO,EAAE,uBAAuB,EAAE,MAAM,8BAA8B,CAAC;AACvE,OAAO,EAAE,iBAAiB,EAAE,MAAM,wBAAwB,CAAC;AAC3D,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AACnD,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AACzC,OAAO,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AAEzD;;;;;;;;;GASG;AAEH,OAAO,EACL,UAAU,EACV,QAAQ,EACR,qBAAqB,EACrB,uBAAuB,EACvB,iBAAiB,EACjB,aAAa,EACb,QAAQ,EACR,gBAAgB,EAChB,kBAAkB,EAClB,sBAAsB,EACtB,aAAa,EACb,gBAAgB,GACjB,CAAC;AAEF,mBAAmB,aAAa,CAAC;AAEjC,YAAY,EAAE,6BAA6B,EAAE,qBAAqB,EAAE,CAAC"}
package/dist/index.js CHANGED
@@ -399,13 +399,28 @@ const getOrCreatePrecacheController = ()=>{
399
399
  };
400
400
 
401
401
  class PrecacheFallbackPlugin {
402
- _fallbackURL;
402
+ _fallbackUrls;
403
403
  _precacheController;
404
- constructor({ fallbackURL, precacheController }){
405
- this._fallbackURL = fallbackURL;
404
+ constructor({ fallbackUrls, precacheController }){
405
+ this._fallbackUrls = fallbackUrls;
406
406
  this._precacheController = precacheController || getOrCreatePrecacheController();
407
407
  }
408
- handlerDidError = ()=>this._precacheController.matchPrecache(this._fallbackURL);
408
+ async handlerDidError(param) {
409
+ for (const fallback of this._fallbackUrls){
410
+ if (typeof fallback === "string") {
411
+ const fallbackResponse = await this._precacheController.matchPrecache(fallback);
412
+ if (fallbackResponse !== undefined) {
413
+ return fallbackResponse;
414
+ }
415
+ } else if (fallback.matcher(param)) {
416
+ const fallbackResponse = await this._precacheController.matchPrecache(fallback.url);
417
+ if (fallbackResponse !== undefined) {
418
+ return fallbackResponse;
419
+ }
420
+ }
421
+ }
422
+ return undefined;
423
+ }
409
424
  }
410
425
 
411
426
  function removeIgnoredSearchParams(urlObject, ignoreURLParametersMatching = []) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@serwist/precaching",
3
- "version": "9.0.0-preview.2",
3
+ "version": "9.0.0-preview.4",
4
4
  "type": "module",
5
5
  "description": "This module efficiently precaches assets.",
6
6
  "files": [
@@ -28,14 +28,14 @@
28
28
  "./package.json": "./package.json"
29
29
  },
30
30
  "dependencies": {
31
- "@serwist/core": "9.0.0-preview.2",
32
- "@serwist/routing": "9.0.0-preview.2",
33
- "@serwist/strategies": "9.0.0-preview.2"
31
+ "@serwist/core": "9.0.0-preview.4",
32
+ "@serwist/routing": "9.0.0-preview.4",
33
+ "@serwist/strategies": "9.0.0-preview.4"
34
34
  },
35
35
  "devDependencies": {
36
36
  "rollup": "4.9.6",
37
37
  "typescript": "5.4.0-dev.20240206",
38
- "@serwist/constants": "9.0.0-preview.2"
38
+ "@serwist/constants": "9.0.0-preview.4"
39
39
  },
40
40
  "peerDependencies": {
41
41
  "typescript": ">=5.0.0"
@@ -6,28 +6,39 @@
6
6
  https://opensource.org/licenses/MIT.
7
7
  */
8
8
 
9
- import type { SerwistPlugin } from "@serwist/core";
9
+ import type { HandlerDidErrorCallbackParam, SerwistPlugin } from "@serwist/core";
10
10
 
11
11
  import type { PrecacheController } from "./PrecacheController.js";
12
12
  import { getOrCreatePrecacheController } from "./utils/getOrCreatePrecacheController.js";
13
13
 
14
- interface PrecacheFallbackPluginOptions {
14
+ export interface PrecacheFallbackEntry {
15
15
  /**
16
- * A precached URL to use as the fallback
16
+ * A function that checks whether the fallback entry can be used
17
+ * for a request.
18
+ */
19
+ matcher: (param: HandlerDidErrorCallbackParam) => boolean;
20
+ /**
21
+ * A precached URL to be used as a fallback.
22
+ */
23
+ url: string;
24
+ }
25
+
26
+ export interface PrecacheFallbackPluginOptions {
27
+ /**
28
+ * Precached URLs to use as the fallback
17
29
  * if the associated strategy can't generate a response.
18
30
  */
19
- fallbackURL: string;
31
+ fallbackUrls: (string | PrecacheFallbackEntry)[];
20
32
  /**
21
- * An optional
22
- * PrecacheController instance. If not provided, the default
23
- * PrecacheController will be used.
33
+ * An optional `PrecacheController` instance. If not provided, the default
34
+ * `PrecacheController` will be used.
24
35
  */
25
36
  precacheController?: PrecacheController;
26
37
  }
27
38
 
28
39
  /**
29
- * `PrecacheFallbackPlugin` allows you to specify an "offline fallback"
30
- * response to be used when a given strategy is unable to generate a response.
40
+ * `PrecacheFallbackPlugin` allows you to specify offline fallbacks
41
+ * to be used when a given strategy is unable to generate a response.
31
42
  *
32
43
  * It does this by intercepting the `handlerDidError` plugin callback
33
44
  * and returning a precached response, taking the expected revision parameter
@@ -37,25 +48,39 @@ interface PrecacheFallbackPluginOptions {
37
48
  * constructor, the default instance will be used. Generally speaking, most
38
49
  * developers will end up using the default.
39
50
  */
40
- class PrecacheFallbackPlugin implements SerwistPlugin {
41
- private readonly _fallbackURL: string;
51
+ export class PrecacheFallbackPlugin implements SerwistPlugin {
52
+ private readonly _fallbackUrls: (string | PrecacheFallbackEntry)[];
42
53
  private readonly _precacheController: PrecacheController;
43
54
 
44
55
  /**
45
- * Constructs a new PrecacheFallbackPlugin with the associated fallbackURL.
56
+ * Constructs a new `PrecacheFallbackPlugin` with the associated `fallbackUrls`.
46
57
  *
47
58
  * @param config
48
59
  */
49
- constructor({ fallbackURL, precacheController }: PrecacheFallbackPluginOptions) {
50
- this._fallbackURL = fallbackURL;
60
+ constructor({ fallbackUrls, precacheController }: PrecacheFallbackPluginOptions) {
61
+ this._fallbackUrls = fallbackUrls;
51
62
  this._precacheController = precacheController || getOrCreatePrecacheController();
52
63
  }
53
64
 
54
65
  /**
55
- * @returns The precache response for the fallback URL.
66
+ * @returns The precache response for one of the fallback URLs, or `undefined` if
67
+ * nothing satisfies the conditions.
56
68
  * @private
57
69
  */
58
- handlerDidError: SerwistPlugin["handlerDidError"] = () => this._precacheController.matchPrecache(this._fallbackURL);
70
+ async handlerDidError(param: HandlerDidErrorCallbackParam) {
71
+ for (const fallback of this._fallbackUrls) {
72
+ if (typeof fallback === "string") {
73
+ const fallbackResponse = await this._precacheController.matchPrecache(fallback);
74
+ if (fallbackResponse !== undefined) {
75
+ return fallbackResponse;
76
+ }
77
+ } else if (fallback.matcher(param)) {
78
+ const fallbackResponse = await this._precacheController.matchPrecache(fallback.url);
79
+ if (fallbackResponse !== undefined) {
80
+ return fallbackResponse;
81
+ }
82
+ }
83
+ }
84
+ return undefined;
85
+ }
59
86
  }
60
-
61
- export { PrecacheFallbackPlugin };
package/src/index.ts CHANGED
@@ -8,6 +8,7 @@
8
8
 
9
9
  import { PrecacheController } from "./PrecacheController.js";
10
10
  import { PrecacheFallbackPlugin } from "./PrecacheFallbackPlugin.js";
11
+ import type { PrecacheFallbackEntry, PrecacheFallbackPluginOptions } from "./PrecacheFallbackPlugin.js";
11
12
  import { PrecacheRoute } from "./PrecacheRoute.js";
12
13
  import { PrecacheStrategy } from "./PrecacheStrategy.js";
13
14
  import { addPlugins } from "./addPlugins.js";
@@ -45,4 +46,6 @@ export {
45
46
  PrecacheStrategy,
46
47
  };
47
48
 
48
- export * from "./_types.js";
49
+ export type * from "./_types.js";
50
+
51
+ export type { PrecacheFallbackPluginOptions, PrecacheFallbackEntry };