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
package/src/lib/{precaching/PrecacheFallbackPlugin.ts → extensions/precache/plugin-fallback.ts}
RENAMED
|
@@ -5,10 +5,10 @@
|
|
|
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
|
|
10
|
-
import
|
|
11
|
-
import type { HandlerDidErrorCallbackParam, StrategyPlugin } from "
|
|
8
|
+
import type { Serwist } from "#lib/core.js";
|
|
9
|
+
import { handleRequest } from "#lib/functions/router.js";
|
|
10
|
+
import { Serwist as LegacySerwist } from "#lib/serwist.js";
|
|
11
|
+
import type { HandlerDidErrorCallbackParam, StrategyPlugin } from "#lib/types.js";
|
|
12
12
|
|
|
13
13
|
export interface PrecacheFallbackEntry {
|
|
14
14
|
/**
|
|
@@ -28,15 +28,10 @@ export interface PrecacheFallbackPluginOptions {
|
|
|
28
28
|
* if the associated strategy can't generate a response.
|
|
29
29
|
*/
|
|
30
30
|
fallbackUrls: (string | PrecacheFallbackEntry)[];
|
|
31
|
-
/**
|
|
32
|
-
* A {@linkcode PrecacheController} instance.
|
|
33
|
-
*/
|
|
34
|
-
precacheController: PrecacheController;
|
|
35
31
|
/**
|
|
36
32
|
* A {@linkcode Serwist} instance.
|
|
37
|
-
* @deprecated Use `precacheController` instead.
|
|
38
33
|
*/
|
|
39
|
-
serwist
|
|
34
|
+
serwist: LegacySerwist | Serwist;
|
|
40
35
|
}
|
|
41
36
|
|
|
42
37
|
/**
|
|
@@ -49,21 +44,17 @@ export interface PrecacheFallbackPluginOptions {
|
|
|
49
44
|
*/
|
|
50
45
|
export class PrecacheFallbackPlugin implements StrategyPlugin {
|
|
51
46
|
private readonly _fallbackUrls: (string | PrecacheFallbackEntry)[];
|
|
52
|
-
private readonly
|
|
47
|
+
private readonly _serwist: Serwist;
|
|
53
48
|
|
|
54
49
|
/**
|
|
55
50
|
* Constructs a new instance with the associated `fallbackUrls`.
|
|
56
51
|
*
|
|
57
52
|
* @param config
|
|
58
53
|
*/
|
|
59
|
-
constructor({ fallbackUrls,
|
|
54
|
+
constructor({ fallbackUrls, serwist }: PrecacheFallbackPluginOptions) {
|
|
60
55
|
this._fallbackUrls = fallbackUrls;
|
|
61
56
|
// TODO(ducanhgh): remove in v11.
|
|
62
|
-
|
|
63
|
-
this._precacheController = precacheController;
|
|
64
|
-
} else {
|
|
65
|
-
this._precacheController = serwist.precache;
|
|
66
|
-
}
|
|
57
|
+
this._serwist = serwist instanceof LegacySerwist ? serwist.state : serwist;
|
|
67
58
|
}
|
|
68
59
|
|
|
69
60
|
/**
|
|
@@ -74,14 +65,24 @@ export class PrecacheFallbackPlugin implements StrategyPlugin {
|
|
|
74
65
|
async handlerDidError(param: HandlerDidErrorCallbackParam) {
|
|
75
66
|
for (const fallback of this._fallbackUrls) {
|
|
76
67
|
if (typeof fallback === "string") {
|
|
77
|
-
const fallbackResponse =
|
|
68
|
+
const fallbackResponse = handleRequest(this._serwist, {
|
|
69
|
+
request: new Request(fallback, {
|
|
70
|
+
credentials: "same-origin",
|
|
71
|
+
}),
|
|
72
|
+
event: param.event,
|
|
73
|
+
});
|
|
78
74
|
if (fallbackResponse !== undefined) {
|
|
79
|
-
return fallbackResponse;
|
|
75
|
+
return await fallbackResponse;
|
|
80
76
|
}
|
|
81
77
|
} else if (fallback.matcher(param)) {
|
|
82
|
-
const fallbackResponse =
|
|
78
|
+
const fallbackResponse = handleRequest(this._serwist, {
|
|
79
|
+
request: new Request(fallback.url, {
|
|
80
|
+
credentials: "same-origin",
|
|
81
|
+
}),
|
|
82
|
+
event: param.event,
|
|
83
|
+
});
|
|
83
84
|
if (fallbackResponse !== undefined) {
|
|
84
|
-
return fallbackResponse;
|
|
85
|
+
return await fallbackResponse;
|
|
85
86
|
}
|
|
86
87
|
}
|
|
87
88
|
}
|
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
https://opensource.org/licenses/MIT.
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
|
-
import type { StrategyPlugin, StrategyPluginCallbackParam } from "
|
|
9
|
+
import type { StrategyPlugin, StrategyPluginCallbackParam } from "#lib/types.js";
|
|
10
10
|
|
|
11
11
|
/**
|
|
12
12
|
* A plugin designed to determine the number of assets that were updated (or not updated)
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2020 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 { Serwist } from "#lib/core.js";
|
|
10
|
+
import { generateURLVariations } from "#utils/generateURLVariations.js";
|
|
11
|
+
import { getFriendlyURL } from "#utils/getFriendlyURL.js";
|
|
12
|
+
import { logger } from "#utils/logger.js";
|
|
13
|
+
import { Route } from "../../route.js";
|
|
14
|
+
import type { RouteMatchCallback, RouteMatchCallbackOptions, UrlManipulation } from "../../types.js";
|
|
15
|
+
import type { Precache } from "./extension.js";
|
|
16
|
+
|
|
17
|
+
export interface PrecacheRouteOptions {
|
|
18
|
+
/**
|
|
19
|
+
* Tells Serwist to check the precache for an entry whose URL is the request URL appended
|
|
20
|
+
* with the specified value. Only applies if the request URL ends with "/".
|
|
21
|
+
*
|
|
22
|
+
* @default "index.html"
|
|
23
|
+
*/
|
|
24
|
+
directoryIndex?: string | null;
|
|
25
|
+
/**
|
|
26
|
+
* An array of `RegExp` objects matching search params that should be removed when looking
|
|
27
|
+
* for a precache match.
|
|
28
|
+
*/
|
|
29
|
+
ignoreURLParametersMatching?: RegExp[];
|
|
30
|
+
/**
|
|
31
|
+
* Tells Serwist to check the precache for an entry whose URL is the request URL appended
|
|
32
|
+
* with ".html".
|
|
33
|
+
*
|
|
34
|
+
* @default true
|
|
35
|
+
*/
|
|
36
|
+
cleanURLs?: boolean;
|
|
37
|
+
/**
|
|
38
|
+
* A function that should take a URL and return an array of alternative URLs that should
|
|
39
|
+
* be checked for precache matches.
|
|
40
|
+
*/
|
|
41
|
+
urlManipulation?: UrlManipulation;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* A subclass of {@linkcode Route} that takes a {@linkcode Serwist} instance and uses it to match
|
|
46
|
+
* incoming requests and handle fetching responses from the precache.
|
|
47
|
+
*/
|
|
48
|
+
export class PrecacheRoute extends Route {
|
|
49
|
+
/**
|
|
50
|
+
* @param precache A {@linkcode Precache} instance.
|
|
51
|
+
* @param options Options to control how requests are matched
|
|
52
|
+
* against the list of precached URLs.
|
|
53
|
+
*/
|
|
54
|
+
constructor(precache: Precache, options?: PrecacheRouteOptions) {
|
|
55
|
+
const match: RouteMatchCallback = ({ request }: RouteMatchCallbackOptions) => {
|
|
56
|
+
const urlsToCacheKeys = precache.getUrlsToPrecacheKeys();
|
|
57
|
+
for (const possibleURL of generateURLVariations(request.url, options)) {
|
|
58
|
+
const cacheKey = urlsToCacheKeys.get(possibleURL);
|
|
59
|
+
if (cacheKey) {
|
|
60
|
+
const integrity = precache.getIntegrityForPrecacheKey(cacheKey);
|
|
61
|
+
return { cacheKey, integrity };
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
if (process.env.NODE_ENV !== "production") {
|
|
65
|
+
logger.debug(`Precaching did not find a match for ${getFriendlyURL(request.url)}.`);
|
|
66
|
+
}
|
|
67
|
+
return;
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
super(match, precache.strategy);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
@@ -6,13 +6,13 @@
|
|
|
6
6
|
https://opensource.org/licenses/MIT.
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
|
-
import {
|
|
9
|
+
import type { Serwist } from "#lib/core.js";
|
|
10
|
+
import type { StrategyPlugin } from "#lib/types.js";
|
|
11
|
+
import { copyResponse } from "#lib/utils.js";
|
|
10
12
|
import { cacheNames as privateCacheNames } from "#utils/cacheNames.js";
|
|
11
13
|
import { getFriendlyURL } from "#utils/getFriendlyURL.js";
|
|
12
14
|
import { logger } from "#utils/logger.js";
|
|
13
|
-
import
|
|
14
|
-
import { copyResponse } from "../../../copyResponse.js";
|
|
15
|
-
import type { StrategyPlugin } from "../../../types.js";
|
|
15
|
+
import { SerwistError } from "#utils/SerwistError.js";
|
|
16
16
|
import type { StrategyOptions } from "../../strategies/Strategy.js";
|
|
17
17
|
import { Strategy } from "../../strategies/Strategy.js";
|
|
18
18
|
import type { StrategyHandler } from "../../strategies/StrategyHandler.js";
|
|
@@ -103,7 +103,7 @@ export class PrecacheStrategy extends Strategy {
|
|
|
103
103
|
}
|
|
104
104
|
|
|
105
105
|
async _handleFetch(request: Request, handler: StrategyHandler): Promise<Response> {
|
|
106
|
-
let response: Response | undefined
|
|
106
|
+
let response: Response | undefined;
|
|
107
107
|
|
|
108
108
|
const params = (handler.params || {}) as {
|
|
109
109
|
cacheKey?: string;
|
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import { parallel } from "@serwist/utils";
|
|
2
|
-
import type { Serwist } from "
|
|
3
|
-
import type {
|
|
4
|
-
import {
|
|
2
|
+
import type { Serwist } from "#lib/core.js";
|
|
3
|
+
import type { Extension } from "#lib/extension.js";
|
|
4
|
+
import { handleRequest, registerCapture } from "#lib/functions/router.js";
|
|
5
5
|
import { Strategy } from "../strategies/Strategy.js";
|
|
6
|
+
import type { RuntimeCaching } from "../types.js";
|
|
7
|
+
import { type PrecacheFallbackEntry, PrecacheFallbackPlugin } from "./precache/plugin-fallback.js";
|
|
6
8
|
|
|
7
9
|
export interface WarmRuntimeCacheEntry {
|
|
8
10
|
integrity?: string;
|
|
@@ -22,7 +24,7 @@ export interface FallbacksOptions {
|
|
|
22
24
|
entries: FallbackEntry[];
|
|
23
25
|
}
|
|
24
26
|
|
|
25
|
-
export interface
|
|
27
|
+
export interface RuntimeCacheOptions {
|
|
26
28
|
/**
|
|
27
29
|
* URLs that should be cached as the service worker is installed.
|
|
28
30
|
*/
|
|
@@ -41,10 +43,10 @@ export interface RuntimeCacheControllerOptions {
|
|
|
41
43
|
fallbacks?: FallbacksOptions;
|
|
42
44
|
}
|
|
43
45
|
|
|
44
|
-
export class
|
|
46
|
+
export class RuntimeCache implements Extension {
|
|
45
47
|
_entries: RuntimeCaching[];
|
|
46
|
-
_options:
|
|
47
|
-
constructor(entries: RuntimeCaching[], options:
|
|
48
|
+
_options: RuntimeCacheOptions;
|
|
49
|
+
constructor(entries: RuntimeCaching[], options: RuntimeCacheOptions = {}) {
|
|
48
50
|
this._entries = entries;
|
|
49
51
|
this._options = options;
|
|
50
52
|
this.init = this.init.bind(this);
|
|
@@ -55,7 +57,7 @@ export class RuntimeCacheController implements Controller {
|
|
|
55
57
|
if (this._options.fallbacks !== undefined) {
|
|
56
58
|
const fallbackPlugin = new PrecacheFallbackPlugin({
|
|
57
59
|
fallbackUrls: this._options.fallbacks.entries,
|
|
58
|
-
|
|
60
|
+
serwist,
|
|
59
61
|
});
|
|
60
62
|
|
|
61
63
|
this._entries.forEach((cacheEntry) => {
|
|
@@ -69,7 +71,7 @@ export class RuntimeCacheController implements Controller {
|
|
|
69
71
|
});
|
|
70
72
|
}
|
|
71
73
|
for (const entry of this._entries) {
|
|
72
|
-
|
|
74
|
+
registerCapture(serwist, entry.matcher, entry.handler, entry.method);
|
|
73
75
|
}
|
|
74
76
|
}
|
|
75
77
|
|
|
@@ -80,11 +82,16 @@ export class RuntimeCacheController implements Controller {
|
|
|
80
82
|
const request =
|
|
81
83
|
entry instanceof Request
|
|
82
84
|
? entry
|
|
83
|
-
:
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
85
|
+
: typeof entry === "string"
|
|
86
|
+
? new Request(entry, {
|
|
87
|
+
credentials: "same-origin",
|
|
88
|
+
})
|
|
89
|
+
: new Request(entry.url, {
|
|
90
|
+
integrity: entry.integrity,
|
|
91
|
+
credentials: "same-origin",
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
await handleRequest(serwist, {
|
|
88
95
|
request,
|
|
89
96
|
event,
|
|
90
97
|
});
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
import { defaultMethod, type HTTPMethod } from "#lib/constants.js";
|
|
2
|
+
import type { Extension, ExtensionParam } from "#lib/extension.js";
|
|
3
|
+
import type { Route } from "#lib/route.js";
|
|
4
|
+
import type { RouteHandler } from "#lib/types.js";
|
|
5
|
+
import { logger } from "#utils/logger.js";
|
|
6
|
+
import { normalizeHandler } from "#utils/normalizeHandler.js";
|
|
7
|
+
import { waitUntil } from "#utils/waitUntil.js";
|
|
8
|
+
import type { Serwist } from "../core.js";
|
|
9
|
+
import { handleRequest } from "./router.js";
|
|
10
|
+
|
|
11
|
+
declare const self: ServiceWorkerGlobalScope;
|
|
12
|
+
|
|
13
|
+
export type RequestArgs = string | [string, RequestInit?];
|
|
14
|
+
|
|
15
|
+
export interface CacheURLsMessageData {
|
|
16
|
+
type: string;
|
|
17
|
+
payload: {
|
|
18
|
+
urlsToCache: RequestArgs[];
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Adds Serwist's event listeners. Before calling it, add your own listeners should you need to.
|
|
24
|
+
*/
|
|
25
|
+
export const addEventListeners = (state: Serwist) => {
|
|
26
|
+
self.addEventListener("install", createInstallHandler(state));
|
|
27
|
+
self.addEventListener("activate", createActivateHandler(state));
|
|
28
|
+
self.addEventListener("fetch", createFetchHandler(state));
|
|
29
|
+
self.addEventListener("message", createCacheHandler(state));
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
export const createInstallHandler = (state: Serwist) => {
|
|
33
|
+
return (event: ExtendableEvent): Promise<void> => {
|
|
34
|
+
return waitUntil(event, async () => {
|
|
35
|
+
for (const callback of iterateExtensions(state, "install")) {
|
|
36
|
+
await callback({ event, serwist: state });
|
|
37
|
+
}
|
|
38
|
+
});
|
|
39
|
+
};
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
export const createActivateHandler = (state: Serwist) => {
|
|
43
|
+
return (event: ExtendableEvent): Promise<void> => {
|
|
44
|
+
return waitUntil(event, async () => {
|
|
45
|
+
for (const callback of iterateExtensions(state, "activate")) {
|
|
46
|
+
await callback({ event, serwist: state });
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
};
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Gets a `Response` from an appropriate `Route`'s handler. Call this method
|
|
54
|
+
* from the service worker's `fetch` event.
|
|
55
|
+
* @param event
|
|
56
|
+
*/
|
|
57
|
+
export const createFetchHandler = (state: Serwist) => {
|
|
58
|
+
return (event: FetchEvent) => {
|
|
59
|
+
const { request } = event;
|
|
60
|
+
const responsePromise = handleRequest(state, { request, event });
|
|
61
|
+
if (responsePromise) {
|
|
62
|
+
event.respondWith(responsePromise);
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Caches new URLs on demand. Call this method from the service worker's
|
|
69
|
+
* `message` event. To trigger the handler, send a message of type `"CACHE_URLS"`
|
|
70
|
+
* alongside a list of URLs that should be cached as `urlsToCache`.
|
|
71
|
+
* @param event
|
|
72
|
+
*/
|
|
73
|
+
export const createCacheHandler = (state: Serwist) => {
|
|
74
|
+
return (event: ExtendableMessageEvent) => {
|
|
75
|
+
if (event.data && event.data.type === "CACHE_URLS") {
|
|
76
|
+
const { payload }: CacheURLsMessageData = event.data;
|
|
77
|
+
|
|
78
|
+
if (process.env.NODE_ENV !== "production") {
|
|
79
|
+
logger.debug("Caching URLs from the window", payload.urlsToCache);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const requestPromises = Promise.all(
|
|
83
|
+
payload.urlsToCache.map((entry: string | [string, RequestInit?]) => {
|
|
84
|
+
let request: Request;
|
|
85
|
+
if (typeof entry === "string") {
|
|
86
|
+
request = new Request(entry);
|
|
87
|
+
} else {
|
|
88
|
+
request = new Request(...entry);
|
|
89
|
+
}
|
|
90
|
+
return handleRequest(state, { request, event });
|
|
91
|
+
}),
|
|
92
|
+
);
|
|
93
|
+
|
|
94
|
+
event.waitUntil(requestPromises);
|
|
95
|
+
|
|
96
|
+
// If a MessageChannel was used, reply to the message on success.
|
|
97
|
+
if (event.ports?.[0]) {
|
|
98
|
+
void requestPromises.then(() => event.ports[0].postMessage(true));
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
};
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Define a default handler that's called when no routes explicitly
|
|
106
|
+
* match the incoming request.
|
|
107
|
+
*
|
|
108
|
+
* Each HTTP method (`'GET'`, `'POST'`, etc.) gets its own default handler.
|
|
109
|
+
*
|
|
110
|
+
* Without a default handler, unmatched requests will go against the
|
|
111
|
+
* network as if there were no service worker present.
|
|
112
|
+
*
|
|
113
|
+
* @param handler A callback function that returns a `Promise` resulting in a `Response`.
|
|
114
|
+
* @param method The HTTP method to associate with this default handler. Each method
|
|
115
|
+
* has its own default. Defaults to `'GET'`.
|
|
116
|
+
*/
|
|
117
|
+
export const setDefaultHandler = (state: Serwist, handler: RouteHandler, method: HTTPMethod = defaultMethod): void => {
|
|
118
|
+
state.defaultHandlerMap.set(method, normalizeHandler(handler));
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* If a {@linkcode Route} throws an error while handling a request, this handler
|
|
123
|
+
* will be called and given a chance to provide a response.
|
|
124
|
+
*
|
|
125
|
+
* @param handler A callback function that returns a `Promise` resulting
|
|
126
|
+
* in a `Response`.
|
|
127
|
+
*/
|
|
128
|
+
export const setCatchHandler = (state: Serwist, handler: RouteHandler): void => {
|
|
129
|
+
state.catchHandler = normalizeHandler(handler);
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Accepts a callback name and returns an iterable of matching plugin callbacks.
|
|
134
|
+
*
|
|
135
|
+
* @param name The name fo the callback to run
|
|
136
|
+
* @returns
|
|
137
|
+
*/
|
|
138
|
+
export function* iterateExtensions<C extends keyof Extension>(state: Serwist, name: C): Generator<NonNullable<Extension[C]>> {
|
|
139
|
+
if (!state.extensions) return;
|
|
140
|
+
|
|
141
|
+
for (const controller of state.extensions) {
|
|
142
|
+
if (typeof controller[name] === "function") {
|
|
143
|
+
const callback = (param: ExtensionParam[C]) => {
|
|
144
|
+
controller[name]!(param as any);
|
|
145
|
+
};
|
|
146
|
+
yield callback as NonNullable<Extension[C]>;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
}
|