@serwist/sw 9.0.0-preview.17 → 9.0.0-preview.18
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/{Serwist.d.ts → abstractions/Serwist.d.ts} +6 -15
- package/dist/abstractions/Serwist.d.ts.map +1 -0
- package/dist/abstractions/disableDevLogs.d.ts.map +1 -0
- package/dist/{fallbacks.d.ts → abstractions/fallbacks.d.ts} +2 -2
- package/dist/abstractions/fallbacks.d.ts.map +1 -0
- package/dist/{handlePrecaching.d.ts → abstractions/handlePrecaching.d.ts} +1 -1
- package/dist/abstractions/handlePrecaching.d.ts.map +1 -0
- package/dist/abstractions/installSerwist.d.ts.map +1 -0
- package/dist/abstractions/registerRuntimeCaching.d.ts.map +1 -0
- package/dist/{types.d.ts → abstractions/types.d.ts} +1 -1
- package/dist/abstractions/types.d.ts.map +1 -0
- package/dist/chunks/NavigationRoute.js +54 -0
- package/dist/chunks/NetworkOnly.js +193 -0
- package/dist/chunks/PrecacheFallbackPlugin.js +573 -0
- package/dist/chunks/Strategy.js +410 -0
- package/dist/chunks/getOrCreatePrecacheController.js +429 -0
- package/dist/chunks/precacheAndRoute.js +112 -0
- package/dist/chunks/registerRoute.js +408 -0
- package/dist/index.d.ts +8 -8
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +11 -9
- package/dist/index.plugins.d.ts +40 -11
- package/dist/index.plugins.d.ts.map +1 -1
- package/dist/index.plugins.js +671 -5
- package/dist/index.precaching.d.ts +24 -0
- package/dist/index.precaching.d.ts.map +1 -0
- package/dist/index.precaching.js +24 -0
- package/dist/index.routing.d.ts +13 -0
- package/dist/index.routing.d.ts.map +1 -0
- package/dist/index.routing.js +21 -0
- package/dist/index.strategies.d.ts +19 -2
- package/dist/index.strategies.d.ts.map +1 -1
- package/dist/index.strategies.js +146 -1
- package/dist/plugins/backgroundSync/BackgroundSyncPlugin.d.ts +23 -0
- package/dist/plugins/backgroundSync/BackgroundSyncPlugin.d.ts.map +1 -0
- package/dist/plugins/backgroundSync/Queue.d.ts +166 -0
- package/dist/plugins/backgroundSync/Queue.d.ts.map +1 -0
- package/dist/plugins/backgroundSync/QueueDb.d.ts +90 -0
- package/dist/plugins/backgroundSync/QueueDb.d.ts.map +1 -0
- package/dist/plugins/backgroundSync/QueueStore.d.ts +75 -0
- package/dist/plugins/backgroundSync/QueueStore.d.ts.map +1 -0
- package/dist/plugins/backgroundSync/StorableRequest.d.ts +51 -0
- package/dist/plugins/backgroundSync/StorableRequest.d.ts.map +1 -0
- package/dist/plugins/broadcastUpdate/BroadcastCacheUpdate.d.ts +45 -0
- package/dist/plugins/broadcastUpdate/BroadcastCacheUpdate.d.ts.map +1 -0
- package/dist/plugins/broadcastUpdate/BroadcastUpdatePlugin.d.ts +27 -0
- package/dist/plugins/broadcastUpdate/BroadcastUpdatePlugin.d.ts.map +1 -0
- package/dist/plugins/broadcastUpdate/constants.d.ts +5 -0
- package/dist/plugins/broadcastUpdate/constants.d.ts.map +1 -0
- package/dist/plugins/broadcastUpdate/responsesAreSame.d.ts +11 -0
- package/dist/plugins/broadcastUpdate/responsesAreSame.d.ts.map +1 -0
- package/dist/plugins/broadcastUpdate/types.d.ts +34 -0
- package/dist/plugins/broadcastUpdate/types.d.ts.map +1 -0
- package/dist/plugins/cacheableResponse/CacheableResponse.d.ts +40 -0
- package/dist/plugins/cacheableResponse/CacheableResponse.d.ts.map +1 -0
- package/dist/plugins/cacheableResponse/CacheableResponsePlugin.d.ts +27 -0
- package/dist/plugins/cacheableResponse/CacheableResponsePlugin.d.ts.map +1 -0
- package/dist/plugins/expiration/CacheExpiration.d.ts +66 -0
- package/dist/plugins/expiration/CacheExpiration.d.ts.map +1 -0
- package/dist/plugins/expiration/ExpirationPlugin.d.ts +116 -0
- package/dist/plugins/expiration/ExpirationPlugin.d.ts.map +1 -0
- package/dist/plugins/expiration/models/CacheTimestampsModel.d.ts +73 -0
- package/dist/plugins/expiration/models/CacheTimestampsModel.d.ts.map +1 -0
- package/dist/plugins/googleAnalytics/constants.d.ts +10 -0
- package/dist/plugins/googleAnalytics/constants.d.ts.map +1 -0
- package/dist/plugins/googleAnalytics/initialize.d.ts +26 -0
- package/dist/plugins/googleAnalytics/initialize.d.ts.map +1 -0
- package/dist/plugins/precaching/PrecacheFallbackPlugin.d.ts +54 -0
- package/dist/plugins/precaching/PrecacheFallbackPlugin.d.ts.map +1 -0
- package/dist/plugins/rangeRequests/RangeRequestsPlugin.d.ts +19 -0
- package/dist/plugins/rangeRequests/RangeRequestsPlugin.d.ts.map +1 -0
- package/dist/plugins/rangeRequests/createPartialResponse.d.ts +18 -0
- package/dist/plugins/rangeRequests/createPartialResponse.d.ts.map +1 -0
- package/dist/plugins/rangeRequests/utils/calculateEffectiveBoundaries.d.ts +14 -0
- package/dist/plugins/rangeRequests/utils/calculateEffectiveBoundaries.d.ts.map +1 -0
- package/dist/plugins/rangeRequests/utils/parseRangeHeader.d.ts +12 -0
- package/dist/plugins/rangeRequests/utils/parseRangeHeader.d.ts.map +1 -0
- package/dist/precaching/PrecacheController.d.ts +145 -0
- package/dist/precaching/PrecacheController.d.ts.map +1 -0
- package/dist/precaching/PrecacheRoute.d.ts +20 -0
- package/dist/precaching/PrecacheRoute.d.ts.map +1 -0
- package/dist/precaching/PrecacheStrategy.d.ts +68 -0
- package/dist/precaching/PrecacheStrategy.d.ts.map +1 -0
- package/dist/precaching/addPlugins.d.ts +8 -0
- package/dist/precaching/addPlugins.d.ts.map +1 -0
- package/dist/precaching/addRoute.d.ts +15 -0
- package/dist/precaching/addRoute.d.ts.map +1 -0
- package/dist/precaching/cleanupOutdatedCaches.d.ts +6 -0
- package/dist/precaching/cleanupOutdatedCaches.d.ts.map +1 -0
- package/dist/precaching/createHandlerBoundToURL.d.ts +17 -0
- package/dist/precaching/createHandlerBoundToURL.d.ts.map +1 -0
- package/dist/precaching/getCacheKeyForURL.d.ts +20 -0
- package/dist/precaching/getCacheKeyForURL.d.ts.map +1 -0
- package/dist/precaching/matchPrecache.d.ts +14 -0
- package/dist/precaching/matchPrecache.d.ts.map +1 -0
- package/dist/precaching/precache.d.ts +19 -0
- package/dist/precaching/precache.d.ts.map +1 -0
- package/dist/precaching/precacheAndRoute.d.ts +14 -0
- package/dist/precaching/precacheAndRoute.d.ts.map +1 -0
- package/dist/precaching/types.d.ts +37 -0
- package/dist/precaching/types.d.ts.map +1 -0
- package/dist/precaching/utils/PrecacheCacheKeyPlugin.d.ts +17 -0
- package/dist/precaching/utils/PrecacheCacheKeyPlugin.d.ts.map +1 -0
- package/dist/precaching/utils/PrecacheInstallReportPlugin.d.ts +15 -0
- package/dist/precaching/utils/PrecacheInstallReportPlugin.d.ts.map +1 -0
- package/dist/precaching/utils/createCacheKey.d.ts +16 -0
- package/dist/precaching/utils/createCacheKey.d.ts.map +1 -0
- package/dist/precaching/utils/deleteOutdatedCaches.d.ts +18 -0
- package/dist/precaching/utils/deleteOutdatedCaches.d.ts.map +1 -0
- package/dist/precaching/utils/generateURLVariations.d.ts +12 -0
- package/dist/precaching/utils/generateURLVariations.d.ts.map +1 -0
- package/dist/precaching/utils/getCacheKeyForURL.d.ts +14 -0
- package/dist/precaching/utils/getCacheKeyForURL.d.ts.map +1 -0
- package/dist/precaching/utils/getOrCreatePrecacheController.d.ts +7 -0
- package/dist/precaching/utils/getOrCreatePrecacheController.d.ts.map +1 -0
- package/dist/precaching/utils/printCleanupDetails.d.ts +6 -0
- package/dist/precaching/utils/printCleanupDetails.d.ts.map +1 -0
- package/dist/precaching/utils/printInstallDetails.d.ts +7 -0
- package/dist/precaching/utils/printInstallDetails.d.ts.map +1 -0
- package/dist/precaching/utils/removeIgnoredSearchParams.d.ts +12 -0
- package/dist/precaching/utils/removeIgnoredSearchParams.d.ts.map +1 -0
- package/dist/routing/NavigationRoute.d.ts +57 -0
- package/dist/routing/NavigationRoute.d.ts.map +1 -0
- package/dist/routing/RegExpRoute.d.ts +24 -0
- package/dist/routing/RegExpRoute.d.ts.map +1 -0
- package/dist/routing/Route.d.ts +33 -0
- package/dist/routing/Route.d.ts.map +1 -0
- package/dist/routing/Router.d.ts +124 -0
- package/dist/routing/Router.d.ts.map +1 -0
- package/dist/routing/registerRoute.d.ts +15 -0
- package/dist/routing/registerRoute.d.ts.map +1 -0
- package/dist/routing/setCatchHandler.d.ts +9 -0
- package/dist/routing/setCatchHandler.d.ts.map +1 -0
- package/dist/routing/setDefaultHandler.d.ts +12 -0
- package/dist/routing/setDefaultHandler.d.ts.map +1 -0
- package/dist/routing/unregisterRoute.d.ts +8 -0
- package/dist/routing/unregisterRoute.d.ts.map +1 -0
- package/dist/routing/utils/constants.d.ts +15 -0
- package/dist/routing/utils/constants.d.ts.map +1 -0
- package/dist/routing/utils/getOrCreateDefaultRouter.d.ts +10 -0
- package/dist/routing/utils/getOrCreateDefaultRouter.d.ts.map +1 -0
- package/dist/routing/utils/normalizeHandler.d.ts +10 -0
- package/dist/routing/utils/normalizeHandler.d.ts.map +1 -0
- package/dist/routing/utils/parseRoute.d.ts +5 -0
- package/dist/routing/utils/parseRoute.d.ts.map +1 -0
- package/dist/strategies/CacheFirst.d.ts +23 -0
- package/dist/strategies/CacheFirst.d.ts.map +1 -0
- package/dist/strategies/CacheOnly.d.ts +20 -0
- package/dist/strategies/CacheOnly.d.ts.map +1 -0
- package/dist/strategies/NetworkFirst.d.ts +61 -0
- package/dist/strategies/NetworkFirst.d.ts.map +1 -0
- package/dist/strategies/NetworkOnly.d.ts +32 -0
- package/dist/strategies/NetworkOnly.d.ts.map +1 -0
- package/dist/strategies/StaleWhileRevalidate.d.ts +35 -0
- package/dist/strategies/StaleWhileRevalidate.d.ts.map +1 -0
- package/dist/strategies/Strategy.d.ts +83 -0
- package/dist/strategies/Strategy.d.ts.map +1 -0
- package/dist/strategies/StrategyHandler.d.ts +189 -0
- package/dist/strategies/StrategyHandler.d.ts.map +1 -0
- package/dist/strategies/plugins/cacheOkAndOpaquePlugin.d.ts +3 -0
- package/dist/strategies/plugins/cacheOkAndOpaquePlugin.d.ts.map +1 -0
- package/dist/strategies/utils/messages.d.ts +5 -0
- package/dist/strategies/utils/messages.d.ts.map +1 -0
- package/package.json +20 -14
- package/src/{Serwist.ts → abstractions/Serwist.ts} +16 -19
- package/src/{fallbacks.ts → abstractions/fallbacks.ts} +5 -5
- package/src/{handlePrecaching.ts → abstractions/handlePrecaching.ts} +6 -3
- package/src/{installSerwist.ts → abstractions/installSerwist.ts} +2 -2
- package/src/{registerRuntimeCaching.ts → abstractions/registerRuntimeCaching.ts} +1 -1
- package/src/{types.ts → abstractions/types.ts} +1 -1
- package/src/index.plugins.ts +65 -16
- package/src/index.precaching.ts +38 -0
- package/src/index.routing.ts +14 -0
- package/src/index.strategies.ts +22 -2
- package/src/index.ts +8 -8
- package/src/plugins/backgroundSync/BackgroundSyncPlugin.ts +39 -0
- package/src/plugins/backgroundSync/Queue.ts +438 -0
- package/src/plugins/backgroundSync/QueueDb.ts +176 -0
- package/src/plugins/backgroundSync/QueueStore.ts +161 -0
- package/src/plugins/backgroundSync/StorableRequest.ts +142 -0
- package/src/plugins/broadcastUpdate/BroadcastCacheUpdate.ts +159 -0
- package/src/plugins/broadcastUpdate/BroadcastUpdatePlugin.ts +43 -0
- package/src/plugins/broadcastUpdate/constants.ts +12 -0
- package/src/plugins/broadcastUpdate/responsesAreSame.ts +48 -0
- package/src/plugins/broadcastUpdate/types.ts +37 -0
- package/src/plugins/cacheableResponse/CacheableResponse.ts +141 -0
- package/src/plugins/cacheableResponse/CacheableResponsePlugin.ts +46 -0
- package/src/plugins/expiration/CacheExpiration.ts +192 -0
- package/src/plugins/expiration/ExpirationPlugin.ts +297 -0
- package/src/plugins/expiration/models/CacheTimestampsModel.ts +184 -0
- package/src/plugins/googleAnalytics/constants.ts +22 -0
- package/src/plugins/googleAnalytics/initialize.ts +200 -0
- package/src/plugins/precaching/PrecacheFallbackPlugin.ts +86 -0
- package/src/plugins/rangeRequests/RangeRequestsPlugin.ts +39 -0
- package/src/plugins/rangeRequests/createPartialResponse.ts +92 -0
- package/src/plugins/rangeRequests/utils/calculateEffectiveBoundaries.ts +58 -0
- package/src/plugins/rangeRequests/utils/parseRangeHeader.ts +54 -0
- package/src/precaching/PrecacheController.ts +332 -0
- package/src/precaching/PrecacheRoute.ts +50 -0
- package/src/precaching/PrecacheStrategy.ts +238 -0
- package/src/precaching/addPlugins.ts +21 -0
- package/src/precaching/addRoute.ts +30 -0
- package/src/precaching/cleanupOutdatedCaches.ts +33 -0
- package/src/precaching/createHandlerBoundToURL.ts +30 -0
- package/src/precaching/getCacheKeyForURL.ts +33 -0
- package/src/precaching/matchPrecache.ts +26 -0
- package/src/precaching/precache.ts +31 -0
- package/src/precaching/precacheAndRoute.ts +27 -0
- package/src/precaching/types.ts +46 -0
- package/src/precaching/utils/PrecacheCacheKeyPlugin.ts +36 -0
- package/src/precaching/utils/PrecacheInstallReportPlugin.ts +49 -0
- package/src/precaching/utils/createCacheKey.ts +68 -0
- package/src/precaching/utils/deleteOutdatedCaches.ts +40 -0
- package/src/precaching/utils/generateURLVariations.ts +55 -0
- package/src/precaching/utils/getCacheKeyForURL.ts +36 -0
- package/src/precaching/utils/getOrCreatePrecacheController.ts +22 -0
- package/src/precaching/utils/printCleanupDetails.ts +38 -0
- package/src/precaching/utils/printInstallDetails.ts +53 -0
- package/src/precaching/utils/removeIgnoredSearchParams.ts +29 -0
- package/src/routing/NavigationRoute.ts +119 -0
- package/src/routing/RegExpRoute.ts +74 -0
- package/src/routing/Route.ts +68 -0
- package/src/routing/Router.ts +432 -0
- package/src/routing/registerRoute.ts +33 -0
- package/src/routing/setCatchHandler.ts +22 -0
- package/src/routing/setDefaultHandler.ts +25 -0
- package/src/routing/unregisterRoute.ts +12 -0
- package/src/routing/utils/constants.ts +24 -0
- package/src/routing/utils/getOrCreateDefaultRouter.ts +29 -0
- package/src/routing/utils/normalizeHandler.ts +40 -0
- package/src/routing/utils/parseRoute.ts +67 -0
- package/src/strategies/CacheFirst.ts +87 -0
- package/src/strategies/CacheOnly.ts +58 -0
- package/src/strategies/NetworkFirst.ts +228 -0
- package/src/strategies/NetworkOnly.ts +96 -0
- package/src/strategies/StaleWhileRevalidate.ts +109 -0
- package/src/strategies/Strategy.ts +202 -0
- package/src/strategies/StrategyHandler.ts +557 -0
- package/src/strategies/plugins/cacheOkAndOpaquePlugin.ts +26 -0
- package/src/strategies/utils/messages.ts +20 -0
- package/dist/Serwist.d.ts.map +0 -1
- package/dist/disableDevLogs.d.ts.map +0 -1
- package/dist/fallbacks.d.ts.map +0 -1
- package/dist/handlePrecaching.d.ts.map +0 -1
- package/dist/installSerwist.d.ts.map +0 -1
- package/dist/registerRuntimeCaching.d.ts.map +0 -1
- package/dist/types.d.ts.map +0 -1
- /package/dist/{disableDevLogs.d.ts → abstractions/disableDevLogs.d.ts} +0 -0
- /package/dist/{installSerwist.d.ts → abstractions/installSerwist.d.ts} +0 -0
- /package/dist/{registerRuntimeCaching.d.ts → abstractions/registerRuntimeCaching.d.ts} +0 -0
- /package/src/{disableDevLogs.ts → abstractions/disableDevLogs.ts} +0 -0
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2018 Google LLC
|
|
3
|
+
|
|
4
|
+
Use of this source code is governed by an MIT-style
|
|
5
|
+
license that can be found in the LICENSE file or at
|
|
6
|
+
https://opensource.org/licenses/MIT.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { SerwistError, logger } from "@serwist/core/internal";
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Given two responses, compares several header values to see if they are
|
|
13
|
+
* the same or not.
|
|
14
|
+
*
|
|
15
|
+
* @param firstResponse The first response.
|
|
16
|
+
* @param secondResponse The second response.
|
|
17
|
+
* @param headersToCheck A list of headers to check.
|
|
18
|
+
* @returns
|
|
19
|
+
*/
|
|
20
|
+
export const responsesAreSame = (firstResponse: Response, secondResponse: Response, headersToCheck: string[]): boolean => {
|
|
21
|
+
if (process.env.NODE_ENV !== "production") {
|
|
22
|
+
if (!(firstResponse instanceof Response && secondResponse instanceof Response)) {
|
|
23
|
+
throw new SerwistError("invalid-responses-are-same-args");
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const atLeastOneHeaderAvailable = headersToCheck.some((header) => {
|
|
28
|
+
return firstResponse.headers.has(header) && secondResponse.headers.has(header);
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
if (!atLeastOneHeaderAvailable) {
|
|
32
|
+
if (process.env.NODE_ENV !== "production") {
|
|
33
|
+
logger.warn("Unable to determine where the response has been updated because none of the headers that would be checked are present.");
|
|
34
|
+
logger.debug("Attempting to compare the following: ", firstResponse, secondResponse, headersToCheck);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// Just return true, indicating the that responses are the same, since we
|
|
38
|
+
// can't determine otherwise.
|
|
39
|
+
return true;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
return headersToCheck.every((header) => {
|
|
43
|
+
const headerStateComparison = firstResponse.headers.has(header) === secondResponse.headers.has(header);
|
|
44
|
+
const headerValueComparison = firstResponse.headers.get(header) === secondResponse.headers.get(header);
|
|
45
|
+
|
|
46
|
+
return headerStateComparison && headerValueComparison;
|
|
47
|
+
});
|
|
48
|
+
};
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type { CacheDidUpdateCallbackParam } from "@serwist/core";
|
|
2
|
+
import type { CACHE_UPDATED_MESSAGE_META, CACHE_UPDATED_MESSAGE_TYPE } from "./constants.js";
|
|
3
|
+
|
|
4
|
+
export interface BroadcastCacheUpdateOptions {
|
|
5
|
+
/**
|
|
6
|
+
* A list of headers that will be used to determine whether the responses
|
|
7
|
+
* differ.
|
|
8
|
+
*
|
|
9
|
+
* @default ['content-length', 'etag', 'last-modified']
|
|
10
|
+
*/
|
|
11
|
+
headersToCheck?: string[];
|
|
12
|
+
/**
|
|
13
|
+
* A function whose return value
|
|
14
|
+
* will be used as the `payload` field in any cache update messages sent
|
|
15
|
+
* to the window clients.
|
|
16
|
+
* @param options
|
|
17
|
+
* @returns
|
|
18
|
+
*/
|
|
19
|
+
generatePayload?: (options: CacheDidUpdateCallbackParam) => Record<string, any>;
|
|
20
|
+
/**
|
|
21
|
+
* If `true` (the default) then all open clients will receive a message. If `false`,
|
|
22
|
+
* then only the client that make the original request will be notified of the update.
|
|
23
|
+
*
|
|
24
|
+
* @default true
|
|
25
|
+
*/
|
|
26
|
+
notifyAllClients?: boolean;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export type BroadcastPayload = Record<string, any>;
|
|
30
|
+
|
|
31
|
+
export type BroadcastPayloadGenerator = (options: CacheDidUpdateCallbackParam) => BroadcastPayload;
|
|
32
|
+
|
|
33
|
+
export interface BroadcastMessage {
|
|
34
|
+
type: typeof CACHE_UPDATED_MESSAGE_TYPE;
|
|
35
|
+
meta: typeof CACHE_UPDATED_MESSAGE_META;
|
|
36
|
+
payload: BroadcastPayload;
|
|
37
|
+
}
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2018 Google LLC
|
|
3
|
+
|
|
4
|
+
Use of this source code is governed by an MIT-style
|
|
5
|
+
license that can be found in the LICENSE file or at
|
|
6
|
+
https://opensource.org/licenses/MIT.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { assert, SerwistError, getFriendlyURL, logger } from "@serwist/core/internal";
|
|
10
|
+
|
|
11
|
+
export interface CacheableResponseOptions {
|
|
12
|
+
/**
|
|
13
|
+
* One or more status codes that a `Response` can have to be considered cacheable.
|
|
14
|
+
*/
|
|
15
|
+
statuses?: number[];
|
|
16
|
+
/**
|
|
17
|
+
* A mapping of header names and expected values that a `Response` can have and be
|
|
18
|
+
* considered cacheable. If multiple headers are provided, only one needs to be present.
|
|
19
|
+
*/
|
|
20
|
+
headers?: HeadersInit;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Allows you to set up rules determining what status codes and/or headers need
|
|
25
|
+
* to be present in order for a [`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response)
|
|
26
|
+
* to be considered cacheable.
|
|
27
|
+
*/
|
|
28
|
+
export class CacheableResponse {
|
|
29
|
+
private readonly _statuses?: CacheableResponseOptions["statuses"];
|
|
30
|
+
private readonly _headers?: Headers;
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* To construct a new CacheableResponse instance you must provide at least
|
|
34
|
+
* one of the `config` properties.
|
|
35
|
+
*
|
|
36
|
+
* If both `statuses` and `headers` are specified, then both conditions must
|
|
37
|
+
* be met for the `Response` to be considered cacheable.
|
|
38
|
+
*
|
|
39
|
+
* @param config
|
|
40
|
+
*/
|
|
41
|
+
constructor(config: CacheableResponseOptions = {}) {
|
|
42
|
+
if (process.env.NODE_ENV !== "production") {
|
|
43
|
+
if (!(config.statuses || config.headers)) {
|
|
44
|
+
throw new SerwistError("statuses-or-headers-required", {
|
|
45
|
+
moduleName: "@serwist/cacheable-response",
|
|
46
|
+
className: "CacheableResponse",
|
|
47
|
+
funcName: "constructor",
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
if (config.statuses) {
|
|
52
|
+
assert!.isArray(config.statuses, {
|
|
53
|
+
moduleName: "@serwist/cacheable-response",
|
|
54
|
+
className: "CacheableResponse",
|
|
55
|
+
funcName: "constructor",
|
|
56
|
+
paramName: "config.statuses",
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
if (config.headers) {
|
|
61
|
+
assert!.isType(config.headers, "object", {
|
|
62
|
+
moduleName: "@serwist/cacheable-response",
|
|
63
|
+
className: "CacheableResponse",
|
|
64
|
+
funcName: "constructor",
|
|
65
|
+
paramName: "config.headers",
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
this._statuses = config.statuses;
|
|
71
|
+
if (config.headers) {
|
|
72
|
+
this._headers = new Headers(config.headers);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Checks a response to see whether it's cacheable or not.
|
|
78
|
+
*
|
|
79
|
+
* @param response The response whose cacheability is being
|
|
80
|
+
* checked.
|
|
81
|
+
* @returns `true` if the `Response` is cacheable, and `false`
|
|
82
|
+
* otherwise.
|
|
83
|
+
*/
|
|
84
|
+
isResponseCacheable(response: Response): boolean {
|
|
85
|
+
if (process.env.NODE_ENV !== "production") {
|
|
86
|
+
assert!.isInstance(response, Response, {
|
|
87
|
+
moduleName: "@serwist/cacheable-response",
|
|
88
|
+
className: "CacheableResponse",
|
|
89
|
+
funcName: "isResponseCacheable",
|
|
90
|
+
paramName: "response",
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
let cacheable = true;
|
|
95
|
+
|
|
96
|
+
if (this._statuses) {
|
|
97
|
+
cacheable = this._statuses.includes(response.status);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
if (this._headers && cacheable) {
|
|
101
|
+
for (const [headerName, headerValue] of this._headers.entries()) {
|
|
102
|
+
if (response.headers.get(headerName) !== headerValue) {
|
|
103
|
+
cacheable = false;
|
|
104
|
+
break;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
if (process.env.NODE_ENV !== "production") {
|
|
110
|
+
if (!cacheable) {
|
|
111
|
+
logger.groupCollapsed(
|
|
112
|
+
`The request for '${getFriendlyURL(response.url)}' returned a response that does not meet the criteria for being cached.`,
|
|
113
|
+
);
|
|
114
|
+
|
|
115
|
+
logger.groupCollapsed("View cacheability criteria here.");
|
|
116
|
+
logger.log(`Cacheable statuses: ${JSON.stringify(this._statuses)}`);
|
|
117
|
+
logger.log(`Cacheable headers: ${JSON.stringify(this._headers, null, 2)}`);
|
|
118
|
+
logger.groupEnd();
|
|
119
|
+
|
|
120
|
+
const logFriendlyHeaders: { [key: string]: string } = {};
|
|
121
|
+
response.headers.forEach((value, key) => {
|
|
122
|
+
logFriendlyHeaders[key] = value;
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
logger.groupCollapsed("View response status and headers here.");
|
|
126
|
+
logger.log(`Response status: ${response.status}`);
|
|
127
|
+
logger.log(`Response headers: ${JSON.stringify(logFriendlyHeaders, null, 2)}`);
|
|
128
|
+
logger.groupEnd();
|
|
129
|
+
|
|
130
|
+
logger.groupCollapsed("View full response details here.");
|
|
131
|
+
logger.log(response.headers);
|
|
132
|
+
logger.log(response);
|
|
133
|
+
logger.groupEnd();
|
|
134
|
+
|
|
135
|
+
logger.groupEnd();
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
return cacheable;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2018 Google LLC
|
|
3
|
+
|
|
4
|
+
Use of this source code is governed by an MIT-style
|
|
5
|
+
license that can be found in the LICENSE file or at
|
|
6
|
+
https://opensource.org/licenses/MIT.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import type { SerwistPlugin } from "@serwist/core";
|
|
10
|
+
|
|
11
|
+
import type { CacheableResponseOptions } from "./CacheableResponse.js";
|
|
12
|
+
import { CacheableResponse } from "./CacheableResponse.js";
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* A class implementing the `cacheWillUpdate` lifecycle callback. This makes it
|
|
16
|
+
* easier to add in cacheability checks to requests made via Serwist's built-in
|
|
17
|
+
* strategies.
|
|
18
|
+
*/
|
|
19
|
+
export class CacheableResponsePlugin implements SerwistPlugin {
|
|
20
|
+
private readonly _cacheableResponse: CacheableResponse;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* To construct a new CacheableResponsePlugin instance you must provide at
|
|
24
|
+
* least one of the `config` properties.
|
|
25
|
+
*
|
|
26
|
+
* If both `statuses` and `headers` are specified, then both conditions must
|
|
27
|
+
* be met for the `Response` to be considered cacheable.
|
|
28
|
+
*
|
|
29
|
+
* @param config
|
|
30
|
+
*/
|
|
31
|
+
constructor(config: CacheableResponseOptions) {
|
|
32
|
+
this._cacheableResponse = new CacheableResponse(config);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* @param options
|
|
37
|
+
* @returns
|
|
38
|
+
* @private
|
|
39
|
+
*/
|
|
40
|
+
cacheWillUpdate: SerwistPlugin["cacheWillUpdate"] = async ({ response }) => {
|
|
41
|
+
if (this._cacheableResponse.isResponseCacheable(response)) {
|
|
42
|
+
return response;
|
|
43
|
+
}
|
|
44
|
+
return null;
|
|
45
|
+
};
|
|
46
|
+
}
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2018 Google LLC
|
|
3
|
+
|
|
4
|
+
Use of this source code is governed by an MIT-style
|
|
5
|
+
license that can be found in the LICENSE file or at
|
|
6
|
+
https://opensource.org/licenses/MIT.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { assert, SerwistError, logger } from "@serwist/core/internal";
|
|
10
|
+
|
|
11
|
+
import { CacheTimestampsModel } from "./models/CacheTimestampsModel.js";
|
|
12
|
+
|
|
13
|
+
interface CacheExpirationConfig {
|
|
14
|
+
/**
|
|
15
|
+
* The maximum number of entries to cache. Entries used the least will
|
|
16
|
+
* be removed as the maximum is reached.
|
|
17
|
+
*/
|
|
18
|
+
maxEntries?: number;
|
|
19
|
+
/**
|
|
20
|
+
* The maximum age of an entry before it's treated as stale and removed.
|
|
21
|
+
*/
|
|
22
|
+
maxAgeSeconds?: number;
|
|
23
|
+
/**
|
|
24
|
+
* The [`CacheQueryOptions`](https://developer.mozilla.org/en-US/docs/Web/API/Cache/delete#Parameters)
|
|
25
|
+
* that will be used when calling `delete()` on the cache.
|
|
26
|
+
*/
|
|
27
|
+
matchOptions?: CacheQueryOptions;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Allows you to expires cached responses based on age or maximum number of entries.
|
|
32
|
+
* @see https://serwist.pages.dev/docs/expiration/cache-expiration
|
|
33
|
+
*/
|
|
34
|
+
export class CacheExpiration {
|
|
35
|
+
private _isRunning = false;
|
|
36
|
+
private _rerunRequested = false;
|
|
37
|
+
private readonly _maxEntries?: number;
|
|
38
|
+
private readonly _maxAgeSeconds?: number;
|
|
39
|
+
private readonly _matchOptions?: CacheQueryOptions;
|
|
40
|
+
private readonly _cacheName: string;
|
|
41
|
+
private readonly _timestampModel: CacheTimestampsModel;
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* To construct a new CacheExpiration instance you must provide at least
|
|
45
|
+
* one of the `config` properties.
|
|
46
|
+
*
|
|
47
|
+
* @param cacheName Name of the cache to apply restrictions to.
|
|
48
|
+
* @param config
|
|
49
|
+
*/
|
|
50
|
+
constructor(cacheName: string, config: CacheExpirationConfig = {}) {
|
|
51
|
+
if (process.env.NODE_ENV !== "production") {
|
|
52
|
+
assert!.isType(cacheName, "string", {
|
|
53
|
+
moduleName: "@serwist/expiration",
|
|
54
|
+
className: "CacheExpiration",
|
|
55
|
+
funcName: "constructor",
|
|
56
|
+
paramName: "cacheName",
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
if (!(config.maxEntries || config.maxAgeSeconds)) {
|
|
60
|
+
throw new SerwistError("max-entries-or-age-required", {
|
|
61
|
+
moduleName: "@serwist/expiration",
|
|
62
|
+
className: "CacheExpiration",
|
|
63
|
+
funcName: "constructor",
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
if (config.maxEntries) {
|
|
68
|
+
assert!.isType(config.maxEntries, "number", {
|
|
69
|
+
moduleName: "@serwist/expiration",
|
|
70
|
+
className: "CacheExpiration",
|
|
71
|
+
funcName: "constructor",
|
|
72
|
+
paramName: "config.maxEntries",
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
if (config.maxAgeSeconds) {
|
|
77
|
+
assert!.isType(config.maxAgeSeconds, "number", {
|
|
78
|
+
moduleName: "@serwist/expiration",
|
|
79
|
+
className: "CacheExpiration",
|
|
80
|
+
funcName: "constructor",
|
|
81
|
+
paramName: "config.maxAgeSeconds",
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
this._maxEntries = config.maxEntries;
|
|
87
|
+
this._maxAgeSeconds = config.maxAgeSeconds;
|
|
88
|
+
this._matchOptions = config.matchOptions;
|
|
89
|
+
this._cacheName = cacheName;
|
|
90
|
+
this._timestampModel = new CacheTimestampsModel(cacheName);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Expires entries for the given cache and given criteria.
|
|
95
|
+
*/
|
|
96
|
+
async expireEntries(): Promise<void> {
|
|
97
|
+
if (this._isRunning) {
|
|
98
|
+
this._rerunRequested = true;
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
this._isRunning = true;
|
|
102
|
+
|
|
103
|
+
const minTimestamp = this._maxAgeSeconds ? Date.now() - this._maxAgeSeconds * 1000 : 0;
|
|
104
|
+
|
|
105
|
+
const urlsExpired = await this._timestampModel.expireEntries(minTimestamp, this._maxEntries);
|
|
106
|
+
|
|
107
|
+
// Delete URLs from the cache
|
|
108
|
+
const cache = await self.caches.open(this._cacheName);
|
|
109
|
+
for (const url of urlsExpired) {
|
|
110
|
+
await cache.delete(url, this._matchOptions);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
if (process.env.NODE_ENV !== "production") {
|
|
114
|
+
if (urlsExpired.length > 0) {
|
|
115
|
+
logger.groupCollapsed(
|
|
116
|
+
`Expired ${urlsExpired.length} ` +
|
|
117
|
+
`${urlsExpired.length === 1 ? "entry" : "entries"} and removed ` +
|
|
118
|
+
`${urlsExpired.length === 1 ? "it" : "them"} from the ` +
|
|
119
|
+
`'${this._cacheName}' cache.`,
|
|
120
|
+
);
|
|
121
|
+
logger.log(`Expired the following ${urlsExpired.length === 1 ? "URL" : "URLs"}:`);
|
|
122
|
+
for (const url of urlsExpired) {
|
|
123
|
+
logger.log(` ${url}`);
|
|
124
|
+
}
|
|
125
|
+
logger.groupEnd();
|
|
126
|
+
} else {
|
|
127
|
+
logger.debug("Cache expiration ran and found no entries to remove.");
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
this._isRunning = false;
|
|
132
|
+
if (this._rerunRequested) {
|
|
133
|
+
this._rerunRequested = false;
|
|
134
|
+
void this.expireEntries();
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Updates the timestamp for the given URL, allowing it to be correctly
|
|
140
|
+
* tracked by the class.
|
|
141
|
+
*
|
|
142
|
+
* @param url
|
|
143
|
+
*/
|
|
144
|
+
async updateTimestamp(url: string): Promise<void> {
|
|
145
|
+
if (process.env.NODE_ENV !== "production") {
|
|
146
|
+
assert!.isType(url, "string", {
|
|
147
|
+
moduleName: "@serwist/expiration",
|
|
148
|
+
className: "CacheExpiration",
|
|
149
|
+
funcName: "updateTimestamp",
|
|
150
|
+
paramName: "url",
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
await this._timestampModel.setTimestamp(url, Date.now());
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Checks if a URL has expired or not before it's used.
|
|
159
|
+
*
|
|
160
|
+
* This looks the timestamp up in IndexedDB and can be slow.
|
|
161
|
+
*
|
|
162
|
+
* Note: This method does not remove an expired entry, call
|
|
163
|
+
* `expireEntries()` to remove such entries instead.
|
|
164
|
+
*
|
|
165
|
+
* @param url
|
|
166
|
+
* @returns
|
|
167
|
+
*/
|
|
168
|
+
async isURLExpired(url: string): Promise<boolean> {
|
|
169
|
+
if (!this._maxAgeSeconds) {
|
|
170
|
+
if (process.env.NODE_ENV !== "production") {
|
|
171
|
+
throw new SerwistError("expired-test-without-max-age", {
|
|
172
|
+
methodName: "isURLExpired",
|
|
173
|
+
paramName: "maxAgeSeconds",
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
return false;
|
|
177
|
+
}
|
|
178
|
+
const timestamp = await this._timestampModel.getTimestamp(url);
|
|
179
|
+
const expireOlderThan = Date.now() - this._maxAgeSeconds * 1000;
|
|
180
|
+
return timestamp !== undefined ? timestamp < expireOlderThan : true;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* Removes the IndexedDB used to keep track of cache expiration metadata.
|
|
185
|
+
*/
|
|
186
|
+
async delete(): Promise<void> {
|
|
187
|
+
// Make sure we don't attempt another rerun if we're called in the middle of
|
|
188
|
+
// a cache expiration.
|
|
189
|
+
this._rerunRequested = false;
|
|
190
|
+
await this._timestampModel.expireEntries(Number.POSITIVE_INFINITY); // Expires all.
|
|
191
|
+
}
|
|
192
|
+
}
|