serwist 9.1.1 → 10.0.0-preview.10
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 +141 -134
- package/dist/index.d.ts +22 -49
- package/dist/index.d.ts.map +1 -1
- package/dist/index.internal.d.ts +16 -16
- package/dist/index.internal.d.ts.map +1 -1
- package/dist/index.internal.js +25 -3
- package/dist/index.js +2231 -452
- package/dist/lib/{backgroundSync → background-sync}/BackgroundSyncPlugin.d.ts +2 -2
- 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 +6 -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 +2 -2
- 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 +6 -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 +3 -3
- package/dist/lib/cacheable-response/CacheableResponsePlugin.d.ts.map +1 -0
- package/dist/lib/cacheable-response/index.d.ts +4 -0
- 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 +2 -2
- package/dist/lib/expiration/ExpirationPlugin.d.ts.map +1 -1
- package/dist/lib/expiration/index.d.ts +4 -0
- package/dist/lib/expiration/index.d.ts.map +1 -0
- 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/{legacy/PrecacheController.d.ts → lib/extensions/precache/extension.d.ts} +56 -66
- 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/extensions/precache/plugin-cache-key.d.ts +16 -0
- 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} +4 -4
- package/dist/lib/extensions/precache/plugin-fallback.d.ts.map +1 -0
- package/dist/lib/extensions/precache/plugin-install-report.d.ts +14 -0
- 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/{strategies/PrecacheStrategy.d.ts → extensions/precache/strategy.d.ts} +7 -7
- package/dist/lib/extensions/precache/strategy.d.ts.map +1 -0
- package/dist/lib/extensions/runtime-cache.d.ts +71 -0
- 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 +3 -3
- 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/range-requests/index.d.ts +3 -0
- 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} +71 -62
- 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 +3 -3
- package/dist/lib/strategies/Strategy.d.ts.map +1 -1
- package/dist/lib/strategies/StrategyHandler.d.ts +4 -4
- package/dist/lib/strategies/StrategyHandler.d.ts.map +1 -1
- package/dist/lib/strategies/index.d.ts +11 -0
- package/dist/lib/strategies/index.d.ts.map +1 -0
- package/dist/lib/strategies/plugins/cacheOkAndOpaquePlugin.d.ts +2 -2
- package/dist/lib/strategies/plugins/cacheOkAndOpaquePlugin.d.ts.map +1 -1
- package/dist/{types.d.ts → lib/types.d.ts} +7 -65
- 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 +2 -2
- 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/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 +2 -2
- package/dist/utils/pluginUtils.d.ts.map +1 -1
- package/package.json +28 -11
- package/src/index.internal.ts +16 -16
- package/src/index.ts +100 -107
- package/src/lib/{backgroundSync → background-sync}/BackgroundSyncPlugin.ts +2 -2
- package/src/lib/{backgroundSync → background-sync}/BackgroundSyncQueue.ts +4 -4
- package/src/lib/{backgroundSync → background-sync}/BackgroundSyncQueueStore.ts +1 -1
- package/src/lib/{backgroundSync → background-sync}/StorableRequest.ts +2 -2
- package/src/lib/background-sync/index.ts +5 -0
- package/src/lib/{broadcastUpdate → broadcast-update}/BroadcastCacheUpdate.ts +5 -5
- package/src/lib/{broadcastUpdate → broadcast-update}/BroadcastUpdatePlugin.ts +2 -2
- package/src/lib/broadcast-update/index.ts +5 -0
- package/src/lib/{broadcastUpdate → broadcast-update}/responsesAreSame.ts +2 -2
- package/src/lib/{broadcastUpdate → broadcast-update}/types.ts +1 -1
- package/src/{cacheNames.ts → lib/cache-name.ts} +1 -1
- package/src/lib/{cacheableResponse → cacheable-response}/CacheableResponse.ts +4 -4
- package/src/lib/{cacheableResponse → cacheable-response}/CacheableResponsePlugin.ts +3 -3
- package/src/lib/cacheable-response/index.ts +3 -0
- package/src/lib/core.ts +128 -0
- package/src/lib/expiration/CacheExpiration.ts +3 -3
- package/src/lib/expiration/ExpirationPlugin.ts +9 -10
- package/src/lib/expiration/index.ts +3 -0
- package/src/lib/extension.ts +37 -0
- package/src/lib/{googleAnalytics/initializeGoogleAnalytics.ts → extensions/google-analytics/extension.ts} +42 -48
- 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/extensions/precache/extension.ts +306 -0
- package/src/lib/extensions/precache/options.ts +49 -0
- package/src/{legacy/utils/PrecacheCacheKeyPlugin.ts → lib/extensions/precache/plugin-cache-key.ts} +7 -8
- package/src/lib/{precaching/PrecacheFallbackPlugin.ts → extensions/precache/plugin-fallback.ts} +19 -8
- package/src/{utils/PrecacheInstallReportPlugin.ts → lib/extensions/precache/plugin-install-report.ts} +5 -5
- package/src/lib/extensions/precache/route.ts +72 -0
- package/src/lib/{strategies/PrecacheStrategy.ts → extensions/precache/strategy.ts} +12 -12
- package/src/lib/extensions/runtime-cache.ts +126 -0
- package/src/lib/functions/handlers.ts +149 -0
- package/src/lib/functions/router.ts +314 -0
- package/src/{navigationPreload.ts → lib/navigation-preload.ts} +1 -1
- package/src/lib/{rangeRequests → range-requests}/RangeRequestsPlugin.ts +3 -3
- package/src/lib/{rangeRequests → range-requests}/createPartialResponse.ts +3 -3
- package/src/lib/range-requests/index.ts +2 -0
- package/src/lib/{rangeRequests → range-requests}/utils/calculateEffectiveBoundaries.ts +2 -2
- package/src/lib/{rangeRequests → range-requests}/utils/parseRangeHeader.ts +2 -2
- package/src/lib/route.ts +234 -0
- package/src/lib/serwist.ts +443 -0
- package/src/lib/strategies/CacheFirst.ts +3 -3
- package/src/lib/strategies/CacheOnly.ts +3 -3
- package/src/lib/strategies/NetworkFirst.ts +4 -4
- package/src/lib/strategies/NetworkOnly.ts +4 -4
- package/src/lib/strategies/StaleWhileRevalidate.ts +4 -4
- package/src/lib/strategies/Strategy.ts +7 -7
- package/src/lib/strategies/StrategyHandler.ts +18 -18
- package/src/lib/strategies/index.ts +10 -0
- package/src/lib/strategies/plugins/cacheOkAndOpaquePlugin.ts +2 -2
- package/src/lib/strategies/utils/messages.ts +2 -2
- package/src/{types.ts → lib/types.ts} +17 -70
- package/src/lib/utils.ts +137 -0
- package/src/models/messages/messageGenerator.ts +1 -1
- package/src/models/messages/messages.ts +3 -3
- package/src/utils/SerwistError.ts +3 -3
- package/src/utils/assert.ts +1 -1
- package/src/utils/createCacheKey.ts +1 -2
- package/src/utils/executeQuotaErrorCallbacks.ts +1 -1
- package/src/utils/generateURLVariations.ts +1 -1
- package/src/utils/logger.ts +1 -1
- package/src/utils/normalizeHandler.ts +1 -1
- package/src/utils/parseRoute.ts +4 -5
- package/src/utils/pluginUtils.ts +2 -2
- package/dist/NavigationRoute.d.ts +0 -56
- package/dist/NavigationRoute.d.ts.map +0 -1
- package/dist/PrecacheRoute.d.ts +0 -16
- package/dist/PrecacheRoute.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/chunks/printInstallDetails.js +0 -1601
- package/dist/chunks/resultingClientExists.js +0 -32
- 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/index.legacy.d.ts +0 -28
- package/dist/index.legacy.d.ts.map +0 -1
- package/dist/index.legacy.js +0 -790
- package/dist/legacy/PrecacheController.d.ts.map +0 -1
- package/dist/legacy/PrecacheFallbackPlugin.d.ts +0 -61
- package/dist/legacy/PrecacheFallbackPlugin.d.ts.map +0 -1
- package/dist/legacy/PrecacheRoute.d.ts +0 -19
- package/dist/legacy/PrecacheRoute.d.ts.map +0 -1
- package/dist/legacy/Router.d.ts +0 -151
- package/dist/legacy/Router.d.ts.map +0 -1
- package/dist/legacy/addPlugins.d.ts +0 -9
- package/dist/legacy/addPlugins.d.ts.map +0 -1
- package/dist/legacy/addRoute.d.ts +0 -14
- package/dist/legacy/addRoute.d.ts.map +0 -1
- package/dist/legacy/constants.d.ts.map +0 -1
- package/dist/legacy/createHandlerBoundToURL.d.ts +0 -17
- package/dist/legacy/createHandlerBoundToURL.d.ts.map +0 -1
- package/dist/legacy/fallbacks.d.ts +0 -59
- package/dist/legacy/fallbacks.d.ts.map +0 -1
- package/dist/legacy/getCacheKeyForURL.d.ts +0 -20
- package/dist/legacy/getCacheKeyForURL.d.ts.map +0 -1
- package/dist/legacy/handlePrecaching.d.ts +0 -54
- package/dist/legacy/handlePrecaching.d.ts.map +0 -1
- package/dist/legacy/initializeGoogleAnalytics.d.ts +0 -38
- package/dist/legacy/initializeGoogleAnalytics.d.ts.map +0 -1
- package/dist/legacy/installSerwist.d.ts +0 -81
- package/dist/legacy/installSerwist.d.ts.map +0 -1
- package/dist/legacy/matchPrecache.d.ts +0 -15
- package/dist/legacy/matchPrecache.d.ts.map +0 -1
- package/dist/legacy/precache.d.ts +0 -20
- package/dist/legacy/precache.d.ts.map +0 -1
- package/dist/legacy/precacheAndRoute.d.ts +0 -14
- package/dist/legacy/precacheAndRoute.d.ts.map +0 -1
- package/dist/legacy/registerRoute.d.ts +0 -16
- package/dist/legacy/registerRoute.d.ts.map +0 -1
- package/dist/legacy/registerRuntimeCaching.d.ts +0 -11
- package/dist/legacy/registerRuntimeCaching.d.ts.map +0 -1
- package/dist/legacy/setCatchHandler.d.ts +0 -10
- package/dist/legacy/setCatchHandler.d.ts.map +0 -1
- package/dist/legacy/setDefaultHandler.d.ts +0 -13
- package/dist/legacy/setDefaultHandler.d.ts.map +0 -1
- package/dist/legacy/singletonPrecacheController.d.ts +0 -34
- package/dist/legacy/singletonPrecacheController.d.ts.map +0 -1
- package/dist/legacy/singletonRouter.d.ts +0 -41
- package/dist/legacy/singletonRouter.d.ts.map +0 -1
- package/dist/legacy/unregisterRoute.d.ts +0 -9
- package/dist/legacy/unregisterRoute.d.ts.map +0 -1
- package/dist/legacy/utils/PrecacheCacheKeyPlugin.d.ts +0 -16
- package/dist/legacy/utils/PrecacheCacheKeyPlugin.d.ts.map +0 -1
- package/dist/legacy/utils/getCacheKeyForURL.d.ts +0 -14
- package/dist/legacy/utils/getCacheKeyForURL.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/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/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/googleAnalytics/constants.d.ts +0 -10
- package/dist/lib/googleAnalytics/constants.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/rangeRequests/RangeRequestsPlugin.d.ts.map +0 -1
- package/dist/lib/rangeRequests/createPartialResponse.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/lib/strategies/PrecacheStrategy.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/dist/utils/PrecacheCacheKeyPlugin.d.ts +0 -16
- package/dist/utils/PrecacheCacheKeyPlugin.d.ts.map +0 -1
- package/dist/utils/PrecacheInstallReportPlugin.d.ts +0 -14
- package/dist/utils/PrecacheInstallReportPlugin.d.ts.map +0 -1
- package/dist/utils/parsePrecacheOptions.d.ts +0 -26
- package/dist/utils/parsePrecacheOptions.d.ts.map +0 -1
- package/src/NavigationRoute.ts +0 -118
- package/src/PrecacheRoute.ts +0 -45
- package/src/RegExpRoute.ts +0 -74
- package/src/Route.ts +0 -67
- package/src/Serwist.ts +0 -867
- package/src/copyResponse.ts +0 -58
- package/src/disableDevLogs.ts +0 -10
- package/src/index.legacy.ts +0 -62
- package/src/legacy/PrecacheController.ts +0 -337
- package/src/legacy/PrecacheFallbackPlugin.ts +0 -92
- package/src/legacy/PrecacheRoute.ts +0 -48
- package/src/legacy/Router.ts +0 -484
- package/src/legacy/addPlugins.ts +0 -21
- package/src/legacy/addRoute.ts +0 -27
- package/src/legacy/createHandlerBoundToURL.ts +0 -30
- package/src/legacy/fallbacks.ts +0 -94
- package/src/legacy/getCacheKeyForURL.ts +0 -32
- package/src/legacy/handlePrecaching.ts +0 -86
- package/src/legacy/initializeGoogleAnalytics.ts +0 -218
- package/src/legacy/installSerwist.ts +0 -170
- package/src/legacy/matchPrecache.ts +0 -27
- package/src/legacy/precache.ts +0 -33
- package/src/legacy/precacheAndRoute.ts +0 -27
- package/src/legacy/registerRoute.ts +0 -28
- package/src/legacy/registerRuntimeCaching.ts +0 -17
- package/src/legacy/setCatchHandler.ts +0 -21
- package/src/legacy/setDefaultHandler.ts +0 -24
- package/src/legacy/singletonPrecacheController.ts +0 -53
- package/src/legacy/singletonRouter.ts +0 -70
- package/src/legacy/unregisterRoute.ts +0 -13
- package/src/legacy/utils/getCacheKeyForURL.ts +0 -36
- package/src/lib/googleAnalytics/constants.ts +0 -22
- package/src/registerQuotaErrorCallback.ts +0 -34
- package/src/setCacheNameDetails.ts +0 -53
- package/src/utils/PrecacheCacheKeyPlugin.ts +0 -33
- package/src/utils/parsePrecacheOptions.ts +0 -47
- /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/{broadcastUpdate → broadcast-update}/constants.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/{legacy → lib/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}/BackgroundSyncQueueDb.ts +0 -0
- /package/src/lib/{broadcastUpdate → broadcast-update}/constants.ts +0 -0
- /package/src/{constants.ts → lib/constants.ts} +0 -0
- /package/src/{legacy → lib/extensions/google-analytics}/constants.ts +0 -0
|
@@ -0,0 +1,306 @@
|
|
|
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";
|
|
5
|
+
import { assert } from "#utils/assert.js";
|
|
6
|
+
import { createCacheKey } from "#utils/createCacheKey.js";
|
|
7
|
+
import { logger } from "#utils/logger.js";
|
|
8
|
+
import { printCleanupDetails } from "#utils/printCleanupDetails.js";
|
|
9
|
+
import { printInstallDetails } from "#utils/printInstallDetails.js";
|
|
10
|
+
import { SerwistError } from "#utils/SerwistError.js";
|
|
11
|
+
import { NavigationRoute } from "../../route.js";
|
|
12
|
+
import type { Strategy } from "../../strategies/Strategy.js";
|
|
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";
|
|
19
|
+
|
|
20
|
+
export interface PrecacheEntry {
|
|
21
|
+
integrity?: string;
|
|
22
|
+
url: string;
|
|
23
|
+
revision?: string | null;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export interface PrecacheExtensionOptions {
|
|
27
|
+
/**
|
|
28
|
+
* The cache to use for precaching.
|
|
29
|
+
*/
|
|
30
|
+
cacheName?: string;
|
|
31
|
+
/**
|
|
32
|
+
* Whether outdated caches should be removed.
|
|
33
|
+
*
|
|
34
|
+
* @default false
|
|
35
|
+
*/
|
|
36
|
+
cleanupOutdatedCaches?: boolean;
|
|
37
|
+
/**
|
|
38
|
+
* The number of precache requests that should be made concurrently.
|
|
39
|
+
*
|
|
40
|
+
* @default 10
|
|
41
|
+
*/
|
|
42
|
+
concurrency?: number;
|
|
43
|
+
/**
|
|
44
|
+
* An URL that should point to a HTML file with which navigation requests for URLs that aren't
|
|
45
|
+
* precached will be fulfilled.
|
|
46
|
+
*/
|
|
47
|
+
navigateFallback?: string;
|
|
48
|
+
/**
|
|
49
|
+
* URLs that should be allowed to use the `navigateFallback` handler.
|
|
50
|
+
*/
|
|
51
|
+
navigateFallbackAllowlist?: RegExp[];
|
|
52
|
+
/**
|
|
53
|
+
* URLs that should not be allowed to use the `navigateFallback` handler. This takes precedence
|
|
54
|
+
* over `navigateFallbackAllowlist`.
|
|
55
|
+
*/
|
|
56
|
+
navigateFallbackDenylist?: RegExp[];
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export interface PrecacheOptions extends PrecacheStrategyOptions, PrecacheRouteOptions, PrecacheExtensionOptions {
|
|
60
|
+
entries: (PrecacheEntry | string)[] | undefined;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export class Precache implements Extension {
|
|
64
|
+
private readonly _urlsToCacheKeys: Map<string, string> = new Map();
|
|
65
|
+
private readonly _urlsToCacheModes: Map<string, "reload" | "default" | "no-store" | "no-cache" | "force-cache" | "only-if-cached"> = new Map();
|
|
66
|
+
private readonly _cacheKeysToIntegrities: Map<string, string> = new Map();
|
|
67
|
+
private readonly _strategy: Strategy;
|
|
68
|
+
private _options: PrecacheExtensionOptions;
|
|
69
|
+
private _routeOptions: PrecacheRouteOptions;
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Create a new {@linkcode Precache} instance.
|
|
73
|
+
*
|
|
74
|
+
* @param options
|
|
75
|
+
*/
|
|
76
|
+
constructor(precacheOptions: PrecacheOptions) {
|
|
77
|
+
const { entries, strategyOptions, routeOptions, extensionOptions: controllerOptions } = parsePrecacheOptions(this, precacheOptions);
|
|
78
|
+
this.addToCacheList(entries);
|
|
79
|
+
this._strategy = new PrecacheStrategy(strategyOptions);
|
|
80
|
+
this._options = controllerOptions;
|
|
81
|
+
this._routeOptions = routeOptions;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* The strategy created by this controller and
|
|
86
|
+
* used to cache assets and respond to `fetch` events.
|
|
87
|
+
*/
|
|
88
|
+
get strategy(): Strategy {
|
|
89
|
+
return this._strategy;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Adds items to the cache list, removing duplicates and ensuring the information is valid.
|
|
94
|
+
*
|
|
95
|
+
* @param entries Array of entries to precache.
|
|
96
|
+
*/
|
|
97
|
+
addToCacheList(entries: (PrecacheEntry | string)[]): void {
|
|
98
|
+
if (process.env.NODE_ENV !== "production") {
|
|
99
|
+
assert!.isArray(entries, {
|
|
100
|
+
moduleName: "serwist",
|
|
101
|
+
className: "PrecacheController",
|
|
102
|
+
funcName: "addEntries",
|
|
103
|
+
paramName: "entries",
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const urlsToWarnAbout: string[] = [];
|
|
108
|
+
for (const entry of entries) {
|
|
109
|
+
// See https://github.com/GoogleChrome/workbox/issues/2259
|
|
110
|
+
if (typeof entry === "string") {
|
|
111
|
+
urlsToWarnAbout.push(entry);
|
|
112
|
+
} else if (entry && !entry.integrity && entry.revision === undefined) {
|
|
113
|
+
urlsToWarnAbout.push(entry.url);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
const { cacheKey, url } = createCacheKey(entry);
|
|
117
|
+
const cacheMode = typeof entry !== "string" && entry.revision ? "reload" : "default";
|
|
118
|
+
|
|
119
|
+
if (this._urlsToCacheKeys.has(url) && this._urlsToCacheKeys.get(url) !== cacheKey) {
|
|
120
|
+
throw new SerwistError("add-to-cache-list-conflicting-entries", {
|
|
121
|
+
firstEntry: this._urlsToCacheKeys.get(url),
|
|
122
|
+
secondEntry: cacheKey,
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
if (typeof entry !== "string" && entry.integrity) {
|
|
127
|
+
if (this._cacheKeysToIntegrities.has(cacheKey) && this._cacheKeysToIntegrities.get(cacheKey) !== entry.integrity) {
|
|
128
|
+
throw new SerwistError("add-to-cache-list-conflicting-integrities", {
|
|
129
|
+
url,
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
this._cacheKeysToIntegrities.set(cacheKey, entry.integrity);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
this._urlsToCacheKeys.set(url, cacheKey);
|
|
136
|
+
this._urlsToCacheModes.set(url, cacheMode);
|
|
137
|
+
|
|
138
|
+
if (urlsToWarnAbout.length > 0) {
|
|
139
|
+
const warningMessage = `Serwist is precaching URLs without revision info: ${urlsToWarnAbout.join(
|
|
140
|
+
", ",
|
|
141
|
+
)}\nThis is generally NOT safe, as you risk serving outdated assets.`;
|
|
142
|
+
if (process.env.NODE_ENV === "production") {
|
|
143
|
+
// Use console directly to display this warning without bloating
|
|
144
|
+
// bundle sizes by pulling in all of the logger codebase in prod.
|
|
145
|
+
console.warn(warningMessage);
|
|
146
|
+
} else {
|
|
147
|
+
logger.warn(warningMessage);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
init({ serwist }: { serwist: Serwist }): void {
|
|
154
|
+
registerRoute(serwist, new PrecacheRoute(this, this._routeOptions));
|
|
155
|
+
|
|
156
|
+
if (this._options.navigateFallback) {
|
|
157
|
+
registerRoute(
|
|
158
|
+
serwist,
|
|
159
|
+
new NavigationRoute(this.createHandlerBoundToUrl(this._options.navigateFallback), {
|
|
160
|
+
allowlist: this._options.navigateFallbackAllowlist,
|
|
161
|
+
denylist: this._options.navigateFallbackDenylist,
|
|
162
|
+
}),
|
|
163
|
+
);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
async install({ event }: { event: ExtendableEvent }): Promise<void> {
|
|
168
|
+
const installReportPlugin = new PrecacheInstallReportPlugin();
|
|
169
|
+
this._strategy.plugins.push(installReportPlugin);
|
|
170
|
+
await parallel(this._options.concurrency!, Array.from(this._urlsToCacheKeys.entries()), async ([url, cacheKey]): Promise<void> => {
|
|
171
|
+
const integrity = this._cacheKeysToIntegrities.get(cacheKey);
|
|
172
|
+
const cacheMode = this._urlsToCacheModes.get(url);
|
|
173
|
+
|
|
174
|
+
const request = new Request(url, {
|
|
175
|
+
integrity,
|
|
176
|
+
cache: cacheMode,
|
|
177
|
+
credentials: "same-origin",
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
await Promise.all(
|
|
181
|
+
this._strategy.handleAll({
|
|
182
|
+
event,
|
|
183
|
+
request,
|
|
184
|
+
url: new URL(request.url),
|
|
185
|
+
params: { cacheKey },
|
|
186
|
+
}),
|
|
187
|
+
);
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
const { updatedURLs, notUpdatedURLs } = installReportPlugin;
|
|
191
|
+
|
|
192
|
+
if (process.env.NODE_ENV !== "production") {
|
|
193
|
+
printInstallDetails(updatedURLs, notUpdatedURLs);
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
async activate(): Promise<void> {
|
|
198
|
+
const cache = await self.caches.open(this._strategy.cacheName);
|
|
199
|
+
const currentlyCachedRequests = await cache.keys();
|
|
200
|
+
const expectedCacheKeys = new Set(this._urlsToCacheKeys.values());
|
|
201
|
+
|
|
202
|
+
const deletedCacheRequests: string[] = [];
|
|
203
|
+
|
|
204
|
+
for (const request of currentlyCachedRequests) {
|
|
205
|
+
if (!expectedCacheKeys.has(request.url)) {
|
|
206
|
+
await cache.delete(request);
|
|
207
|
+
deletedCacheRequests.push(request.url);
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
if (process.env.NODE_ENV !== "production") {
|
|
212
|
+
printCleanupDetails(deletedCacheRequests);
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* Returns a mapping of a precached URL to the corresponding cache key, taking
|
|
218
|
+
* into account the revision information for the URL.
|
|
219
|
+
*
|
|
220
|
+
* @returns A URL to cache key mapping.
|
|
221
|
+
*/
|
|
222
|
+
getUrlsToPrecacheKeys(): Map<string, string> {
|
|
223
|
+
return this._urlsToCacheKeys;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* Returns a list of all the URLs that have been precached by the current
|
|
228
|
+
* service worker.
|
|
229
|
+
*
|
|
230
|
+
* @returns The precached URLs.
|
|
231
|
+
*/
|
|
232
|
+
getPrecachedUrls(): string[] {
|
|
233
|
+
return [...this._urlsToCacheKeys.keys()];
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* Returns the cache key used for storing a given URL. If that URL is
|
|
238
|
+
* unversioned, like "/index.html", then the cache key will be the original
|
|
239
|
+
* URL with a search parameter appended to it.
|
|
240
|
+
*
|
|
241
|
+
* @param url A URL whose cache key you want to look up.
|
|
242
|
+
* @returns The versioned URL that corresponds to a cache key
|
|
243
|
+
* for the original URL, or undefined if that URL isn't precached.
|
|
244
|
+
*/
|
|
245
|
+
getPrecacheKeyForUrl(url: string): string | undefined {
|
|
246
|
+
const urlObject = new URL(url, location.href);
|
|
247
|
+
return this._urlsToCacheKeys.get(urlObject.href);
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* @param url A cache key whose SRI you want to look up.
|
|
252
|
+
* @returns The subresource integrity associated with the cache key,
|
|
253
|
+
* or undefined if it's not set.
|
|
254
|
+
*/
|
|
255
|
+
getIntegrityForPrecacheKey(cacheKey: string): string | undefined {
|
|
256
|
+
return this._cacheKeysToIntegrities.get(cacheKey);
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
/**
|
|
260
|
+
* This acts as a drop-in replacement for
|
|
261
|
+
* [`cache.match()`](https://developer.mozilla.org/en-US/docs/Web/API/Cache/match)
|
|
262
|
+
* with the following differences:
|
|
263
|
+
*
|
|
264
|
+
* - It knows what the name of the precache is, and only checks in that cache.
|
|
265
|
+
* - It allows you to pass in an "original" URL without versioning parameters,
|
|
266
|
+
* and it will automatically look up the correct cache key for the currently
|
|
267
|
+
* active revision of that URL.
|
|
268
|
+
*
|
|
269
|
+
* E.g., `matchPrecache('index.html')` will find the correct precached
|
|
270
|
+
* response for the currently active service worker, even if the actual cache
|
|
271
|
+
* key is `'/index.html?__WB_REVISION__=1234abcd'`.
|
|
272
|
+
*
|
|
273
|
+
* @param request The key (without revisioning parameters)
|
|
274
|
+
* to look up in the precache.
|
|
275
|
+
* @returns
|
|
276
|
+
*/
|
|
277
|
+
async matchPrecache(request: string | Request): Promise<Response | undefined> {
|
|
278
|
+
const url = request instanceof Request ? request.url : request;
|
|
279
|
+
const cacheKey = this.getPrecacheKeyForUrl(url);
|
|
280
|
+
if (cacheKey) {
|
|
281
|
+
const cache = await self.caches.open(this._strategy.cacheName);
|
|
282
|
+
return cache.match(cacheKey);
|
|
283
|
+
}
|
|
284
|
+
return undefined;
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
/**
|
|
288
|
+
* Returns a function that looks up `url` in the precache (taking into
|
|
289
|
+
* account revision information), and returns the corresponding `Response`.
|
|
290
|
+
*
|
|
291
|
+
* @param url The precached URL which will be used to lookup the response.
|
|
292
|
+
* @return
|
|
293
|
+
*/
|
|
294
|
+
createHandlerBoundToUrl(url: string): RouteHandlerCallback {
|
|
295
|
+
const cacheKey = this.getPrecacheKeyForUrl(url);
|
|
296
|
+
if (!cacheKey) {
|
|
297
|
+
throw new SerwistError("non-precached-url", { url });
|
|
298
|
+
}
|
|
299
|
+
return (options) => {
|
|
300
|
+
options.request = new Request(url);
|
|
301
|
+
options.params = { cacheKey, ...options.params };
|
|
302
|
+
|
|
303
|
+
return this._strategy.handle(options);
|
|
304
|
+
};
|
|
305
|
+
}
|
|
306
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import type { Require } from "@serwist/utils";
|
|
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";
|
|
7
|
+
|
|
8
|
+
export const parsePrecacheOptions = (
|
|
9
|
+
controller: Precache,
|
|
10
|
+
{
|
|
11
|
+
entries,
|
|
12
|
+
cacheName,
|
|
13
|
+
plugins,
|
|
14
|
+
fetchOptions,
|
|
15
|
+
matchOptions,
|
|
16
|
+
fallbackToNetwork,
|
|
17
|
+
directoryIndex,
|
|
18
|
+
ignoreURLParametersMatching,
|
|
19
|
+
cleanURLs,
|
|
20
|
+
urlManipulation,
|
|
21
|
+
cleanupOutdatedCaches,
|
|
22
|
+
concurrency,
|
|
23
|
+
navigateFallback,
|
|
24
|
+
navigateFallbackAllowlist,
|
|
25
|
+
navigateFallbackDenylist,
|
|
26
|
+
}: PrecacheOptions,
|
|
27
|
+
) => ({
|
|
28
|
+
entries: entries ?? [],
|
|
29
|
+
strategyOptions: {
|
|
30
|
+
cacheName: privateCacheNames.getPrecacheName(cacheName),
|
|
31
|
+
plugins: [...(plugins ?? []), new PrecacheCacheKeyPlugin({ precacheController: controller })],
|
|
32
|
+
fetchOptions,
|
|
33
|
+
matchOptions,
|
|
34
|
+
fallbackToNetwork,
|
|
35
|
+
} satisfies Require<PrecacheStrategyOptions, "cacheName" | "plugins">,
|
|
36
|
+
routeOptions: {
|
|
37
|
+
directoryIndex,
|
|
38
|
+
ignoreURLParametersMatching,
|
|
39
|
+
cleanURLs,
|
|
40
|
+
urlManipulation,
|
|
41
|
+
} satisfies PrecacheRouteOptions,
|
|
42
|
+
extensionOptions: {
|
|
43
|
+
cleanupOutdatedCaches,
|
|
44
|
+
concurrency: concurrency ?? 10,
|
|
45
|
+
navigateFallback,
|
|
46
|
+
navigateFallbackAllowlist,
|
|
47
|
+
navigateFallbackDenylist,
|
|
48
|
+
} satisfies Require<PrecacheExtensionOptions, "concurrency">,
|
|
49
|
+
});
|
package/src/{legacy/utils/PrecacheCacheKeyPlugin.ts → lib/extensions/precache/plugin-cache-key.ts}
RENAMED
|
@@ -5,9 +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
|
-
|
|
9
|
-
import type {
|
|
10
|
-
import type { PrecacheController } from "../PrecacheController.js";
|
|
8
|
+
import type { StrategyPlugin, StrategyPluginCallbackParam } from "#lib/types.js";
|
|
9
|
+
import type { Precache } from "./extension.js";
|
|
11
10
|
|
|
12
11
|
/**
|
|
13
12
|
* A plugin, designed to be used with PrecacheController, to translate URLs into
|
|
@@ -15,17 +14,17 @@ import type { PrecacheController } from "../PrecacheController.js";
|
|
|
15
14
|
*
|
|
16
15
|
* @private
|
|
17
16
|
*/
|
|
18
|
-
export class PrecacheCacheKeyPlugin implements
|
|
19
|
-
private readonly _precacheController:
|
|
17
|
+
export class PrecacheCacheKeyPlugin implements StrategyPlugin {
|
|
18
|
+
private readonly _precacheController: Precache;
|
|
20
19
|
|
|
21
|
-
constructor({ precacheController }: { precacheController:
|
|
20
|
+
constructor({ precacheController }: { precacheController: Precache }) {
|
|
22
21
|
this._precacheController = precacheController;
|
|
23
22
|
}
|
|
24
23
|
|
|
25
|
-
cacheKeyWillBeUsed:
|
|
24
|
+
cacheKeyWillBeUsed: StrategyPlugin["cacheKeyWillBeUsed"] = async ({ request, params }: StrategyPluginCallbackParam["cacheKeyWillBeUsed"]) => {
|
|
26
25
|
// Params is type any, can't change right now.
|
|
27
26
|
/* eslint-disable */
|
|
28
|
-
const cacheKey = params?.cacheKey || this._precacheController.
|
|
27
|
+
const cacheKey = params?.cacheKey || this._precacheController.getPrecacheKeyForUrl(request.url);
|
|
29
28
|
/* eslint-enable */
|
|
30
29
|
|
|
31
30
|
return cacheKey ? new Request(cacheKey, { headers: request.headers }) : request;
|
package/src/lib/{precaching/PrecacheFallbackPlugin.ts → extensions/precache/plugin-fallback.ts}
RENAMED
|
@@ -5,9 +5,9 @@
|
|
|
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 type { HandlerDidErrorCallbackParam,
|
|
8
|
+
import type { Serwist } from "#lib/core.js";
|
|
9
|
+
import { handleRequest } from "#lib/functions/router.js";
|
|
10
|
+
import type { HandlerDidErrorCallbackParam, StrategyPlugin } from "#lib/types.js";
|
|
11
11
|
|
|
12
12
|
export interface PrecacheFallbackEntry {
|
|
13
13
|
/**
|
|
@@ -41,7 +41,7 @@ export interface PrecacheFallbackPluginOptions {
|
|
|
41
41
|
* and returning a precached response, taking the expected revision parameter
|
|
42
42
|
* into account automatically.
|
|
43
43
|
*/
|
|
44
|
-
export class PrecacheFallbackPlugin implements
|
|
44
|
+
export class PrecacheFallbackPlugin implements StrategyPlugin {
|
|
45
45
|
private readonly _fallbackUrls: (string | PrecacheFallbackEntry)[];
|
|
46
46
|
private readonly _serwist: Serwist;
|
|
47
47
|
|
|
@@ -52,6 +52,7 @@ export class PrecacheFallbackPlugin implements SerwistPlugin {
|
|
|
52
52
|
*/
|
|
53
53
|
constructor({ fallbackUrls, serwist }: PrecacheFallbackPluginOptions) {
|
|
54
54
|
this._fallbackUrls = fallbackUrls;
|
|
55
|
+
// TODO(ducanhgh): remove in v11.
|
|
55
56
|
this._serwist = serwist;
|
|
56
57
|
}
|
|
57
58
|
|
|
@@ -63,14 +64,24 @@ export class PrecacheFallbackPlugin implements SerwistPlugin {
|
|
|
63
64
|
async handlerDidError(param: HandlerDidErrorCallbackParam) {
|
|
64
65
|
for (const fallback of this._fallbackUrls) {
|
|
65
66
|
if (typeof fallback === "string") {
|
|
66
|
-
const fallbackResponse =
|
|
67
|
+
const fallbackResponse = handleRequest(this._serwist, {
|
|
68
|
+
request: new Request(fallback, {
|
|
69
|
+
credentials: "same-origin",
|
|
70
|
+
}),
|
|
71
|
+
event: param.event,
|
|
72
|
+
});
|
|
67
73
|
if (fallbackResponse !== undefined) {
|
|
68
|
-
return fallbackResponse;
|
|
74
|
+
return await fallbackResponse;
|
|
69
75
|
}
|
|
70
76
|
} else if (fallback.matcher(param)) {
|
|
71
|
-
const fallbackResponse =
|
|
77
|
+
const fallbackResponse = handleRequest(this._serwist, {
|
|
78
|
+
request: new Request(fallback.url, {
|
|
79
|
+
credentials: "same-origin",
|
|
80
|
+
}),
|
|
81
|
+
event: param.event,
|
|
82
|
+
});
|
|
72
83
|
if (fallbackResponse !== undefined) {
|
|
73
|
-
return fallbackResponse;
|
|
84
|
+
return await fallbackResponse;
|
|
74
85
|
}
|
|
75
86
|
}
|
|
76
87
|
}
|
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
https://opensource.org/licenses/MIT.
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
|
-
import type {
|
|
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)
|
|
@@ -14,22 +14,22 @@ import type { SerwistPlugin, SerwistPluginCallbackParam } from "../types.js";
|
|
|
14
14
|
*
|
|
15
15
|
* @private
|
|
16
16
|
*/
|
|
17
|
-
export class PrecacheInstallReportPlugin implements
|
|
17
|
+
export class PrecacheInstallReportPlugin implements StrategyPlugin {
|
|
18
18
|
updatedURLs: string[] = [];
|
|
19
19
|
notUpdatedURLs: string[] = [];
|
|
20
20
|
|
|
21
|
-
handlerWillStart:
|
|
21
|
+
handlerWillStart: StrategyPlugin["handlerWillStart"] = async ({ request, state }: StrategyPluginCallbackParam["handlerWillStart"]) => {
|
|
22
22
|
// TODO: `state` should never be undefined...
|
|
23
23
|
if (state) {
|
|
24
24
|
state.originalRequest = request;
|
|
25
25
|
}
|
|
26
26
|
};
|
|
27
27
|
|
|
28
|
-
cachedResponseWillBeUsed:
|
|
28
|
+
cachedResponseWillBeUsed: StrategyPlugin["cachedResponseWillBeUsed"] = async ({
|
|
29
29
|
event,
|
|
30
30
|
state,
|
|
31
31
|
cachedResponse,
|
|
32
|
-
}:
|
|
32
|
+
}: StrategyPluginCallbackParam["cachedResponseWillBeUsed"]) => {
|
|
33
33
|
if (event.type === "install") {
|
|
34
34
|
if (state?.originalRequest && state.originalRequest instanceof Request) {
|
|
35
35
|
// TODO: `state` should never be undefined...
|
|
@@ -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,16 +6,16 @@
|
|
|
6
6
|
https://opensource.org/licenses/MIT.
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
|
-
import {
|
|
10
|
-
import type {
|
|
11
|
-
import {
|
|
12
|
-
import { cacheNames as privateCacheNames } from "
|
|
13
|
-
import { getFriendlyURL } from "
|
|
14
|
-
import { logger } from "
|
|
15
|
-
import
|
|
16
|
-
import type { StrategyOptions } from "
|
|
17
|
-
import { Strategy } from "
|
|
18
|
-
import type { StrategyHandler } from "
|
|
9
|
+
import type { Serwist } from "#lib/core.js";
|
|
10
|
+
import type { StrategyPlugin } from "#lib/types.js";
|
|
11
|
+
import { copyResponse } from "#lib/utils.js";
|
|
12
|
+
import { cacheNames as privateCacheNames } from "#utils/cacheNames.js";
|
|
13
|
+
import { getFriendlyURL } from "#utils/getFriendlyURL.js";
|
|
14
|
+
import { logger } from "#utils/logger.js";
|
|
15
|
+
import { SerwistError } from "#utils/SerwistError.js";
|
|
16
|
+
import type { StrategyOptions } from "../../strategies/Strategy.js";
|
|
17
|
+
import { Strategy } from "../../strategies/Strategy.js";
|
|
18
|
+
import type { StrategyHandler } from "../../strategies/StrategyHandler.js";
|
|
19
19
|
|
|
20
20
|
export interface PrecacheStrategyOptions extends StrategyOptions {
|
|
21
21
|
/**
|
|
@@ -39,7 +39,7 @@ export interface PrecacheStrategyOptions extends StrategyOptions {
|
|
|
39
39
|
export class PrecacheStrategy extends Strategy {
|
|
40
40
|
private readonly _fallbackToNetwork: boolean;
|
|
41
41
|
|
|
42
|
-
static readonly defaultPrecacheCacheabilityPlugin:
|
|
42
|
+
static readonly defaultPrecacheCacheabilityPlugin: StrategyPlugin = {
|
|
43
43
|
async cacheWillUpdate({ response }) {
|
|
44
44
|
if (!response || response.status >= 400) {
|
|
45
45
|
return null;
|
|
@@ -49,7 +49,7 @@ export class PrecacheStrategy extends Strategy {
|
|
|
49
49
|
},
|
|
50
50
|
};
|
|
51
51
|
|
|
52
|
-
static readonly copyRedirectedCacheableResponsesPlugin:
|
|
52
|
+
static readonly copyRedirectedCacheableResponsesPlugin: StrategyPlugin = {
|
|
53
53
|
async cacheWillUpdate({ response }) {
|
|
54
54
|
return response.redirected ? await copyResponse(response) : response;
|
|
55
55
|
},
|