serwist 9.0.12 → 10.0.0-preview.1

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.
Files changed (151) hide show
  1. package/dist/Serwist.d.ts +74 -52
  2. package/dist/Serwist.d.ts.map +1 -1
  3. package/dist/chunks/resultingClientExists.js +426 -2
  4. package/dist/{legacy/utils → controllers/PrecacheController}/PrecacheCacheKeyPlugin.d.ts +4 -4
  5. package/dist/controllers/PrecacheController/PrecacheCacheKeyPlugin.d.ts.map +1 -0
  6. package/dist/{legacy → controllers/PrecacheController}/PrecacheController.d.ts +73 -64
  7. package/dist/controllers/PrecacheController/PrecacheController.d.ts.map +1 -0
  8. package/dist/controllers/PrecacheController/PrecacheInstallReportPlugin.d.ts +14 -0
  9. package/dist/controllers/PrecacheController/PrecacheInstallReportPlugin.d.ts.map +1 -0
  10. package/dist/{PrecacheRoute.d.ts → controllers/PrecacheController/PrecacheRoute.d.ts} +4 -5
  11. package/dist/controllers/PrecacheController/PrecacheRoute.d.ts.map +1 -0
  12. package/dist/{lib/strategies → controllers/PrecacheController}/PrecacheStrategy.d.ts +6 -6
  13. package/dist/controllers/PrecacheController/PrecacheStrategy.d.ts.map +1 -0
  14. package/dist/controllers/PrecacheController/parsePrecacheOptions.d.ts +25 -0
  15. package/dist/controllers/PrecacheController/parsePrecacheOptions.d.ts.map +1 -0
  16. package/dist/controllers/RuntimeCacheController.d.ts +65 -0
  17. package/dist/controllers/RuntimeCacheController.d.ts.map +1 -0
  18. package/dist/index.d.ts +4 -3
  19. package/dist/index.d.ts.map +1 -1
  20. package/dist/index.internal.js +25 -3
  21. package/dist/index.js +1868 -211
  22. package/dist/lib/backgroundSync/BackgroundSyncPlugin.d.ts +2 -2
  23. package/dist/lib/backgroundSync/BackgroundSyncPlugin.d.ts.map +1 -1
  24. package/dist/lib/broadcastUpdate/BroadcastUpdatePlugin.d.ts +2 -2
  25. package/dist/lib/broadcastUpdate/BroadcastUpdatePlugin.d.ts.map +1 -1
  26. package/dist/lib/cacheableResponse/CacheableResponsePlugin.d.ts +3 -3
  27. package/dist/lib/cacheableResponse/CacheableResponsePlugin.d.ts.map +1 -1
  28. package/dist/lib/expiration/ExpirationPlugin.d.ts +2 -2
  29. package/dist/lib/expiration/ExpirationPlugin.d.ts.map +1 -1
  30. package/dist/lib/precaching/PrecacheFallbackPlugin.d.ts +7 -7
  31. package/dist/lib/precaching/PrecacheFallbackPlugin.d.ts.map +1 -1
  32. package/dist/lib/rangeRequests/RangeRequestsPlugin.d.ts +3 -3
  33. package/dist/lib/rangeRequests/RangeRequestsPlugin.d.ts.map +1 -1
  34. package/dist/lib/strategies/Strategy.d.ts +3 -3
  35. package/dist/lib/strategies/Strategy.d.ts.map +1 -1
  36. package/dist/lib/strategies/StrategyHandler.d.ts +4 -4
  37. package/dist/lib/strategies/StrategyHandler.d.ts.map +1 -1
  38. package/dist/lib/strategies/plugins/cacheOkAndOpaquePlugin.d.ts +2 -2
  39. package/dist/lib/strategies/plugins/cacheOkAndOpaquePlugin.d.ts.map +1 -1
  40. package/dist/types.d.ts +16 -64
  41. package/dist/types.d.ts.map +1 -1
  42. package/dist/utils/pluginUtils.d.ts +2 -2
  43. package/dist/utils/pluginUtils.d.ts.map +1 -1
  44. package/package.json +5 -12
  45. package/src/Serwist.ts +157 -262
  46. package/src/{legacy/utils → controllers/PrecacheController}/PrecacheCacheKeyPlugin.ts +5 -6
  47. package/src/{legacy → controllers/PrecacheController}/PrecacheController.ts +143 -155
  48. package/src/{utils → controllers/PrecacheController}/PrecacheInstallReportPlugin.ts +5 -5
  49. package/src/{PrecacheRoute.ts → controllers/PrecacheController/PrecacheRoute.ts} +11 -12
  50. package/src/{lib/strategies → controllers/PrecacheController}/PrecacheStrategy.ts +7 -7
  51. package/src/controllers/PrecacheController/parsePrecacheOptions.ts +46 -0
  52. package/src/controllers/RuntimeCacheController.ts +119 -0
  53. package/src/index.ts +5 -2
  54. package/src/lib/backgroundSync/BackgroundSyncPlugin.ts +2 -2
  55. package/src/lib/broadcastUpdate/BroadcastUpdatePlugin.ts +2 -2
  56. package/src/lib/cacheableResponse/CacheableResponsePlugin.ts +3 -3
  57. package/src/lib/expiration/ExpirationPlugin.ts +2 -2
  58. package/src/lib/precaching/PrecacheFallbackPlugin.ts +10 -10
  59. package/src/lib/rangeRequests/RangeRequestsPlugin.ts +3 -3
  60. package/src/lib/strategies/Strategy.ts +3 -3
  61. package/src/lib/strategies/StrategyHandler.ts +9 -9
  62. package/src/lib/strategies/plugins/cacheOkAndOpaquePlugin.ts +2 -2
  63. package/src/types.ts +24 -67
  64. package/src/utils/pluginUtils.ts +2 -2
  65. package/dist/PrecacheRoute.d.ts.map +0 -1
  66. package/dist/chunks/printInstallDetails.js +0 -1601
  67. package/dist/chunks/waitUntil.js +0 -449
  68. package/dist/index.legacy.d.ts +0 -28
  69. package/dist/index.legacy.d.ts.map +0 -1
  70. package/dist/index.legacy.js +0 -790
  71. package/dist/legacy/PrecacheController.d.ts.map +0 -1
  72. package/dist/legacy/PrecacheFallbackPlugin.d.ts +0 -61
  73. package/dist/legacy/PrecacheFallbackPlugin.d.ts.map +0 -1
  74. package/dist/legacy/PrecacheRoute.d.ts +0 -19
  75. package/dist/legacy/PrecacheRoute.d.ts.map +0 -1
  76. package/dist/legacy/Router.d.ts +0 -151
  77. package/dist/legacy/Router.d.ts.map +0 -1
  78. package/dist/legacy/addPlugins.d.ts +0 -9
  79. package/dist/legacy/addPlugins.d.ts.map +0 -1
  80. package/dist/legacy/addRoute.d.ts +0 -14
  81. package/dist/legacy/addRoute.d.ts.map +0 -1
  82. package/dist/legacy/constants.d.ts +0 -10
  83. package/dist/legacy/constants.d.ts.map +0 -1
  84. package/dist/legacy/createHandlerBoundToURL.d.ts +0 -17
  85. package/dist/legacy/createHandlerBoundToURL.d.ts.map +0 -1
  86. package/dist/legacy/fallbacks.d.ts +0 -59
  87. package/dist/legacy/fallbacks.d.ts.map +0 -1
  88. package/dist/legacy/getCacheKeyForURL.d.ts +0 -20
  89. package/dist/legacy/getCacheKeyForURL.d.ts.map +0 -1
  90. package/dist/legacy/handlePrecaching.d.ts +0 -54
  91. package/dist/legacy/handlePrecaching.d.ts.map +0 -1
  92. package/dist/legacy/initializeGoogleAnalytics.d.ts +0 -38
  93. package/dist/legacy/initializeGoogleAnalytics.d.ts.map +0 -1
  94. package/dist/legacy/installSerwist.d.ts +0 -81
  95. package/dist/legacy/installSerwist.d.ts.map +0 -1
  96. package/dist/legacy/matchPrecache.d.ts +0 -15
  97. package/dist/legacy/matchPrecache.d.ts.map +0 -1
  98. package/dist/legacy/precache.d.ts +0 -20
  99. package/dist/legacy/precache.d.ts.map +0 -1
  100. package/dist/legacy/precacheAndRoute.d.ts +0 -14
  101. package/dist/legacy/precacheAndRoute.d.ts.map +0 -1
  102. package/dist/legacy/registerRoute.d.ts +0 -16
  103. package/dist/legacy/registerRoute.d.ts.map +0 -1
  104. package/dist/legacy/registerRuntimeCaching.d.ts +0 -11
  105. package/dist/legacy/registerRuntimeCaching.d.ts.map +0 -1
  106. package/dist/legacy/setCatchHandler.d.ts +0 -10
  107. package/dist/legacy/setCatchHandler.d.ts.map +0 -1
  108. package/dist/legacy/setDefaultHandler.d.ts +0 -13
  109. package/dist/legacy/setDefaultHandler.d.ts.map +0 -1
  110. package/dist/legacy/singletonPrecacheController.d.ts +0 -34
  111. package/dist/legacy/singletonPrecacheController.d.ts.map +0 -1
  112. package/dist/legacy/singletonRouter.d.ts +0 -41
  113. package/dist/legacy/singletonRouter.d.ts.map +0 -1
  114. package/dist/legacy/unregisterRoute.d.ts +0 -9
  115. package/dist/legacy/unregisterRoute.d.ts.map +0 -1
  116. package/dist/legacy/utils/PrecacheCacheKeyPlugin.d.ts.map +0 -1
  117. package/dist/legacy/utils/getCacheKeyForURL.d.ts +0 -14
  118. package/dist/legacy/utils/getCacheKeyForURL.d.ts.map +0 -1
  119. package/dist/lib/strategies/PrecacheStrategy.d.ts.map +0 -1
  120. package/dist/utils/PrecacheCacheKeyPlugin.d.ts +0 -16
  121. package/dist/utils/PrecacheCacheKeyPlugin.d.ts.map +0 -1
  122. package/dist/utils/PrecacheInstallReportPlugin.d.ts +0 -14
  123. package/dist/utils/PrecacheInstallReportPlugin.d.ts.map +0 -1
  124. package/dist/utils/parsePrecacheOptions.d.ts +0 -26
  125. package/dist/utils/parsePrecacheOptions.d.ts.map +0 -1
  126. package/src/index.legacy.ts +0 -62
  127. package/src/legacy/PrecacheFallbackPlugin.ts +0 -92
  128. package/src/legacy/PrecacheRoute.ts +0 -48
  129. package/src/legacy/Router.ts +0 -484
  130. package/src/legacy/addPlugins.ts +0 -21
  131. package/src/legacy/addRoute.ts +0 -27
  132. package/src/legacy/constants.ts +0 -22
  133. package/src/legacy/createHandlerBoundToURL.ts +0 -30
  134. package/src/legacy/fallbacks.ts +0 -94
  135. package/src/legacy/getCacheKeyForURL.ts +0 -32
  136. package/src/legacy/handlePrecaching.ts +0 -86
  137. package/src/legacy/initializeGoogleAnalytics.ts +0 -218
  138. package/src/legacy/installSerwist.ts +0 -170
  139. package/src/legacy/matchPrecache.ts +0 -27
  140. package/src/legacy/precache.ts +0 -33
  141. package/src/legacy/precacheAndRoute.ts +0 -27
  142. package/src/legacy/registerRoute.ts +0 -28
  143. package/src/legacy/registerRuntimeCaching.ts +0 -17
  144. package/src/legacy/setCatchHandler.ts +0 -21
  145. package/src/legacy/setDefaultHandler.ts +0 -24
  146. package/src/legacy/singletonPrecacheController.ts +0 -53
  147. package/src/legacy/singletonRouter.ts +0 -70
  148. package/src/legacy/unregisterRoute.ts +0 -13
  149. package/src/legacy/utils/getCacheKeyForURL.ts +0 -36
  150. package/src/utils/PrecacheCacheKeyPlugin.ts +0 -33
  151. package/src/utils/parsePrecacheOptions.ts +0 -47
@@ -0,0 +1,119 @@
1
+ import { parallel } from "@serwist/utils";
2
+ import { PrecacheFallbackPlugin, type PrecacheFallbackEntry } from "../lib/precaching/PrecacheFallbackPlugin.js";
3
+ import { Strategy } from "../lib/strategies/Strategy.js";
4
+ import type { Serwist } from "../Serwist.js";
5
+ import type { Controller, RuntimeCaching } from "../types.js";
6
+
7
+ export interface WarmRuntimeCacheEntry {
8
+ integrity?: string;
9
+ url: string;
10
+ }
11
+
12
+ export interface WarmRuntimeCacheOptions {
13
+ concurrency?: number;
14
+ }
15
+
16
+ export interface FallbackEntry extends PrecacheFallbackEntry {}
17
+
18
+ export interface FallbacksOptions {
19
+ /**
20
+ * A list of fallback entries.
21
+ */
22
+ entries: FallbackEntry[];
23
+ }
24
+
25
+ export interface RuntimeCacheControllerOptions {
26
+ /**
27
+ * URLs that should be cached as the service worker is installed.
28
+ */
29
+ warmEntries?: (WarmRuntimeCacheEntry | string | Request)[];
30
+ /**
31
+ * Options for `warmEntries`.
32
+ */
33
+ warmOptions?: WarmRuntimeCacheOptions;
34
+ /**
35
+ * Precaches routes so that they can be used as a fallback when
36
+ * a {@linkcode Strategy} fails to generate a response.
37
+ *
38
+ * Note: This option mutates `runtimeCaching`. It also expects the URLs
39
+ * defined in `entries` to have been precached beforehand.
40
+ */
41
+ fallbacks?: FallbacksOptions;
42
+ }
43
+
44
+ export class RuntimeCacheController implements Controller {
45
+ _entries: RuntimeCaching[];
46
+ _options: RuntimeCacheControllerOptions;
47
+ constructor(entries: RuntimeCaching[], options: RuntimeCacheControllerOptions = {}) {
48
+ this._entries = entries;
49
+ this._options = options;
50
+ this.init = this.init.bind(this);
51
+ this.install = this.install.bind(this);
52
+ }
53
+
54
+ init(serwist: Serwist): void {
55
+ if (this._options.fallbacks !== undefined) {
56
+ const fallbackPlugin = new PrecacheFallbackPlugin({
57
+ fallbackUrls: this._options.fallbacks.entries,
58
+ precacheController: serwist.precache,
59
+ });
60
+
61
+ this._entries.forEach((cacheEntry) => {
62
+ if (
63
+ cacheEntry.handler instanceof Strategy &&
64
+ // This also filters entries with `PrecacheFallbackPlugin` as it also has `handlerDidError`.
65
+ !cacheEntry.handler.plugins.some((plugin) => "handlerDidError" in plugin)
66
+ ) {
67
+ cacheEntry.handler.plugins.push(fallbackPlugin);
68
+ }
69
+ });
70
+ }
71
+ for (const entry of this._entries) {
72
+ serwist.registerCapture(entry.matcher, entry.handler, entry.method);
73
+ }
74
+ }
75
+
76
+ async install(event: ExtendableEvent, serwist: Serwist) {
77
+ const concurrency = this._options.warmOptions?.concurrency ?? 10;
78
+ if (this._options.warmEntries) {
79
+ await parallel(concurrency, this._options.warmEntries, async (entry) => {
80
+ const request =
81
+ entry instanceof Request
82
+ ? entry
83
+ : new Request(typeof entry === "string" ? entry : entry.url, {
84
+ integrity: typeof entry !== "string" ? entry.integrity : undefined,
85
+ credentials: "same-origin",
86
+ });
87
+ await serwist.handleRequest({
88
+ request,
89
+ event,
90
+ });
91
+ });
92
+ }
93
+ }
94
+
95
+ /**
96
+ * Any assets you wish to cache ahead of time which can't be revisioned
97
+ * should be cached with this method. All assets are cached on install
98
+ * regardless of whether an older version of the request is in the cache.
99
+ *
100
+ * The input can be a string or a [Request](https://developer.mozilla.org/en-US/docs/Web/API/Request).
101
+ *
102
+ * @example
103
+ * ```js
104
+ * // For unrevisioned assets that should always be downloaded
105
+ * // with every service worker update, use this method.
106
+ * serwist.warmRuntimeCache([
107
+ * "/scripts/main.js',
108
+ * "/images/default-avater.png",
109
+ * new Request("/images/logo.png"),
110
+ * new Request("/api/data.json"),
111
+ * ]);
112
+ * ```
113
+ * @param entries A set of urls to cache when the service worker is installed.
114
+ */
115
+ warmRuntimeCache(entries: (WarmRuntimeCacheEntry | string | Request)[]): void {
116
+ if (!this._options.warmEntries) this._options.warmEntries = [];
117
+ this._options.warmEntries.push(...entries);
118
+ }
119
+ }
package/src/index.ts CHANGED
@@ -1,6 +1,8 @@
1
+ import { PrecacheStrategy } from "./controllers/PrecacheController/PrecacheStrategy.js";
2
+ import { PrecacheRoute } from "./controllers/PrecacheController/PrecacheRoute.js";
3
+ import { RuntimeCacheController } from "./controllers/RuntimeCacheController.js";
1
4
  import type { NavigationRouteMatchOptions } from "./NavigationRoute.js";
2
5
  import { NavigationRoute } from "./NavigationRoute.js";
3
- import { PrecacheRoute } from "./PrecacheRoute.js";
4
6
  import { RegExpRoute } from "./RegExpRoute.js";
5
7
  import { Route } from "./Route.js";
6
8
  import { Serwist, type SerwistOptions } from "./Serwist.js";
@@ -36,7 +38,6 @@ import type { NetworkFirstOptions } from "./lib/strategies/NetworkFirst.js";
36
38
  import { NetworkFirst } from "./lib/strategies/NetworkFirst.js";
37
39
  import type { NetworkOnlyOptions } from "./lib/strategies/NetworkOnly.js";
38
40
  import { NetworkOnly } from "./lib/strategies/NetworkOnly.js";
39
- import { PrecacheStrategy } from "./lib/strategies/PrecacheStrategy.js";
40
41
  import { StaleWhileRevalidate } from "./lib/strategies/StaleWhileRevalidate.js";
41
42
  import type { StrategyOptions } from "./lib/strategies/Strategy.js";
42
43
  import { Strategy } from "./lib/strategies/Strategy.js";
@@ -56,6 +57,8 @@ export {
56
57
  isNavigationPreloadSupported,
57
58
  registerQuotaErrorCallback,
58
59
  setCacheNameDetails,
60
+ // Controllers
61
+ RuntimeCacheController,
59
62
  // Caching strategies
60
63
  CacheFirst,
61
64
  CacheOnly,
@@ -6,7 +6,7 @@
6
6
  https://opensource.org/licenses/MIT.
7
7
  */
8
8
 
9
- import type { FetchDidFailCallbackParam, SerwistPlugin } from "../../types.js";
9
+ import type { FetchDidFailCallbackParam, StrategyPlugin } from "../../types.js";
10
10
  import type { BackgroundSyncQueueOptions } from "./BackgroundSyncQueue.js";
11
11
  import { BackgroundSyncQueue } from "./BackgroundSyncQueue.js";
12
12
 
@@ -14,7 +14,7 @@ import { BackgroundSyncQueue } from "./BackgroundSyncQueue.js";
14
14
  * A class implementing the `fetchDidFail` lifecycle callback. This makes it
15
15
  * easier to add failed requests to a {@linkcode BackgroundSyncQueue}.
16
16
  */
17
- export class BackgroundSyncPlugin implements SerwistPlugin {
17
+ export class BackgroundSyncPlugin implements StrategyPlugin {
18
18
  private readonly _queue: BackgroundSyncQueue;
19
19
 
20
20
  /**
@@ -6,7 +6,7 @@
6
6
  https://opensource.org/licenses/MIT.
7
7
  */
8
8
 
9
- import type { CacheDidUpdateCallbackParam, SerwistPlugin } from "../../types.js";
9
+ import type { CacheDidUpdateCallbackParam, StrategyPlugin } from "../../types.js";
10
10
  import { BroadcastCacheUpdate } from "./BroadcastCacheUpdate.js";
11
11
  import type { BroadcastCacheUpdateOptions } from "./types.js";
12
12
 
@@ -14,7 +14,7 @@ import type { BroadcastCacheUpdateOptions } from "./types.js";
14
14
  * A class implementing the `cacheDidUpdate` lifecycle callback. It will automatically
15
15
  * broadcast a message whenever a cached response is updated.
16
16
  */
17
- export class BroadcastUpdatePlugin implements SerwistPlugin {
17
+ export class BroadcastUpdatePlugin implements StrategyPlugin {
18
18
  private readonly _broadcastUpdate: BroadcastCacheUpdate;
19
19
 
20
20
  /**
@@ -6,7 +6,7 @@
6
6
  https://opensource.org/licenses/MIT.
7
7
  */
8
8
 
9
- import type { SerwistPlugin } from "../../types.js";
9
+ import type { StrategyPlugin } from "../../types.js";
10
10
  import type { CacheableResponseOptions } from "./CacheableResponse.js";
11
11
  import { CacheableResponse } from "./CacheableResponse.js";
12
12
 
@@ -15,7 +15,7 @@ import { CacheableResponse } from "./CacheableResponse.js";
15
15
  * easier to add in cacheability checks to requests made via Serwist's built-in
16
16
  * strategies.
17
17
  */
18
- export class CacheableResponsePlugin implements SerwistPlugin {
18
+ export class CacheableResponsePlugin implements StrategyPlugin {
19
19
  private readonly _cacheableResponse: CacheableResponse;
20
20
 
21
21
  /**
@@ -36,7 +36,7 @@ export class CacheableResponsePlugin implements SerwistPlugin {
36
36
  * @returns
37
37
  * @private
38
38
  */
39
- cacheWillUpdate: SerwistPlugin["cacheWillUpdate"] = async ({ response }) => {
39
+ cacheWillUpdate: StrategyPlugin["cacheWillUpdate"] = async ({ response }) => {
40
40
  if (this._cacheableResponse.isResponseCacheable(response)) {
41
41
  return response;
42
42
  }
@@ -7,7 +7,7 @@
7
7
  */
8
8
 
9
9
  import { registerQuotaErrorCallback } from "../../registerQuotaErrorCallback.js";
10
- import type { CacheDidUpdateCallbackParam, CachedResponseWillBeUsedCallbackParam, SerwistPlugin } from "../../types.js";
10
+ import type { CacheDidUpdateCallbackParam, CachedResponseWillBeUsedCallbackParam, StrategyPlugin } from "../../types.js";
11
11
  import { SerwistError } from "../../utils/SerwistError.js";
12
12
  import { assert } from "../../utils/assert.js";
13
13
  import { cacheNames as privateCacheNames } from "../../utils/cacheNames.js";
@@ -66,7 +66,7 @@ export interface ExpirationPluginOptions {
66
66
  *
67
67
  * @see https://serwist.pages.dev/docs/serwist/runtime-caching/plugins/expiration-plugin
68
68
  */
69
- export class ExpirationPlugin implements SerwistPlugin {
69
+ export class ExpirationPlugin implements StrategyPlugin {
70
70
  private readonly _config: ExpirationPluginOptions;
71
71
  private _cacheExpirations: Map<string, CacheExpiration>;
72
72
 
@@ -6,8 +6,8 @@
6
6
  https://opensource.org/licenses/MIT.
7
7
  */
8
8
 
9
- import type { Serwist } from "../../Serwist.js";
10
- import type { HandlerDidErrorCallbackParam, SerwistPlugin } from "../../types.js";
9
+ import type { PrecacheController } from "../../controllers/PrecacheController/PrecacheController.js";
10
+ import type { HandlerDidErrorCallbackParam, StrategyPlugin } from "../../types.js";
11
11
 
12
12
  export interface PrecacheFallbackEntry {
13
13
  /**
@@ -28,9 +28,9 @@ export interface PrecacheFallbackPluginOptions {
28
28
  */
29
29
  fallbackUrls: (string | PrecacheFallbackEntry)[];
30
30
  /**
31
- * A {@linkcode Serwist} instance.
31
+ * A {@linkcode PrecacheController} instance.
32
32
  */
33
- serwist: Serwist;
33
+ precacheController: PrecacheController;
34
34
  }
35
35
 
36
36
  /**
@@ -41,18 +41,18 @@ export interface PrecacheFallbackPluginOptions {
41
41
  * and returning a precached response, taking the expected revision parameter
42
42
  * into account automatically.
43
43
  */
44
- export class PrecacheFallbackPlugin implements SerwistPlugin {
44
+ export class PrecacheFallbackPlugin implements StrategyPlugin {
45
45
  private readonly _fallbackUrls: (string | PrecacheFallbackEntry)[];
46
- private readonly _serwist: Serwist;
46
+ private readonly _precacheController: PrecacheController;
47
47
 
48
48
  /**
49
49
  * Constructs a new instance with the associated `fallbackUrls`.
50
50
  *
51
51
  * @param config
52
52
  */
53
- constructor({ fallbackUrls, serwist }: PrecacheFallbackPluginOptions) {
53
+ constructor({ fallbackUrls, precacheController }: PrecacheFallbackPluginOptions) {
54
54
  this._fallbackUrls = fallbackUrls;
55
- this._serwist = serwist;
55
+ this._precacheController = precacheController;
56
56
  }
57
57
 
58
58
  /**
@@ -63,12 +63,12 @@ export class PrecacheFallbackPlugin implements SerwistPlugin {
63
63
  async handlerDidError(param: HandlerDidErrorCallbackParam) {
64
64
  for (const fallback of this._fallbackUrls) {
65
65
  if (typeof fallback === "string") {
66
- const fallbackResponse = await this._serwist.matchPrecache(fallback);
66
+ const fallbackResponse = await this._precacheController.matchPrecache(fallback);
67
67
  if (fallbackResponse !== undefined) {
68
68
  return fallbackResponse;
69
69
  }
70
70
  } else if (fallback.matcher(param)) {
71
- const fallbackResponse = await this._serwist.matchPrecache(fallback.url);
71
+ const fallbackResponse = await this._precacheController.matchPrecache(fallback.url);
72
72
  if (fallbackResponse !== undefined) {
73
73
  return fallbackResponse;
74
74
  }
@@ -6,7 +6,7 @@
6
6
  https://opensource.org/licenses/MIT.
7
7
  */
8
8
 
9
- import type { SerwistPlugin } from "../../types.js";
9
+ import type { StrategyPlugin } from "../../types.js";
10
10
  import { createPartialResponse } from "./createPartialResponse.js";
11
11
 
12
12
  /**
@@ -15,7 +15,7 @@ import { createPartialResponse } from "./createPartialResponse.js";
15
15
  * It does this by intercepting the `cachedResponseWillBeUsed` plugin callback
16
16
  * and returning the appropriate subset of the cached response body.
17
17
  */
18
- export class RangeRequestsPlugin implements SerwistPlugin {
18
+ export class RangeRequestsPlugin implements StrategyPlugin {
19
19
  /**
20
20
  * @param options
21
21
  * @returns If request contains a `Range` header, then a
@@ -23,7 +23,7 @@ export class RangeRequestsPlugin implements SerwistPlugin {
23
23
  * returned. Otherwise, `cachedResponse` is returned as-is.
24
24
  * @private
25
25
  */
26
- cachedResponseWillBeUsed: SerwistPlugin["cachedResponseWillBeUsed"] = async ({ request, cachedResponse }) => {
26
+ cachedResponseWillBeUsed: StrategyPlugin["cachedResponseWillBeUsed"] = async ({ request, cachedResponse }) => {
27
27
  // Only return a sliced response if there's something valid in the cache,
28
28
  // and there's a Range: header in the request.
29
29
  if (cachedResponse && request.headers.has("range")) {
@@ -6,7 +6,7 @@
6
6
  https://opensource.org/licenses/MIT.
7
7
  */
8
8
 
9
- import type { HandlerCallbackOptions, RouteHandlerObject, SerwistPlugin } from "../../types.js";
9
+ import type { HandlerCallbackOptions, RouteHandlerObject, StrategyPlugin } from "../../types.js";
10
10
  import { SerwistError } from "../../utils/SerwistError.js";
11
11
  import { cacheNames as privateCacheNames } from "../../utils/cacheNames.js";
12
12
  import { getFriendlyURL } from "../../utils/getFriendlyURL.js";
@@ -21,7 +21,7 @@ export interface StrategyOptions {
21
21
  /**
22
22
  * [Plugins](https://serwist.pages.dev/docs/serwist/runtime-caching/plugins) to use in conjunction with this caching strategy.
23
23
  */
24
- plugins?: SerwistPlugin[];
24
+ plugins?: StrategyPlugin[];
25
25
  /**
26
26
  * Options passed to [non-navigation](https://github.com/GoogleChrome/workbox/issues/1796) `fetch()` calls made by
27
27
  * this strategy.
@@ -42,7 +42,7 @@ export interface StrategyOptions {
42
42
  */
43
43
  export abstract class Strategy implements RouteHandlerObject {
44
44
  cacheName: string;
45
- plugins: SerwistPlugin[];
45
+ plugins: StrategyPlugin[];
46
46
  fetchOptions?: RequestInit;
47
47
  matchOptions?: CacheQueryOptions;
48
48
 
@@ -6,7 +6,7 @@
6
6
  https://opensource.org/licenses/MIT.
7
7
  */
8
8
 
9
- import type { HandlerCallbackOptions, MapLikeObject, SerwistPlugin, SerwistPluginCallbackParam } from "../../types.js";
9
+ import type { HandlerCallbackOptions, MapLikeObject, StrategyPlugin, StrategyPluginCallbackParam } from "../../types.js";
10
10
  import { Deferred } from "../../utils/Deferred.js";
11
11
  import { SerwistError } from "../../utils/SerwistError.js";
12
12
  import { assert } from "../../utils/assert.js";
@@ -59,8 +59,8 @@ export class StrategyHandler {
59
59
  private readonly _strategy: Strategy;
60
60
  private readonly _handlerDeferred: Deferred<any>;
61
61
  private readonly _extendLifetimePromises: Promise<any>[];
62
- private readonly _plugins: SerwistPlugin[];
63
- private readonly _pluginStateMap: Map<SerwistPlugin, MapLikeObject>;
62
+ private readonly _plugins: StrategyPlugin[];
63
+ private readonly _pluginStateMap: Map<StrategyPlugin, MapLikeObject>;
64
64
 
65
65
  /**
66
66
  * Creates a new instance associated with the passed strategy and event
@@ -405,7 +405,7 @@ export class StrategyHandler {
405
405
  * @param name The name of the callback to check for.
406
406
  * @returns
407
407
  */
408
- hasCallback<C extends keyof SerwistPlugin>(name: C): boolean {
408
+ hasCallback<C extends keyof StrategyPlugin>(name: C): boolean {
409
409
  for (const plugin of this._strategy.plugins) {
410
410
  if (name in plugin) {
411
411
  return true;
@@ -426,10 +426,10 @@ export class StrategyHandler {
426
426
  * @param param The object to pass as the first (and only) param when executing each callback. This object will be merged with the
427
427
  * current plugin state prior to callback execution.
428
428
  */
429
- async runCallbacks<C extends keyof NonNullable<SerwistPlugin>>(name: C, param: Omit<SerwistPluginCallbackParam[C], "state">): Promise<void> {
429
+ async runCallbacks<C extends keyof NonNullable<StrategyPlugin>>(name: C, param: Omit<StrategyPluginCallbackParam[C], "state">): Promise<void> {
430
430
  for (const callback of this.iterateCallbacks(name)) {
431
431
  // TODO(philipwalton): not sure why `any` is needed. It seems like
432
- // this should work with `as SerwistPluginCallbackParam[C]`.
432
+ // this should work with `as StrategyPluginCallbackParam[C]`.
433
433
  await callback(param as any);
434
434
  }
435
435
  }
@@ -440,18 +440,18 @@ export class StrategyHandler {
440
440
  * @param name The name fo the callback to run
441
441
  * @returns
442
442
  */
443
- *iterateCallbacks<C extends keyof SerwistPlugin>(name: C): Generator<NonNullable<SerwistPlugin[C]>> {
443
+ *iterateCallbacks<C extends keyof StrategyPlugin>(name: C): Generator<NonNullable<StrategyPlugin[C]>> {
444
444
  for (const plugin of this._strategy.plugins) {
445
445
  if (typeof plugin[name] === "function") {
446
446
  const state = this._pluginStateMap.get(plugin);
447
- const statefulCallback = (param: Omit<SerwistPluginCallbackParam[C], "state">) => {
447
+ const statefulCallback = (param: Omit<StrategyPluginCallbackParam[C], "state">) => {
448
448
  const statefulParam = { ...param, state };
449
449
 
450
450
  // TODO(philipwalton): not sure why `any` is needed. It seems like
451
451
  // this should work with `as WorkboxPluginCallbackParam[C]`.
452
452
  return plugin[name]!(statefulParam as any);
453
453
  };
454
- yield statefulCallback as NonNullable<SerwistPlugin[C]>;
454
+ yield statefulCallback as NonNullable<StrategyPlugin[C]>;
455
455
  }
456
456
  }
457
457
  }
@@ -6,9 +6,9 @@
6
6
  https://opensource.org/licenses/MIT.
7
7
  */
8
8
 
9
- import type { SerwistPlugin } from "../../../types.js";
9
+ import type { StrategyPlugin } from "../../../types.js";
10
10
 
11
- export const cacheOkAndOpaquePlugin: SerwistPlugin = {
11
+ export const cacheOkAndOpaquePlugin: StrategyPlugin = {
12
12
  /**
13
13
  * Returns a valid response (to allow caching) if the status is 200 (OK) or
14
14
  * 0 (opaque).
package/src/types.ts CHANGED
@@ -1,7 +1,8 @@
1
+ import type { Prettify } from "@serwist/utils";
1
2
  import type { HTTPMethod } from "./constants.js";
2
- import type { PrecacheStrategyOptions } from "./lib/strategies/PrecacheStrategy.js";
3
3
  import type { Route } from "./Route.js";
4
4
  import type { Serwist } from "./Serwist.js";
5
+ import type { PrecacheControllerOptions, PrecacheEntry, PrecacheOptions, PrecacheRouteOptions } from "./controllers/PrecacheController/PrecacheController.js";
5
6
 
6
7
  export type PromiseOrNot<T> = T | Promise<T>;
7
8
 
@@ -15,6 +16,15 @@ export interface MapLikeObject {
15
16
  */
16
17
  export type PluginState = MapLikeObject;
17
18
 
19
+ /**
20
+ * An object with optional lifecycle callback properties for Serwist's operations.
21
+ */
22
+ export interface Controller {
23
+ init?(serwist: Serwist): void;
24
+ install?(event: ExtendableEvent, serwist: Serwist): Promise<void>;
25
+ activate?(event: ExtendableEvent, serwist: Serwist): Promise<void>;
26
+ }
27
+
18
28
  /**
19
29
  * Options passed to a {@linkcode RouteMatchCallback} function.
20
30
  */
@@ -74,7 +84,7 @@ export interface ManualHandlerCallbackOptions {
74
84
  params?: never;
75
85
  }
76
86
 
77
- export type HandlerCallbackOptions = RouteHandlerCallbackOptions | ManualHandlerCallbackOptions;
87
+ export type HandlerCallbackOptions = Prettify<RouteHandlerCallbackOptions | ManualHandlerCallbackOptions>;
78
88
 
79
89
  /**
80
90
  * The "handler" callback is invoked whenever a request is matched to a
@@ -110,7 +120,7 @@ export interface RouteHandlerObject {
110
120
  * Either a {@linkcode RouteHandlerCallback} or a {@linkcode RouteHandlerObject}.
111
121
  * Most APIs that accept route handlers take either.
112
122
  */
113
- export type RouteHandler = RouteHandlerCallback | RouteHandlerObject;
123
+ export type RouteHandler = RouteHandlerCallback | Prettify<RouteHandlerObject>;
114
124
 
115
125
  export interface HandlerWillStartCallbackParam {
116
126
  request: Request;
@@ -256,7 +266,7 @@ export type HandlerDidCompleteCallback = (param: HandlerDidCompleteCallbackParam
256
266
  * An object with optional lifecycle callback properties for the fetch and
257
267
  * cache operations.
258
268
  */
259
- export declare interface SerwistPlugin {
269
+ export interface StrategyPlugin {
260
270
  cacheDidUpdate?: CacheDidUpdateCallback;
261
271
  cachedResponseWillBeUsed?: CachedResponseWillBeUsedCallback;
262
272
  cacheKeyWillBeUsed?: CacheKeyWillBeUsedCallback;
@@ -271,7 +281,7 @@ export declare interface SerwistPlugin {
271
281
  requestWillFetch?: RequestWillFetchCallback;
272
282
  }
273
283
 
274
- export interface SerwistPluginCallbackParam {
284
+ export interface StrategyPluginCallbackParam {
275
285
  cacheDidUpdate: CacheDidUpdateCallbackParam;
276
286
  cachedResponseWillBeUsed: CachedResponseWillBeUsedCallbackParam;
277
287
  cacheKeyWillBeUsed: CacheKeyWillBeUsedCallbackParam;
@@ -331,66 +341,13 @@ export interface CleanupResult {
331
341
  deletedCacheRequests: string[];
332
342
  }
333
343
 
334
- export declare interface PrecacheEntry {
335
- integrity?: string;
336
- url: string;
337
- revision?: string | null;
338
- }
339
-
340
- export interface PrecacheRouteOptions {
341
- /**
342
- * Tells Serwist to check the precache for an entry whose URL is the request URL appended
343
- * with the specified value. Only applies if the request URL ends with "/".
344
- *
345
- * @default "index.html"
346
- */
347
- directoryIndex?: string | null;
348
- /**
349
- * An array of `RegExp` objects matching search params that should be removed when looking
350
- * for a precache match.
351
- */
352
- ignoreURLParametersMatching?: RegExp[];
353
- /**
354
- * Tells Serwist to check the precache for an entry whose URL is the request URL appended
355
- * with ".html".
356
- *
357
- * @default true
358
- */
359
- cleanURLs?: boolean;
360
- /**
361
- * A function that should take a URL and return an array of alternative URLs that should
362
- * be checked for precache matches.
363
- */
364
- urlManipulation?: UrlManipulation;
365
- }
366
-
367
- export interface PrecacheOptions extends PrecacheStrategyOptions, PrecacheRouteOptions {
368
- /**
369
- * Whether outdated caches should be removed.
370
- *
371
- * @default false
372
- */
373
- cleanupOutdatedCaches?: boolean;
374
- /**
375
- * The number of precache requests that should be made concurrently.
376
- *
377
- * @default 10
378
- */
379
- concurrency?: number;
380
- /**
381
- * An URL that should point to a HTML file with which navigation requests for URLs that aren't
382
- * precached will be fulfilled.
383
- */
384
- navigateFallback?: string;
385
- /**
386
- * URLs that should be allowed to use the `navigateFallback` handler.
387
- */
388
- navigateFallbackAllowlist?: RegExp[];
389
- /**
390
- * URLs that should not be allowed to use the `navigateFallback` handler. This takes precedence
391
- * over `navigateFallbackAllowlist`.
392
- */
393
- navigateFallbackDenylist?: RegExp[];
394
- }
395
-
396
344
  export type UrlManipulation = ({ url }: { url: URL }) => URL[];
345
+
346
+ export type {
347
+ StrategyPlugin as SerwistPlugin,
348
+ StrategyPluginCallbackParam as SerwistPluginCallbackParam,
349
+ PrecacheEntry,
350
+ PrecacheRouteOptions,
351
+ PrecacheControllerOptions,
352
+ PrecacheOptions,
353
+ };
@@ -6,10 +6,10 @@
6
6
  https://opensource.org/licenses/MIT.
7
7
  */
8
8
 
9
- import type { SerwistPlugin } from "../types.js";
9
+ import type { StrategyPlugin } from "../types.js";
10
10
 
11
11
  export const pluginUtils = {
12
- filter: (plugins: SerwistPlugin[], callbackName: string): SerwistPlugin[] => {
12
+ filter: (plugins: StrategyPlugin[], callbackName: string): StrategyPlugin[] => {
13
13
  return plugins.filter((plugin) => callbackName in plugin);
14
14
  },
15
15
  };
@@ -1 +0,0 @@
1
- {"version":3,"file":"PrecacheRoute.d.ts","sourceRoot":"","sources":["../src/PrecacheRoute.ts"],"names":[],"mappings":"AAQA,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AACnC,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAE5C,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,YAAY,CAAC;AAKvD;;;GAGG;AACH,qBAAa,aAAc,SAAQ,KAAK;IACtC;;;;OAIG;gBACS,OAAO,EAAE,OAAO,EAAE,OAAO,CAAC,EAAE,oBAAoB;CAkB7D"}