serwist 10.0.0-preview.7 → 10.0.0-preview.9
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/chunks/waitUntil.js +129 -129
- package/dist/index.d.ts +19 -21
- package/dist/index.d.ts.map +1 -1
- package/dist/index.internal.d.ts +2 -2
- package/dist/index.internal.d.ts.map +1 -1
- package/dist/index.internal.js +3 -3
- package/dist/index.js +2026 -1917
- package/dist/lib/{backgroundSync → background-sync}/BackgroundSyncPlugin.d.ts +1 -1
- package/dist/lib/background-sync/BackgroundSyncPlugin.d.ts.map +1 -0
- package/dist/lib/background-sync/BackgroundSyncQueue.d.ts.map +1 -0
- package/dist/lib/background-sync/BackgroundSyncQueueDb.d.ts.map +1 -0
- package/dist/lib/background-sync/BackgroundSyncQueueStore.d.ts.map +1 -0
- package/dist/lib/{backgroundSync → background-sync}/StorableRequest.d.ts +1 -1
- package/dist/lib/background-sync/StorableRequest.d.ts.map +1 -0
- package/dist/lib/background-sync/index.d.ts.map +1 -0
- package/dist/lib/{broadcastUpdate → broadcast-update}/BroadcastCacheUpdate.d.ts +1 -1
- package/dist/lib/broadcast-update/BroadcastCacheUpdate.d.ts.map +1 -0
- package/dist/lib/{broadcastUpdate → broadcast-update}/BroadcastUpdatePlugin.d.ts +1 -1
- package/dist/lib/broadcast-update/BroadcastUpdatePlugin.d.ts.map +1 -0
- package/dist/lib/broadcast-update/constants.d.ts.map +1 -0
- package/dist/lib/broadcast-update/index.d.ts.map +1 -0
- package/dist/lib/broadcast-update/responsesAreSame.d.ts.map +1 -0
- package/dist/lib/{broadcastUpdate → broadcast-update}/types.d.ts +1 -1
- package/dist/lib/broadcast-update/types.d.ts.map +1 -0
- package/dist/{cacheNames.d.ts → lib/cache-name.d.ts} +1 -1
- package/dist/lib/cache-name.d.ts.map +1 -0
- package/dist/lib/cacheable-response/CacheableResponse.d.ts.map +1 -0
- package/dist/lib/{cacheableResponse → cacheable-response}/CacheableResponsePlugin.d.ts +1 -1
- package/dist/lib/cacheable-response/CacheableResponsePlugin.d.ts.map +1 -0
- package/dist/lib/{cacheableResponse → cacheable-response}/index.d.ts +1 -1
- package/dist/lib/cacheable-response/index.d.ts.map +1 -0
- package/dist/{constants.d.ts → lib/constants.d.ts} +1 -1
- package/dist/lib/constants.d.ts.map +1 -0
- package/dist/lib/core.d.ts +62 -0
- package/dist/lib/core.d.ts.map +1 -0
- package/dist/lib/expiration/ExpirationPlugin.d.ts +1 -1
- package/dist/lib/expiration/ExpirationPlugin.d.ts.map +1 -1
- package/dist/lib/extension.d.ts +30 -0
- package/dist/lib/extension.d.ts.map +1 -0
- package/dist/lib/extensions/google-analytics/constants.d.ts.map +1 -0
- package/dist/lib/extensions/google-analytics/extension.d.ts +79 -0
- package/dist/lib/extensions/google-analytics/extension.d.ts.map +1 -0
- package/dist/lib/extensions/google-analytics/index.d.ts +2 -0
- package/dist/lib/extensions/google-analytics/index.d.ts.map +1 -0
- package/dist/lib/extensions/google-analytics/initialize.d.ts +12 -0
- package/dist/lib/extensions/google-analytics/initialize.d.ts.map +1 -0
- package/dist/lib/extensions/index.d.ts +14 -0
- package/dist/lib/extensions/index.d.ts.map +1 -0
- package/dist/lib/{controllers/PrecacheController/PrecacheController.d.ts → extensions/precache/extension.d.ts} +11 -34
- package/dist/lib/extensions/precache/extension.d.ts.map +1 -0
- package/dist/lib/extensions/precache/options.d.ts +26 -0
- package/dist/lib/extensions/precache/options.d.ts.map +1 -0
- package/dist/lib/{controllers/PrecacheController/PrecacheCacheKeyPlugin.d.ts → extensions/precache/plugin-cache-key.d.ts} +4 -4
- package/dist/lib/extensions/precache/plugin-cache-key.d.ts.map +1 -0
- package/dist/lib/{precaching/PrecacheFallbackPlugin.d.ts → extensions/precache/plugin-fallback.d.ts} +7 -12
- package/dist/lib/extensions/precache/plugin-fallback.d.ts.map +1 -0
- package/dist/lib/{controllers/PrecacheController/PrecacheInstallReportPlugin.d.ts → extensions/precache/plugin-install-report.d.ts} +2 -2
- package/dist/lib/extensions/precache/plugin-install-report.d.ts.map +1 -0
- package/dist/lib/extensions/precache/route.d.ts +42 -0
- package/dist/lib/extensions/precache/route.d.ts.map +1 -0
- package/dist/lib/{controllers/PrecacheController/PrecacheStrategy.d.ts → extensions/precache/strategy.d.ts} +2 -2
- package/dist/lib/extensions/precache/strategy.d.ts.map +1 -0
- package/dist/lib/{controllers/RuntimeCacheController.d.ts → extensions/runtime-cache.d.ts} +9 -8
- package/dist/lib/extensions/runtime-cache.d.ts.map +1 -0
- package/dist/lib/functions/handlers.d.ts +60 -0
- package/dist/lib/functions/handlers.d.ts.map +1 -0
- package/dist/lib/functions/router.d.ts +60 -0
- package/dist/lib/functions/router.d.ts.map +1 -0
- package/dist/{navigationPreload.d.ts → lib/navigation-preload.d.ts} +1 -1
- package/dist/lib/navigation-preload.d.ts.map +1 -0
- package/dist/lib/{rangeRequests → range-requests}/RangeRequestsPlugin.d.ts +1 -1
- package/dist/lib/range-requests/RangeRequestsPlugin.d.ts.map +1 -0
- package/dist/lib/range-requests/createPartialResponse.d.ts.map +1 -0
- package/dist/lib/{rangeRequests → range-requests}/index.d.ts +1 -1
- package/dist/lib/range-requests/index.d.ts.map +1 -0
- package/dist/lib/range-requests/utils/calculateEffectiveBoundaries.d.ts.map +1 -0
- package/dist/lib/range-requests/utils/parseRangeHeader.d.ts.map +1 -0
- package/dist/lib/route.d.ts +106 -0
- package/dist/lib/route.d.ts.map +1 -0
- package/dist/{Serwist.d.ts → lib/serwist.d.ts} +27 -40
- package/dist/lib/serwist.d.ts.map +1 -0
- package/dist/lib/strategies/NetworkFirst.d.ts.map +1 -1
- package/dist/lib/strategies/StaleWhileRevalidate.d.ts.map +1 -1
- package/dist/lib/strategies/Strategy.d.ts +1 -1
- package/dist/lib/strategies/Strategy.d.ts.map +1 -1
- package/dist/lib/strategies/StrategyHandler.d.ts +1 -1
- package/dist/lib/strategies/StrategyHandler.d.ts.map +1 -1
- package/dist/lib/strategies/plugins/cacheOkAndOpaquePlugin.d.ts +1 -1
- package/dist/lib/strategies/plugins/cacheOkAndOpaquePlugin.d.ts.map +1 -1
- package/dist/{types.d.ts → lib/types.d.ts} +15 -43
- package/dist/lib/types.d.ts.map +1 -0
- package/dist/{copyResponse.d.ts → lib/utils.d.ts} +22 -1
- package/dist/lib/utils.d.ts.map +1 -0
- package/dist/models/messages/messageGenerator.d.ts +1 -1
- package/dist/models/messages/messageGenerator.d.ts.map +1 -1
- package/dist/utils/SerwistError.d.ts +1 -1
- package/dist/utils/SerwistError.d.ts.map +1 -1
- package/dist/utils/assert.d.ts +1 -1
- package/dist/utils/assert.d.ts.map +1 -1
- package/dist/utils/cacheNames.d.ts.map +1 -1
- package/dist/utils/createCacheKey.d.ts +1 -1
- package/dist/utils/createCacheKey.d.ts.map +1 -1
- package/dist/utils/generateURLVariations.d.ts +1 -1
- package/dist/utils/generateURLVariations.d.ts.map +1 -1
- package/dist/utils/logger.d.ts +1 -1
- package/dist/utils/logger.d.ts.map +1 -1
- package/dist/utils/normalizeHandler.d.ts +1 -1
- package/dist/utils/normalizeHandler.d.ts.map +1 -1
- package/dist/utils/parseRoute.d.ts +3 -3
- package/dist/utils/parseRoute.d.ts.map +1 -1
- package/dist/utils/pluginUtils.d.ts +1 -1
- package/dist/utils/pluginUtils.d.ts.map +1 -1
- package/package.json +5 -5
- package/src/index.internal.ts +2 -2
- package/src/index.ts +51 -33
- package/src/lib/{backgroundSync → background-sync}/BackgroundSyncPlugin.ts +1 -1
- package/src/lib/{backgroundSync → background-sync}/BackgroundSyncQueue.ts +4 -4
- package/src/lib/{backgroundSync → background-sync}/BackgroundSyncQueueDb.ts +1 -1
- package/src/lib/{backgroundSync → background-sync}/BackgroundSyncQueueStore.ts +0 -2
- package/src/lib/{backgroundSync → background-sync}/StorableRequest.ts +1 -1
- package/src/lib/{broadcastUpdate → broadcast-update}/BroadcastCacheUpdate.ts +1 -1
- package/src/lib/{broadcastUpdate → broadcast-update}/BroadcastUpdatePlugin.ts +1 -1
- package/src/lib/{broadcastUpdate → broadcast-update}/types.ts +1 -1
- package/src/lib/{cacheableResponse → cacheable-response}/CacheableResponsePlugin.ts +1 -1
- package/src/lib/core.ts +128 -0
- package/src/lib/expiration/CacheExpiration.ts +1 -1
- package/src/lib/expiration/ExpirationPlugin.ts +4 -5
- package/src/lib/extension.ts +37 -0
- package/src/lib/{googleAnalytics/initializeGoogleAnalytics.ts → extensions/google-analytics/extension.ts} +40 -46
- package/src/lib/extensions/google-analytics/index.ts +0 -0
- package/src/lib/extensions/google-analytics/initialize.ts +48 -0
- package/src/lib/extensions/index.ts +13 -0
- package/src/lib/{controllers/PrecacheController/PrecacheController.ts → extensions/precache/extension.ts} +23 -45
- package/src/lib/{controllers/PrecacheController/parsePrecacheOptions.ts → extensions/precache/options.ts} +11 -8
- package/src/lib/{controllers/PrecacheController/PrecacheCacheKeyPlugin.ts → extensions/precache/plugin-cache-key.ts} +4 -4
- package/src/lib/{precaching/PrecacheFallbackPlugin.ts → extensions/precache/plugin-fallback.ts} +22 -21
- package/src/lib/{controllers/PrecacheController/PrecacheInstallReportPlugin.ts → extensions/precache/plugin-install-report.ts} +1 -1
- package/src/lib/extensions/precache/route.ts +72 -0
- package/src/lib/{controllers/PrecacheController/PrecacheStrategy.ts → extensions/precache/strategy.ts} +5 -5
- package/src/lib/{controllers/RuntimeCacheController.ts → extensions/runtime-cache.ts} +21 -14
- package/src/lib/functions/handlers.ts +149 -0
- package/src/lib/functions/router.ts +314 -0
- package/src/lib/{rangeRequests → range-requests}/RangeRequestsPlugin.ts +1 -1
- package/src/lib/route.ts +234 -0
- package/src/lib/serwist.ts +443 -0
- package/src/lib/strategies/CacheFirst.ts +2 -2
- package/src/lib/strategies/CacheOnly.ts +1 -1
- package/src/lib/strategies/NetworkFirst.ts +4 -4
- package/src/lib/strategies/NetworkOnly.ts +2 -2
- package/src/lib/strategies/StaleWhileRevalidate.ts +3 -3
- package/src/lib/strategies/Strategy.ts +6 -6
- package/src/lib/strategies/StrategyHandler.ts +5 -5
- package/src/lib/strategies/plugins/cacheOkAndOpaquePlugin.ts +1 -1
- package/src/{types.ts → lib/types.ts} +17 -57
- package/src/{copyResponse.ts → lib/utils.ts} +81 -2
- package/src/models/messages/messageGenerator.ts +1 -1
- package/src/utils/SerwistError.ts +1 -1
- package/src/utils/assert.ts +1 -2
- package/src/utils/cacheNames.ts +0 -2
- package/src/utils/canConstructReadableStream.ts +1 -1
- package/src/utils/canConstructResponseFromBodyStream.ts +1 -1
- package/src/utils/createCacheKey.ts +1 -2
- package/src/utils/generateURLVariations.ts +1 -1
- package/src/utils/normalizeHandler.ts +1 -1
- package/src/utils/parseRoute.ts +4 -5
- package/src/utils/pluginUtils.ts +1 -1
- package/src/utils/resultingClientExists.ts +1 -1
- package/dist/NavigationRoute.d.ts +0 -56
- package/dist/NavigationRoute.d.ts.map +0 -1
- package/dist/RegExpRoute.d.ts +0 -24
- package/dist/RegExpRoute.d.ts.map +0 -1
- package/dist/Route.d.ts +0 -33
- package/dist/Route.d.ts.map +0 -1
- package/dist/Serwist.d.ts.map +0 -1
- package/dist/cacheNames.d.ts.map +0 -1
- package/dist/constants.d.ts.map +0 -1
- package/dist/copyResponse.d.ts.map +0 -1
- package/dist/disableDevLogs.d.ts +0 -7
- package/dist/disableDevLogs.d.ts.map +0 -1
- package/dist/lib/backgroundSync/BackgroundSyncPlugin.d.ts.map +0 -1
- package/dist/lib/backgroundSync/BackgroundSyncQueue.d.ts.map +0 -1
- package/dist/lib/backgroundSync/BackgroundSyncQueueDb.d.ts.map +0 -1
- package/dist/lib/backgroundSync/BackgroundSyncQueueStore.d.ts.map +0 -1
- package/dist/lib/backgroundSync/StorableRequest.d.ts.map +0 -1
- package/dist/lib/backgroundSync/index.d.ts.map +0 -1
- package/dist/lib/broadcastUpdate/BroadcastCacheUpdate.d.ts.map +0 -1
- package/dist/lib/broadcastUpdate/BroadcastUpdatePlugin.d.ts.map +0 -1
- package/dist/lib/broadcastUpdate/constants.d.ts.map +0 -1
- package/dist/lib/broadcastUpdate/index.d.ts.map +0 -1
- package/dist/lib/broadcastUpdate/responsesAreSame.d.ts.map +0 -1
- package/dist/lib/broadcastUpdate/types.d.ts.map +0 -1
- package/dist/lib/cacheableResponse/CacheableResponse.d.ts.map +0 -1
- package/dist/lib/cacheableResponse/CacheableResponsePlugin.d.ts.map +0 -1
- package/dist/lib/cacheableResponse/index.d.ts.map +0 -1
- package/dist/lib/controllers/PrecacheController/PrecacheCacheKeyPlugin.d.ts.map +0 -1
- package/dist/lib/controllers/PrecacheController/PrecacheController.d.ts.map +0 -1
- package/dist/lib/controllers/PrecacheController/PrecacheInstallReportPlugin.d.ts.map +0 -1
- package/dist/lib/controllers/PrecacheController/PrecacheRoute.d.ts +0 -15
- package/dist/lib/controllers/PrecacheController/PrecacheRoute.d.ts.map +0 -1
- package/dist/lib/controllers/PrecacheController/PrecacheStrategy.d.ts.map +0 -1
- package/dist/lib/controllers/PrecacheController/parsePrecacheOptions.d.ts +0 -25
- package/dist/lib/controllers/PrecacheController/parsePrecacheOptions.d.ts.map +0 -1
- package/dist/lib/controllers/RuntimeCacheController.d.ts.map +0 -1
- package/dist/lib/controllers/index.d.ts +0 -4
- package/dist/lib/controllers/index.d.ts.map +0 -1
- package/dist/lib/googleAnalytics/constants.d.ts.map +0 -1
- package/dist/lib/googleAnalytics/index.d.ts +0 -3
- package/dist/lib/googleAnalytics/index.d.ts.map +0 -1
- package/dist/lib/googleAnalytics/initializeGoogleAnalytics.d.ts +0 -30
- package/dist/lib/googleAnalytics/initializeGoogleAnalytics.d.ts.map +0 -1
- package/dist/lib/precaching/PrecacheFallbackPlugin.d.ts.map +0 -1
- package/dist/lib/precaching/index.d.ts +0 -3
- package/dist/lib/precaching/index.d.ts.map +0 -1
- package/dist/lib/rangeRequests/RangeRequestsPlugin.d.ts.map +0 -1
- package/dist/lib/rangeRequests/createPartialResponse.d.ts.map +0 -1
- package/dist/lib/rangeRequests/index.d.ts.map +0 -1
- package/dist/lib/rangeRequests/utils/calculateEffectiveBoundaries.d.ts.map +0 -1
- package/dist/lib/rangeRequests/utils/parseRangeHeader.d.ts.map +0 -1
- package/dist/navigationPreload.d.ts.map +0 -1
- package/dist/registerQuotaErrorCallback.d.ts +0 -8
- package/dist/registerQuotaErrorCallback.d.ts.map +0 -1
- package/dist/setCacheNameDetails.d.ts +0 -9
- package/dist/setCacheNameDetails.d.ts.map +0 -1
- package/dist/types.d.ts.map +0 -1
- package/src/NavigationRoute.ts +0 -118
- package/src/RegExpRoute.ts +0 -74
- package/src/Route.ts +0 -67
- package/src/Serwist.ts +0 -766
- package/src/disableDevLogs.ts +0 -10
- package/src/lib/controllers/PrecacheController/PrecacheRoute.ts +0 -44
- package/src/lib/controllers/index.ts +0 -3
- package/src/lib/googleAnalytics/index.ts +0 -2
- package/src/lib/precaching/index.ts +0 -2
- package/src/registerQuotaErrorCallback.ts +0 -34
- package/src/setCacheNameDetails.ts +0 -53
- package/dist/lib/{backgroundSync → background-sync}/BackgroundSyncQueue.d.ts +0 -0
- package/dist/lib/{backgroundSync → background-sync}/BackgroundSyncQueueDb.d.ts +0 -0
- package/dist/lib/{backgroundSync → background-sync}/BackgroundSyncQueueStore.d.ts +0 -0
- package/dist/lib/{backgroundSync → background-sync}/index.d.ts +0 -0
- package/dist/lib/{broadcastUpdate → broadcast-update}/constants.d.ts +0 -0
- package/dist/lib/{broadcastUpdate → broadcast-update}/index.d.ts +0 -0
- package/dist/lib/{broadcastUpdate → broadcast-update}/responsesAreSame.d.ts +0 -0
- package/dist/lib/{cacheableResponse → cacheable-response}/CacheableResponse.d.ts +0 -0
- package/dist/lib/{googleAnalytics → extensions/google-analytics}/constants.d.ts +0 -0
- package/dist/lib/{rangeRequests → range-requests}/createPartialResponse.d.ts +0 -0
- package/dist/lib/{rangeRequests → range-requests}/utils/calculateEffectiveBoundaries.d.ts +0 -0
- package/dist/lib/{rangeRequests → range-requests}/utils/parseRangeHeader.d.ts +0 -0
- package/src/lib/{backgroundSync → background-sync}/index.ts +0 -0
- package/src/lib/{broadcastUpdate → broadcast-update}/constants.ts +0 -0
- package/src/lib/{broadcastUpdate → broadcast-update}/index.ts +0 -0
- package/src/lib/{broadcastUpdate → broadcast-update}/responsesAreSame.ts +1 -1
- package/src/{cacheNames.ts → lib/cache-name.ts} +0 -0
- package/src/lib/{cacheableResponse → cacheable-response}/CacheableResponse.ts +1 -1
- package/src/lib/{cacheableResponse → cacheable-response}/index.ts +1 -1
- package/src/{constants.ts → lib/constants.ts} +0 -0
- package/src/lib/{googleAnalytics → extensions/google-analytics}/constants.ts +0 -0
- package/src/{navigationPreload.ts → lib/navigation-preload.ts} +0 -0
- package/src/lib/{rangeRequests → range-requests}/createPartialResponse.ts +1 -1
- package/src/lib/{rangeRequests → range-requests}/index.ts +1 -1
- package/src/lib/{rangeRequests → range-requests}/utils/calculateEffectiveBoundaries.ts +1 -1
- package/src/lib/{rangeRequests → range-requests}/utils/parseRangeHeader.ts +1 -1
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { CacheDidUpdateCallbackParam } from "
|
|
1
|
+
import type { CacheDidUpdateCallbackParam } from "#lib/types.js";
|
|
2
2
|
import type { BROADCAST_UPDATE_MESSAGE_META, BROADCAST_UPDATE_MESSAGE_TYPE } from "./constants.js";
|
|
3
3
|
|
|
4
4
|
export interface BroadcastCacheUpdateOptions {
|
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
https://opensource.org/licenses/MIT.
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
|
-
import type { StrategyPlugin } from "
|
|
9
|
+
import type { StrategyPlugin } from "#lib/types.js";
|
|
10
10
|
import type { CacheableResponseOptions } from "./CacheableResponse.js";
|
|
11
11
|
import { CacheableResponse } from "./CacheableResponse.js";
|
|
12
12
|
|
package/src/lib/core.ts
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import { clientsClaim as clientsClaimImpl } from "#utils/clientsClaim.js";
|
|
2
|
+
import type { HTTPMethod } from "./constants.js";
|
|
3
|
+
import type { Extension } from "./extension.js";
|
|
4
|
+
import { Precache } from "./extensions/index.js";
|
|
5
|
+
import { iterateExtensions } from "./functions/handlers.js";
|
|
6
|
+
import { enableNavigationPreload } from "./navigation-preload.js";
|
|
7
|
+
import type { Route } from "./route.js";
|
|
8
|
+
import type { PrecacheEntry, PrecacheOptions, RouteHandlerObject } from "./types.js";
|
|
9
|
+
import { disableDevLogs as disableDevLogsImpl, setCacheNameDetails } from "./utils.js";
|
|
10
|
+
|
|
11
|
+
export interface Serwist {
|
|
12
|
+
readonly routes: Map<HTTPMethod, Route[]>;
|
|
13
|
+
readonly defaultHandlerMap: Map<HTTPMethod, RouteHandlerObject>;
|
|
14
|
+
readonly precache: Precache;
|
|
15
|
+
readonly extensions: Extension[];
|
|
16
|
+
catchHandler?: RouteHandlerObject;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export interface SerwistOptions {
|
|
20
|
+
/**
|
|
21
|
+
* Options to customize precaching.
|
|
22
|
+
*/
|
|
23
|
+
precache?: PrecacheOptions & {
|
|
24
|
+
entries: (PrecacheEntry | string)[] | undefined;
|
|
25
|
+
};
|
|
26
|
+
/**
|
|
27
|
+
* Forces the waiting service worker to become the active one.
|
|
28
|
+
*
|
|
29
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerGlobalScope/skipWaiting
|
|
30
|
+
*/
|
|
31
|
+
skipWaiting?: boolean;
|
|
32
|
+
/**
|
|
33
|
+
* Imports external scripts. They are executed in the order they
|
|
34
|
+
* are passed.
|
|
35
|
+
*
|
|
36
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/API/WorkerGlobalScope/importScripts
|
|
37
|
+
*/
|
|
38
|
+
importScripts?: string[];
|
|
39
|
+
/**
|
|
40
|
+
* Enables navigation preloading if it is supported.
|
|
41
|
+
*
|
|
42
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerRegistration/navigationPreload
|
|
43
|
+
*/
|
|
44
|
+
navigationPreload?: boolean;
|
|
45
|
+
/**
|
|
46
|
+
* Modifies the prefix of the default cache names used by Serwist packages.
|
|
47
|
+
*/
|
|
48
|
+
cacheId?: string | undefined;
|
|
49
|
+
/**
|
|
50
|
+
* Claims any currently available clients once the service worker
|
|
51
|
+
* becomes active. This is normally used in conjunction with `skipWaiting()`.
|
|
52
|
+
*
|
|
53
|
+
* @default false
|
|
54
|
+
*/
|
|
55
|
+
clientsClaim?: boolean;
|
|
56
|
+
/**
|
|
57
|
+
* Disables Serwist's logging in development mode.
|
|
58
|
+
*
|
|
59
|
+
* @default false
|
|
60
|
+
*/
|
|
61
|
+
disableDevLogs?: boolean;
|
|
62
|
+
/**
|
|
63
|
+
* A list of extensions.
|
|
64
|
+
*/
|
|
65
|
+
extensions?: Extension[];
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
declare const self: ServiceWorkerGlobalScope;
|
|
69
|
+
|
|
70
|
+
export const createSerwist = ({
|
|
71
|
+
precache,
|
|
72
|
+
skipWaiting = false,
|
|
73
|
+
importScripts,
|
|
74
|
+
navigationPreload = false,
|
|
75
|
+
cacheId,
|
|
76
|
+
clientsClaim = false,
|
|
77
|
+
disableDevLogs = false,
|
|
78
|
+
extensions = [],
|
|
79
|
+
}: SerwistOptions = {}): Serwist => {
|
|
80
|
+
if (importScripts?.length) self.importScripts(...importScripts);
|
|
81
|
+
|
|
82
|
+
if (navigationPreload) enableNavigationPreload();
|
|
83
|
+
|
|
84
|
+
if (cacheId) setCacheNameDetails({ prefix: cacheId });
|
|
85
|
+
|
|
86
|
+
if (skipWaiting) {
|
|
87
|
+
self.skipWaiting();
|
|
88
|
+
} else {
|
|
89
|
+
self.addEventListener("message", (event) => {
|
|
90
|
+
if (event.data?.type === "SKIP_WAITING") {
|
|
91
|
+
self.skipWaiting();
|
|
92
|
+
}
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
if (clientsClaim) clientsClaimImpl();
|
|
97
|
+
|
|
98
|
+
const precacheExtension = new Precache(precache ?? { entries: [] });
|
|
99
|
+
|
|
100
|
+
const routes = new Map<HTTPMethod, Route[]>();
|
|
101
|
+
|
|
102
|
+
const defaultHandlerMap = new Map<HTTPMethod, RouteHandlerObject>();
|
|
103
|
+
|
|
104
|
+
const exts = [precacheExtension, ...extensions];
|
|
105
|
+
|
|
106
|
+
const state: Serwist = {
|
|
107
|
+
get routes() {
|
|
108
|
+
return routes;
|
|
109
|
+
},
|
|
110
|
+
get defaultHandlerMap() {
|
|
111
|
+
return defaultHandlerMap;
|
|
112
|
+
},
|
|
113
|
+
get precache() {
|
|
114
|
+
return precacheExtension;
|
|
115
|
+
},
|
|
116
|
+
get extensions() {
|
|
117
|
+
return exts;
|
|
118
|
+
},
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
for (const callback of iterateExtensions(state, "init")) {
|
|
122
|
+
callback({ serwist: state });
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
if (disableDevLogs) disableDevLogsImpl();
|
|
126
|
+
|
|
127
|
+
return state;
|
|
128
|
+
};
|
|
@@ -6,9 +6,9 @@
|
|
|
6
6
|
https://opensource.org/licenses/MIT.
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
|
-
import { SerwistError } from "#utils/SerwistError.js";
|
|
10
9
|
import { assert } from "#utils/assert.js";
|
|
11
10
|
import { logger } from "#utils/logger.js";
|
|
11
|
+
import { SerwistError } from "#utils/SerwistError.js";
|
|
12
12
|
import { CacheTimestampsModel } from "./models/CacheTimestampsModel.js";
|
|
13
13
|
|
|
14
14
|
interface CacheExpirationConfig {
|
|
@@ -5,14 +5,13 @@
|
|
|
5
5
|
license that can be found in the LICENSE file or at
|
|
6
6
|
https://opensource.org/licenses/MIT.
|
|
7
7
|
*/
|
|
8
|
-
|
|
9
|
-
import {
|
|
8
|
+
import type { CacheDidUpdateCallbackParam, CachedResponseWillBeUsedCallbackParam, StrategyPlugin } from "#lib/types.js";
|
|
9
|
+
import { registerQuotaErrorCallback } from "#lib/utils.js";
|
|
10
10
|
import { assert } from "#utils/assert.js";
|
|
11
11
|
import { cacheNames as privateCacheNames } from "#utils/cacheNames.js";
|
|
12
12
|
import { getFriendlyURL } from "#utils/getFriendlyURL.js";
|
|
13
13
|
import { logger } from "#utils/logger.js";
|
|
14
|
-
import {
|
|
15
|
-
import type { CacheDidUpdateCallbackParam, CachedResponseWillBeUsedCallbackParam, StrategyPlugin } from "../../types.js";
|
|
14
|
+
import { SerwistError } from "#utils/SerwistError.js";
|
|
16
15
|
import type { Strategy } from "../strategies/Strategy.js";
|
|
17
16
|
import { CacheExpiration } from "./CacheExpiration.js";
|
|
18
17
|
|
|
@@ -182,7 +181,7 @@ export class ExpirationPlugin implements StrategyPlugin {
|
|
|
182
181
|
})();
|
|
183
182
|
try {
|
|
184
183
|
event.waitUntil(done);
|
|
185
|
-
} catch
|
|
184
|
+
} catch {
|
|
186
185
|
if (process.env.NODE_ENV !== "production") {
|
|
187
186
|
if (event instanceof FetchEvent) {
|
|
188
187
|
logger.warn(`Unable to ensure service worker stays alive when updating cache entry for '${getFriendlyURL(event.request.url)}'.`);
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type { MaybePromise } from "@serwist/utils";
|
|
2
|
+
import type { Serwist } from "./core.js";
|
|
3
|
+
|
|
4
|
+
export interface InitCallbackParam {
|
|
5
|
+
serwist: Serwist;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export type InitCallback = (param: InitCallbackParam) => MaybePromise<void>;
|
|
9
|
+
|
|
10
|
+
export interface InstallCallbackParam {
|
|
11
|
+
event: ExtendableEvent;
|
|
12
|
+
serwist: Serwist;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export type InstallCallback = (param: InstallCallbackParam) => MaybePromise<void>;
|
|
16
|
+
|
|
17
|
+
export interface ActivateCallbackParam {
|
|
18
|
+
event: ExtendableEvent;
|
|
19
|
+
serwist: Serwist;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export type ActivateCallback = (param: ActivateCallbackParam) => MaybePromise<void>;
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* An object with optional lifecycle callback properties for Serwist's operations.
|
|
26
|
+
*/
|
|
27
|
+
export interface Extension {
|
|
28
|
+
init?: InitCallback;
|
|
29
|
+
install?: InstallCallback;
|
|
30
|
+
activate?: ActivateCallback;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export interface ExtensionParam {
|
|
34
|
+
init: InitCallbackParam;
|
|
35
|
+
install: InstallCallbackParam;
|
|
36
|
+
activate: ActivateCallbackParam;
|
|
37
|
+
}
|
|
@@ -1,21 +1,15 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
1
|
+
import { BackgroundSyncPlugin } from "#lib/background-sync/BackgroundSyncPlugin.js";
|
|
2
|
+
import type { BackgroundSyncQueue, BackgroundSyncQueueEntry } from "#lib/background-sync/BackgroundSyncQueue.js";
|
|
3
|
+
import type { Serwist } from "#lib/core.js";
|
|
4
|
+
import type { Extension } from "#lib/extension.js";
|
|
5
|
+
import { registerRoute } from "#lib/functions/router.js";
|
|
6
|
+
import { Route } from "#lib/route.js";
|
|
7
|
+
import { NetworkFirst } from "#lib/strategies/NetworkFirst.js";
|
|
8
|
+
import { NetworkOnly } from "#lib/strategies/NetworkOnly.js";
|
|
9
|
+
import type { RouteMatchCallbackOptions } from "#lib/types.js";
|
|
9
10
|
import { cacheNames as privateCacheNames } from "#utils/cacheNames.js";
|
|
10
11
|
import { getFriendlyURL } from "#utils/getFriendlyURL.js";
|
|
11
12
|
import { logger } from "#utils/logger.js";
|
|
12
|
-
import { Route } from "../../Route.js";
|
|
13
|
-
import type { Serwist } from "../../Serwist.js";
|
|
14
|
-
import type { RouteMatchCallbackOptions } from "../../types.js";
|
|
15
|
-
import { BackgroundSyncPlugin } from "../backgroundSync/BackgroundSyncPlugin.js";
|
|
16
|
-
import type { BackgroundSyncQueue, BackgroundSyncQueueEntry } from "../backgroundSync/BackgroundSyncQueue.js";
|
|
17
|
-
import { NetworkFirst } from "../strategies/NetworkFirst.js";
|
|
18
|
-
import { NetworkOnly } from "../strategies/NetworkOnly.js";
|
|
19
13
|
import {
|
|
20
14
|
ANALYTICS_JS_PATH,
|
|
21
15
|
COLLECT_PATHS_REGEX,
|
|
@@ -27,8 +21,7 @@ import {
|
|
|
27
21
|
QUEUE_NAME,
|
|
28
22
|
} from "./constants.js";
|
|
29
23
|
|
|
30
|
-
export interface
|
|
31
|
-
serwist: Serwist;
|
|
24
|
+
export interface GoogleAnalyticsOptions {
|
|
32
25
|
/**
|
|
33
26
|
* The cache name to store and retrieve analytics.js. Defaults to Serwist's default cache names.
|
|
34
27
|
*/
|
|
@@ -58,9 +51,9 @@ export interface GoogleAnalyticsInitializeOptions {
|
|
|
58
51
|
* @returns The requestWillDequeue callback function.
|
|
59
52
|
* @private
|
|
60
53
|
*/
|
|
61
|
-
const createOnSyncCallback = (config: Pick<
|
|
54
|
+
export const createOnSyncCallback = (config: Pick<GoogleAnalyticsOptions, "parameterOverrides" | "hitFilter">) => {
|
|
62
55
|
return async ({ queue }: { queue: BackgroundSyncQueue }) => {
|
|
63
|
-
let entry: BackgroundSyncQueueEntry | undefined
|
|
56
|
+
let entry: BackgroundSyncQueueEntry | undefined;
|
|
64
57
|
while ((entry = await queue.shiftRequest())) {
|
|
65
58
|
const { request, timestamp } = entry;
|
|
66
59
|
const url = new URL(request.url);
|
|
@@ -128,7 +121,7 @@ const createOnSyncCallback = (config: Pick<GoogleAnalyticsInitializeOptions, "pa
|
|
|
128
121
|
* @returns The created routes.
|
|
129
122
|
* @private
|
|
130
123
|
*/
|
|
131
|
-
const createCollectRoutes = (bgSyncPlugin: BackgroundSyncPlugin) => {
|
|
124
|
+
export const createCollectRoutes = (bgSyncPlugin: BackgroundSyncPlugin) => {
|
|
132
125
|
const match = ({ url }: RouteMatchCallbackOptions) => url.hostname === GOOGLE_ANALYTICS_HOST && COLLECT_PATHS_REGEX.test(url.pathname);
|
|
133
126
|
|
|
134
127
|
const handler = new NetworkOnly({
|
|
@@ -145,7 +138,7 @@ const createCollectRoutes = (bgSyncPlugin: BackgroundSyncPlugin) => {
|
|
|
145
138
|
* @returns The created route.
|
|
146
139
|
* @private
|
|
147
140
|
*/
|
|
148
|
-
const createAnalyticsJsRoute = (cacheName: string) => {
|
|
141
|
+
export const createAnalyticsJsRoute = (cacheName: string) => {
|
|
149
142
|
const match = ({ url }: RouteMatchCallbackOptions) => url.hostname === GOOGLE_ANALYTICS_HOST && url.pathname === ANALYTICS_JS_PATH;
|
|
150
143
|
|
|
151
144
|
const handler = new NetworkFirst({ cacheName });
|
|
@@ -160,7 +153,7 @@ const createAnalyticsJsRoute = (cacheName: string) => {
|
|
|
160
153
|
* @returns The created route.
|
|
161
154
|
* @private
|
|
162
155
|
*/
|
|
163
|
-
const createGtagJsRoute = (cacheName: string) => {
|
|
156
|
+
export const createGtagJsRoute = (cacheName: string) => {
|
|
164
157
|
const match = ({ url }: RouteMatchCallbackOptions) => url.hostname === GTM_HOST && url.pathname === GTAG_JS_PATH;
|
|
165
158
|
|
|
166
159
|
const handler = new NetworkFirst({ cacheName });
|
|
@@ -175,7 +168,7 @@ const createGtagJsRoute = (cacheName: string) => {
|
|
|
175
168
|
* @returns The created route.
|
|
176
169
|
* @private
|
|
177
170
|
*/
|
|
178
|
-
const createGtmJsRoute = (cacheName: string) => {
|
|
171
|
+
export const createGtmJsRoute = (cacheName: string) => {
|
|
179
172
|
const match = ({ url }: RouteMatchCallbackOptions) => url.hostname === GTM_HOST && url.pathname === GTM_JS_PATH;
|
|
180
173
|
|
|
181
174
|
const handler = new NetworkFirst({ cacheName });
|
|
@@ -183,27 +176,28 @@ const createGtmJsRoute = (cacheName: string) => {
|
|
|
183
176
|
return new Route(match, handler, "GET");
|
|
184
177
|
};
|
|
185
178
|
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
*/
|
|
191
|
-
export const initializeGoogleAnalytics = ({ serwist, cacheName, ...options }: GoogleAnalyticsInitializeOptions): void => {
|
|
192
|
-
const resolvedCacheName = privateCacheNames.getGoogleAnalyticsName(cacheName);
|
|
193
|
-
|
|
194
|
-
const bgSyncPlugin = new BackgroundSyncPlugin(QUEUE_NAME, {
|
|
195
|
-
maxRetentionTime: MAX_RETENTION_TIME,
|
|
196
|
-
onSync: createOnSyncCallback(options),
|
|
197
|
-
});
|
|
198
|
-
|
|
199
|
-
const routes = [
|
|
200
|
-
createGtmJsRoute(resolvedCacheName),
|
|
201
|
-
createAnalyticsJsRoute(resolvedCacheName),
|
|
202
|
-
createGtagJsRoute(resolvedCacheName),
|
|
203
|
-
...createCollectRoutes(bgSyncPlugin),
|
|
204
|
-
];
|
|
205
|
-
|
|
206
|
-
for (const route of routes) {
|
|
207
|
-
serwist.registerRoute(route);
|
|
179
|
+
export class GoogleAnalytics implements Extension {
|
|
180
|
+
private _options: GoogleAnalyticsOptions;
|
|
181
|
+
constructor(options: GoogleAnalyticsOptions = {}) {
|
|
182
|
+
this._options = options;
|
|
208
183
|
}
|
|
209
|
-
}
|
|
184
|
+
init({ serwist }: { serwist: Serwist }) {
|
|
185
|
+
const resolvedCacheName = privateCacheNames.getGoogleAnalyticsName(this._options.cacheName);
|
|
186
|
+
|
|
187
|
+
const bgSyncPlugin = new BackgroundSyncPlugin(QUEUE_NAME, {
|
|
188
|
+
maxRetentionTime: MAX_RETENTION_TIME,
|
|
189
|
+
onSync: createOnSyncCallback(this._options),
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
const routes = [
|
|
193
|
+
createGtmJsRoute(resolvedCacheName),
|
|
194
|
+
createAnalyticsJsRoute(resolvedCacheName),
|
|
195
|
+
createGtagJsRoute(resolvedCacheName),
|
|
196
|
+
...createCollectRoutes(bgSyncPlugin),
|
|
197
|
+
];
|
|
198
|
+
|
|
199
|
+
for (const route of routes) {
|
|
200
|
+
registerRoute(serwist, route);
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
}
|
|
File without changes
|
|
@@ -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
|
+
import { BackgroundSyncPlugin } from "#lib/background-sync/BackgroundSyncPlugin.js";
|
|
9
|
+
import type { Serwist } from "#lib/serwist.js";
|
|
10
|
+
import { cacheNames as privateCacheNames } from "#utils/cacheNames.js";
|
|
11
|
+
import { MAX_RETENTION_TIME, QUEUE_NAME } from "./constants.js";
|
|
12
|
+
import {
|
|
13
|
+
createAnalyticsJsRoute,
|
|
14
|
+
createCollectRoutes,
|
|
15
|
+
createGtagJsRoute,
|
|
16
|
+
createGtmJsRoute,
|
|
17
|
+
createOnSyncCallback,
|
|
18
|
+
type GoogleAnalyticsOptions,
|
|
19
|
+
} from "./extension.js";
|
|
20
|
+
|
|
21
|
+
export interface GoogleAnalyticsInitializeOptions extends GoogleAnalyticsOptions {
|
|
22
|
+
serwist: Serwist;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Initialize Serwist's offline Google Analytics v3 support.
|
|
27
|
+
*
|
|
28
|
+
* @param options
|
|
29
|
+
*/
|
|
30
|
+
export const initializeGoogleAnalytics = ({ serwist, cacheName, ...options }: GoogleAnalyticsInitializeOptions): void => {
|
|
31
|
+
const resolvedCacheName = privateCacheNames.getGoogleAnalyticsName(cacheName);
|
|
32
|
+
|
|
33
|
+
const bgSyncPlugin = new BackgroundSyncPlugin(QUEUE_NAME, {
|
|
34
|
+
maxRetentionTime: MAX_RETENTION_TIME,
|
|
35
|
+
onSync: createOnSyncCallback(options),
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
const routes = [
|
|
39
|
+
createGtmJsRoute(resolvedCacheName),
|
|
40
|
+
createAnalyticsJsRoute(resolvedCacheName),
|
|
41
|
+
createGtagJsRoute(resolvedCacheName),
|
|
42
|
+
...createCollectRoutes(bgSyncPlugin),
|
|
43
|
+
];
|
|
44
|
+
|
|
45
|
+
for (const route of routes) {
|
|
46
|
+
serwist.registerRoute(route);
|
|
47
|
+
}
|
|
48
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export type { GoogleAnalyticsOptions } from "./google-analytics/extension.js";
|
|
2
|
+
export { GoogleAnalytics } from "./google-analytics/extension.js";
|
|
3
|
+
export type { GoogleAnalyticsInitializeOptions } from "./google-analytics/initialize.js";
|
|
4
|
+
export { initializeGoogleAnalytics } from "./google-analytics/initialize.js";
|
|
5
|
+
export type { PrecacheEntry, PrecacheExtensionOptions, PrecacheOptions } from "./precache/extension.js";
|
|
6
|
+
export { Precache } from "./precache/extension.js";
|
|
7
|
+
export type { PrecacheFallbackEntry, PrecacheFallbackPluginOptions } from "./precache/plugin-fallback.js";
|
|
8
|
+
export { PrecacheFallbackPlugin } from "./precache/plugin-fallback.js";
|
|
9
|
+
export type { PrecacheRouteOptions } from "./precache/route.js";
|
|
10
|
+
export { PrecacheRoute } from "./precache/route.js";
|
|
11
|
+
export type { PrecacheStrategyOptions } from "./precache/strategy.js";
|
|
12
|
+
export { PrecacheStrategy } from "./precache/strategy.js";
|
|
13
|
+
export { RuntimeCache, type RuntimeCacheOptions } from "./runtime-cache.js";
|
|
@@ -1,19 +1,21 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { parallel } from "@serwist/utils";
|
|
2
|
+
import type { Serwist } from "#lib/core.js";
|
|
3
|
+
import type { Extension } from "#lib/extension.js";
|
|
4
|
+
import { registerRoute } from "#lib/functions/router.js";
|
|
2
5
|
import { assert } from "#utils/assert.js";
|
|
3
6
|
import { createCacheKey } from "#utils/createCacheKey.js";
|
|
4
7
|
import { logger } from "#utils/logger.js";
|
|
5
8
|
import { printCleanupDetails } from "#utils/printCleanupDetails.js";
|
|
6
9
|
import { printInstallDetails } from "#utils/printInstallDetails.js";
|
|
7
|
-
import {
|
|
8
|
-
import { NavigationRoute } from "
|
|
9
|
-
import type { Serwist } from "../../../Serwist.js";
|
|
10
|
-
import type { Controller, RouteHandlerCallback, UrlManipulation } from "../../../types.js";
|
|
10
|
+
import { SerwistError } from "#utils/SerwistError.js";
|
|
11
|
+
import { NavigationRoute } from "../../route.js";
|
|
11
12
|
import type { Strategy } from "../../strategies/Strategy.js";
|
|
12
|
-
import {
|
|
13
|
-
import {
|
|
14
|
-
import {
|
|
15
|
-
import type
|
|
16
|
-
import {
|
|
13
|
+
import type { RouteHandlerCallback } from "../../types.js";
|
|
14
|
+
import { parsePrecacheOptions } from "./options.js";
|
|
15
|
+
import { PrecacheInstallReportPlugin } from "./plugin-install-report.js";
|
|
16
|
+
import { PrecacheRoute, type PrecacheRouteOptions } from "./route.js";
|
|
17
|
+
import type { PrecacheStrategyOptions } from "./strategy.js";
|
|
18
|
+
import { PrecacheStrategy } from "./strategy.js";
|
|
17
19
|
|
|
18
20
|
export interface PrecacheEntry {
|
|
19
21
|
integrity?: string;
|
|
@@ -21,34 +23,7 @@ export interface PrecacheEntry {
|
|
|
21
23
|
revision?: string | null;
|
|
22
24
|
}
|
|
23
25
|
|
|
24
|
-
export interface
|
|
25
|
-
/**
|
|
26
|
-
* Tells Serwist to check the precache for an entry whose URL is the request URL appended
|
|
27
|
-
* with the specified value. Only applies if the request URL ends with "/".
|
|
28
|
-
*
|
|
29
|
-
* @default "index.html"
|
|
30
|
-
*/
|
|
31
|
-
directoryIndex?: string | null;
|
|
32
|
-
/**
|
|
33
|
-
* An array of `RegExp` objects matching search params that should be removed when looking
|
|
34
|
-
* for a precache match.
|
|
35
|
-
*/
|
|
36
|
-
ignoreURLParametersMatching?: RegExp[];
|
|
37
|
-
/**
|
|
38
|
-
* Tells Serwist to check the precache for an entry whose URL is the request URL appended
|
|
39
|
-
* with ".html".
|
|
40
|
-
*
|
|
41
|
-
* @default true
|
|
42
|
-
*/
|
|
43
|
-
cleanURLs?: boolean;
|
|
44
|
-
/**
|
|
45
|
-
* A function that should take a URL and return an array of alternative URLs that should
|
|
46
|
-
* be checked for precache matches.
|
|
47
|
-
*/
|
|
48
|
-
urlManipulation?: UrlManipulation;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
export interface PrecacheControllerOptions {
|
|
26
|
+
export interface PrecacheExtensionOptions {
|
|
52
27
|
/**
|
|
53
28
|
* The cache to use for precaching.
|
|
54
29
|
*/
|
|
@@ -81,14 +56,16 @@ export interface PrecacheControllerOptions {
|
|
|
81
56
|
navigateFallbackDenylist?: RegExp[];
|
|
82
57
|
}
|
|
83
58
|
|
|
84
|
-
export interface PrecacheOptions extends PrecacheStrategyOptions, PrecacheRouteOptions,
|
|
59
|
+
export interface PrecacheOptions extends PrecacheStrategyOptions, PrecacheRouteOptions, PrecacheExtensionOptions {
|
|
60
|
+
entries: (PrecacheEntry | string)[] | undefined;
|
|
61
|
+
}
|
|
85
62
|
|
|
86
|
-
export class
|
|
63
|
+
export class Precache implements Extension {
|
|
87
64
|
private readonly _urlsToCacheKeys: Map<string, string> = new Map();
|
|
88
65
|
private readonly _urlsToCacheModes: Map<string, "reload" | "default" | "no-store" | "no-cache" | "force-cache" | "only-if-cached"> = new Map();
|
|
89
66
|
private readonly _cacheKeysToIntegrities: Map<string, string> = new Map();
|
|
90
67
|
private readonly _strategy: Strategy;
|
|
91
|
-
private _options:
|
|
68
|
+
private _options: PrecacheExtensionOptions;
|
|
92
69
|
private _routeOptions: PrecacheRouteOptions;
|
|
93
70
|
|
|
94
71
|
/**
|
|
@@ -96,8 +73,8 @@ export class PrecacheController implements Controller {
|
|
|
96
73
|
*
|
|
97
74
|
* @param options
|
|
98
75
|
*/
|
|
99
|
-
constructor(
|
|
100
|
-
const { strategyOptions, routeOptions, controllerOptions } = parsePrecacheOptions(this, precacheOptions);
|
|
76
|
+
constructor(precacheOptions: PrecacheOptions) {
|
|
77
|
+
const { entries, strategyOptions, routeOptions, extensionOptions: controllerOptions } = parsePrecacheOptions(this, precacheOptions);
|
|
101
78
|
this.addToCacheList(entries);
|
|
102
79
|
this._strategy = new PrecacheStrategy(strategyOptions);
|
|
103
80
|
this._options = controllerOptions;
|
|
@@ -174,10 +151,11 @@ export class PrecacheController implements Controller {
|
|
|
174
151
|
}
|
|
175
152
|
|
|
176
153
|
init({ serwist }: { serwist: Serwist }): void {
|
|
177
|
-
|
|
154
|
+
registerRoute(serwist, new PrecacheRoute(this, this._routeOptions));
|
|
178
155
|
|
|
179
156
|
if (this._options.navigateFallback) {
|
|
180
|
-
|
|
157
|
+
registerRoute(
|
|
158
|
+
serwist,
|
|
181
159
|
new NavigationRoute(this.createHandlerBoundToUrl(this._options.navigateFallback), {
|
|
182
160
|
allowlist: this._options.navigateFallbackAllowlist,
|
|
183
161
|
denylist: this._options.navigateFallbackDenylist,
|
|
@@ -1,12 +1,14 @@
|
|
|
1
|
-
import { privateCacheNames } from "#index.internal";
|
|
2
1
|
import type { Require } from "@serwist/utils";
|
|
3
|
-
import {
|
|
4
|
-
import type {
|
|
5
|
-
import
|
|
2
|
+
import { privateCacheNames } from "#index.internal";
|
|
3
|
+
import type { Precache, PrecacheExtensionOptions, PrecacheOptions } from "./extension.js";
|
|
4
|
+
import { PrecacheCacheKeyPlugin } from "./plugin-cache-key.js";
|
|
5
|
+
import type { PrecacheRouteOptions } from "./route.js";
|
|
6
|
+
import type { PrecacheStrategyOptions } from "./strategy.js";
|
|
6
7
|
|
|
7
8
|
export const parsePrecacheOptions = (
|
|
8
|
-
controller:
|
|
9
|
+
controller: Precache,
|
|
9
10
|
{
|
|
11
|
+
entries,
|
|
10
12
|
cacheName,
|
|
11
13
|
plugins,
|
|
12
14
|
fetchOptions,
|
|
@@ -21,8 +23,9 @@ export const parsePrecacheOptions = (
|
|
|
21
23
|
navigateFallback,
|
|
22
24
|
navigateFallbackAllowlist,
|
|
23
25
|
navigateFallbackDenylist,
|
|
24
|
-
}: PrecacheOptions
|
|
26
|
+
}: PrecacheOptions,
|
|
25
27
|
) => ({
|
|
28
|
+
entries: entries ?? [],
|
|
26
29
|
strategyOptions: {
|
|
27
30
|
cacheName: privateCacheNames.getPrecacheName(cacheName),
|
|
28
31
|
plugins: [...(plugins ?? []), new PrecacheCacheKeyPlugin({ precacheController: controller })],
|
|
@@ -36,11 +39,11 @@ export const parsePrecacheOptions = (
|
|
|
36
39
|
cleanURLs,
|
|
37
40
|
urlManipulation,
|
|
38
41
|
} satisfies PrecacheRouteOptions,
|
|
39
|
-
|
|
42
|
+
extensionOptions: {
|
|
40
43
|
cleanupOutdatedCaches,
|
|
41
44
|
concurrency: concurrency ?? 10,
|
|
42
45
|
navigateFallback,
|
|
43
46
|
navigateFallbackAllowlist,
|
|
44
47
|
navigateFallbackDenylist,
|
|
45
|
-
} satisfies Require<
|
|
48
|
+
} satisfies Require<PrecacheExtensionOptions, "concurrency">,
|
|
46
49
|
});
|
|
@@ -5,8 +5,8 @@
|
|
|
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 { StrategyPlugin, StrategyPluginCallbackParam } from "
|
|
9
|
-
import type {
|
|
8
|
+
import type { StrategyPlugin, StrategyPluginCallbackParam } from "#lib/types.js";
|
|
9
|
+
import type { Precache } from "./extension.js";
|
|
10
10
|
|
|
11
11
|
/**
|
|
12
12
|
* A plugin, designed to be used with PrecacheController, to translate URLs into
|
|
@@ -15,9 +15,9 @@ import type { PrecacheController } from "./PrecacheController.js";
|
|
|
15
15
|
* @private
|
|
16
16
|
*/
|
|
17
17
|
export class PrecacheCacheKeyPlugin implements StrategyPlugin {
|
|
18
|
-
private readonly _precacheController:
|
|
18
|
+
private readonly _precacheController: Precache;
|
|
19
19
|
|
|
20
|
-
constructor({ precacheController }: { precacheController:
|
|
20
|
+
constructor({ precacheController }: { precacheController: Precache }) {
|
|
21
21
|
this._precacheController = precacheController;
|
|
22
22
|
}
|
|
23
23
|
|