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.
- package/dist/NavigationRoute.d.ts +2 -2
- package/dist/PrecacheRoute.d.ts +2 -3
- package/dist/PrecacheRoute.d.ts.map +1 -1
- package/dist/RegExpRoute.d.ts +2 -2
- package/dist/Route.d.ts +4 -4
- package/dist/Serwist.d.ts +19 -19
- package/dist/chunks/printInstallDetails.js +6 -10
- package/dist/copyResponse.d.ts.map +1 -1
- package/dist/legacy/PrecacheController.d.ts +1 -1
- package/dist/legacy/PrecacheFallbackPlugin.d.ts +6 -7
- package/dist/legacy/PrecacheFallbackPlugin.d.ts.map +1 -1
- package/dist/legacy/PrecacheRoute.d.ts +3 -3
- package/dist/legacy/Router.d.ts +23 -23
- package/dist/legacy/addRoute.d.ts +6 -8
- package/dist/legacy/addRoute.d.ts.map +1 -1
- package/dist/legacy/createHandlerBoundToURL.d.ts +5 -6
- package/dist/legacy/createHandlerBoundToURL.d.ts.map +1 -1
- package/dist/legacy/fallbacks.d.ts +3 -3
- package/dist/legacy/handlePrecaching.d.ts +3 -3
- package/dist/legacy/initializeGoogleAnalytics.d.ts +1 -1
- package/dist/legacy/installSerwist.d.ts +4 -4
- package/dist/legacy/matchPrecache.d.ts +4 -4
- package/dist/legacy/matchPrecache.d.ts.map +1 -1
- package/dist/legacy/precache.d.ts +2 -2
- package/dist/legacy/precache.d.ts.map +1 -1
- package/dist/legacy/precacheAndRoute.d.ts +3 -4
- package/dist/legacy/precacheAndRoute.d.ts.map +1 -1
- package/dist/legacy/registerRoute.d.ts +6 -6
- package/dist/legacy/registerRoute.d.ts.map +1 -1
- package/dist/legacy/registerRuntimeCaching.d.ts +1 -1
- package/dist/legacy/setCatchHandler.d.ts +2 -2
- package/dist/legacy/setDefaultHandler.d.ts +3 -3
- package/dist/legacy/singletonPrecacheController.d.ts +7 -7
- package/dist/legacy/singletonRouter.d.ts +9 -9
- package/dist/legacy/unregisterRoute.d.ts +1 -1
- package/dist/legacy/unregisterRoute.d.ts.map +1 -1
- package/dist/lib/backgroundSync/BackgroundSyncPlugin.d.ts +3 -3
- package/dist/lib/backgroundSync/BackgroundSyncQueue.d.ts +1 -1
- package/dist/lib/backgroundSync/StorableRequest.d.ts +1 -1
- package/dist/lib/broadcastUpdate/BroadcastCacheUpdate.d.ts +1 -2
- package/dist/lib/broadcastUpdate/BroadcastCacheUpdate.d.ts.map +1 -1
- package/dist/lib/broadcastUpdate/BroadcastUpdatePlugin.d.ts +4 -7
- package/dist/lib/broadcastUpdate/BroadcastUpdatePlugin.d.ts.map +1 -1
- package/dist/lib/broadcastUpdate/types.d.ts +2 -1
- package/dist/lib/broadcastUpdate/types.d.ts.map +1 -1
- package/dist/lib/cacheableResponse/CacheableResponse.d.ts +6 -6
- package/dist/lib/cacheableResponse/CacheableResponsePlugin.d.ts +2 -2
- package/dist/lib/expiration/CacheExpiration.d.ts +2 -2
- package/dist/lib/expiration/ExpirationPlugin.d.ts +11 -11
- package/dist/lib/expiration/ExpirationPlugin.d.ts.map +1 -1
- package/dist/lib/precaching/PrecacheFallbackPlugin.d.ts +4 -4
- package/dist/lib/rangeRequests/RangeRequestsPlugin.d.ts +3 -4
- package/dist/lib/rangeRequests/RangeRequestsPlugin.d.ts.map +1 -1
- package/dist/lib/rangeRequests/createPartialResponse.d.ts +7 -7
- package/dist/lib/rangeRequests/createPartialResponse.d.ts.map +1 -1
- package/dist/lib/rangeRequests/utils/parseRangeHeader.d.ts +2 -2
- package/dist/lib/strategies/CacheFirst.d.ts +1 -1
- package/dist/lib/strategies/CacheOnly.d.ts +1 -1
- package/dist/lib/strategies/NetworkFirst.d.ts +1 -1
- package/dist/lib/strategies/NetworkOnly.d.ts +1 -1
- package/dist/lib/strategies/PrecacheStrategy.d.ts +3 -3
- package/dist/lib/strategies/PrecacheStrategy.d.ts.map +1 -1
- package/dist/lib/strategies/StaleWhileRevalidate.d.ts +1 -1
- package/dist/lib/strategies/Strategy.d.ts +7 -7
- package/dist/lib/strategies/StrategyHandler.d.ts +12 -13
- package/dist/lib/strategies/StrategyHandler.d.ts.map +1 -1
- package/dist/types.d.ts +37 -36
- package/dist/types.d.ts.map +1 -1
- package/dist/utils/cacheNames.d.ts +1 -1
- package/dist/utils/cacheNames.d.ts.map +1 -1
- package/dist/utils/canConstructResponseFromBodyStream.d.ts +2 -2
- package/dist/utils/deleteOutdatedCaches.d.ts.map +1 -1
- package/dist/utils/executeQuotaErrorCallbacks.d.ts.map +1 -1
- package/dist/utils/parseRoute.d.ts +8 -8
- package/dist/utils/waitUntil.d.ts.map +1 -1
- package/package.json +5 -5
- package/src/NavigationRoute.ts +2 -2
- package/src/PrecacheRoute.ts +2 -3
- package/src/RegExpRoute.ts +2 -2
- package/src/Route.ts +4 -4
- package/src/Serwist.ts +19 -19
- package/src/legacy/PrecacheController.ts +1 -1
- package/src/legacy/PrecacheFallbackPlugin.ts +6 -7
- package/src/legacy/PrecacheRoute.ts +3 -3
- package/src/legacy/Router.ts +23 -23
- package/src/legacy/addRoute.ts +6 -8
- package/src/legacy/createHandlerBoundToURL.ts +6 -6
- package/src/legacy/fallbacks.ts +3 -3
- package/src/legacy/handlePrecaching.ts +3 -3
- package/src/legacy/initializeGoogleAnalytics.ts +1 -1
- package/src/legacy/installSerwist.ts +4 -4
- package/src/legacy/matchPrecache.ts +5 -4
- package/src/legacy/precache.ts +4 -2
- package/src/legacy/precacheAndRoute.ts +3 -4
- package/src/legacy/registerRoute.ts +8 -7
- package/src/legacy/registerRuntimeCaching.ts +1 -1
- package/src/legacy/setCatchHandler.ts +2 -2
- package/src/legacy/setDefaultHandler.ts +3 -3
- package/src/legacy/singletonPrecacheController.ts +7 -7
- package/src/legacy/singletonRouter.ts +9 -9
- package/src/legacy/unregisterRoute.ts +2 -1
- package/src/lib/backgroundSync/BackgroundSyncPlugin.ts +3 -3
- package/src/lib/backgroundSync/BackgroundSyncQueue.ts +1 -1
- package/src/lib/backgroundSync/StorableRequest.ts +1 -1
- package/src/lib/broadcastUpdate/BroadcastCacheUpdate.ts +1 -2
- package/src/lib/broadcastUpdate/BroadcastUpdatePlugin.ts +4 -7
- package/src/lib/broadcastUpdate/types.ts +2 -1
- package/src/lib/cacheableResponse/CacheableResponse.ts +6 -6
- package/src/lib/cacheableResponse/CacheableResponsePlugin.ts +2 -2
- package/src/lib/expiration/CacheExpiration.ts +2 -2
- package/src/lib/expiration/ExpirationPlugin.ts +12 -11
- package/src/lib/precaching/PrecacheFallbackPlugin.ts +4 -4
- package/src/lib/rangeRequests/RangeRequestsPlugin.ts +3 -4
- package/src/lib/rangeRequests/createPartialResponse.ts +7 -7
- package/src/lib/rangeRequests/utils/parseRangeHeader.ts +2 -2
- package/src/lib/strategies/CacheFirst.ts +1 -1
- package/src/lib/strategies/CacheOnly.ts +1 -1
- package/src/lib/strategies/NetworkFirst.ts +1 -1
- package/src/lib/strategies/NetworkOnly.ts +1 -1
- package/src/lib/strategies/PrecacheStrategy.ts +4 -3
- package/src/lib/strategies/StaleWhileRevalidate.ts +1 -1
- package/src/lib/strategies/Strategy.ts +7 -7
- package/src/lib/strategies/StrategyHandler.ts +21 -25
- package/src/types.ts +39 -36
- package/src/utils/cacheNames.ts +2 -1
- package/src/utils/canConstructResponseFromBodyStream.ts +2 -2
- 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
|
|
21
|
-
*
|
|
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
|
|
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
|
|
147
|
-
*
|
|
148
|
-
*
|
|
149
|
-
*
|
|
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
|
|
251
|
-
*
|
|
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
|
-
*
|
|
31
|
+
* A {@linkcode Serwist} instance.
|
|
32
32
|
*/
|
|
33
33
|
serwist: Serwist;
|
|
34
34
|
}
|
|
35
35
|
|
|
36
36
|
/**
|
|
37
|
-
*
|
|
38
|
-
*
|
|
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
|
|
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
|
-
*
|
|
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
|
|
23
|
-
*
|
|
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
|
|
17
|
-
* promise
|
|
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
|
|
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
|
|
29
|
-
* conditions of the `Range
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
29
|
-
*
|
|
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
|
-
*
|
|
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
|
|
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
|
|
70
|
-
* a
|
|
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
|
|
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 `
|
|
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
|
|
91
|
-
* resolves to a
|
|
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
|
-
*
|
|
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
|
|
26
|
-
*
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
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
|
|
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
|
|
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
|
|
27
|
-
* particular URL and request.
|
|
28
|
-
*
|
|
29
|
-
*
|
|
30
|
-
*
|
|
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
|
-
*
|
|
33
|
-
*
|
|
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
|
|
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
|
|
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
|
|
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
|
|
76
|
-
*
|
|
77
|
-
* return a
|
|
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
|
|
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
|
|
86
|
-
*
|
|
87
|
-
* return a
|
|
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
|
|
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
|
|
99
|
+
* An object with a `handle` method of type {@linkcode RouteHandlerCallback}.
|
|
96
100
|
*
|
|
97
|
-
* A
|
|
98
|
-
* function or this
|
|
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
|
|
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
|
-
*
|
|
129
|
+
* The possibly updated response.
|
|
127
130
|
*/
|
|
128
131
|
newResponse: Response;
|
|
129
132
|
/**
|
|
130
|
-
* The
|
|
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
|
-
*
|
|
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
|
-
*
|
|
304
|
-
* generate a response for any
|
|
305
|
-
* URLs. If multiple
|
|
306
|
-
*
|
|
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
|
-
*
|
|
310
|
-
*
|
|
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
|
|
318
|
+
* can be a {@linkcode RouteHandler} callback function with custom
|
|
316
319
|
* response logic.
|
|
317
320
|
*/
|
|
318
321
|
handler: RouteHandler;
|
package/src/utils/cacheNames.ts
CHANGED
|
@@ -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
|
|
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
|
|
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
|
|
16
|
+
* a response from a `response.body` stream, `false` otherwise.
|
|
17
17
|
* @private
|
|
18
18
|
*/
|
|
19
19
|
function canConstructResponseFromBodyStream(): boolean {
|
package/src/utils/parseRoute.ts
CHANGED
|
@@ -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
|
|
10
|
-
* when you want to create a
|
|
11
|
-
* you want to call
|
|
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
|
|
14
|
-
* @param handler A callback function that returns a
|
|
15
|
-
* This parameter is required if `capture` is not a
|
|
16
|
-
* @param method The HTTP method to match the
|
|
17
|
-
* @returns The generated
|
|
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") {
|