@serwist/sw 9.0.0-preview.20 → 9.0.0-preview.24
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/index.d.ts +4 -11
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -156
- package/package.json +5 -40
- package/src/index.ts +5 -22
- package/README.md +0 -1
- package/dist/abstractions/Serwist.d.ts +0 -82
- package/dist/abstractions/Serwist.d.ts.map +0 -1
- package/dist/abstractions/disableDevLogs.d.ts +0 -7
- package/dist/abstractions/disableDevLogs.d.ts.map +0 -1
- package/dist/abstractions/fallbacks.d.ts +0 -39
- package/dist/abstractions/fallbacks.d.ts.map +0 -1
- package/dist/abstractions/handlePrecaching.d.ts +0 -39
- package/dist/abstractions/handlePrecaching.d.ts.map +0 -1
- package/dist/abstractions/installSerwist.d.ts +0 -15
- package/dist/abstractions/installSerwist.d.ts.map +0 -1
- package/dist/abstractions/navigationPreload.d.ts +0 -20
- package/dist/abstractions/navigationPreload.d.ts.map +0 -1
- package/dist/abstractions/registerRuntimeCaching.d.ts +0 -11
- package/dist/abstractions/registerRuntimeCaching.d.ts.map +0 -1
- package/dist/abstractions/types.d.ts +0 -29
- package/dist/abstractions/types.d.ts.map +0 -1
- package/dist/chunks/NavigationRoute.js +0 -54
- package/dist/chunks/NetworkOnly.js +0 -193
- package/dist/chunks/PrecacheFallbackPlugin.js +0 -573
- package/dist/chunks/Strategy.js +0 -410
- package/dist/chunks/precacheAndRoute.js +0 -113
- package/dist/chunks/registerRoute.js +0 -7
- package/dist/chunks/singletonPrecacheController.js +0 -433
- package/dist/chunks/singletonRouter.js +0 -435
- package/dist/index.plugins.d.ts +0 -41
- package/dist/index.plugins.d.ts.map +0 -1
- package/dist/index.plugins.js +0 -671
- package/dist/index.precaching.d.ts +0 -25
- package/dist/index.precaching.d.ts.map +0 -1
- package/dist/index.precaching.js +0 -24
- package/dist/index.routing.d.ts +0 -15
- package/dist/index.routing.d.ts.map +0 -1
- package/dist/index.routing.js +0 -19
- package/dist/index.strategies.d.ts +0 -22
- package/dist/index.strategies.d.ts.map +0 -1
- package/dist/index.strategies.js +0 -146
- package/dist/plugins/backgroundSync/BackgroundSyncPlugin.d.ts +0 -23
- package/dist/plugins/backgroundSync/BackgroundSyncPlugin.d.ts.map +0 -1
- package/dist/plugins/backgroundSync/Queue.d.ts +0 -166
- package/dist/plugins/backgroundSync/Queue.d.ts.map +0 -1
- package/dist/plugins/backgroundSync/QueueDb.d.ts +0 -90
- package/dist/plugins/backgroundSync/QueueDb.d.ts.map +0 -1
- package/dist/plugins/backgroundSync/QueueStore.d.ts +0 -75
- package/dist/plugins/backgroundSync/QueueStore.d.ts.map +0 -1
- package/dist/plugins/backgroundSync/StorableRequest.d.ts +0 -51
- package/dist/plugins/backgroundSync/StorableRequest.d.ts.map +0 -1
- package/dist/plugins/broadcastUpdate/BroadcastCacheUpdate.d.ts +0 -45
- package/dist/plugins/broadcastUpdate/BroadcastCacheUpdate.d.ts.map +0 -1
- package/dist/plugins/broadcastUpdate/BroadcastUpdatePlugin.d.ts +0 -27
- package/dist/plugins/broadcastUpdate/BroadcastUpdatePlugin.d.ts.map +0 -1
- package/dist/plugins/broadcastUpdate/constants.d.ts +0 -5
- package/dist/plugins/broadcastUpdate/constants.d.ts.map +0 -1
- package/dist/plugins/broadcastUpdate/responsesAreSame.d.ts +0 -11
- package/dist/plugins/broadcastUpdate/responsesAreSame.d.ts.map +0 -1
- package/dist/plugins/broadcastUpdate/types.d.ts +0 -34
- package/dist/plugins/broadcastUpdate/types.d.ts.map +0 -1
- package/dist/plugins/cacheableResponse/CacheableResponse.d.ts +0 -40
- package/dist/plugins/cacheableResponse/CacheableResponse.d.ts.map +0 -1
- package/dist/plugins/cacheableResponse/CacheableResponsePlugin.d.ts +0 -27
- package/dist/plugins/cacheableResponse/CacheableResponsePlugin.d.ts.map +0 -1
- package/dist/plugins/expiration/CacheExpiration.d.ts +0 -66
- package/dist/plugins/expiration/CacheExpiration.d.ts.map +0 -1
- package/dist/plugins/expiration/ExpirationPlugin.d.ts +0 -116
- package/dist/plugins/expiration/ExpirationPlugin.d.ts.map +0 -1
- package/dist/plugins/expiration/models/CacheTimestampsModel.d.ts +0 -73
- package/dist/plugins/expiration/models/CacheTimestampsModel.d.ts.map +0 -1
- package/dist/plugins/googleAnalytics/constants.d.ts +0 -10
- package/dist/plugins/googleAnalytics/constants.d.ts.map +0 -1
- package/dist/plugins/googleAnalytics/initialize.d.ts +0 -34
- package/dist/plugins/googleAnalytics/initialize.d.ts.map +0 -1
- package/dist/plugins/precaching/PrecacheFallbackPlugin.d.ts +0 -54
- package/dist/plugins/precaching/PrecacheFallbackPlugin.d.ts.map +0 -1
- package/dist/plugins/rangeRequests/RangeRequestsPlugin.d.ts +0 -19
- package/dist/plugins/rangeRequests/RangeRequestsPlugin.d.ts.map +0 -1
- package/dist/plugins/rangeRequests/createPartialResponse.d.ts +0 -18
- package/dist/plugins/rangeRequests/createPartialResponse.d.ts.map +0 -1
- package/dist/plugins/rangeRequests/utils/calculateEffectiveBoundaries.d.ts +0 -14
- package/dist/plugins/rangeRequests/utils/calculateEffectiveBoundaries.d.ts.map +0 -1
- package/dist/plugins/rangeRequests/utils/parseRangeHeader.d.ts +0 -12
- package/dist/plugins/rangeRequests/utils/parseRangeHeader.d.ts.map +0 -1
- package/dist/precaching/PrecacheController.d.ts +0 -145
- package/dist/precaching/PrecacheController.d.ts.map +0 -1
- package/dist/precaching/PrecacheRoute.d.ts +0 -20
- package/dist/precaching/PrecacheRoute.d.ts.map +0 -1
- package/dist/precaching/PrecacheStrategy.d.ts +0 -68
- package/dist/precaching/PrecacheStrategy.d.ts.map +0 -1
- package/dist/precaching/addPlugins.d.ts +0 -8
- package/dist/precaching/addPlugins.d.ts.map +0 -1
- package/dist/precaching/addRoute.d.ts +0 -15
- package/dist/precaching/addRoute.d.ts.map +0 -1
- package/dist/precaching/cleanupOutdatedCaches.d.ts +0 -6
- package/dist/precaching/cleanupOutdatedCaches.d.ts.map +0 -1
- package/dist/precaching/createHandlerBoundToURL.d.ts +0 -17
- package/dist/precaching/createHandlerBoundToURL.d.ts.map +0 -1
- package/dist/precaching/getCacheKeyForURL.d.ts +0 -20
- package/dist/precaching/getCacheKeyForURL.d.ts.map +0 -1
- package/dist/precaching/matchPrecache.d.ts +0 -14
- package/dist/precaching/matchPrecache.d.ts.map +0 -1
- package/dist/precaching/precache.d.ts +0 -19
- package/dist/precaching/precache.d.ts.map +0 -1
- package/dist/precaching/precacheAndRoute.d.ts +0 -14
- package/dist/precaching/precacheAndRoute.d.ts.map +0 -1
- package/dist/precaching/singletonPrecacheController.d.ts +0 -38
- package/dist/precaching/singletonPrecacheController.d.ts.map +0 -1
- package/dist/precaching/types.d.ts +0 -37
- package/dist/precaching/types.d.ts.map +0 -1
- package/dist/precaching/utils/PrecacheCacheKeyPlugin.d.ts +0 -17
- package/dist/precaching/utils/PrecacheCacheKeyPlugin.d.ts.map +0 -1
- package/dist/precaching/utils/PrecacheInstallReportPlugin.d.ts +0 -15
- package/dist/precaching/utils/PrecacheInstallReportPlugin.d.ts.map +0 -1
- package/dist/precaching/utils/createCacheKey.d.ts +0 -16
- package/dist/precaching/utils/createCacheKey.d.ts.map +0 -1
- package/dist/precaching/utils/deleteOutdatedCaches.d.ts +0 -18
- package/dist/precaching/utils/deleteOutdatedCaches.d.ts.map +0 -1
- package/dist/precaching/utils/generateURLVariations.d.ts +0 -12
- package/dist/precaching/utils/generateURLVariations.d.ts.map +0 -1
- package/dist/precaching/utils/getCacheKeyForURL.d.ts +0 -14
- package/dist/precaching/utils/getCacheKeyForURL.d.ts.map +0 -1
- package/dist/precaching/utils/printCleanupDetails.d.ts +0 -6
- package/dist/precaching/utils/printCleanupDetails.d.ts.map +0 -1
- package/dist/precaching/utils/printInstallDetails.d.ts +0 -7
- package/dist/precaching/utils/printInstallDetails.d.ts.map +0 -1
- package/dist/precaching/utils/removeIgnoredSearchParams.d.ts +0 -12
- package/dist/precaching/utils/removeIgnoredSearchParams.d.ts.map +0 -1
- package/dist/routing/NavigationRoute.d.ts +0 -57
- package/dist/routing/NavigationRoute.d.ts.map +0 -1
- package/dist/routing/RegExpRoute.d.ts +0 -24
- package/dist/routing/RegExpRoute.d.ts.map +0 -1
- package/dist/routing/Route.d.ts +0 -33
- package/dist/routing/Route.d.ts.map +0 -1
- package/dist/routing/Router.d.ts +0 -150
- package/dist/routing/Router.d.ts.map +0 -1
- package/dist/routing/parseRoute.d.ts +0 -16
- package/dist/routing/parseRoute.d.ts.map +0 -1
- package/dist/routing/registerRoute.d.ts +0 -15
- package/dist/routing/registerRoute.d.ts.map +0 -1
- package/dist/routing/setCatchHandler.d.ts +0 -9
- package/dist/routing/setCatchHandler.d.ts.map +0 -1
- package/dist/routing/setDefaultHandler.d.ts +0 -12
- package/dist/routing/setDefaultHandler.d.ts.map +0 -1
- package/dist/routing/singletonRouter.d.ts +0 -47
- package/dist/routing/singletonRouter.d.ts.map +0 -1
- package/dist/routing/unregisterRoute.d.ts +0 -8
- package/dist/routing/unregisterRoute.d.ts.map +0 -1
- package/dist/routing/utils/constants.d.ts +0 -15
- package/dist/routing/utils/constants.d.ts.map +0 -1
- package/dist/routing/utils/normalizeHandler.d.ts +0 -10
- package/dist/routing/utils/normalizeHandler.d.ts.map +0 -1
- package/dist/strategies/CacheFirst.d.ts +0 -23
- package/dist/strategies/CacheFirst.d.ts.map +0 -1
- package/dist/strategies/CacheOnly.d.ts +0 -20
- package/dist/strategies/CacheOnly.d.ts.map +0 -1
- package/dist/strategies/NetworkFirst.d.ts +0 -61
- package/dist/strategies/NetworkFirst.d.ts.map +0 -1
- package/dist/strategies/NetworkOnly.d.ts +0 -32
- package/dist/strategies/NetworkOnly.d.ts.map +0 -1
- package/dist/strategies/StaleWhileRevalidate.d.ts +0 -35
- package/dist/strategies/StaleWhileRevalidate.d.ts.map +0 -1
- package/dist/strategies/Strategy.d.ts +0 -83
- package/dist/strategies/Strategy.d.ts.map +0 -1
- package/dist/strategies/StrategyHandler.d.ts +0 -189
- package/dist/strategies/StrategyHandler.d.ts.map +0 -1
- package/dist/strategies/plugins/cacheOkAndOpaquePlugin.d.ts +0 -3
- package/dist/strategies/plugins/cacheOkAndOpaquePlugin.d.ts.map +0 -1
- package/dist/strategies/utils/messages.d.ts +0 -5
- package/dist/strategies/utils/messages.d.ts.map +0 -1
- package/src/abstractions/Serwist.ts +0 -177
- package/src/abstractions/disableDevLogs.ts +0 -10
- package/src/abstractions/fallbacks.ts +0 -65
- package/src/abstractions/handlePrecaching.ts +0 -65
- package/src/abstractions/installSerwist.ts +0 -28
- package/src/abstractions/navigationPreload.ts +0 -64
- package/src/abstractions/registerRuntimeCaching.ts +0 -17
- package/src/abstractions/types.ts +0 -29
- package/src/index.plugins.ts +0 -95
- package/src/index.precaching.ts +0 -41
- package/src/index.routing.ts +0 -28
- package/src/index.strategies.ts +0 -26
- package/src/plugins/backgroundSync/BackgroundSyncPlugin.ts +0 -39
- package/src/plugins/backgroundSync/Queue.ts +0 -438
- package/src/plugins/backgroundSync/QueueDb.ts +0 -176
- package/src/plugins/backgroundSync/QueueStore.ts +0 -161
- package/src/plugins/backgroundSync/StorableRequest.ts +0 -142
- package/src/plugins/broadcastUpdate/BroadcastCacheUpdate.ts +0 -159
- package/src/plugins/broadcastUpdate/BroadcastUpdatePlugin.ts +0 -43
- package/src/plugins/broadcastUpdate/constants.ts +0 -12
- package/src/plugins/broadcastUpdate/responsesAreSame.ts +0 -48
- package/src/plugins/broadcastUpdate/types.ts +0 -37
- package/src/plugins/cacheableResponse/CacheableResponse.ts +0 -141
- package/src/plugins/cacheableResponse/CacheableResponsePlugin.ts +0 -46
- package/src/plugins/expiration/CacheExpiration.ts +0 -192
- package/src/plugins/expiration/ExpirationPlugin.ts +0 -297
- package/src/plugins/expiration/models/CacheTimestampsModel.ts +0 -184
- package/src/plugins/googleAnalytics/constants.ts +0 -22
- package/src/plugins/googleAnalytics/initialize.ts +0 -213
- package/src/plugins/precaching/PrecacheFallbackPlugin.ts +0 -86
- package/src/plugins/rangeRequests/RangeRequestsPlugin.ts +0 -39
- package/src/plugins/rangeRequests/createPartialResponse.ts +0 -92
- package/src/plugins/rangeRequests/utils/calculateEffectiveBoundaries.ts +0 -58
- package/src/plugins/rangeRequests/utils/parseRangeHeader.ts +0 -54
- package/src/precaching/PrecacheController.ts +0 -332
- package/src/precaching/PrecacheRoute.ts +0 -50
- package/src/precaching/PrecacheStrategy.ts +0 -238
- package/src/precaching/addPlugins.ts +0 -21
- package/src/precaching/addRoute.ts +0 -30
- package/src/precaching/cleanupOutdatedCaches.ts +0 -33
- package/src/precaching/createHandlerBoundToURL.ts +0 -30
- package/src/precaching/getCacheKeyForURL.ts +0 -33
- package/src/precaching/matchPrecache.ts +0 -25
- package/src/precaching/precache.ts +0 -31
- package/src/precaching/precacheAndRoute.ts +0 -27
- package/src/precaching/singletonPrecacheController.ts +0 -57
- package/src/precaching/types.ts +0 -46
- package/src/precaching/utils/PrecacheCacheKeyPlugin.ts +0 -36
- package/src/precaching/utils/PrecacheInstallReportPlugin.ts +0 -49
- package/src/precaching/utils/createCacheKey.ts +0 -68
- package/src/precaching/utils/deleteOutdatedCaches.ts +0 -40
- package/src/precaching/utils/generateURLVariations.ts +0 -55
- package/src/precaching/utils/getCacheKeyForURL.ts +0 -36
- package/src/precaching/utils/printCleanupDetails.ts +0 -38
- package/src/precaching/utils/printInstallDetails.ts +0 -53
- package/src/precaching/utils/removeIgnoredSearchParams.ts +0 -29
- package/src/routing/NavigationRoute.ts +0 -119
- package/src/routing/RegExpRoute.ts +0 -74
- package/src/routing/Route.ts +0 -68
- package/src/routing/Router.ts +0 -481
- package/src/routing/parseRoute.ts +0 -78
- package/src/routing/registerRoute.ts +0 -27
- package/src/routing/setCatchHandler.ts +0 -21
- package/src/routing/setDefaultHandler.ts +0 -24
- package/src/routing/singletonRouter.ts +0 -76
- package/src/routing/unregisterRoute.ts +0 -11
- package/src/routing/utils/constants.ts +0 -24
- package/src/routing/utils/normalizeHandler.ts +0 -40
- package/src/strategies/CacheFirst.ts +0 -87
- package/src/strategies/CacheOnly.ts +0 -58
- package/src/strategies/NetworkFirst.ts +0 -228
- package/src/strategies/NetworkOnly.ts +0 -96
- package/src/strategies/StaleWhileRevalidate.ts +0 -109
- package/src/strategies/Strategy.ts +0 -202
- package/src/strategies/StrategyHandler.ts +0 -557
- package/src/strategies/plugins/cacheOkAndOpaquePlugin.ts +0 -26
- package/src/strategies/utils/messages.ts +0 -20
package/dist/chunks/Strategy.js
DELETED
|
@@ -1,410 +0,0 @@
|
|
|
1
|
-
import { assert, Deferred, logger, getFriendlyURL, SerwistError, timeout, cacheMatchIgnoreParams, executeQuotaErrorCallbacks, privateCacheNames } from '@serwist/core/internal';
|
|
2
|
-
|
|
3
|
-
function toRequest(input) {
|
|
4
|
-
return typeof input === "string" ? new Request(input) : input;
|
|
5
|
-
}
|
|
6
|
-
class StrategyHandler {
|
|
7
|
-
event;
|
|
8
|
-
request;
|
|
9
|
-
url;
|
|
10
|
-
params;
|
|
11
|
-
_cacheKeys = {};
|
|
12
|
-
_strategy;
|
|
13
|
-
_handlerDeferred;
|
|
14
|
-
_extendLifetimePromises;
|
|
15
|
-
_plugins;
|
|
16
|
-
_pluginStateMap;
|
|
17
|
-
constructor(strategy, options){
|
|
18
|
-
if (process.env.NODE_ENV !== "production") {
|
|
19
|
-
assert.isInstance(options.event, ExtendableEvent, {
|
|
20
|
-
moduleName: "@serwist/strategies",
|
|
21
|
-
className: "StrategyHandler",
|
|
22
|
-
funcName: "constructor",
|
|
23
|
-
paramName: "options.event"
|
|
24
|
-
});
|
|
25
|
-
assert.isInstance(options.request, Request, {
|
|
26
|
-
moduleName: "@serwist/strategies",
|
|
27
|
-
className: "StrategyHandler",
|
|
28
|
-
funcName: "constructor",
|
|
29
|
-
paramName: "options.request"
|
|
30
|
-
});
|
|
31
|
-
}
|
|
32
|
-
this.event = options.event;
|
|
33
|
-
this.request = options.request;
|
|
34
|
-
if (options.url) {
|
|
35
|
-
this.url = options.url;
|
|
36
|
-
this.params = options.params;
|
|
37
|
-
}
|
|
38
|
-
this._strategy = strategy;
|
|
39
|
-
this._handlerDeferred = new Deferred();
|
|
40
|
-
this._extendLifetimePromises = [];
|
|
41
|
-
this._plugins = [
|
|
42
|
-
...strategy.plugins
|
|
43
|
-
];
|
|
44
|
-
this._pluginStateMap = new Map();
|
|
45
|
-
for (const plugin of this._plugins){
|
|
46
|
-
this._pluginStateMap.set(plugin, {});
|
|
47
|
-
}
|
|
48
|
-
this.event.waitUntil(this._handlerDeferred.promise);
|
|
49
|
-
}
|
|
50
|
-
async fetch(input) {
|
|
51
|
-
const { event } = this;
|
|
52
|
-
let request = toRequest(input);
|
|
53
|
-
if (request.mode === "navigate" && event instanceof FetchEvent && event.preloadResponse) {
|
|
54
|
-
const possiblePreloadResponse = await event.preloadResponse;
|
|
55
|
-
if (possiblePreloadResponse) {
|
|
56
|
-
if (process.env.NODE_ENV !== "production") {
|
|
57
|
-
logger.log(`Using a preloaded navigation response for '${getFriendlyURL(request.url)}'`);
|
|
58
|
-
}
|
|
59
|
-
return possiblePreloadResponse;
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
const originalRequest = this.hasCallback("fetchDidFail") ? request.clone() : null;
|
|
63
|
-
try {
|
|
64
|
-
for (const cb of this.iterateCallbacks("requestWillFetch")){
|
|
65
|
-
request = await cb({
|
|
66
|
-
request: request.clone(),
|
|
67
|
-
event
|
|
68
|
-
});
|
|
69
|
-
}
|
|
70
|
-
} catch (err) {
|
|
71
|
-
if (err instanceof Error) {
|
|
72
|
-
throw new SerwistError("plugin-error-request-will-fetch", {
|
|
73
|
-
thrownErrorMessage: err.message
|
|
74
|
-
});
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
const pluginFilteredRequest = request.clone();
|
|
78
|
-
try {
|
|
79
|
-
let fetchResponse;
|
|
80
|
-
fetchResponse = await fetch(request, request.mode === "navigate" ? undefined : this._strategy.fetchOptions);
|
|
81
|
-
if (process.env.NODE_ENV !== "production") {
|
|
82
|
-
logger.debug(`Network request for '${getFriendlyURL(request.url)}' returned a response with status '${fetchResponse.status}'.`);
|
|
83
|
-
}
|
|
84
|
-
for (const callback of this.iterateCallbacks("fetchDidSucceed")){
|
|
85
|
-
fetchResponse = await callback({
|
|
86
|
-
event,
|
|
87
|
-
request: pluginFilteredRequest,
|
|
88
|
-
response: fetchResponse
|
|
89
|
-
});
|
|
90
|
-
}
|
|
91
|
-
return fetchResponse;
|
|
92
|
-
} catch (error) {
|
|
93
|
-
if (process.env.NODE_ENV !== "production") {
|
|
94
|
-
logger.log(`Network request for '${getFriendlyURL(request.url)}' threw an error.`, error);
|
|
95
|
-
}
|
|
96
|
-
if (originalRequest) {
|
|
97
|
-
await this.runCallbacks("fetchDidFail", {
|
|
98
|
-
error: error,
|
|
99
|
-
event,
|
|
100
|
-
originalRequest: originalRequest.clone(),
|
|
101
|
-
request: pluginFilteredRequest.clone()
|
|
102
|
-
});
|
|
103
|
-
}
|
|
104
|
-
throw error;
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
async fetchAndCachePut(input) {
|
|
108
|
-
const response = await this.fetch(input);
|
|
109
|
-
const responseClone = response.clone();
|
|
110
|
-
void this.waitUntil(this.cachePut(input, responseClone));
|
|
111
|
-
return response;
|
|
112
|
-
}
|
|
113
|
-
async cacheMatch(key) {
|
|
114
|
-
const request = toRequest(key);
|
|
115
|
-
let cachedResponse;
|
|
116
|
-
const { cacheName, matchOptions } = this._strategy;
|
|
117
|
-
const effectiveRequest = await this.getCacheKey(request, "read");
|
|
118
|
-
const multiMatchOptions = {
|
|
119
|
-
...matchOptions,
|
|
120
|
-
...{
|
|
121
|
-
cacheName
|
|
122
|
-
}
|
|
123
|
-
};
|
|
124
|
-
cachedResponse = await caches.match(effectiveRequest, multiMatchOptions);
|
|
125
|
-
if (process.env.NODE_ENV !== "production") {
|
|
126
|
-
if (cachedResponse) {
|
|
127
|
-
logger.debug(`Found a cached response in '${cacheName}'.`);
|
|
128
|
-
} else {
|
|
129
|
-
logger.debug(`No cached response found in '${cacheName}'.`);
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
for (const callback of this.iterateCallbacks("cachedResponseWillBeUsed")){
|
|
133
|
-
cachedResponse = await callback({
|
|
134
|
-
cacheName,
|
|
135
|
-
matchOptions,
|
|
136
|
-
cachedResponse,
|
|
137
|
-
request: effectiveRequest,
|
|
138
|
-
event: this.event
|
|
139
|
-
}) || undefined;
|
|
140
|
-
}
|
|
141
|
-
return cachedResponse;
|
|
142
|
-
}
|
|
143
|
-
async cachePut(key, response) {
|
|
144
|
-
const request = toRequest(key);
|
|
145
|
-
await timeout(0);
|
|
146
|
-
const effectiveRequest = await this.getCacheKey(request, "write");
|
|
147
|
-
if (process.env.NODE_ENV !== "production") {
|
|
148
|
-
if (effectiveRequest.method && effectiveRequest.method !== "GET") {
|
|
149
|
-
throw new SerwistError("attempt-to-cache-non-get-request", {
|
|
150
|
-
url: getFriendlyURL(effectiveRequest.url),
|
|
151
|
-
method: effectiveRequest.method
|
|
152
|
-
});
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
if (!response) {
|
|
156
|
-
if (process.env.NODE_ENV !== "production") {
|
|
157
|
-
logger.error(`Cannot cache non-existent response for '${getFriendlyURL(effectiveRequest.url)}'.`);
|
|
158
|
-
}
|
|
159
|
-
throw new SerwistError("cache-put-with-no-response", {
|
|
160
|
-
url: getFriendlyURL(effectiveRequest.url)
|
|
161
|
-
});
|
|
162
|
-
}
|
|
163
|
-
const responseToCache = await this._ensureResponseSafeToCache(response);
|
|
164
|
-
if (!responseToCache) {
|
|
165
|
-
if (process.env.NODE_ENV !== "production") {
|
|
166
|
-
logger.debug(`Response '${getFriendlyURL(effectiveRequest.url)}' will not be cached.`, responseToCache);
|
|
167
|
-
}
|
|
168
|
-
return false;
|
|
169
|
-
}
|
|
170
|
-
const { cacheName, matchOptions } = this._strategy;
|
|
171
|
-
const cache = await self.caches.open(cacheName);
|
|
172
|
-
if (process.env.NODE_ENV !== "production") {
|
|
173
|
-
const vary = response.headers.get("Vary");
|
|
174
|
-
if (vary && matchOptions?.ignoreVary !== true) {
|
|
175
|
-
logger.debug(`The response for ${getFriendlyURL(effectiveRequest.url)} has a 'Vary: ${vary}' header. Consider setting the {ignoreVary: true} option on your strategy to ensure cache matching and deletion works as expected.`);
|
|
176
|
-
}
|
|
177
|
-
}
|
|
178
|
-
const hasCacheUpdateCallback = this.hasCallback("cacheDidUpdate");
|
|
179
|
-
const oldResponse = hasCacheUpdateCallback ? await cacheMatchIgnoreParams(cache, effectiveRequest.clone(), [
|
|
180
|
-
"__WB_REVISION__"
|
|
181
|
-
], matchOptions) : null;
|
|
182
|
-
if (process.env.NODE_ENV !== "production") {
|
|
183
|
-
logger.debug(`Updating the '${cacheName}' cache with a new Response for ${getFriendlyURL(effectiveRequest.url)}.`);
|
|
184
|
-
}
|
|
185
|
-
try {
|
|
186
|
-
await cache.put(effectiveRequest, hasCacheUpdateCallback ? responseToCache.clone() : responseToCache);
|
|
187
|
-
} catch (error) {
|
|
188
|
-
if (error instanceof Error) {
|
|
189
|
-
if (error.name === "QuotaExceededError") {
|
|
190
|
-
await executeQuotaErrorCallbacks();
|
|
191
|
-
}
|
|
192
|
-
throw error;
|
|
193
|
-
}
|
|
194
|
-
}
|
|
195
|
-
for (const callback of this.iterateCallbacks("cacheDidUpdate")){
|
|
196
|
-
await callback({
|
|
197
|
-
cacheName,
|
|
198
|
-
oldResponse,
|
|
199
|
-
newResponse: responseToCache.clone(),
|
|
200
|
-
request: effectiveRequest,
|
|
201
|
-
event: this.event
|
|
202
|
-
});
|
|
203
|
-
}
|
|
204
|
-
return true;
|
|
205
|
-
}
|
|
206
|
-
async getCacheKey(request, mode) {
|
|
207
|
-
const key = `${request.url} | ${mode}`;
|
|
208
|
-
if (!this._cacheKeys[key]) {
|
|
209
|
-
let effectiveRequest = request;
|
|
210
|
-
for (const callback of this.iterateCallbacks("cacheKeyWillBeUsed")){
|
|
211
|
-
effectiveRequest = toRequest(await callback({
|
|
212
|
-
mode,
|
|
213
|
-
request: effectiveRequest,
|
|
214
|
-
event: this.event,
|
|
215
|
-
params: this.params
|
|
216
|
-
}));
|
|
217
|
-
}
|
|
218
|
-
this._cacheKeys[key] = effectiveRequest;
|
|
219
|
-
}
|
|
220
|
-
return this._cacheKeys[key];
|
|
221
|
-
}
|
|
222
|
-
hasCallback(name) {
|
|
223
|
-
for (const plugin of this._strategy.plugins){
|
|
224
|
-
if (name in plugin) {
|
|
225
|
-
return true;
|
|
226
|
-
}
|
|
227
|
-
}
|
|
228
|
-
return false;
|
|
229
|
-
}
|
|
230
|
-
async runCallbacks(name, param) {
|
|
231
|
-
for (const callback of this.iterateCallbacks(name)){
|
|
232
|
-
await callback(param);
|
|
233
|
-
}
|
|
234
|
-
}
|
|
235
|
-
*iterateCallbacks(name) {
|
|
236
|
-
for (const plugin of this._strategy.plugins){
|
|
237
|
-
if (typeof plugin[name] === "function") {
|
|
238
|
-
const state = this._pluginStateMap.get(plugin);
|
|
239
|
-
const statefulCallback = (param)=>{
|
|
240
|
-
const statefulParam = {
|
|
241
|
-
...param,
|
|
242
|
-
state
|
|
243
|
-
};
|
|
244
|
-
return plugin[name](statefulParam);
|
|
245
|
-
};
|
|
246
|
-
yield statefulCallback;
|
|
247
|
-
}
|
|
248
|
-
}
|
|
249
|
-
}
|
|
250
|
-
waitUntil(promise) {
|
|
251
|
-
this._extendLifetimePromises.push(promise);
|
|
252
|
-
return promise;
|
|
253
|
-
}
|
|
254
|
-
async doneWaiting() {
|
|
255
|
-
let promise = undefined;
|
|
256
|
-
while(promise = this._extendLifetimePromises.shift()){
|
|
257
|
-
await promise;
|
|
258
|
-
}
|
|
259
|
-
}
|
|
260
|
-
destroy() {
|
|
261
|
-
this._handlerDeferred.resolve(null);
|
|
262
|
-
}
|
|
263
|
-
async _ensureResponseSafeToCache(response) {
|
|
264
|
-
let responseToCache = response;
|
|
265
|
-
let pluginsUsed = false;
|
|
266
|
-
for (const callback of this.iterateCallbacks("cacheWillUpdate")){
|
|
267
|
-
responseToCache = await callback({
|
|
268
|
-
request: this.request,
|
|
269
|
-
response: responseToCache,
|
|
270
|
-
event: this.event
|
|
271
|
-
}) || undefined;
|
|
272
|
-
pluginsUsed = true;
|
|
273
|
-
if (!responseToCache) {
|
|
274
|
-
break;
|
|
275
|
-
}
|
|
276
|
-
}
|
|
277
|
-
if (!pluginsUsed) {
|
|
278
|
-
if (responseToCache && responseToCache.status !== 200) {
|
|
279
|
-
responseToCache = undefined;
|
|
280
|
-
}
|
|
281
|
-
if (process.env.NODE_ENV !== "production") {
|
|
282
|
-
if (responseToCache) {
|
|
283
|
-
if (responseToCache.status !== 200) {
|
|
284
|
-
if (responseToCache.status === 0) {
|
|
285
|
-
logger.warn(`The response for '${this.request.url}' is an opaque response. The caching strategy that you're using will not cache opaque responses by default.`);
|
|
286
|
-
} else {
|
|
287
|
-
logger.debug(`The response for '${this.request.url}' returned a status code of '${response.status}' and won't be cached as a result.`);
|
|
288
|
-
}
|
|
289
|
-
}
|
|
290
|
-
}
|
|
291
|
-
}
|
|
292
|
-
}
|
|
293
|
-
return responseToCache;
|
|
294
|
-
}
|
|
295
|
-
}
|
|
296
|
-
|
|
297
|
-
class Strategy {
|
|
298
|
-
cacheName;
|
|
299
|
-
plugins;
|
|
300
|
-
fetchOptions;
|
|
301
|
-
matchOptions;
|
|
302
|
-
constructor(options = {}){
|
|
303
|
-
this.cacheName = privateCacheNames.getRuntimeName(options.cacheName);
|
|
304
|
-
this.plugins = options.plugins || [];
|
|
305
|
-
this.fetchOptions = options.fetchOptions;
|
|
306
|
-
this.matchOptions = options.matchOptions;
|
|
307
|
-
}
|
|
308
|
-
handle(options) {
|
|
309
|
-
const [responseDone] = this.handleAll(options);
|
|
310
|
-
return responseDone;
|
|
311
|
-
}
|
|
312
|
-
handleAll(options) {
|
|
313
|
-
if (options instanceof FetchEvent) {
|
|
314
|
-
options = {
|
|
315
|
-
event: options,
|
|
316
|
-
request: options.request
|
|
317
|
-
};
|
|
318
|
-
}
|
|
319
|
-
const event = options.event;
|
|
320
|
-
const request = typeof options.request === "string" ? new Request(options.request) : options.request;
|
|
321
|
-
const handler = new StrategyHandler(this, options.url ? {
|
|
322
|
-
event,
|
|
323
|
-
request,
|
|
324
|
-
url: options.url,
|
|
325
|
-
params: options.params
|
|
326
|
-
} : {
|
|
327
|
-
event,
|
|
328
|
-
request
|
|
329
|
-
});
|
|
330
|
-
const responseDone = this._getResponse(handler, request, event);
|
|
331
|
-
const handlerDone = this._awaitComplete(responseDone, handler, request, event);
|
|
332
|
-
return [
|
|
333
|
-
responseDone,
|
|
334
|
-
handlerDone
|
|
335
|
-
];
|
|
336
|
-
}
|
|
337
|
-
async _getResponse(handler, request, event) {
|
|
338
|
-
await handler.runCallbacks("handlerWillStart", {
|
|
339
|
-
event,
|
|
340
|
-
request
|
|
341
|
-
});
|
|
342
|
-
let response = undefined;
|
|
343
|
-
try {
|
|
344
|
-
response = await this._handle(request, handler);
|
|
345
|
-
if (response === undefined || response.type === "error") {
|
|
346
|
-
throw new SerwistError("no-response", {
|
|
347
|
-
url: request.url
|
|
348
|
-
});
|
|
349
|
-
}
|
|
350
|
-
} catch (error) {
|
|
351
|
-
if (error instanceof Error) {
|
|
352
|
-
for (const callback of handler.iterateCallbacks("handlerDidError")){
|
|
353
|
-
response = await callback({
|
|
354
|
-
error,
|
|
355
|
-
event,
|
|
356
|
-
request
|
|
357
|
-
});
|
|
358
|
-
if (response !== undefined) {
|
|
359
|
-
break;
|
|
360
|
-
}
|
|
361
|
-
}
|
|
362
|
-
}
|
|
363
|
-
if (!response) {
|
|
364
|
-
throw error;
|
|
365
|
-
}
|
|
366
|
-
if (process.env.NODE_ENV !== "production") {
|
|
367
|
-
throw logger.log(`While responding to '${getFriendlyURL(request.url)}', an ${error instanceof Error ? error.toString() : ""} error occurred. Using a fallback response provided by a handlerDidError plugin.`);
|
|
368
|
-
}
|
|
369
|
-
}
|
|
370
|
-
for (const callback of handler.iterateCallbacks("handlerWillRespond")){
|
|
371
|
-
response = await callback({
|
|
372
|
-
event,
|
|
373
|
-
request,
|
|
374
|
-
response
|
|
375
|
-
});
|
|
376
|
-
}
|
|
377
|
-
return response;
|
|
378
|
-
}
|
|
379
|
-
async _awaitComplete(responseDone, handler, request, event) {
|
|
380
|
-
let response = undefined;
|
|
381
|
-
let error = undefined;
|
|
382
|
-
try {
|
|
383
|
-
response = await responseDone;
|
|
384
|
-
} catch (error) {}
|
|
385
|
-
try {
|
|
386
|
-
await handler.runCallbacks("handlerDidRespond", {
|
|
387
|
-
event,
|
|
388
|
-
request,
|
|
389
|
-
response
|
|
390
|
-
});
|
|
391
|
-
await handler.doneWaiting();
|
|
392
|
-
} catch (waitUntilError) {
|
|
393
|
-
if (waitUntilError instanceof Error) {
|
|
394
|
-
error = waitUntilError;
|
|
395
|
-
}
|
|
396
|
-
}
|
|
397
|
-
await handler.runCallbacks("handlerDidComplete", {
|
|
398
|
-
event,
|
|
399
|
-
request,
|
|
400
|
-
response,
|
|
401
|
-
error
|
|
402
|
-
});
|
|
403
|
-
handler.destroy();
|
|
404
|
-
if (error) {
|
|
405
|
-
throw error;
|
|
406
|
-
}
|
|
407
|
-
}
|
|
408
|
-
}
|
|
409
|
-
|
|
410
|
-
export { Strategy as S, StrategyHandler as a };
|
|
@@ -1,113 +0,0 @@
|
|
|
1
|
-
import { logger, getFriendlyURL, privateCacheNames } from '@serwist/core/internal';
|
|
2
|
-
import { g as getSingletonPrecacheController } from './singletonPrecacheController.js';
|
|
3
|
-
import { r as registerRoute } from './registerRoute.js';
|
|
4
|
-
import { R as Route } from './singletonRouter.js';
|
|
5
|
-
|
|
6
|
-
function removeIgnoredSearchParams(urlObject, ignoreURLParametersMatching = []) {
|
|
7
|
-
for (const paramName of [
|
|
8
|
-
...urlObject.searchParams.keys()
|
|
9
|
-
]){
|
|
10
|
-
if (ignoreURLParametersMatching.some((regExp)=>regExp.test(paramName))) {
|
|
11
|
-
urlObject.searchParams.delete(paramName);
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
return urlObject;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
function* generateURLVariations(url, { ignoreURLParametersMatching = [
|
|
18
|
-
/^utm_/,
|
|
19
|
-
/^fbclid$/
|
|
20
|
-
], directoryIndex = "index.html", cleanURLs = true, urlManipulation } = {}) {
|
|
21
|
-
const urlObject = new URL(url, location.href);
|
|
22
|
-
urlObject.hash = "";
|
|
23
|
-
yield urlObject.href;
|
|
24
|
-
const urlWithoutIgnoredParams = removeIgnoredSearchParams(urlObject, ignoreURLParametersMatching);
|
|
25
|
-
yield urlWithoutIgnoredParams.href;
|
|
26
|
-
if (directoryIndex && urlWithoutIgnoredParams.pathname.endsWith("/")) {
|
|
27
|
-
const directoryURL = new URL(urlWithoutIgnoredParams.href);
|
|
28
|
-
directoryURL.pathname += directoryIndex;
|
|
29
|
-
yield directoryURL.href;
|
|
30
|
-
}
|
|
31
|
-
if (cleanURLs) {
|
|
32
|
-
const cleanURL = new URL(urlWithoutIgnoredParams.href);
|
|
33
|
-
cleanURL.pathname += ".html";
|
|
34
|
-
yield cleanURL.href;
|
|
35
|
-
}
|
|
36
|
-
if (urlManipulation) {
|
|
37
|
-
const additionalURLs = urlManipulation({
|
|
38
|
-
url: urlObject
|
|
39
|
-
});
|
|
40
|
-
for (const urlToAttempt of additionalURLs){
|
|
41
|
-
yield urlToAttempt.href;
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
class PrecacheRoute extends Route {
|
|
47
|
-
constructor(precacheController, options){
|
|
48
|
-
const match = ({ request })=>{
|
|
49
|
-
const urlsToCacheKeys = precacheController.getURLsToCacheKeys();
|
|
50
|
-
for (const possibleURL of generateURLVariations(request.url, options)){
|
|
51
|
-
const cacheKey = urlsToCacheKeys.get(possibleURL);
|
|
52
|
-
if (cacheKey) {
|
|
53
|
-
const integrity = precacheController.getIntegrityForCacheKey(cacheKey);
|
|
54
|
-
return {
|
|
55
|
-
cacheKey,
|
|
56
|
-
integrity
|
|
57
|
-
};
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
if (process.env.NODE_ENV !== "production") {
|
|
61
|
-
logger.debug(`Precaching did not find a match for ${getFriendlyURL(request.url)}`);
|
|
62
|
-
}
|
|
63
|
-
return;
|
|
64
|
-
};
|
|
65
|
-
super(match, precacheController.strategy);
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
const SUBSTRING_TO_FIND = "-precache-";
|
|
70
|
-
const deleteOutdatedCaches = async (currentPrecacheName, substringToFind = SUBSTRING_TO_FIND)=>{
|
|
71
|
-
const cacheNames = await self.caches.keys();
|
|
72
|
-
const cacheNamesToDelete = cacheNames.filter((cacheName)=>{
|
|
73
|
-
return cacheName.includes(substringToFind) && cacheName.includes(self.registration.scope) && cacheName !== currentPrecacheName;
|
|
74
|
-
});
|
|
75
|
-
await Promise.all(cacheNamesToDelete.map((cacheName)=>self.caches.delete(cacheName)));
|
|
76
|
-
return cacheNamesToDelete;
|
|
77
|
-
};
|
|
78
|
-
|
|
79
|
-
const cleanupOutdatedCaches = ()=>{
|
|
80
|
-
self.addEventListener("activate", (event)=>{
|
|
81
|
-
const cacheName = privateCacheNames.getPrecacheName();
|
|
82
|
-
event.waitUntil(deleteOutdatedCaches(cacheName).then((cachesDeleted)=>{
|
|
83
|
-
if (process.env.NODE_ENV !== "production") {
|
|
84
|
-
if (cachesDeleted.length > 0) {
|
|
85
|
-
logger.log("The following out-of-date precaches were cleaned up automatically:", cachesDeleted);
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
}));
|
|
89
|
-
});
|
|
90
|
-
};
|
|
91
|
-
|
|
92
|
-
const createHandlerBoundToURL = (url)=>{
|
|
93
|
-
const precacheController = getSingletonPrecacheController();
|
|
94
|
-
return precacheController.createHandlerBoundToURL(url);
|
|
95
|
-
};
|
|
96
|
-
|
|
97
|
-
const addRoute = (options)=>{
|
|
98
|
-
const precacheController = getSingletonPrecacheController();
|
|
99
|
-
const precacheRoute = new PrecacheRoute(precacheController, options);
|
|
100
|
-
registerRoute(precacheRoute);
|
|
101
|
-
};
|
|
102
|
-
|
|
103
|
-
const precache = (entries)=>{
|
|
104
|
-
const precacheController = getSingletonPrecacheController();
|
|
105
|
-
precacheController.precache(entries);
|
|
106
|
-
};
|
|
107
|
-
|
|
108
|
-
const precacheAndRoute = (entries, options)=>{
|
|
109
|
-
precache(entries);
|
|
110
|
-
addRoute(options);
|
|
111
|
-
};
|
|
112
|
-
|
|
113
|
-
export { PrecacheRoute as P, createHandlerBoundToURL as a, addRoute as b, cleanupOutdatedCaches as c, precache as d, precacheAndRoute as p };
|