serwist 9.0.2 → 9.0.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.
Files changed (127) hide show
  1. package/dist/NavigationRoute.d.ts +2 -2
  2. package/dist/PrecacheRoute.d.ts +2 -3
  3. package/dist/PrecacheRoute.d.ts.map +1 -1
  4. package/dist/RegExpRoute.d.ts +2 -2
  5. package/dist/Route.d.ts +4 -4
  6. package/dist/Serwist.d.ts +19 -19
  7. package/dist/chunks/printInstallDetails.js +6 -10
  8. package/dist/copyResponse.d.ts.map +1 -1
  9. package/dist/legacy/PrecacheController.d.ts +1 -1
  10. package/dist/legacy/PrecacheFallbackPlugin.d.ts +6 -7
  11. package/dist/legacy/PrecacheFallbackPlugin.d.ts.map +1 -1
  12. package/dist/legacy/PrecacheRoute.d.ts +3 -3
  13. package/dist/legacy/Router.d.ts +23 -23
  14. package/dist/legacy/addRoute.d.ts +6 -8
  15. package/dist/legacy/addRoute.d.ts.map +1 -1
  16. package/dist/legacy/createHandlerBoundToURL.d.ts +5 -6
  17. package/dist/legacy/createHandlerBoundToURL.d.ts.map +1 -1
  18. package/dist/legacy/fallbacks.d.ts +3 -3
  19. package/dist/legacy/handlePrecaching.d.ts +3 -3
  20. package/dist/legacy/initializeGoogleAnalytics.d.ts +1 -1
  21. package/dist/legacy/installSerwist.d.ts +4 -4
  22. package/dist/legacy/matchPrecache.d.ts +4 -4
  23. package/dist/legacy/matchPrecache.d.ts.map +1 -1
  24. package/dist/legacy/precache.d.ts +2 -2
  25. package/dist/legacy/precache.d.ts.map +1 -1
  26. package/dist/legacy/precacheAndRoute.d.ts +3 -4
  27. package/dist/legacy/precacheAndRoute.d.ts.map +1 -1
  28. package/dist/legacy/registerRoute.d.ts +6 -6
  29. package/dist/legacy/registerRoute.d.ts.map +1 -1
  30. package/dist/legacy/registerRuntimeCaching.d.ts +1 -1
  31. package/dist/legacy/setCatchHandler.d.ts +2 -2
  32. package/dist/legacy/setDefaultHandler.d.ts +3 -3
  33. package/dist/legacy/singletonPrecacheController.d.ts +7 -7
  34. package/dist/legacy/singletonRouter.d.ts +9 -9
  35. package/dist/legacy/unregisterRoute.d.ts +1 -1
  36. package/dist/legacy/unregisterRoute.d.ts.map +1 -1
  37. package/dist/lib/backgroundSync/BackgroundSyncPlugin.d.ts +3 -3
  38. package/dist/lib/backgroundSync/BackgroundSyncQueue.d.ts +1 -1
  39. package/dist/lib/backgroundSync/StorableRequest.d.ts +1 -1
  40. package/dist/lib/broadcastUpdate/BroadcastCacheUpdate.d.ts +1 -2
  41. package/dist/lib/broadcastUpdate/BroadcastCacheUpdate.d.ts.map +1 -1
  42. package/dist/lib/broadcastUpdate/BroadcastUpdatePlugin.d.ts +4 -7
  43. package/dist/lib/broadcastUpdate/BroadcastUpdatePlugin.d.ts.map +1 -1
  44. package/dist/lib/broadcastUpdate/types.d.ts +2 -1
  45. package/dist/lib/broadcastUpdate/types.d.ts.map +1 -1
  46. package/dist/lib/cacheableResponse/CacheableResponse.d.ts +6 -6
  47. package/dist/lib/cacheableResponse/CacheableResponsePlugin.d.ts +2 -2
  48. package/dist/lib/expiration/CacheExpiration.d.ts +2 -2
  49. package/dist/lib/expiration/ExpirationPlugin.d.ts +11 -11
  50. package/dist/lib/expiration/ExpirationPlugin.d.ts.map +1 -1
  51. package/dist/lib/precaching/PrecacheFallbackPlugin.d.ts +4 -4
  52. package/dist/lib/rangeRequests/RangeRequestsPlugin.d.ts +3 -4
  53. package/dist/lib/rangeRequests/RangeRequestsPlugin.d.ts.map +1 -1
  54. package/dist/lib/rangeRequests/createPartialResponse.d.ts +7 -7
  55. package/dist/lib/rangeRequests/createPartialResponse.d.ts.map +1 -1
  56. package/dist/lib/rangeRequests/utils/parseRangeHeader.d.ts +2 -2
  57. package/dist/lib/strategies/CacheFirst.d.ts +1 -1
  58. package/dist/lib/strategies/CacheOnly.d.ts +1 -1
  59. package/dist/lib/strategies/NetworkFirst.d.ts +1 -1
  60. package/dist/lib/strategies/NetworkOnly.d.ts +1 -1
  61. package/dist/lib/strategies/PrecacheStrategy.d.ts +3 -3
  62. package/dist/lib/strategies/PrecacheStrategy.d.ts.map +1 -1
  63. package/dist/lib/strategies/StaleWhileRevalidate.d.ts +1 -1
  64. package/dist/lib/strategies/Strategy.d.ts +7 -7
  65. package/dist/lib/strategies/StrategyHandler.d.ts +12 -13
  66. package/dist/lib/strategies/StrategyHandler.d.ts.map +1 -1
  67. package/dist/types.d.ts +37 -36
  68. package/dist/types.d.ts.map +1 -1
  69. package/dist/utils/cacheNames.d.ts +1 -1
  70. package/dist/utils/cacheNames.d.ts.map +1 -1
  71. package/dist/utils/canConstructResponseFromBodyStream.d.ts +2 -2
  72. package/dist/utils/deleteOutdatedCaches.d.ts.map +1 -1
  73. package/dist/utils/executeQuotaErrorCallbacks.d.ts.map +1 -1
  74. package/dist/utils/parseRoute.d.ts +8 -8
  75. package/dist/utils/waitUntil.d.ts.map +1 -1
  76. package/package.json +5 -5
  77. package/src/NavigationRoute.ts +2 -2
  78. package/src/PrecacheRoute.ts +2 -3
  79. package/src/RegExpRoute.ts +2 -2
  80. package/src/Route.ts +4 -4
  81. package/src/Serwist.ts +19 -19
  82. package/src/legacy/PrecacheController.ts +1 -1
  83. package/src/legacy/PrecacheFallbackPlugin.ts +6 -7
  84. package/src/legacy/PrecacheRoute.ts +3 -3
  85. package/src/legacy/Router.ts +23 -23
  86. package/src/legacy/addRoute.ts +6 -8
  87. package/src/legacy/createHandlerBoundToURL.ts +6 -6
  88. package/src/legacy/fallbacks.ts +3 -3
  89. package/src/legacy/handlePrecaching.ts +3 -3
  90. package/src/legacy/initializeGoogleAnalytics.ts +1 -1
  91. package/src/legacy/installSerwist.ts +4 -4
  92. package/src/legacy/matchPrecache.ts +5 -4
  93. package/src/legacy/precache.ts +4 -2
  94. package/src/legacy/precacheAndRoute.ts +3 -4
  95. package/src/legacy/registerRoute.ts +8 -7
  96. package/src/legacy/registerRuntimeCaching.ts +1 -1
  97. package/src/legacy/setCatchHandler.ts +2 -2
  98. package/src/legacy/setDefaultHandler.ts +3 -3
  99. package/src/legacy/singletonPrecacheController.ts +7 -7
  100. package/src/legacy/singletonRouter.ts +9 -9
  101. package/src/legacy/unregisterRoute.ts +2 -1
  102. package/src/lib/backgroundSync/BackgroundSyncPlugin.ts +3 -3
  103. package/src/lib/backgroundSync/BackgroundSyncQueue.ts +1 -1
  104. package/src/lib/backgroundSync/StorableRequest.ts +1 -1
  105. package/src/lib/broadcastUpdate/BroadcastCacheUpdate.ts +1 -2
  106. package/src/lib/broadcastUpdate/BroadcastUpdatePlugin.ts +4 -7
  107. package/src/lib/broadcastUpdate/types.ts +2 -1
  108. package/src/lib/cacheableResponse/CacheableResponse.ts +6 -6
  109. package/src/lib/cacheableResponse/CacheableResponsePlugin.ts +2 -2
  110. package/src/lib/expiration/CacheExpiration.ts +2 -2
  111. package/src/lib/expiration/ExpirationPlugin.ts +12 -11
  112. package/src/lib/precaching/PrecacheFallbackPlugin.ts +4 -4
  113. package/src/lib/rangeRequests/RangeRequestsPlugin.ts +3 -4
  114. package/src/lib/rangeRequests/createPartialResponse.ts +7 -7
  115. package/src/lib/rangeRequests/utils/parseRangeHeader.ts +2 -2
  116. package/src/lib/strategies/CacheFirst.ts +1 -1
  117. package/src/lib/strategies/CacheOnly.ts +1 -1
  118. package/src/lib/strategies/NetworkFirst.ts +1 -1
  119. package/src/lib/strategies/NetworkOnly.ts +1 -1
  120. package/src/lib/strategies/PrecacheStrategy.ts +4 -3
  121. package/src/lib/strategies/StaleWhileRevalidate.ts +1 -1
  122. package/src/lib/strategies/Strategy.ts +7 -7
  123. package/src/lib/strategies/StrategyHandler.ts +21 -25
  124. package/src/types.ts +39 -36
  125. package/src/utils/cacheNames.ts +2 -1
  126. package/src/utils/canConstructResponseFromBodyStream.ts +2 -2
  127. package/src/utils/parseRoute.ts +8 -8
@@ -14,11 +14,13 @@ import { cacheNames as privateCacheNames } from "../../utils/cacheNames.js";
14
14
  import { getFriendlyURL } from "../../utils/getFriendlyURL.js";
15
15
  import { logger } from "../../utils/logger.js";
16
16
  import { CacheExpiration } from "./CacheExpiration.js";
17
+ import type { Strategy } from "../strategies/Strategy.js";
17
18
 
18
19
  export interface ExpirationPluginOptions {
19
20
  /**
20
- * The maximum number of entries to cache. Entries used the least will be removed
21
- * as the maximum is reached.
21
+ * The maximum number of entries to cache. Entries used (if `maxAgeFrom` is
22
+ * `"last-used"`) or fetched from the network (if `maxAgeFrom` is `"last-fetched"`)
23
+ * least recently will be removed as the maximum is reached.
22
24
  */
23
25
  maxEntries?: number;
24
26
  /**
@@ -44,10 +46,10 @@ export interface ExpirationPluginOptions {
44
46
  }
45
47
 
46
48
  /**
47
- * This plugin can be used in a `serwist/strategies` Strategy to regularly enforce a
49
+ * This plugin can be used in a {@linkcode Strategy} to regularly enforce a
48
50
  * limit on the age and/or the number of cached requests.
49
51
  *
50
- * It can only be used with Strategy instances that have a custom `cacheName` property set.
52
+ * It can only be used with {@linkcode Strategy} instances that have a custom `cacheName` property set.
51
53
  * In other words, it can't be used to expire entries in strategies that use the default runtime
52
54
  * cache name.
53
55
  *
@@ -143,11 +145,10 @@ export class ExpirationPlugin implements SerwistPlugin {
143
145
  }
144
146
 
145
147
  /**
146
- * A "lifecycle" callback that will be triggered automatically by the
147
- * `serwist/strategies` handlers when a `Response` is about to be returned
148
- * from a [Cache](https://developer.mozilla.org/en-US/docs/Web/API/Cache) to
149
- * the handler. It allows the `Response` to be inspected for freshness and
150
- * prevents it from being used if the `Response`'s `Date` header value is
148
+ * A lifecycle callback that will be triggered automatically when a
149
+ * response is about to be returned from a [`Cache`](https://developer.mozilla.org/en-US/docs/Web/API/Cache).
150
+ * It allows the response to be inspected for freshness and
151
+ * prevents it from being used if the response's `Date` header value is
151
152
  * older than the configured `maxAgeSeconds`.
152
153
  *
153
154
  * @param options
@@ -247,8 +248,8 @@ export class ExpirationPlugin implements SerwistPlugin {
247
248
  }
248
249
 
249
250
  /**
250
- * A "lifecycle" callback that will be triggered automatically by the
251
- * `serwist/strategies` handlers when an entry is added to a cache.
251
+ * A lifecycle callback that will be triggered automatically when an entry is added
252
+ * to a cache.
252
253
  *
253
254
  * @param options
254
255
  * @private
@@ -28,14 +28,14 @@ export interface PrecacheFallbackPluginOptions {
28
28
  */
29
29
  fallbackUrls: (string | PrecacheFallbackEntry)[];
30
30
  /**
31
- * Your `Serwist` instance.
31
+ * A {@linkcode Serwist} instance.
32
32
  */
33
33
  serwist: Serwist;
34
34
  }
35
35
 
36
36
  /**
37
- * `PrecacheFallbackPlugin` allows you to specify offline fallbacks
38
- * to be used when a given strategy is unable to generate a response.
37
+ * Allows you to specify offline fallbacks to be used when a given strategy
38
+ * is unable to generate a response.
39
39
  *
40
40
  * It does this by intercepting the `handlerDidError` plugin callback
41
41
  * and returning a precached response, taking the expected revision parameter
@@ -46,7 +46,7 @@ export class PrecacheFallbackPlugin implements SerwistPlugin {
46
46
  private readonly _serwist: Serwist;
47
47
 
48
48
  /**
49
- * Constructs a new `PrecacheFallbackPlugin` with the associated `fallbackUrls`.
49
+ * Constructs a new instance with the associated `fallbackUrls`.
50
50
  *
51
51
  * @param config
52
52
  */
@@ -10,8 +10,7 @@ import type { SerwistPlugin } from "../../types.js";
10
10
  import { createPartialResponse } from "./createPartialResponse.js";
11
11
 
12
12
  /**
13
- * The range request plugin makes it easy for a request with a 'Range' header to
14
- * be fulfilled by a cached response.
13
+ * Makes it easy for a request with a `Range` header to be fulfilled by a cached response.
15
14
  *
16
15
  * It does this by intercepting the `cachedResponseWillBeUsed` plugin callback
17
16
  * and returning the appropriate subset of the cached response body.
@@ -19,8 +18,8 @@ import { createPartialResponse } from "./createPartialResponse.js";
19
18
  export class RangeRequestsPlugin implements SerwistPlugin {
20
19
  /**
21
20
  * @param options
22
- * @returns If request contains a 'Range' header, then a
23
- * new response with status 206 whose body is a subset of `cachedResponse` is
21
+ * @returns If request contains a `Range` header, then a
22
+ * partial response whose body is a subset of `cachedResponse` is
24
23
  * returned. Otherwise, `cachedResponse` is returned as-is.
25
24
  * @private
26
25
  */
@@ -13,20 +13,20 @@ import { calculateEffectiveBoundaries } from "./utils/calculateEffectiveBoundari
13
13
  import { parseRangeHeader } from "./utils/parseRangeHeader.js";
14
14
 
15
15
  /**
16
- * Given a `Request` and `Response` objects as input, this will return a
17
- * promise for a new `Response`.
16
+ * Given a request and a response, this will return a
17
+ * promise that resolves to a partial response.
18
18
  *
19
- * If the original `Response` already contains partial content (i.e. it has
20
- * a status of 206), then this assumes it already fulfills the `Range:`
19
+ * If the original response already contains partial content (i.e. it has
20
+ * a status of 206), then this assumes it already fulfills the `Range`
21
21
  * requirements, and will return it as-is.
22
22
  *
23
- * @param request A request, which should contain a Range:
23
+ * @param request A request, which should contain a `Range`
24
24
  * header.
25
25
  * @param originalResponse A response.
26
26
  * @returns Either a `206 Partial Content` response, with
27
27
  * the response body set to the slice of content specified by the request's
28
- * `Range:` header, or a `416 Range Not Satisfiable` response if the
29
- * conditions of the `Range:` header can't be met.
28
+ * `Range` header, or a `416 Range Not Satisfiable` response if the
29
+ * conditions of the `Range` header can't be met.
30
30
  */
31
31
  export const createPartialResponse = async (request: Request, originalResponse: Response): Promise<Response> => {
32
32
  try {
@@ -10,9 +10,9 @@ import { SerwistError } from "../../../utils/SerwistError.js";
10
10
  import { assert } from "../../../utils/assert.js";
11
11
 
12
12
  /**
13
- * @param rangeHeader A Range: header value.
13
+ * @param rangeHeader A `Range` header value.
14
14
  * @returns An object with `start` and `end` properties, reflecting
15
- * the parsed value of the Range: header. If either the `start` or `end` are
15
+ * the parsed value of the `Range` header. If either the `start` or `end` are
16
16
  * omitted, then `null` will be returned.
17
17
  * @private
18
18
  */
@@ -22,7 +22,7 @@ import { messages } from "./utils/messages.js";
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
25
- * a `SerwistError` exception.
25
+ * a {@linkcode SerwistError} exception.
26
26
  */
27
27
  export class CacheFirst extends Strategy {
28
28
  /**
@@ -19,7 +19,7 @@ import { messages } from "./utils/messages.js";
19
19
  *
20
20
  * This class is useful if you already have your own precaching step.
21
21
  *
22
- * If there is no cache match, this will throw a `SerwistError` exception.
22
+ * If there is no cache match, this will throw a {@linkcode SerwistError} exception.
23
23
  */
24
24
  export class CacheOnly extends Strategy {
25
25
  /**
@@ -32,7 +32,7 @@ export interface NetworkFirstOptions extends StrategyOptions {
32
32
  * support [CORS](https://enable-cors.org/).
33
33
  *
34
34
  * If the network request fails, and there is no cache match, this will throw
35
- * a `SerwistError` exception.
35
+ * a {@linkcode SerwistError} exception.
36
36
  */
37
37
  export class NetworkFirst extends Strategy {
38
38
  private readonly _networkTimeoutSeconds: number;
@@ -28,7 +28,7 @@ export interface NetworkOnlyOptions extends Omit<StrategyOptions, "cacheName" |
28
28
  *
29
29
  * This class is useful if you require specific requests to only be fulfilled from the network.
30
30
  *
31
- * If the network request fails, this will throw a `SerwistError` exception.
31
+ * If the network request fails, this will throw a {@linkcode SerwistError} exception.
32
32
  */
33
33
  export class NetworkOnly extends Strategy {
34
34
  private readonly _networkTimeoutSeconds: number;
@@ -12,6 +12,7 @@ import { SerwistError } from "../../utils/SerwistError.js";
12
12
  import { cacheNames as privateCacheNames } from "../../utils/cacheNames.js";
13
13
  import { getFriendlyURL } from "../../utils/getFriendlyURL.js";
14
14
  import { logger } from "../../utils/logger.js";
15
+ import type { Serwist } from "../../Serwist.js";
15
16
  import type { StrategyOptions } from "./Strategy.js";
16
17
  import { Strategy } from "./Strategy.js";
17
18
  import type { StrategyHandler } from "./StrategyHandler.js";
@@ -25,11 +26,11 @@ interface PrecacheStrategyOptions extends StrategyOptions {
25
26
  }
26
27
 
27
28
  /**
28
- * A `serwist/strategies.Strategy` implementation
29
- * specifically designed to both cache and fetch precached assets.
29
+ * A {@linkcode Strategy} implementation specifically designed to both cache
30
+ * and fetch precached assets.
30
31
  *
31
32
  * Note: an instance of this class is created automatically when creating a
32
- * `Serwist` instance; it's generally not necessary to create this yourself.
33
+ * {@linkcode Serwist} instance; it's generally not necessary to create this yourself.
33
34
  */
34
35
  export class PrecacheStrategy extends Strategy {
35
36
  private readonly _fallbackToNetwork: boolean;
@@ -31,7 +31,7 @@ import { messages } from "./utils/messages.js";
31
31
  * support [CORS](https://enable-cors.org/).
32
32
  *
33
33
  * If the network request fails, and there is no cache match, this will throw
34
- * a `SerwistError` exception.
34
+ * a {@linkcode SerwistError} exception.
35
35
  */
36
36
  export class StaleWhileRevalidate extends Strategy {
37
37
  /**
@@ -66,13 +66,13 @@ export abstract class Strategy implements RouteHandlerObject {
66
66
  }
67
67
 
68
68
  /**
69
- * Performs a request strategy and returns a `Promise` that will resolve with
70
- * a `Response`, invoking all relevant plugin callbacks.
69
+ * Performs a request strategy and returns a promise that will resolve to
70
+ * a response, invoking all relevant plugin callbacks.
71
71
  *
72
- * When a strategy instance is registered with a `Route`, this method is automatically
72
+ * When a strategy instance is registered with a route, this method is automatically
73
73
  * called when the route matches.
74
74
  *
75
- * Alternatively, this method can be used in a standalone `FetchEvent`
75
+ * Alternatively, this method can be used in a standalone `fetch` event
76
76
  * listener by passing it to `event.respondWith()`.
77
77
  *
78
78
  * @param options A `FetchEvent` or an object with the properties listed below.
@@ -87,10 +87,10 @@ export abstract class Strategy implements RouteHandlerObject {
87
87
  }
88
88
 
89
89
  /**
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,
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
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
93
+ * promise that will resolve once all promises added to `event.waitUntil()` as a part
94
94
  * of performing the strategy have completed.
95
95
  *
96
96
  * You can await the `done` promise to ensure any extra work performed by
@@ -16,14 +16,15 @@ import { getFriendlyURL } from "../../utils/getFriendlyURL.js";
16
16
  import { logger } from "../../utils/logger.js";
17
17
  import { timeout } from "../../utils/timeout.js";
18
18
  import type { Strategy } from "./Strategy.js";
19
+ import type { Route } from "../../Route.js";
19
20
 
20
21
  function toRequest(input: RequestInfo) {
21
22
  return typeof input === "string" ? new Request(input) : input;
22
23
  }
23
24
 
24
25
  /**
25
- * A class created every time a `Strategy` instance calls `Strategy.handle` or
26
- * `Strategy.handleAll` that wraps all fetch and cache actions around plugin callbacks
26
+ * A class created every time a {@linkcode Strategy} instance calls {@linkcode Strategy.handle} or
27
+ * {@linkcode Strategy.handleAll} that wraps all fetch and cache actions around plugin callbacks
27
28
  * and keeps track of when the strategy is "done" (i.e. when all added `event.waitUntil()` promises
28
29
  * have resolved).
29
30
  */
@@ -41,15 +42,16 @@ export class StrategyHandler {
41
42
  * A `URL` instance of `request.url` (if passed to the strategy's
42
43
  * `handle()` or `handleAll()` method).
43
44
  * Note: the `url` param will be present if the strategy is invoked
44
- * from a `Route` object.
45
+ * from a {@linkcode Route} object.
45
46
  */
46
47
  public url?: URL;
47
48
  /**
48
49
  * Some additional params (if passed to the strategy's
49
50
  * `handle()` or `handleAll()` method).
51
+ *
50
52
  * 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
+ * from a {@linkcode Route} object and that route's matcher returned a truthy
54
+ * value (it will be that value).
53
55
  */
54
56
  public params?: string[] | MapLikeObject;
55
57
 
@@ -115,7 +117,7 @@ export class StrategyHandler {
115
117
  /**
116
118
  * Fetches a given request (and invokes any applicable plugin callback
117
119
  * methods), taking the `fetchOptions` (for non-navigation requests) and
118
- * `plugins` provided to the `Strategy` object into account.
120
+ * `plugins` provided to the {@linkcode Strategy} object into account.
119
121
  *
120
122
  * The following plugin lifecycle methods are invoked when using this method:
121
123
  * - `requestWillFetch()`
@@ -262,7 +264,7 @@ export class StrategyHandler {
262
264
  /**
263
265
  * Puts a request/response pair into the cache (and invokes any applicable
264
266
  * plugin callback method) using the `cacheName` and `plugins` provided to
265
- * the `Strategy` object.
267
+ * the {@linkcode Strategy} object.
266
268
  *
267
269
  * The following plugin lifecycle methods are invoked when using this method:
268
270
  * - `cacheKeyWillBeUsed`
@@ -271,7 +273,7 @@ export class StrategyHandler {
271
273
  *
272
274
  * @param key The request or URL to use as the cache key.
273
275
  * @param response The response to cache.
274
- * @returns `false` if a cacheWillUpdate caused the response
276
+ * @returns `false` if a `cacheWillUpdate` caused the response to
275
277
  * not be cached, and `true` otherwise.
276
278
  */
277
279
  async cachePut(key: RequestInfo, response: Response): Promise<boolean> {
@@ -369,7 +371,7 @@ export class StrategyHandler {
369
371
  }
370
372
 
371
373
  /**
372
- * Checks the `plugins` provided to the `Strategy` object for `cacheKeyWillBeUsed`
374
+ * Checks the `plugins` provided to the {@linkcode Strategy} object for `cacheKeyWillBeUsed`
373
375
  * callbacks and executes found callbacks in sequence. The final `Request`
374
376
  * object returned by the last plugin is treated as the cache key for cache
375
377
  * reads and/or writes. If no `cacheKeyWillBeUsed` plugin callbacks have
@@ -422,7 +424,7 @@ export class StrategyHandler {
422
424
  *
423
425
  * Note: since this method runs all plugins, it's not suitable for cases
424
426
  * where the return value of a callback needs to be applied prior to calling
425
- * the next callback. See `serwist/strategies.iterateCallbacks` for how to handle that case.
427
+ * the next callback. See {@linkcode StrategyHandler.iterateCallbacks} for how to handle that case.
426
428
  *
427
429
  * @param name The name of the callback to run within each plugin.
428
430
  * @param param The object to pass as the first (and only) param when executing each callback. This object will be merged with the
@@ -463,9 +465,7 @@ export class StrategyHandler {
463
465
  * [extend lifetime promises](https://w3c.github.io/ServiceWorker/#extendableevent-extend-lifetime-promises)
464
466
  * of the event event associated with the request being handled (usually a `FetchEvent`).
465
467
  *
466
- * Note: you can await
467
- * `serwist/strategies.StrategyHandler.doneWaiting`
468
- * to know when all added promises have settled.
468
+ * Note: you can await {@linkcode StrategyHandler.doneWaiting} to know when all added promises have settled.
469
469
  *
470
470
  * @param promise A promise to add to the extend lifetime promises of
471
471
  * the event that triggered the request.
@@ -526,20 +526,16 @@ export class StrategyHandler {
526
526
 
527
527
  if (!pluginsUsed) {
528
528
  if (responseToCache && responseToCache.status !== 200) {
529
- responseToCache = undefined;
530
- }
531
- if (process.env.NODE_ENV !== "production") {
532
- if (responseToCache) {
533
- if (responseToCache.status !== 200) {
534
- if (responseToCache.status === 0) {
535
- logger.warn(
536
- `The response for '${this.request.url}' is an opaque response. The caching strategy that you're using will not cache opaque responses by default.`,
537
- );
538
- } else {
539
- logger.debug(`The response for '${this.request.url}' returned a status code of '${response.status}' and won't be cached as a result.`);
540
- }
529
+ if (process.env.NODE_ENV !== "production") {
530
+ if (responseToCache.status === 0) {
531
+ logger.warn(
532
+ `The response for '${this.request.url}' is an opaque response. The caching strategy that you're using will not cache opaque responses by default.`,
533
+ );
534
+ } else {
535
+ logger.debug(`The response for '${this.request.url}' returned a status code of '${response.status}' and won't be cached as a result.`);
541
536
  }
542
537
  }
538
+ responseToCache = undefined;
543
539
  }
544
540
  }
545
541
 
package/src/types.ts CHANGED
@@ -1,4 +1,6 @@
1
1
  import type { HTTPMethod } from "./constants.js";
2
+ import type { Route } from "./Route.js";
3
+ import type { Serwist } from "./Serwist.js";
2
4
 
3
5
  export type PromiseOrNot<T> = T | Promise<T>;
4
6
 
@@ -7,13 +9,13 @@ export interface MapLikeObject {
7
9
  }
8
10
 
9
11
  /**
10
- * Using a plain `MapLikeObject` for now, but could extend/restrict this
12
+ * Using a plain {@linkcode MapLikeObject} for now, but could extend/restrict this
11
13
  * in the future.
12
14
  */
13
15
  export type PluginState = MapLikeObject;
14
16
 
15
17
  /**
16
- * Options passed to a `RouteMatchCallback` function.
18
+ * Options passed to a {@linkcode RouteMatchCallback} function.
17
19
  */
18
20
  export interface RouteMatchCallbackOptions {
19
21
  event: ExtendableEvent;
@@ -23,20 +25,22 @@ export interface RouteMatchCallbackOptions {
23
25
  }
24
26
 
25
27
  /**
26
- * The "match" callback is used to determine if a `Route` should apply for a
27
- * particular URL and request. When matching occurs in response to a fetch
28
- * event from the client, the `event` object is also supplied. However, since
29
- * the match callback can be invoked outside of a fetch event, matching logic
30
- * should not assume the `event` object will always be available.
28
+ * The match callback is used to determine if a route should apply for a
29
+ * particular URL and request.
30
+ *
31
+ * When matching occurs in response to a `fetch` event from the client, the `event`
32
+ * object is also supplied. However, since the match callback can be invoked outside
33
+ * of a `fetch` event, matching logic should not assume the `event` object will always
34
+ * be available.
35
+ *
31
36
  * If the match callback returns a truthy value, the matching route's
32
- * `RouteHandlerCallback` will be invoked immediately. If the value returned
33
- * is a non-empty array or object, that value will be set on the handler's
34
- * `options.params` argument.
37
+ * handler will be invoked immediately. If the value returned is a non-empty array
38
+ * or object, that value will be the handler's `options.params` argument.
35
39
  */
36
40
  export type RouteMatchCallback = (options: RouteMatchCallbackOptions) => any;
37
41
 
38
42
  /**
39
- * Options passed to a `RouteHandlerCallback` function.
43
+ * Options passed to a {@linkcode RouteHandlerCallback} function.
40
44
  */
41
45
  export interface RouteHandlerCallbackOptions {
42
46
  /**
@@ -51,7 +55,7 @@ export interface RouteHandlerCallbackOptions {
51
55
  params?: string[] | MapLikeObject;
52
56
  }
53
57
  /**
54
- * Options passed to a `ManualHandlerCallback` function.
58
+ * Options passed to a {@linkcode ManualHandlerCallback} function.
55
59
  */
56
60
  export interface ManualHandlerCallbackOptions {
57
61
  /**
@@ -64,7 +68,7 @@ export interface ManualHandlerCallbackOptions {
64
68
  request: Request | string;
65
69
  url?: never;
66
70
  /**
67
- * The return value from `serwist.matchCallback` (if applicable).
71
+ * The return value from {@linkcode RouteMatchCallback} (if applicable).
68
72
  */
69
73
  params?: never;
70
74
  }
@@ -72,38 +76,37 @@ export interface ManualHandlerCallbackOptions {
72
76
  export type HandlerCallbackOptions = RouteHandlerCallbackOptions | ManualHandlerCallbackOptions;
73
77
 
74
78
  /**
75
- * The "handler" callback is invoked whenever a `Router` matches a URL/Request
76
- * to a `Route` via its `RouteMatchCallback`. This handler callback should
77
- * return a `Promise` that resolves with a `Response`.
79
+ * The "handler" callback is invoked whenever a request is matched to a
80
+ * {@linkcode Route} via its {@linkcode RouteMatchCallback} This handler
81
+ * callback should return a promise that resolves to a response.
78
82
  *
79
- * If a non-empty array or object is returned by the `RouteMatchCallback` it
83
+ * If a non-empty array or object is returned by the matcher, it
80
84
  * will be passed in as this handler's `options.params` argument.
81
85
  */
82
86
  export type RouteHandlerCallback = (options: RouteHandlerCallbackOptions) => Promise<Response>;
83
87
 
84
88
  /**
85
- * The "handler" callback is invoked whenever a `Router` matches a URL/Request
86
- * to a `Route` via its `RouteMatchCallback`. This handler callback should
87
- * return a `Promise` that resolves with a `Response`.
89
+ * The "handler" callback is invoked whenever a request is matched to a
90
+ * {@linkcode Route} via its {@linkcode RouteMatchCallback} This handler
91
+ * callback should return a promise that resolves to a response.
88
92
  *
89
- * If a non-empty array or object is returned by the `RouteMatchCallback` it
93
+ * If a non-empty array or object is returned by the matcher, it
90
94
  * will be passed in as this handler's `options.params` argument.
91
95
  */
92
96
  export type ManualHandlerCallback = (options: ManualHandlerCallbackOptions) => Promise<Response>;
93
97
 
94
98
  /**
95
- * An object with a `handle` method of type `RouteHandlerCallback`.
99
+ * An object with a `handle` method of type {@linkcode RouteHandlerCallback}.
96
100
  *
97
- * A `Route` object can be created with either an `RouteHandlerCallback`
98
- * function or this `RouteHandler` object. The benefit of the `RouteHandler`
99
- * is it can be extended (as is done by the `serwist/strategies` package).
101
+ * A {@linkcode Route} object can be created with either an `RouteHandlerCallback`
102
+ * function or this {@linkcode RouteHandler} object.
100
103
  */
101
104
  export interface RouteHandlerObject {
102
105
  handle: RouteHandlerCallback;
103
106
  }
104
107
 
105
108
  /**
106
- * Either a `RouteHandlerCallback` or a `RouteHandlerObject`.
109
+ * Either a {@linkcode RouteHandlerCallback} or a {@linkcode RouteHandlerObject}.
107
110
  * Most APIs that accept route handlers take either.
108
111
  */
109
112
  export type RouteHandler = RouteHandlerCallback | RouteHandlerObject;
@@ -123,11 +126,11 @@ export interface CacheDidUpdateCallbackParam {
123
126
  */
124
127
  cacheName: string;
125
128
  /**
126
- * Possibly updated response to compare.
129
+ * The possibly updated response.
127
130
  */
128
131
  newResponse: Response;
129
132
  /**
130
- * The `Request` object for the cached entry.
133
+ * The request for the cached entry.
131
134
  */
132
135
  request: Request;
133
136
  /**
@@ -135,7 +138,7 @@ export interface CacheDidUpdateCallbackParam {
135
138
  */
136
139
  event: ExtendableEvent;
137
140
  /**
138
- * Cached response to compare.
141
+ * The previous cached response.
139
142
  */
140
143
  oldResponse?: Response | null;
141
144
  state?: PluginState;
@@ -300,19 +303,19 @@ export interface RuntimeCaching {
300
303
  */
301
304
  method?: HTTPMethod;
302
305
  /**
303
- * This match criteria determines whether the configured handler will
304
- * generate a response for any requests that don't match one of the precached
305
- * URLs. If multiple `RuntimeCaching` routes are defined, then the first one
306
- * whose `matcher` matches will be the one that responds.
306
+ * The match callback determines whether the configured handler will be used to
307
+ * generate a response for any request that doesn't match one of the precached
308
+ * URLs. If multiple routes are defined, then the first one to match the request
309
+ * will be the one that responds.
307
310
  *
308
311
  * This value directly maps to the first parameter passed to
309
- * `Serwist.registerRoute`. It's recommended to use a
310
- * `serwist.RouteMatchCallback` function for greatest flexibility.
312
+ * {@linkcode Serwist.registerRoute}. It's recommended to use a
313
+ * {@linkcode RouteMatchCallback} function for greatest flexibility.
311
314
  */
312
315
  matcher: RegExp | string | RouteMatchCallback;
313
316
  /**
314
317
  * This determines how the runtime route will generate a response. It
315
- * can be a `serwist.RouteHandler` callback function with custom
318
+ * can be a {@linkcode RouteHandler} callback function with custom
316
319
  * response logic.
317
320
  */
318
321
  handler: RouteHandler;
@@ -5,6 +5,7 @@
5
5
  license that can be found in the LICENSE file or at
6
6
  https://opensource.org/licenses/MIT.
7
7
  */
8
+ import type { initializeGoogleAnalytics } from "../lib/googleAnalytics/initializeGoogleAnalytics.js";
8
9
 
9
10
  declare let registration: ServiceWorkerRegistration | undefined;
10
11
 
@@ -34,7 +35,7 @@ export interface PartialCacheNameDetails {
34
35
  */
35
36
  runtime?: string;
36
37
  /**
37
- * The cache name to use for `@serwist/google-analytics` caching.
38
+ * The cache name to use for {@linkcode initializeGoogleAnalytics}.
38
39
  */
39
40
  googleAnalytics?: string;
40
41
  [propName: string]: string | undefined;
@@ -10,10 +10,10 @@ let supportStatus: boolean | undefined;
10
10
 
11
11
  /**
12
12
  * A utility function that determines whether the current browser supports
13
- * constructing a new `Response` from a `response.body` stream.
13
+ * constructing a new response from a `response.body` stream.
14
14
  *
15
15
  * @returns `true`, if the current browser can successfully construct
16
- * a `Response` from a `response.body` stream, `false` otherwise.
16
+ * a response from a `response.body` stream, `false` otherwise.
17
17
  * @private
18
18
  */
19
19
  function canConstructResponseFromBodyStream(): boolean {
@@ -6,15 +6,15 @@ import { SerwistError } from "./SerwistError.js";
6
6
  import { logger } from "./logger.js";
7
7
 
8
8
  /**
9
- * Parses a `RegExp`, string, or function with a caching strategy into a `Route`. This is for
10
- * when you want to create a `Route`, but you don't want to register it just yet: sometimes
11
- * you want to call `setCatchHandler` first, for example.
9
+ * Parses a `RegExp`, string, or function with a caching strategy into a {@linkcode Route}. This is for
10
+ * when you want to create a {@linkcode Route}, but you don't want to register it just yet: sometimes
11
+ * you want to call {@linkcode Route.setCatchHandler} first, for example.
12
12
  *
13
- * @param capture If the capture param is a `Route`, all other arguments will be ignored.
14
- * @param handler A callback function that returns a `Promise` resulting in a `Response`.
15
- * This parameter is required if `capture` is not a `Route` object.
16
- * @param method The HTTP method to match the `Route` against. Defaults to `'GET'`.
17
- * @returns The generated `Route`.
13
+ * @param capture If the capture param is a {@linkcode Route} object, all other arguments will be ignored.
14
+ * @param handler A callback function that returns a promise resulting in a response.
15
+ * This parameter is required if `capture` is not a {@linkcode Route} object.
16
+ * @param method The HTTP method to match the route against. Defaults to `'GET'`.
17
+ * @returns The generated {@linkcode Route}.
18
18
  */
19
19
  export const parseRoute = (capture: RegExp | string | RouteMatchCallback | Route, handler?: RouteHandler, method?: HTTPMethod): Route => {
20
20
  if (typeof capture === "string") {