serwist 9.0.0-preview.25 → 9.0.0

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 (76) hide show
  1. package/dist/NavigationRoute.d.ts +5 -6
  2. package/dist/NavigationRoute.d.ts.map +1 -1
  3. package/dist/PrecacheRoute.d.ts +2 -2
  4. package/dist/RegExpRoute.d.ts +3 -3
  5. package/dist/Route.d.ts +9 -9
  6. package/dist/Serwist.d.ts +1 -1
  7. package/dist/Serwist.d.ts.map +1 -1
  8. package/dist/chunks/printInstallDetails.js +50 -76
  9. package/dist/chunks/waitUntil.js +27 -1
  10. package/dist/copyResponse.d.ts +7 -9
  11. package/dist/copyResponse.d.ts.map +1 -1
  12. package/dist/index.d.ts +2 -5
  13. package/dist/index.d.ts.map +1 -1
  14. package/dist/index.internal.d.ts +3 -1
  15. package/dist/index.internal.d.ts.map +1 -1
  16. package/dist/index.internal.js +1 -1
  17. package/dist/index.js +5 -5
  18. package/dist/index.legacy.d.ts +1 -7
  19. package/dist/index.legacy.d.ts.map +1 -1
  20. package/dist/index.legacy.js +4 -5
  21. package/dist/legacy/Router.d.ts.map +1 -1
  22. package/dist/legacy/handlePrecaching.d.ts.map +1 -1
  23. package/dist/legacy/installSerwist.d.ts.map +1 -1
  24. package/dist/lib/backgroundSync/StorableRequest.d.ts +4 -4
  25. package/dist/lib/expiration/ExpirationPlugin.d.ts +1 -1
  26. package/dist/lib/precaching/PrecacheFallbackPlugin.d.ts +0 -4
  27. package/dist/lib/precaching/PrecacheFallbackPlugin.d.ts.map +1 -1
  28. package/dist/lib/strategies/CacheFirst.d.ts +1 -1
  29. package/dist/lib/strategies/CacheOnly.d.ts +2 -2
  30. package/dist/lib/strategies/NetworkFirst.d.ts +2 -2
  31. package/dist/lib/strategies/NetworkOnly.d.ts +2 -2
  32. package/dist/lib/strategies/{PrecacheOnly.d.ts → PrecacheStrategy.d.ts} +2 -2
  33. package/dist/lib/strategies/PrecacheStrategy.d.ts.map +1 -0
  34. package/dist/lib/strategies/StaleWhileRevalidate.d.ts +1 -1
  35. package/dist/lib/strategies/Strategy.d.ts +14 -16
  36. package/dist/lib/strategies/Strategy.d.ts.map +1 -1
  37. package/dist/lib/strategies/StrategyHandler.d.ts +34 -40
  38. package/dist/lib/strategies/StrategyHandler.d.ts.map +1 -1
  39. package/dist/utils/cleanupOutdatedCaches.d.ts.map +1 -0
  40. package/dist/utils/clientsClaim.d.ts.map +1 -0
  41. package/dist/{parseRoute.d.ts → utils/parseRoute.d.ts} +3 -3
  42. package/dist/utils/parseRoute.d.ts.map +1 -0
  43. package/package.json +3 -3
  44. package/src/NavigationRoute.ts +5 -6
  45. package/src/PrecacheRoute.ts +2 -2
  46. package/src/RegExpRoute.ts +3 -3
  47. package/src/Route.ts +9 -9
  48. package/src/Serwist.ts +10 -6
  49. package/src/copyResponse.ts +7 -9
  50. package/src/index.internal.ts +4 -0
  51. package/src/index.legacy.ts +0 -15
  52. package/src/index.ts +4 -11
  53. package/src/legacy/PrecacheController.ts +2 -2
  54. package/src/legacy/Router.ts +1 -1
  55. package/src/legacy/handlePrecaching.ts +1 -1
  56. package/src/legacy/installSerwist.ts +1 -1
  57. package/src/lib/backgroundSync/StorableRequest.ts +4 -4
  58. package/src/lib/expiration/ExpirationPlugin.ts +1 -1
  59. package/src/lib/precaching/PrecacheFallbackPlugin.ts +0 -4
  60. package/src/lib/strategies/CacheFirst.ts +1 -1
  61. package/src/lib/strategies/CacheOnly.ts +2 -2
  62. package/src/lib/strategies/NetworkFirst.ts +2 -2
  63. package/src/lib/strategies/NetworkOnly.ts +2 -2
  64. package/src/lib/strategies/{PrecacheOnly.ts → PrecacheStrategy.ts} +5 -5
  65. package/src/lib/strategies/StaleWhileRevalidate.ts +1 -1
  66. package/src/lib/strategies/Strategy.ts +14 -16
  67. package/src/lib/strategies/StrategyHandler.ts +34 -40
  68. package/src/{cleanupOutdatedCaches.ts → utils/cleanupOutdatedCaches.ts} +3 -4
  69. package/src/{parseRoute.ts → utils/parseRoute.ts} +21 -23
  70. package/dist/cleanupOutdatedCaches.d.ts.map +0 -1
  71. package/dist/clientsClaim.d.ts.map +0 -1
  72. package/dist/lib/strategies/PrecacheOnly.d.ts.map +0 -1
  73. package/dist/parseRoute.d.ts.map +0 -1
  74. /package/dist/{cleanupOutdatedCaches.d.ts → utils/cleanupOutdatedCaches.d.ts} +0 -0
  75. /package/dist/{clientsClaim.d.ts → utils/clientsClaim.d.ts} +0 -0
  76. /package/src/{clientsClaim.ts → utils/clientsClaim.ts} +0 -0
@@ -5,6 +5,8 @@ import { cacheMatchIgnoreParams } from "./utils/cacheMatchIgnoreParams.js";
5
5
  import { cacheNames as privateCacheNames } from "./utils/cacheNames.js";
6
6
  import { canConstructReadableStream } from "./utils/canConstructReadableStream.js";
7
7
  import { canConstructResponseFromBodyStream } from "./utils/canConstructResponseFromBodyStream.js";
8
+ import { cleanupOutdatedCaches } from "./utils/cleanupOutdatedCaches.js";
9
+ import { clientsClaim } from "./utils/clientsClaim.js";
8
10
  import { dontWaitFor } from "./utils/dontWaitFor.js";
9
11
  import { executeQuotaErrorCallbacks } from "./utils/executeQuotaErrorCallbacks.js";
10
12
  import { getFriendlyURL } from "./utils/getFriendlyURL.js";
@@ -19,6 +21,8 @@ export {
19
21
  Deferred,
20
22
  SerwistError,
21
23
  assert,
24
+ cleanupOutdatedCaches,
25
+ clientsClaim,
22
26
  cacheMatchIgnoreParams,
23
27
  privateCacheNames,
24
28
  canConstructReadableStream,
@@ -1,7 +1,3 @@
1
- import { NavigationRoute } from "./NavigationRoute.js";
2
- import { RegExpRoute } from "./RegExpRoute.js";
3
- import { Route } from "./Route.js";
4
- import { cleanupOutdatedCaches } from "./cleanupOutdatedCaches.js";
5
1
  import { PrecacheController } from "./legacy/PrecacheController.js";
6
2
  import { PrecacheFallbackPlugin } from "./legacy/PrecacheFallbackPlugin.js";
7
3
  import type { PrecacheFallbackEntry, PrecacheFallbackPluginOptions } from "./legacy/PrecacheFallbackPlugin.js";
@@ -27,19 +23,8 @@ import { setDefaultHandler } from "./legacy/setDefaultHandler.js";
27
23
  import { getSingletonPrecacheController, setSingletonPrecacheController } from "./legacy/singletonPrecacheController.js";
28
24
  import { getSingletonRouter, setSingletonRouter } from "./legacy/singletonRouter.js";
29
25
  import { unregisterRoute } from "./legacy/unregisterRoute.js";
30
- import { PrecacheOnly } from "./lib/strategies/PrecacheOnly.js";
31
- import { parseRoute } from "./parseRoute.js";
32
26
 
33
27
  export {
34
- // These are not deprecated, but we export them so legacy packages
35
- // can re-export them without having to import both the normal and legacy
36
- // import paths.
37
- NavigationRoute,
38
- PrecacheOnly,
39
- RegExpRoute,
40
- Route,
41
- cleanupOutdatedCaches,
42
- parseRoute,
43
28
  // Actual deprecated modules start here
44
29
  addPlugins,
45
30
  addRoute,
package/src/index.ts CHANGED
@@ -5,8 +5,6 @@ import { RegExpRoute } from "./RegExpRoute.js";
5
5
  import { Route } from "./Route.js";
6
6
  import { Serwist, type SerwistOptions } from "./Serwist.js";
7
7
  import { cacheNames } from "./cacheNames.js";
8
- import { cleanupOutdatedCaches } from "./cleanupOutdatedCaches.js";
9
- import { clientsClaim } from "./clientsClaim.js";
10
8
  import type { HTTPMethod } from "./constants.js";
11
9
  import { copyResponse } from "./copyResponse.js";
12
10
  import { disableDevLogs } from "./disableDevLogs.js";
@@ -38,13 +36,12 @@ import type { NetworkFirstOptions } from "./lib/strategies/NetworkFirst.js";
38
36
  import { NetworkFirst } from "./lib/strategies/NetworkFirst.js";
39
37
  import type { NetworkOnlyOptions } from "./lib/strategies/NetworkOnly.js";
40
38
  import { NetworkOnly } from "./lib/strategies/NetworkOnly.js";
41
- import { PrecacheOnly } from "./lib/strategies/PrecacheOnly.js";
39
+ import { PrecacheStrategy } from "./lib/strategies/PrecacheStrategy.js";
42
40
  import { StaleWhileRevalidate } from "./lib/strategies/StaleWhileRevalidate.js";
43
41
  import type { StrategyOptions } from "./lib/strategies/Strategy.js";
44
42
  import { Strategy } from "./lib/strategies/Strategy.js";
45
43
  import { StrategyHandler } from "./lib/strategies/StrategyHandler.js";
46
44
  import { disableNavigationPreload, enableNavigationPreload, isNavigationPreloadSupported } from "./navigationPreload.js";
47
- import { parseRoute } from "./parseRoute.js";
48
45
  import { registerQuotaErrorCallback } from "./registerQuotaErrorCallback.js";
49
46
  import { setCacheNameDetails } from "./setCacheNameDetails.js";
50
47
 
@@ -52,19 +49,19 @@ export {
52
49
  // Core
53
50
  Serwist,
54
51
  cacheNames,
55
- cleanupOutdatedCaches,
56
- clientsClaim,
57
52
  copyResponse,
58
53
  disableDevLogs,
59
54
  disableNavigationPreload,
60
55
  enableNavigationPreload,
61
56
  isNavigationPreloadSupported,
57
+ registerQuotaErrorCallback,
58
+ setCacheNameDetails,
62
59
  // Caching strategies
63
60
  CacheFirst,
64
61
  CacheOnly,
65
62
  NetworkFirst,
66
63
  NetworkOnly,
67
- PrecacheOnly,
64
+ PrecacheStrategy,
68
65
  StaleWhileRevalidate,
69
66
  Strategy,
70
67
  StrategyHandler,
@@ -73,7 +70,6 @@ export {
73
70
  PrecacheRoute,
74
71
  RegExpRoute,
75
72
  Route,
76
- parseRoute,
77
73
  // Background synchronizing
78
74
  BackgroundSyncPlugin,
79
75
  BackgroundSyncQueue,
@@ -97,9 +93,6 @@ export {
97
93
  // Handling range requests
98
94
  createPartialResponse,
99
95
  RangeRequestsPlugin,
100
- // Misc
101
- registerQuotaErrorCallback,
102
- setCacheNameDetails,
103
96
  };
104
97
  export type {
105
98
  // Core
@@ -7,7 +7,7 @@
7
7
  */
8
8
 
9
9
  import { parallel } from "@serwist/utils";
10
- import { PrecacheOnly } from "../lib/strategies/PrecacheOnly.js";
10
+ import { PrecacheStrategy } from "../lib/strategies/PrecacheStrategy.js";
11
11
  import type { Strategy } from "../lib/strategies/Strategy.js";
12
12
  import type { RouteHandlerCallback, SerwistPlugin } from "../types.js";
13
13
  import type { CleanupResult, InstallResult, PrecacheEntry } from "../types.js";
@@ -67,7 +67,7 @@ export class PrecacheController {
67
67
  */
68
68
  constructor({ cacheName, plugins = [], fallbackToNetwork = true, concurrentPrecaching = 1 }: PrecacheControllerOptions = {}) {
69
69
  this._concurrentPrecaching = concurrentPrecaching;
70
- this._strategy = new PrecacheOnly({
70
+ this._strategy = new PrecacheStrategy({
71
71
  cacheName: privateCacheNames.getPrecacheName(cacheName),
72
72
  plugins: [...plugins, new PrecacheCacheKeyPlugin({ precacheController: this })],
73
73
  fallbackToNetwork,
@@ -9,13 +9,13 @@
9
9
  import type { Route } from "../Route.js";
10
10
  import type { HTTPMethod } from "../constants.js";
11
11
  import { defaultMethod } from "../constants.js";
12
- import { parseRoute } from "../parseRoute.js";
13
12
  import type { RouteHandler, RouteHandlerCallbackOptions, RouteHandlerObject, RouteMatchCallback, RouteMatchCallbackOptions } from "../types.js";
14
13
  import { SerwistError } from "../utils/SerwistError.js";
15
14
  import { assert } from "../utils/assert.js";
16
15
  import { getFriendlyURL } from "../utils/getFriendlyURL.js";
17
16
  import { logger } from "../utils/logger.js";
18
17
  import { normalizeHandler } from "../utils/normalizeHandler.js";
18
+ import { parseRoute } from "../utils/parseRoute.js";
19
19
 
20
20
  declare const self: ServiceWorkerGlobalScope;
21
21
 
@@ -1,6 +1,6 @@
1
1
  import { NavigationRoute } from "../NavigationRoute.js";
2
- import { cleanupOutdatedCaches as cleanupOutdatedCachesImpl } from "../cleanupOutdatedCaches.js";
3
2
  import type { PrecacheEntry, PrecacheRouteOptions } from "../types.js";
3
+ import { cleanupOutdatedCaches as cleanupOutdatedCachesImpl } from "../utils/cleanupOutdatedCaches.js";
4
4
  import type { PrecacheController } from "./PrecacheController.js";
5
5
  import { PrecacheRoute } from "./PrecacheRoute.js";
6
6
  import type { Router } from "./Router.js";
@@ -1,8 +1,8 @@
1
- import { clientsClaim as clientsClaimImpl } from "../clientsClaim.js";
2
1
  import { disableDevLogs as disableDevLogsImpl } from "../disableDevLogs.js";
3
2
  import { enableNavigationPreload } from "../navigationPreload.js";
4
3
  import { setCacheNameDetails } from "../setCacheNameDetails.js";
5
4
  import type { RuntimeCaching } from "../types.js";
5
+ import { clientsClaim as clientsClaimImpl } from "../utils/clientsClaim.js";
6
6
  import type { PrecacheController } from "./PrecacheController.js";
7
7
  import type { Router } from "./Router.js";
8
8
  import type { FallbacksOptions } from "./fallbacks.js";
@@ -41,7 +41,7 @@ export class StorableRequest {
41
41
 
42
42
  /**
43
43
  * Converts a Request object to a plain object that can be structured
44
- * cloned or JSON-stringified.
44
+ * cloned or stringified to JSON.
45
45
  *
46
46
  * @param request
47
47
  * @returns
@@ -79,8 +79,8 @@ export class StorableRequest {
79
79
  * Accepts an object of request data that can be used to construct a
80
80
  * `Request` but can also be stored in IndexedDB.
81
81
  *
82
- * @param requestData An object of request data that includes the `url` plus any relevant properties of
83
- * [requestInit](https://fetch.spec.whatwg.org/#requestinit).
82
+ * @param requestData An object of request data that includes the `url` plus any relevant property of
83
+ * [`requestInit`](https://fetch.spec.whatwg.org/#requestinit).
84
84
  */
85
85
  constructor(requestData: RequestData) {
86
86
  if (process.env.NODE_ENV !== "production") {
@@ -108,7 +108,7 @@ export class StorableRequest {
108
108
  }
109
109
 
110
110
  /**
111
- * Returns a deep clone of the instances `_requestData` object.
111
+ * Returns a deep clone of the instance's `requestData` object.
112
112
  *
113
113
  * @returns
114
114
  */
@@ -62,7 +62,7 @@ export interface ExpirationPluginOptions {
62
62
  * When using `maxEntries`, the least recently requested entry will be removed
63
63
  * from the cache.
64
64
  *
65
- * @see https://serwist.pages.dev/docs/serwist/plugins/expiration-plugin
65
+ * @see https://serwist.pages.dev/docs/serwist/runtime-caching/plugins/expiration-plugin
66
66
  */
67
67
  export class ExpirationPlugin implements SerwistPlugin {
68
68
  private readonly _config: ExpirationPluginOptions;
@@ -40,10 +40,6 @@ export interface PrecacheFallbackPluginOptions {
40
40
  * It does this by intercepting the `handlerDidError` plugin callback
41
41
  * and returning a precached response, taking the expected revision parameter
42
42
  * into account automatically.
43
- *
44
- * Unless you explicitly pass in a `PrecacheController` instance to the
45
- * constructor, the default instance will be used. Generally speaking, most
46
- * developers will end up using the default.
47
43
  */
48
44
  export class PrecacheFallbackPlugin implements SerwistPlugin {
49
45
  private readonly _fallbackUrls: (string | PrecacheFallbackEntry)[];
@@ -18,7 +18,7 @@ import { messages } from "./utils/messages.js";
18
18
  * request strategy.
19
19
  *
20
20
  * A cache first strategy is useful for assets that have been revisioned,
21
- * such as URLs like `/styles/example.a8f5f1.css`, since they
21
+ * such as URLs like "/styles/example.a8f5f1.css", since they
22
22
  * can be cached for long periods of time.
23
23
  *
24
24
  * If the network request fails, and there is no cache match, this will throw
@@ -14,10 +14,10 @@ import type { StrategyHandler } from "./StrategyHandler.js";
14
14
  import { messages } from "./utils/messages.js";
15
15
 
16
16
  /**
17
- * An implementation of the [cache only](https://developer.chrome.com/docs/workbox/caching-strategies-overview/#cache-only)
17
+ * An implementation of the [cache only](https://developer.chrome.com/docs/workbox/caching-strategies-overview/#cache_only)
18
18
  * request strategy.
19
19
  *
20
- * This class is useful if you want to take advantage of any Serwist plugin.
20
+ * This class is useful if you already have your own precaching step.
21
21
  *
22
22
  * If there is no cache match, this will throw a `SerwistError` exception.
23
23
  */
@@ -23,11 +23,11 @@ export interface NetworkFirstOptions extends StrategyOptions {
23
23
  }
24
24
 
25
25
  /**
26
- * An implementation of the [network first](https://developer.chrome.com/docs/workbox/caching-strategies-overview/#network-first-falling-back-to-cache)
26
+ * An implementation of the [network first](https://developer.chrome.com/docs/workbox/caching-strategies-overview/#network_first_falling_back_to_cache)
27
27
  * request strategy.
28
28
  *
29
29
  * By default, this strategy will cache responses with a 200 status code as
30
- * well as [opaque responses](https://developer.chrome.com/docs/workbox/caching-resources-during-runtime/#opaque-responses).
30
+ * well as [opaque responses](https://developer.chrome.com/docs/workbox/caching-resources-during-runtime/#opaque_responses).
31
31
  * Opaque responses are are cross-origin requests where the response doesn't
32
32
  * support [CORS](https://enable-cors.org/).
33
33
  *
@@ -23,10 +23,10 @@ export interface NetworkOnlyOptions extends Omit<StrategyOptions, "cacheName" |
23
23
  }
24
24
 
25
25
  /**
26
- * An implementation of the [network only](https://developer.chrome.com/docs/workbox/caching-strategies-overview/#network-only)
26
+ * An implementation of the [network only](https://developer.chrome.com/docs/workbox/caching-strategies-overview/#network_only)
27
27
  * request strategy.
28
28
  *
29
- * This class is useful if you want to take advantage of any Serwist plugin.
29
+ * This class is useful if you require specific requests to only be fulfilled from the network.
30
30
  *
31
31
  * If the network request fails, this will throw a `SerwistError` exception.
32
32
  */
@@ -31,7 +31,7 @@ interface PrecacheStrategyOptions extends StrategyOptions {
31
31
  * Note: an instance of this class is created automatically when creating a
32
32
  * `Serwist` instance; it's generally not necessary to create this yourself.
33
33
  */
34
- export class PrecacheOnly extends Strategy {
34
+ export class PrecacheStrategy extends Strategy {
35
35
  private readonly _fallbackToNetwork: boolean;
36
36
 
37
37
  static readonly defaultPrecacheCacheabilityPlugin: SerwistPlugin = {
@@ -64,7 +64,7 @@ export class PrecacheOnly extends Strategy {
64
64
  // any redirected response must be "copied" rather than cloned, so the new
65
65
  // response doesn't contain the `redirected` flag. See:
66
66
  // https://bugs.chromium.org/p/chromium/issues/detail?id=669363&desc=2#c1
67
- this.plugins.push(PrecacheOnly.copyRedirectedCacheableResponsesPlugin);
67
+ this.plugins.push(PrecacheStrategy.copyRedirectedCacheableResponsesPlugin);
68
68
  }
69
69
 
70
70
  /**
@@ -218,12 +218,12 @@ export class PrecacheOnly extends Strategy {
218
218
 
219
219
  for (const [index, plugin] of this.plugins.entries()) {
220
220
  // Ignore the copy redirected plugin when determining what to do.
221
- if (plugin === PrecacheOnly.copyRedirectedCacheableResponsesPlugin) {
221
+ if (plugin === PrecacheStrategy.copyRedirectedCacheableResponsesPlugin) {
222
222
  continue;
223
223
  }
224
224
 
225
225
  // Save the default plugin's index, in case it needs to be removed.
226
- if (plugin === PrecacheOnly.defaultPrecacheCacheabilityPlugin) {
226
+ if (plugin === PrecacheStrategy.defaultPrecacheCacheabilityPlugin) {
227
227
  defaultPluginIndex = index;
228
228
  }
229
229
 
@@ -233,7 +233,7 @@ export class PrecacheOnly extends Strategy {
233
233
  }
234
234
 
235
235
  if (cacheWillUpdatePluginCount === 0) {
236
- this.plugins.push(PrecacheOnly.defaultPrecacheCacheabilityPlugin);
236
+ this.plugins.push(PrecacheStrategy.defaultPrecacheCacheabilityPlugin);
237
237
  } else if (cacheWillUpdatePluginCount > 1 && defaultPluginIndex !== null) {
238
238
  // Only remove the default plugin; multiple custom plugins are allowed.
239
239
  this.plugins.splice(defaultPluginIndex, 1);
@@ -17,7 +17,7 @@ import { messages } from "./utils/messages.js";
17
17
 
18
18
  /**
19
19
  * An implementation of the
20
- * [stale-while-revalidate](https://developer.chrome.com/docs/workbox/caching-strategies-overview/#stale-while-revalidate)
20
+ * [stale-while-revalidate](https://developer.chrome.com/docs/workbox/caching-strategies-overview/#stale_while_revalidate)
21
21
  * request strategy.
22
22
  *
23
23
  * Resources are requested from both the cache and the network in parallel.
@@ -19,27 +19,26 @@ export interface StrategyOptions {
19
19
  */
20
20
  cacheName?: string;
21
21
  /**
22
- * [Plugins](https://developer.chrome.com/docs/workbox/using-plugins)
23
- * to use in conjunction with this caching strategy.
22
+ * [Plugins](https://serwist.pages.dev/docs/serwist/runtime-caching/plugins) to use in conjunction with this caching strategy.
24
23
  */
25
24
  plugins?: SerwistPlugin[];
26
25
  /**
27
- * Values passed along to the [`init`](https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch#Parameters)
28
- * of [non-navigation](https://github.com/GoogleChrome/workbox/issues/1796) `fetch()` requests made by this strategy.
26
+ * Options passed to [non-navigation](https://github.com/GoogleChrome/workbox/issues/1796) `fetch()` calls made by
27
+ * this strategy.
29
28
  */
30
29
  fetchOptions?: RequestInit;
31
30
  /**
32
31
  * The [`CacheQueryOptions`](https://w3c.github.io/ServiceWorker/#dictdef-cachequeryoptions)
33
- * for any `cache.match()` or `cache.put()` calls made by this strategy.
32
+ * passed to any `cache.match()` or `cache.put()` call made by this strategy.
34
33
  */
35
34
  matchOptions?: CacheQueryOptions;
36
35
  }
37
36
 
38
37
  /**
39
- * Classes extending the `Strategy` based class should implement this method,
40
- * and leverage `serwist/strategies`'s `StrategyHandler` arg to perform all
41
- * fetching and cache logic, which will ensure all relevant cache, cache options,
42
- * fetch options and plugins are used (per the current strategy instance).
38
+ * Abstract class for implementing runtime caching strategies.
39
+ *
40
+ * Custom strategies should extend this class and leverage `StrategyHandler`, which will ensure all relevant cache options,
41
+ * fetch options, and plugins are used (per the current strategy instance), to perform all fetching and caching logic.
43
42
  */
44
43
  export abstract class Strategy implements RouteHandlerObject {
45
44
  cacheName: string;
@@ -67,7 +66,7 @@ export abstract class Strategy implements RouteHandlerObject {
67
66
  }
68
67
 
69
68
  /**
70
- * Perform a request strategy and returns a `Promise` that will resolve with
69
+ * Performs a request strategy and returns a `Promise` that will resolve with
71
70
  * a `Response`, invoking all relevant plugin callbacks.
72
71
  *
73
72
  * When a strategy instance is registered with a `Route`, this method is automatically
@@ -88,12 +87,11 @@ export abstract class Strategy implements RouteHandlerObject {
88
87
  }
89
88
 
90
89
  /**
91
- * Similar to `serwist/strategies`'s `Strategy.handle`, but
92
- * instead of just returning a `Promise` that resolves to a `Response` it
93
- * it will return an tuple of `[response, done]` promises, where the former
94
- * (`response`) is equivalent to what `handle()` returns, and the latter is a
95
- * Promise that will resolve once any promises that were added to
96
- * `event.waitUntil()` as part of performing the strategy have completed.
90
+ * Similar to `handle()`, but instead of just returning a `Promise` that
91
+ * resolves to a `Response`, it will return an tuple of `[response, done]` promises,
92
+ * where `response` is equivalent to what `handle()` returns, and `done` is a
93
+ * `Promise` that will resolve once all promises added to `event.waitUntil()` as a part
94
+ * of performing the strategy have completed.
97
95
  *
98
96
  * You can await the `done` promise to ensure any extra work performed by
99
97
  * the strategy (usually caching responses) completes successfully.
@@ -22,9 +22,9 @@ function toRequest(input: RequestInfo) {
22
22
  }
23
23
 
24
24
  /**
25
- * A class created every time a Strategy instance instance calls `Strategy.handle` or
25
+ * A class created every time a `Strategy` instance calls `Strategy.handle` or
26
26
  * `Strategy.handleAll` that wraps all fetch and cache actions around plugin callbacks
27
- * and keeps track of when the strategy is "done" (i.e. all added `event.waitUntil()` promises
27
+ * and keeps track of when the strategy is "done" (i.e. when all added `event.waitUntil()` promises
28
28
  * have resolved).
29
29
  */
30
30
  export class StrategyHandler {
@@ -33,23 +33,23 @@ export class StrategyHandler {
33
33
  */
34
34
  public event: ExtendableEvent;
35
35
  /**
36
- * The request the strategy is performing (passed to the strategy's
36
+ * The request the strategy is processing (passed to the strategy's
37
37
  * `handle()` or `handleAll()` method).
38
38
  */
39
39
  public request: Request;
40
40
  /**
41
41
  * A `URL` instance of `request.url` (if passed to the strategy's
42
42
  * `handle()` or `handleAll()` method).
43
- * Note: the `url` param will be present if the strategy was invoked
43
+ * Note: the `url` param will be present if the strategy is invoked
44
44
  * from a `Route` object.
45
45
  */
46
46
  public url?: URL;
47
47
  /**
48
- * A `param` value (if passed to the strategy's
48
+ * Some additional params (if passed to the strategy's
49
49
  * `handle()` or `handleAll()` method).
50
- * Note: the `param` param will be present if the strategy was invoked
51
- * from a `Route` object and the `serwist/strategies.matchCallback`
52
- * returned a truthy value (it will be that value).
50
+ * Note: the `params` param will be present if the strategy is invoked
51
+ * from a `Route` object and that route's matcher returned a truthy value
52
+ * (it will be that value).
53
53
  */
54
54
  public params?: string[] | MapLikeObject;
55
55
 
@@ -114,8 +114,8 @@ export class StrategyHandler {
114
114
 
115
115
  /**
116
116
  * Fetches a given request (and invokes any applicable plugin callback
117
- * methods) using the `fetchOptions` (for non-navigation requests) and
118
- * `plugins` defined on the `Strategy` object.
117
+ * methods), taking the `fetchOptions` (for non-navigation requests) and
118
+ * `plugins` provided to the `Strategy` object into account.
119
119
  *
120
120
  * The following plugin lifecycle methods are invoked when using this method:
121
121
  * - `requestWillFetch()`
@@ -199,11 +199,10 @@ export class StrategyHandler {
199
199
  }
200
200
 
201
201
  /**
202
- * Calls `this.fetch()` and (in the background) runs `this.cachePut()` on
203
- * the response generated by `this.fetch()`.
202
+ * Calls `this.fetch()` and (in the background) caches the generated response.
204
203
  *
205
204
  * The call to `this.cachePut()` automatically invokes `this.waitUntil()`,
206
- * so you do not have to manually call `waitUntil()` on the event.
205
+ * so you do not have to call `waitUntil()` yourself.
207
206
  *
208
207
  * @param input The request or URL to fetch and cache.
209
208
  * @returns
@@ -219,14 +218,14 @@ export class StrategyHandler {
219
218
 
220
219
  /**
221
220
  * Matches a request from the cache (and invokes any applicable plugin
222
- * callback methods) using the `cacheName`, `matchOptions`, and `plugins`
223
- * defined on the strategy object.
221
+ * callback method) using the `cacheName`, `matchOptions`, and `plugins`
222
+ * provided to the `Strategy` object.
224
223
  *
225
- * The following plugin lifecycle methods are invoked when using this method:
226
- * - cacheKeyWillByUsed()
227
- * - cachedResponseWillByUsed()
224
+ * The following lifecycle methods are invoked when using this method:
225
+ * - `cacheKeyWillByUsed`
226
+ * - `cachedResponseWillByUsed`
228
227
  *
229
- * @param key The Request or URL to use as the cache key.
228
+ * @param key The `Request` or `URL` object to use as the cache key.
230
229
  * @returns A matching response, if found.
231
230
  */
232
231
  async cacheMatch(key: RequestInfo): Promise<Response | undefined> {
@@ -261,14 +260,14 @@ export class StrategyHandler {
261
260
  }
262
261
 
263
262
  /**
264
- * Puts a request/response pair in the cache (and invokes any applicable
265
- * plugin callback methods) using the `cacheName` and `plugins` defined on
266
- * the strategy object.
263
+ * Puts a request/response pair into the cache (and invokes any applicable
264
+ * plugin callback method) using the `cacheName` and `plugins` provided to
265
+ * the `Strategy` object.
267
266
  *
268
267
  * The following plugin lifecycle methods are invoked when using this method:
269
- * - cacheKeyWillByUsed()
270
- * - cacheWillUpdate()
271
- * - cacheDidUpdate()
268
+ * - `cacheKeyWillByUsed`
269
+ * - `cacheWillUpdate`
270
+ * - `cacheDidUpdate`
272
271
  *
273
272
  * @param key The request or URL to use as the cache key.
274
273
  * @param response The response to cache.
@@ -370,11 +369,11 @@ export class StrategyHandler {
370
369
  }
371
370
 
372
371
  /**
373
- * Checks the list of plugins for the `cacheKeyWillBeUsed` callback, and
374
- * executes any of those callbacks found in sequence. The final `Request`
372
+ * Checks the `plugins` provided to the `Strategy` object for `cacheKeyWillBeUsed`
373
+ * callbacks and executes found callbacks in sequence. The final `Request`
375
374
  * object returned by the last plugin is treated as the cache key for cache
376
375
  * reads and/or writes. If no `cacheKeyWillBeUsed` plugin callbacks have
377
- * been registered, the passed request is returned unmodified
376
+ * been registered, the passed request is returned unmodified.
378
377
  *
379
378
  * @param request
380
379
  * @param mode
@@ -402,7 +401,7 @@ export class StrategyHandler {
402
401
  }
403
402
 
404
403
  /**
405
- * Returns true if the strategy has at least one plugin with the given
404
+ * Returns `true` if the strategy has at least one plugin with the given
406
405
  * callback.
407
406
  *
408
407
  * @param name The name of the callback to check for.
@@ -419,8 +418,7 @@ export class StrategyHandler {
419
418
 
420
419
  /**
421
420
  * Runs all plugin callbacks matching the given name, in order, passing the
422
- * given param object (merged ith the current plugin state) as the only
423
- * argument.
421
+ * given param object as the only argument.
424
422
  *
425
423
  * Note: since this method runs all plugins, it's not suitable for cases
426
424
  * where the return value of a callback needs to be applied prior to calling
@@ -439,10 +437,7 @@ export class StrategyHandler {
439
437
  }
440
438
 
441
439
  /**
442
- * Accepts a callback and returns an iterable of matching plugin callbacks,
443
- * where each callback is wrapped with the current handler state (i.e. when
444
- * you call each callback, whatever object parameter you pass it will
445
- * be merged with the plugin's current state).
440
+ * Accepts a callback name and returns an iterable of matching plugin callbacks.
446
441
  *
447
442
  * @param name The name fo the callback to run
448
443
  * @returns
@@ -482,12 +477,11 @@ export class StrategyHandler {
482
477
 
483
478
  /**
484
479
  * Returns a promise that resolves once all promises passed to
485
- * `serwist/strategies.StrategyHandler.waitUntil` have settled.
480
+ * `this.waitUntil()` have settled.
486
481
  *
487
482
  * Note: any work done after `doneWaiting()` settles should be manually
488
- * passed to an event's `waitUntil()` method (not this handler's
489
- * `waitUntil()` method), otherwise the service worker thread my be killed
490
- * prior to your work completing.
483
+ * passed to an event's `waitUntil()` method (not `this.waitUntil()`), otherwise
484
+ * the service worker thread may be killed prior to your work completing.
491
485
  */
492
486
  async doneWaiting(): Promise<void> {
493
487
  let promise: Promise<any> | undefined = undefined;
@@ -498,7 +492,7 @@ export class StrategyHandler {
498
492
 
499
493
  /**
500
494
  * Stops running the strategy and immediately resolves any pending
501
- * `waitUntil()` promises.
495
+ * `waitUntil()` promise.
502
496
  */
503
497
  destroy(): void {
504
498
  this._handlerDeferred.resolve(null);
@@ -6,10 +6,9 @@
6
6
  https://opensource.org/licenses/MIT.
7
7
  */
8
8
 
9
- import { cacheNames as privateCacheNames } from "./utils/cacheNames.js";
10
- import { logger } from "./utils/logger.js";
11
-
12
- import { deleteOutdatedCaches } from "./utils/deleteOutdatedCaches.js";
9
+ import { cacheNames as privateCacheNames } from "./cacheNames.js";
10
+ import { deleteOutdatedCaches } from "./deleteOutdatedCaches.js";
11
+ import { logger } from "./logger.js";
13
12
 
14
13
  declare const self: ServiceWorkerGlobalScope;
15
14
 
@@ -1,9 +1,9 @@
1
- import { RegExpRoute } from "./RegExpRoute.js";
2
- import { Route } from "./Route.js";
3
- import type { HTTPMethod } from "./constants.js";
4
- import type { RouteHandler, RouteMatchCallback } from "./types.js";
5
- import { SerwistError } from "./utils/SerwistError.js";
6
- import { logger } from "./utils/logger.js";
1
+ import { RegExpRoute } from "../RegExpRoute.js";
2
+ import { Route } from "../Route.js";
3
+ import type { HTTPMethod } from "../constants.js";
4
+ import type { RouteHandler, RouteMatchCallback } from "../types.js";
5
+ import { SerwistError } from "./SerwistError.js";
6
+ import { logger } from "./logger.js";
7
7
 
8
8
  /**
9
9
  * Parses a `RegExp`, string, or function with a caching strategy into a `Route`. This is for
@@ -17,8 +17,6 @@ import { logger } from "./utils/logger.js";
17
17
  * @returns The generated `Route`.
18
18
  */
19
19
  export const parseRoute = (capture: RegExp | string | RouteMatchCallback | Route, handler?: RouteHandler, method?: HTTPMethod): Route => {
20
- let route: Route;
21
-
22
20
  if (typeof capture === "string") {
23
21
  const captureUrl = new URL(capture, location.href);
24
22
 
@@ -57,22 +55,22 @@ export const parseRoute = (capture: RegExp | string | RouteMatchCallback | Route
57
55
  };
58
56
 
59
57
  // If `capture` is a string then `handler` and `method` must be present.
60
- route = new Route(matchCallback, handler!, method);
61
- } else if (capture instanceof RegExp) {
58
+ return new Route(matchCallback, handler!, method);
59
+ }
60
+ if (capture instanceof RegExp) {
62
61
  // If `capture` is a `RegExp` then `handler` and `method` must be present.
63
- route = new RegExpRoute(capture, handler!, method);
64
- } else if (typeof capture === "function") {
62
+ return new RegExpRoute(capture, handler!, method);
63
+ }
64
+ if (typeof capture === "function") {
65
65
  // If `capture` is a function then `handler` and `method` must be present.
66
- route = new Route(capture, handler!, method);
67
- } else if (capture instanceof Route) {
68
- route = capture;
69
- } else {
70
- throw new SerwistError("unsupported-route-type", {
71
- moduleName: "serwist",
72
- funcName: "parseRoute",
73
- paramName: "capture",
74
- });
66
+ return new Route(capture, handler!, method);
75
67
  }
76
-
77
- return route;
68
+ if (capture instanceof Route) {
69
+ return capture;
70
+ }
71
+ throw new SerwistError("unsupported-route-type", {
72
+ moduleName: "serwist",
73
+ funcName: "parseRoute",
74
+ paramName: "capture",
75
+ });
78
76
  };
@@ -1 +0,0 @@
1
- {"version":3,"file":"cleanupOutdatedCaches.d.ts","sourceRoot":"","sources":["../src/cleanupOutdatedCaches.ts"],"names":[],"mappings":"AAeA;;;GAGG;AACH,eAAO,MAAM,qBAAqB,eAAgB,MAAM,KAAG,IAY1D,CAAC"}