@serwist/precaching 9.0.0-preview.8 → 9.0.0
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 +5 -26
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -583
- package/package.json +7 -11
- package/src/index.ts +5 -40
- package/dist/PrecacheController.d.ts +0 -146
- package/dist/PrecacheController.d.ts.map +0 -1
- package/dist/PrecacheFallbackPlugin.d.ts +0 -54
- package/dist/PrecacheFallbackPlugin.d.ts.map +0 -1
- package/dist/PrecacheRoute.d.ts +0 -20
- package/dist/PrecacheRoute.d.ts.map +0 -1
- package/dist/PrecacheStrategy.d.ts +0 -67
- package/dist/PrecacheStrategy.d.ts.map +0 -1
- package/dist/_types.d.ts +0 -37
- package/dist/_types.d.ts.map +0 -1
- package/dist/addPlugins.d.ts +0 -9
- package/dist/addPlugins.d.ts.map +0 -1
- package/dist/addRoute.d.ts +0 -15
- package/dist/addRoute.d.ts.map +0 -1
- package/dist/cleanupOutdatedCaches.d.ts +0 -6
- package/dist/cleanupOutdatedCaches.d.ts.map +0 -1
- package/dist/createHandlerBoundToURL.d.ts +0 -17
- package/dist/createHandlerBoundToURL.d.ts.map +0 -1
- package/dist/getCacheKeyForURL.d.ts +0 -20
- package/dist/getCacheKeyForURL.d.ts.map +0 -1
- package/dist/matchPrecache.d.ts +0 -15
- package/dist/matchPrecache.d.ts.map +0 -1
- package/dist/precache.d.ts +0 -19
- package/dist/precache.d.ts.map +0 -1
- package/dist/precacheAndRoute.d.ts +0 -14
- package/dist/precacheAndRoute.d.ts.map +0 -1
- package/dist/utils/PrecacheCacheKeyPlugin.d.ts +0 -17
- package/dist/utils/PrecacheCacheKeyPlugin.d.ts.map +0 -1
- package/dist/utils/PrecacheInstallReportPlugin.d.ts +0 -15
- package/dist/utils/PrecacheInstallReportPlugin.d.ts.map +0 -1
- package/dist/utils/createCacheKey.d.ts +0 -16
- package/dist/utils/createCacheKey.d.ts.map +0 -1
- package/dist/utils/deleteOutdatedCaches.d.ts +0 -18
- package/dist/utils/deleteOutdatedCaches.d.ts.map +0 -1
- package/dist/utils/generateURLVariations.d.ts +0 -12
- package/dist/utils/generateURLVariations.d.ts.map +0 -1
- package/dist/utils/getCacheKeyForURL.d.ts +0 -14
- package/dist/utils/getCacheKeyForURL.d.ts.map +0 -1
- package/dist/utils/getOrCreatePrecacheController.d.ts +0 -7
- package/dist/utils/getOrCreatePrecacheController.d.ts.map +0 -1
- package/dist/utils/printCleanupDetails.d.ts +0 -6
- package/dist/utils/printCleanupDetails.d.ts.map +0 -1
- package/dist/utils/printInstallDetails.d.ts +0 -7
- package/dist/utils/printInstallDetails.d.ts.map +0 -1
- package/dist/utils/removeIgnoredSearchParams.d.ts +0 -12
- package/dist/utils/removeIgnoredSearchParams.d.ts.map +0 -1
- package/src/PrecacheController.ts +0 -343
- package/src/PrecacheFallbackPlugin.ts +0 -86
- package/src/PrecacheRoute.ts +0 -50
- package/src/PrecacheStrategy.ts +0 -239
- package/src/_types.ts +0 -46
- package/src/addPlugins.ts +0 -23
- package/src/addRoute.ts +0 -31
- package/src/cleanupOutdatedCaches.ts +0 -33
- package/src/createHandlerBoundToURL.ts +0 -30
- package/src/getCacheKeyForURL.ts +0 -33
- package/src/matchPrecache.ts +0 -28
- package/src/precache.ts +0 -31
- package/src/precacheAndRoute.ts +0 -27
- package/src/utils/PrecacheCacheKeyPlugin.ts +0 -36
- package/src/utils/PrecacheInstallReportPlugin.ts +0 -49
- package/src/utils/createCacheKey.ts +0 -68
- package/src/utils/deleteOutdatedCaches.ts +0 -40
- package/src/utils/generateURLVariations.ts +0 -55
- package/src/utils/getCacheKeyForURL.ts +0 -36
- package/src/utils/getOrCreatePrecacheController.ts +0 -22
- package/src/utils/printCleanupDetails.ts +0 -38
- package/src/utils/printInstallDetails.ts +0 -53
- package/src/utils/removeIgnoredSearchParams.ts +0 -29
package/dist/index.d.ts
CHANGED
|
@@ -1,27 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
import { addPlugins } from "./addPlugins.js";
|
|
7
|
-
import { addRoute } from "./addRoute.js";
|
|
8
|
-
import { cleanupOutdatedCaches } from "./cleanupOutdatedCaches.js";
|
|
9
|
-
import { createHandlerBoundToURL } from "./createHandlerBoundToURL.js";
|
|
10
|
-
import { getCacheKeyForURL } from "./getCacheKeyForURL.js";
|
|
11
|
-
import { matchPrecache } from "./matchPrecache.js";
|
|
12
|
-
import { precache } from "./precache.js";
|
|
13
|
-
import { precacheAndRoute } from "./precacheAndRoute.js";
|
|
14
|
-
/**
|
|
15
|
-
* Most consumers of this module will want to use the
|
|
16
|
-
* `@serwist/precaching.precacheAndRoute`
|
|
17
|
-
* method to add assets to the cache and respond to network requests with these
|
|
18
|
-
* cached assets.
|
|
19
|
-
*
|
|
20
|
-
* If you require more control over caching and routing, you can use the
|
|
21
|
-
* `@serwist/precaching.PrecacheController`
|
|
22
|
-
* interface.
|
|
23
|
-
*/
|
|
24
|
-
export { addPlugins, addRoute, cleanupOutdatedCaches, createHandlerBoundToURL, getCacheKeyForURL, matchPrecache, precache, precacheAndRoute, PrecacheController, PrecacheFallbackPlugin, PrecacheRoute, PrecacheStrategy, };
|
|
25
|
-
export type * from "./_types.js";
|
|
26
|
-
export type { PrecacheFallbackPluginOptions, PrecacheFallbackEntry };
|
|
1
|
+
export { PrecacheStrategy } from "serwist";
|
|
2
|
+
export { addPlugins, addRoute, createHandlerBoundToURL, getCacheKeyForURL, matchPrecache, precache, precacheAndRoute, PrecacheController, PrecacheFallbackPlugin, PrecacheRoute, } from "serwist/legacy";
|
|
3
|
+
export { cleanupOutdatedCaches } from "serwist/internal";
|
|
4
|
+
export type { CleanupResult, InstallResult, PrecacheEntry, PrecacheRouteOptions, UrlManipulation as urlManipulation } from "serwist";
|
|
5
|
+
export type { PrecacheFallbackEntry, PrecacheFallbackPluginOptions } from "serwist/legacy";
|
|
27
6
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAC;AAC3C,OAAO,EACL,UAAU,EACV,QAAQ,EACR,uBAAuB,EACvB,iBAAiB,EACjB,aAAa,EACb,QAAQ,EACR,gBAAgB,EAChB,kBAAkB,EAClB,sBAAsB,EACtB,aAAa,GACd,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAE,qBAAqB,EAAE,MAAM,kBAAkB,CAAC;AACzD,YAAY,EAAE,aAAa,EAAE,aAAa,EAAE,aAAa,EAAE,oBAAoB,EAAE,eAAe,IAAI,eAAe,EAAE,MAAM,SAAS,CAAC;AACrI,YAAY,EAAE,qBAAqB,EAAE,6BAA6B,EAAE,MAAM,gBAAgB,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -1,583 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
import { Route, registerRoute } from '@serwist/routing';
|
|
5
|
-
|
|
6
|
-
const parallel = async (limit, array, func)=>{
|
|
7
|
-
const work = array.map((item, index)=>({
|
|
8
|
-
index,
|
|
9
|
-
item
|
|
10
|
-
}));
|
|
11
|
-
const processor = async (res)=>{
|
|
12
|
-
const results = [];
|
|
13
|
-
while(true){
|
|
14
|
-
const next = work.pop();
|
|
15
|
-
if (!next) {
|
|
16
|
-
return res(results);
|
|
17
|
-
}
|
|
18
|
-
const result = await func(next.item);
|
|
19
|
-
results.push({
|
|
20
|
-
result: result,
|
|
21
|
-
index: next.index
|
|
22
|
-
});
|
|
23
|
-
}
|
|
24
|
-
};
|
|
25
|
-
const queues = Array.from({
|
|
26
|
-
length: limit
|
|
27
|
-
}, ()=>new Promise(processor));
|
|
28
|
-
const results = (await Promise.all(queues)).flat().sort((a, b)=>a.index < b.index ? -1 : 1).map((res)=>res.result);
|
|
29
|
-
return results;
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
class PrecacheStrategy extends Strategy {
|
|
33
|
-
_fallbackToNetwork;
|
|
34
|
-
static defaultPrecacheCacheabilityPlugin = {
|
|
35
|
-
async cacheWillUpdate ({ response }) {
|
|
36
|
-
if (!response || response.status >= 400) {
|
|
37
|
-
return null;
|
|
38
|
-
}
|
|
39
|
-
return response;
|
|
40
|
-
}
|
|
41
|
-
};
|
|
42
|
-
static copyRedirectedCacheableResponsesPlugin = {
|
|
43
|
-
async cacheWillUpdate ({ response }) {
|
|
44
|
-
return response.redirected ? await copyResponse(response) : response;
|
|
45
|
-
}
|
|
46
|
-
};
|
|
47
|
-
constructor(options = {}){
|
|
48
|
-
options.cacheName = privateCacheNames.getPrecacheName(options.cacheName);
|
|
49
|
-
super(options);
|
|
50
|
-
this._fallbackToNetwork = options.fallbackToNetwork === false ? false : true;
|
|
51
|
-
this.plugins.push(PrecacheStrategy.copyRedirectedCacheableResponsesPlugin);
|
|
52
|
-
}
|
|
53
|
-
async _handle(request, handler) {
|
|
54
|
-
const response = await handler.cacheMatch(request);
|
|
55
|
-
if (response) {
|
|
56
|
-
return response;
|
|
57
|
-
}
|
|
58
|
-
if (handler.event && handler.event.type === "install") {
|
|
59
|
-
return await this._handleInstall(request, handler);
|
|
60
|
-
}
|
|
61
|
-
return await this._handleFetch(request, handler);
|
|
62
|
-
}
|
|
63
|
-
async _handleFetch(request, handler) {
|
|
64
|
-
let response = undefined;
|
|
65
|
-
const params = handler.params || {};
|
|
66
|
-
if (this._fallbackToNetwork) {
|
|
67
|
-
if (process.env.NODE_ENV !== "production") {
|
|
68
|
-
logger.warn(`The precached response for ${getFriendlyURL(request.url)} in ${this.cacheName} was not found. Falling back to the network.`);
|
|
69
|
-
}
|
|
70
|
-
const integrityInManifest = params.integrity;
|
|
71
|
-
const integrityInRequest = request.integrity;
|
|
72
|
-
const noIntegrityConflict = !integrityInRequest || integrityInRequest === integrityInManifest;
|
|
73
|
-
response = await handler.fetch(new Request(request, {
|
|
74
|
-
integrity: request.mode !== "no-cors" ? integrityInRequest || integrityInManifest : undefined
|
|
75
|
-
}));
|
|
76
|
-
if (integrityInManifest && noIntegrityConflict && request.mode !== "no-cors") {
|
|
77
|
-
this._useDefaultCacheabilityPluginIfNeeded();
|
|
78
|
-
const wasCached = await handler.cachePut(request, response.clone());
|
|
79
|
-
if (process.env.NODE_ENV !== "production") {
|
|
80
|
-
if (wasCached) {
|
|
81
|
-
logger.log(`A response for ${getFriendlyURL(request.url)} was used to "repair" the precache.`);
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
} else {
|
|
86
|
-
throw new SerwistError("missing-precache-entry", {
|
|
87
|
-
cacheName: this.cacheName,
|
|
88
|
-
url: request.url
|
|
89
|
-
});
|
|
90
|
-
}
|
|
91
|
-
if (process.env.NODE_ENV !== "production") {
|
|
92
|
-
const cacheKey = params.cacheKey || await handler.getCacheKey(request, "read");
|
|
93
|
-
logger.groupCollapsed(`Precaching is responding to: ${getFriendlyURL(request.url)}`);
|
|
94
|
-
logger.log(`Serving the precached url: ${getFriendlyURL(cacheKey instanceof Request ? cacheKey.url : cacheKey)}`);
|
|
95
|
-
logger.groupCollapsed("View request details here.");
|
|
96
|
-
logger.log(request);
|
|
97
|
-
logger.groupEnd();
|
|
98
|
-
logger.groupCollapsed("View response details here.");
|
|
99
|
-
logger.log(response);
|
|
100
|
-
logger.groupEnd();
|
|
101
|
-
logger.groupEnd();
|
|
102
|
-
}
|
|
103
|
-
return response;
|
|
104
|
-
}
|
|
105
|
-
async _handleInstall(request, handler) {
|
|
106
|
-
this._useDefaultCacheabilityPluginIfNeeded();
|
|
107
|
-
const response = await handler.fetch(request);
|
|
108
|
-
const wasCached = await handler.cachePut(request, response.clone());
|
|
109
|
-
if (!wasCached) {
|
|
110
|
-
throw new SerwistError("bad-precaching-response", {
|
|
111
|
-
url: request.url,
|
|
112
|
-
status: response.status
|
|
113
|
-
});
|
|
114
|
-
}
|
|
115
|
-
return response;
|
|
116
|
-
}
|
|
117
|
-
_useDefaultCacheabilityPluginIfNeeded() {
|
|
118
|
-
let defaultPluginIndex = null;
|
|
119
|
-
let cacheWillUpdatePluginCount = 0;
|
|
120
|
-
for (const [index, plugin] of this.plugins.entries()){
|
|
121
|
-
if (plugin === PrecacheStrategy.copyRedirectedCacheableResponsesPlugin) {
|
|
122
|
-
continue;
|
|
123
|
-
}
|
|
124
|
-
if (plugin === PrecacheStrategy.defaultPrecacheCacheabilityPlugin) {
|
|
125
|
-
defaultPluginIndex = index;
|
|
126
|
-
}
|
|
127
|
-
if (plugin.cacheWillUpdate) {
|
|
128
|
-
cacheWillUpdatePluginCount++;
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
if (cacheWillUpdatePluginCount === 0) {
|
|
132
|
-
this.plugins.push(PrecacheStrategy.defaultPrecacheCacheabilityPlugin);
|
|
133
|
-
} else if (cacheWillUpdatePluginCount > 1 && defaultPluginIndex !== null) {
|
|
134
|
-
this.plugins.splice(defaultPluginIndex, 1);
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
class PrecacheCacheKeyPlugin {
|
|
140
|
-
_precacheController;
|
|
141
|
-
constructor({ precacheController }){
|
|
142
|
-
this._precacheController = precacheController;
|
|
143
|
-
}
|
|
144
|
-
cacheKeyWillBeUsed = async ({ request, params })=>{
|
|
145
|
-
const cacheKey = params?.cacheKey || this._precacheController.getCacheKeyForURL(request.url);
|
|
146
|
-
return cacheKey ? new Request(cacheKey, {
|
|
147
|
-
headers: request.headers
|
|
148
|
-
}) : request;
|
|
149
|
-
};
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
class PrecacheInstallReportPlugin {
|
|
153
|
-
updatedURLs = [];
|
|
154
|
-
notUpdatedURLs = [];
|
|
155
|
-
handlerWillStart = async ({ request, state })=>{
|
|
156
|
-
if (state) {
|
|
157
|
-
state.originalRequest = request;
|
|
158
|
-
}
|
|
159
|
-
};
|
|
160
|
-
cachedResponseWillBeUsed = async ({ event, state, cachedResponse })=>{
|
|
161
|
-
if (event.type === "install") {
|
|
162
|
-
if (state?.originalRequest && state.originalRequest instanceof Request) {
|
|
163
|
-
const url = state.originalRequest.url;
|
|
164
|
-
if (cachedResponse) {
|
|
165
|
-
this.notUpdatedURLs.push(url);
|
|
166
|
-
} else {
|
|
167
|
-
this.updatedURLs.push(url);
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
return cachedResponse;
|
|
172
|
-
};
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
const REVISION_SEARCH_PARAM = "__WB_REVISION__";
|
|
176
|
-
function createCacheKey(entry) {
|
|
177
|
-
if (!entry) {
|
|
178
|
-
throw new SerwistError("add-to-cache-list-unexpected-type", {
|
|
179
|
-
entry
|
|
180
|
-
});
|
|
181
|
-
}
|
|
182
|
-
if (typeof entry === "string") {
|
|
183
|
-
const urlObject = new URL(entry, location.href);
|
|
184
|
-
return {
|
|
185
|
-
cacheKey: urlObject.href,
|
|
186
|
-
url: urlObject.href
|
|
187
|
-
};
|
|
188
|
-
}
|
|
189
|
-
const { revision, url } = entry;
|
|
190
|
-
if (!url) {
|
|
191
|
-
throw new SerwistError("add-to-cache-list-unexpected-type", {
|
|
192
|
-
entry
|
|
193
|
-
});
|
|
194
|
-
}
|
|
195
|
-
if (!revision) {
|
|
196
|
-
const urlObject = new URL(url, location.href);
|
|
197
|
-
return {
|
|
198
|
-
cacheKey: urlObject.href,
|
|
199
|
-
url: urlObject.href
|
|
200
|
-
};
|
|
201
|
-
}
|
|
202
|
-
const cacheKeyURL = new URL(url, location.href);
|
|
203
|
-
const originalURL = new URL(url, location.href);
|
|
204
|
-
cacheKeyURL.searchParams.set(REVISION_SEARCH_PARAM, revision);
|
|
205
|
-
return {
|
|
206
|
-
cacheKey: cacheKeyURL.href,
|
|
207
|
-
url: originalURL.href
|
|
208
|
-
};
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
const logGroup = (groupTitle, deletedURLs)=>{
|
|
212
|
-
logger.groupCollapsed(groupTitle);
|
|
213
|
-
for (const url of deletedURLs){
|
|
214
|
-
logger.log(url);
|
|
215
|
-
}
|
|
216
|
-
logger.groupEnd();
|
|
217
|
-
};
|
|
218
|
-
function printCleanupDetails(deletedURLs) {
|
|
219
|
-
const deletionCount = deletedURLs.length;
|
|
220
|
-
if (deletionCount > 0) {
|
|
221
|
-
logger.groupCollapsed(`During precaching cleanup, ${deletionCount} cached request${deletionCount === 1 ? " was" : "s were"} deleted.`);
|
|
222
|
-
logGroup("Deleted Cache Requests", deletedURLs);
|
|
223
|
-
logger.groupEnd();
|
|
224
|
-
}
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
function _nestedGroup(groupTitle, urls) {
|
|
228
|
-
if (urls.length === 0) {
|
|
229
|
-
return;
|
|
230
|
-
}
|
|
231
|
-
logger.groupCollapsed(groupTitle);
|
|
232
|
-
for (const url of urls){
|
|
233
|
-
logger.log(url);
|
|
234
|
-
}
|
|
235
|
-
logger.groupEnd();
|
|
236
|
-
}
|
|
237
|
-
function printInstallDetails(urlsToPrecache, urlsAlreadyPrecached) {
|
|
238
|
-
const precachedCount = urlsToPrecache.length;
|
|
239
|
-
const alreadyPrecachedCount = urlsAlreadyPrecached.length;
|
|
240
|
-
if (precachedCount || alreadyPrecachedCount) {
|
|
241
|
-
let message = `Precaching ${precachedCount} file${precachedCount === 1 ? "" : "s"}.`;
|
|
242
|
-
if (alreadyPrecachedCount > 0) {
|
|
243
|
-
message += ` ${alreadyPrecachedCount} ` + `file${alreadyPrecachedCount === 1 ? " is" : "s are"} already cached.`;
|
|
244
|
-
}
|
|
245
|
-
logger.groupCollapsed(message);
|
|
246
|
-
_nestedGroup("View newly precached URLs.", urlsToPrecache);
|
|
247
|
-
_nestedGroup("View previously precached URLs.", urlsAlreadyPrecached);
|
|
248
|
-
logger.groupEnd();
|
|
249
|
-
}
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
class PrecacheController {
|
|
253
|
-
_installAndActiveListenersAdded;
|
|
254
|
-
_concurrentPrecaching;
|
|
255
|
-
_strategy;
|
|
256
|
-
_urlsToCacheKeys = new Map();
|
|
257
|
-
_urlsToCacheModes = new Map();
|
|
258
|
-
_cacheKeysToIntegrities = new Map();
|
|
259
|
-
constructor({ cacheName, plugins = [], fallbackToNetwork = true, concurrentPrecaching } = {}){
|
|
260
|
-
this._strategy = new PrecacheStrategy({
|
|
261
|
-
cacheName: privateCacheNames.getPrecacheName(cacheName),
|
|
262
|
-
plugins: [
|
|
263
|
-
...plugins,
|
|
264
|
-
new PrecacheCacheKeyPlugin({
|
|
265
|
-
precacheController: this
|
|
266
|
-
})
|
|
267
|
-
],
|
|
268
|
-
fallbackToNetwork
|
|
269
|
-
});
|
|
270
|
-
this._concurrentPrecaching = concurrentPrecaching;
|
|
271
|
-
this.install = this.install.bind(this);
|
|
272
|
-
this.activate = this.activate.bind(this);
|
|
273
|
-
}
|
|
274
|
-
get strategy() {
|
|
275
|
-
return this._strategy;
|
|
276
|
-
}
|
|
277
|
-
precache(entries) {
|
|
278
|
-
this.addToCacheList(entries);
|
|
279
|
-
if (!this._installAndActiveListenersAdded) {
|
|
280
|
-
self.addEventListener("install", this.install);
|
|
281
|
-
self.addEventListener("activate", this.activate);
|
|
282
|
-
this._installAndActiveListenersAdded = true;
|
|
283
|
-
}
|
|
284
|
-
}
|
|
285
|
-
addToCacheList(entries) {
|
|
286
|
-
if (process.env.NODE_ENV !== "production") {
|
|
287
|
-
assert.isArray(entries, {
|
|
288
|
-
moduleName: "@serwist/precaching",
|
|
289
|
-
className: "PrecacheController",
|
|
290
|
-
funcName: "addToCacheList",
|
|
291
|
-
paramName: "entries"
|
|
292
|
-
});
|
|
293
|
-
}
|
|
294
|
-
const urlsToWarnAbout = [];
|
|
295
|
-
for (const entry of entries){
|
|
296
|
-
if (typeof entry === "string") {
|
|
297
|
-
urlsToWarnAbout.push(entry);
|
|
298
|
-
} else if (entry && entry.revision === undefined) {
|
|
299
|
-
urlsToWarnAbout.push(entry.url);
|
|
300
|
-
}
|
|
301
|
-
const { cacheKey, url } = createCacheKey(entry);
|
|
302
|
-
const cacheMode = typeof entry !== "string" && entry.revision ? "reload" : "default";
|
|
303
|
-
if (this._urlsToCacheKeys.has(url) && this._urlsToCacheKeys.get(url) !== cacheKey) {
|
|
304
|
-
throw new SerwistError("add-to-cache-list-conflicting-entries", {
|
|
305
|
-
firstEntry: this._urlsToCacheKeys.get(url),
|
|
306
|
-
secondEntry: cacheKey
|
|
307
|
-
});
|
|
308
|
-
}
|
|
309
|
-
if (typeof entry !== "string" && entry.integrity) {
|
|
310
|
-
if (this._cacheKeysToIntegrities.has(cacheKey) && this._cacheKeysToIntegrities.get(cacheKey) !== entry.integrity) {
|
|
311
|
-
throw new SerwistError("add-to-cache-list-conflicting-integrities", {
|
|
312
|
-
url
|
|
313
|
-
});
|
|
314
|
-
}
|
|
315
|
-
this._cacheKeysToIntegrities.set(cacheKey, entry.integrity);
|
|
316
|
-
}
|
|
317
|
-
this._urlsToCacheKeys.set(url, cacheKey);
|
|
318
|
-
this._urlsToCacheModes.set(url, cacheMode);
|
|
319
|
-
if (urlsToWarnAbout.length > 0) {
|
|
320
|
-
const warningMessage = `Serwist is precaching URLs without revision info: ${urlsToWarnAbout.join(", ")}\nThis is generally NOT safe. Learn more at https://bit.ly/wb-precache`;
|
|
321
|
-
if (process.env.NODE_ENV === "production") {
|
|
322
|
-
console.warn(warningMessage);
|
|
323
|
-
} else {
|
|
324
|
-
logger.warn(warningMessage);
|
|
325
|
-
}
|
|
326
|
-
}
|
|
327
|
-
}
|
|
328
|
-
}
|
|
329
|
-
install(event) {
|
|
330
|
-
return waitUntil(event, async ()=>{
|
|
331
|
-
const installReportPlugin = new PrecacheInstallReportPlugin();
|
|
332
|
-
this.strategy.plugins.push(installReportPlugin);
|
|
333
|
-
let concurrents = this._concurrentPrecaching;
|
|
334
|
-
if (concurrents === undefined) {
|
|
335
|
-
if (!("__WB_CONCURRENT_PRECACHING" in globalThis)) {
|
|
336
|
-
self.__WB_CONCURRENT_PRECACHING = 1;
|
|
337
|
-
}
|
|
338
|
-
concurrents = self.__WB_CONCURRENT_PRECACHING;
|
|
339
|
-
}
|
|
340
|
-
await parallel(concurrents, Array.from(this._urlsToCacheKeys.entries()), async ([url, cacheKey])=>{
|
|
341
|
-
const integrity = this._cacheKeysToIntegrities.get(cacheKey);
|
|
342
|
-
const cacheMode = this._urlsToCacheModes.get(url);
|
|
343
|
-
const request = new Request(url, {
|
|
344
|
-
integrity,
|
|
345
|
-
cache: cacheMode,
|
|
346
|
-
credentials: "same-origin"
|
|
347
|
-
});
|
|
348
|
-
await Promise.all(this.strategy.handleAll({
|
|
349
|
-
params: {
|
|
350
|
-
cacheKey
|
|
351
|
-
},
|
|
352
|
-
request,
|
|
353
|
-
event
|
|
354
|
-
}));
|
|
355
|
-
});
|
|
356
|
-
const { updatedURLs, notUpdatedURLs } = installReportPlugin;
|
|
357
|
-
if (process.env.NODE_ENV !== "production") {
|
|
358
|
-
printInstallDetails(updatedURLs, notUpdatedURLs);
|
|
359
|
-
}
|
|
360
|
-
return {
|
|
361
|
-
updatedURLs,
|
|
362
|
-
notUpdatedURLs
|
|
363
|
-
};
|
|
364
|
-
});
|
|
365
|
-
}
|
|
366
|
-
activate(event) {
|
|
367
|
-
return waitUntil(event, async ()=>{
|
|
368
|
-
const cache = await self.caches.open(this.strategy.cacheName);
|
|
369
|
-
const currentlyCachedRequests = await cache.keys();
|
|
370
|
-
const expectedCacheKeys = new Set(this._urlsToCacheKeys.values());
|
|
371
|
-
const deletedCacheRequests = [];
|
|
372
|
-
for (const request of currentlyCachedRequests){
|
|
373
|
-
if (!expectedCacheKeys.has(request.url)) {
|
|
374
|
-
await cache.delete(request);
|
|
375
|
-
deletedCacheRequests.push(request.url);
|
|
376
|
-
}
|
|
377
|
-
}
|
|
378
|
-
if (process.env.NODE_ENV !== "production") {
|
|
379
|
-
printCleanupDetails(deletedCacheRequests);
|
|
380
|
-
}
|
|
381
|
-
return {
|
|
382
|
-
deletedCacheRequests
|
|
383
|
-
};
|
|
384
|
-
});
|
|
385
|
-
}
|
|
386
|
-
getURLsToCacheKeys() {
|
|
387
|
-
return this._urlsToCacheKeys;
|
|
388
|
-
}
|
|
389
|
-
getCachedURLs() {
|
|
390
|
-
return [
|
|
391
|
-
...this._urlsToCacheKeys.keys()
|
|
392
|
-
];
|
|
393
|
-
}
|
|
394
|
-
getCacheKeyForURL(url) {
|
|
395
|
-
const urlObject = new URL(url, location.href);
|
|
396
|
-
return this._urlsToCacheKeys.get(urlObject.href);
|
|
397
|
-
}
|
|
398
|
-
getIntegrityForCacheKey(cacheKey) {
|
|
399
|
-
return this._cacheKeysToIntegrities.get(cacheKey);
|
|
400
|
-
}
|
|
401
|
-
async matchPrecache(request) {
|
|
402
|
-
const url = request instanceof Request ? request.url : request;
|
|
403
|
-
const cacheKey = this.getCacheKeyForURL(url);
|
|
404
|
-
if (cacheKey) {
|
|
405
|
-
const cache = await self.caches.open(this.strategy.cacheName);
|
|
406
|
-
return cache.match(cacheKey);
|
|
407
|
-
}
|
|
408
|
-
return undefined;
|
|
409
|
-
}
|
|
410
|
-
createHandlerBoundToURL(url) {
|
|
411
|
-
const cacheKey = this.getCacheKeyForURL(url);
|
|
412
|
-
if (!cacheKey) {
|
|
413
|
-
throw new SerwistError("non-precached-url", {
|
|
414
|
-
url
|
|
415
|
-
});
|
|
416
|
-
}
|
|
417
|
-
return (options)=>{
|
|
418
|
-
options.request = new Request(url);
|
|
419
|
-
options.params = {
|
|
420
|
-
cacheKey,
|
|
421
|
-
...options.params
|
|
422
|
-
};
|
|
423
|
-
return this.strategy.handle(options);
|
|
424
|
-
};
|
|
425
|
-
}
|
|
426
|
-
}
|
|
427
|
-
|
|
428
|
-
let precacheController;
|
|
429
|
-
const getOrCreatePrecacheController = ()=>{
|
|
430
|
-
if (!precacheController) {
|
|
431
|
-
precacheController = new PrecacheController();
|
|
432
|
-
}
|
|
433
|
-
return precacheController;
|
|
434
|
-
};
|
|
435
|
-
|
|
436
|
-
class PrecacheFallbackPlugin {
|
|
437
|
-
_fallbackUrls;
|
|
438
|
-
_precacheController;
|
|
439
|
-
constructor({ fallbackUrls, precacheController }){
|
|
440
|
-
this._fallbackUrls = fallbackUrls;
|
|
441
|
-
this._precacheController = precacheController || getOrCreatePrecacheController();
|
|
442
|
-
}
|
|
443
|
-
async handlerDidError(param) {
|
|
444
|
-
for (const fallback of this._fallbackUrls){
|
|
445
|
-
if (typeof fallback === "string") {
|
|
446
|
-
const fallbackResponse = await this._precacheController.matchPrecache(fallback);
|
|
447
|
-
if (fallbackResponse !== undefined) {
|
|
448
|
-
return fallbackResponse;
|
|
449
|
-
}
|
|
450
|
-
} else if (fallback.matcher(param)) {
|
|
451
|
-
const fallbackResponse = await this._precacheController.matchPrecache(fallback.url);
|
|
452
|
-
if (fallbackResponse !== undefined) {
|
|
453
|
-
return fallbackResponse;
|
|
454
|
-
}
|
|
455
|
-
}
|
|
456
|
-
}
|
|
457
|
-
return undefined;
|
|
458
|
-
}
|
|
459
|
-
}
|
|
460
|
-
|
|
461
|
-
function removeIgnoredSearchParams(urlObject, ignoreURLParametersMatching = []) {
|
|
462
|
-
for (const paramName of [
|
|
463
|
-
...urlObject.searchParams.keys()
|
|
464
|
-
]){
|
|
465
|
-
if (ignoreURLParametersMatching.some((regExp)=>regExp.test(paramName))) {
|
|
466
|
-
urlObject.searchParams.delete(paramName);
|
|
467
|
-
}
|
|
468
|
-
}
|
|
469
|
-
return urlObject;
|
|
470
|
-
}
|
|
471
|
-
|
|
472
|
-
function* generateURLVariations(url, { ignoreURLParametersMatching = [
|
|
473
|
-
/^utm_/,
|
|
474
|
-
/^fbclid$/
|
|
475
|
-
], directoryIndex = "index.html", cleanURLs = true, urlManipulation } = {}) {
|
|
476
|
-
const urlObject = new URL(url, location.href);
|
|
477
|
-
urlObject.hash = "";
|
|
478
|
-
yield urlObject.href;
|
|
479
|
-
const urlWithoutIgnoredParams = removeIgnoredSearchParams(urlObject, ignoreURLParametersMatching);
|
|
480
|
-
yield urlWithoutIgnoredParams.href;
|
|
481
|
-
if (directoryIndex && urlWithoutIgnoredParams.pathname.endsWith("/")) {
|
|
482
|
-
const directoryURL = new URL(urlWithoutIgnoredParams.href);
|
|
483
|
-
directoryURL.pathname += directoryIndex;
|
|
484
|
-
yield directoryURL.href;
|
|
485
|
-
}
|
|
486
|
-
if (cleanURLs) {
|
|
487
|
-
const cleanURL = new URL(urlWithoutIgnoredParams.href);
|
|
488
|
-
cleanURL.pathname += ".html";
|
|
489
|
-
yield cleanURL.href;
|
|
490
|
-
}
|
|
491
|
-
if (urlManipulation) {
|
|
492
|
-
const additionalURLs = urlManipulation({
|
|
493
|
-
url: urlObject
|
|
494
|
-
});
|
|
495
|
-
for (const urlToAttempt of additionalURLs){
|
|
496
|
-
yield urlToAttempt.href;
|
|
497
|
-
}
|
|
498
|
-
}
|
|
499
|
-
}
|
|
500
|
-
|
|
501
|
-
class PrecacheRoute extends Route {
|
|
502
|
-
constructor(precacheController, options){
|
|
503
|
-
const match = ({ request })=>{
|
|
504
|
-
const urlsToCacheKeys = precacheController.getURLsToCacheKeys();
|
|
505
|
-
for (const possibleURL of generateURLVariations(request.url, options)){
|
|
506
|
-
const cacheKey = urlsToCacheKeys.get(possibleURL);
|
|
507
|
-
if (cacheKey) {
|
|
508
|
-
const integrity = precacheController.getIntegrityForCacheKey(cacheKey);
|
|
509
|
-
return {
|
|
510
|
-
cacheKey,
|
|
511
|
-
integrity
|
|
512
|
-
};
|
|
513
|
-
}
|
|
514
|
-
}
|
|
515
|
-
if (process.env.NODE_ENV !== "production") {
|
|
516
|
-
logger.debug(`Precaching did not find a match for ${getFriendlyURL(request.url)}`);
|
|
517
|
-
}
|
|
518
|
-
return;
|
|
519
|
-
};
|
|
520
|
-
super(match, precacheController.strategy);
|
|
521
|
-
}
|
|
522
|
-
}
|
|
523
|
-
|
|
524
|
-
function addPlugins(plugins) {
|
|
525
|
-
const precacheController = getOrCreatePrecacheController();
|
|
526
|
-
precacheController.strategy.plugins.push(...plugins);
|
|
527
|
-
}
|
|
528
|
-
|
|
529
|
-
const addRoute = (options)=>{
|
|
530
|
-
const precacheController = getOrCreatePrecacheController();
|
|
531
|
-
const precacheRoute = new PrecacheRoute(precacheController, options);
|
|
532
|
-
registerRoute(precacheRoute);
|
|
533
|
-
};
|
|
534
|
-
|
|
535
|
-
const SUBSTRING_TO_FIND = "-precache-";
|
|
536
|
-
const deleteOutdatedCaches = async (currentPrecacheName, substringToFind = SUBSTRING_TO_FIND)=>{
|
|
537
|
-
const cacheNames = await self.caches.keys();
|
|
538
|
-
const cacheNamesToDelete = cacheNames.filter((cacheName)=>{
|
|
539
|
-
return cacheName.includes(substringToFind) && cacheName.includes(self.registration.scope) && cacheName !== currentPrecacheName;
|
|
540
|
-
});
|
|
541
|
-
await Promise.all(cacheNamesToDelete.map((cacheName)=>self.caches.delete(cacheName)));
|
|
542
|
-
return cacheNamesToDelete;
|
|
543
|
-
};
|
|
544
|
-
|
|
545
|
-
const cleanupOutdatedCaches = ()=>{
|
|
546
|
-
self.addEventListener("activate", (event)=>{
|
|
547
|
-
const cacheName = privateCacheNames.getPrecacheName();
|
|
548
|
-
event.waitUntil(deleteOutdatedCaches(cacheName).then((cachesDeleted)=>{
|
|
549
|
-
if (process.env.NODE_ENV !== "production") {
|
|
550
|
-
if (cachesDeleted.length > 0) {
|
|
551
|
-
logger.log("The following out-of-date precaches were cleaned up automatically:", cachesDeleted);
|
|
552
|
-
}
|
|
553
|
-
}
|
|
554
|
-
}));
|
|
555
|
-
});
|
|
556
|
-
};
|
|
557
|
-
|
|
558
|
-
const createHandlerBoundToURL = (url)=>{
|
|
559
|
-
const precacheController = getOrCreatePrecacheController();
|
|
560
|
-
return precacheController.createHandlerBoundToURL(url);
|
|
561
|
-
};
|
|
562
|
-
|
|
563
|
-
function getCacheKeyForURL(url) {
|
|
564
|
-
const precacheController = getOrCreatePrecacheController();
|
|
565
|
-
return precacheController.getCacheKeyForURL(url);
|
|
566
|
-
}
|
|
567
|
-
|
|
568
|
-
function matchPrecache(request) {
|
|
569
|
-
const precacheController = getOrCreatePrecacheController();
|
|
570
|
-
return precacheController.matchPrecache(request);
|
|
571
|
-
}
|
|
572
|
-
|
|
573
|
-
const precache = (entries)=>{
|
|
574
|
-
const precacheController = getOrCreatePrecacheController();
|
|
575
|
-
precacheController.precache(entries);
|
|
576
|
-
};
|
|
577
|
-
|
|
578
|
-
const precacheAndRoute = (entries, options)=>{
|
|
579
|
-
precache(entries);
|
|
580
|
-
addRoute(options);
|
|
581
|
-
};
|
|
582
|
-
|
|
583
|
-
export { PrecacheController, PrecacheFallbackPlugin, PrecacheRoute, PrecacheStrategy, addPlugins, addRoute, cleanupOutdatedCaches, createHandlerBoundToURL, getCacheKeyForURL, matchPrecache, precache, precacheAndRoute };
|
|
1
|
+
export { PrecacheStrategy } from 'serwist';
|
|
2
|
+
export { PrecacheController, PrecacheFallbackPlugin, PrecacheRoute, addPlugins, addRoute, createHandlerBoundToURL, getCacheKeyForURL, matchPrecache, precache, precacheAndRoute } from 'serwist/legacy';
|
|
3
|
+
export { cleanupOutdatedCaches } from 'serwist/internal';
|
package/package.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@serwist/precaching",
|
|
3
|
-
"version": "9.0.0
|
|
3
|
+
"version": "9.0.0",
|
|
4
4
|
"type": "module",
|
|
5
|
-
"description": "
|
|
5
|
+
"description": "A module that efficiently precaches assets.",
|
|
6
6
|
"files": [
|
|
7
7
|
"src",
|
|
8
8
|
"dist"
|
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
],
|
|
16
16
|
"author": "Google's Web DevRel Team, Serwist's Team",
|
|
17
17
|
"license": "MIT",
|
|
18
|
-
"repository": "serwist/serwist",
|
|
18
|
+
"repository": "https://github.com/serwist/serwist",
|
|
19
19
|
"bugs": "https://github.com/serwist/serwist/issues",
|
|
20
20
|
"homepage": "https://serwist.pages.dev",
|
|
21
21
|
"main": "./dist/index.js",
|
|
@@ -28,15 +28,12 @@
|
|
|
28
28
|
"./package.json": "./package.json"
|
|
29
29
|
},
|
|
30
30
|
"dependencies": {
|
|
31
|
-
"
|
|
32
|
-
"@serwist/routing": "9.0.0-preview.8",
|
|
33
|
-
"@serwist/strategies": "9.0.0-preview.8"
|
|
31
|
+
"serwist": "9.0.0"
|
|
34
32
|
},
|
|
35
33
|
"devDependencies": {
|
|
36
|
-
"rollup": "4.
|
|
37
|
-
"typescript": "5.
|
|
38
|
-
"@serwist/
|
|
39
|
-
"@serwist/utils": "9.0.0-preview.8"
|
|
34
|
+
"rollup": "4.14.3",
|
|
35
|
+
"typescript": "5.5.0-dev.20240415",
|
|
36
|
+
"@serwist/configs": "9.0.0"
|
|
40
37
|
},
|
|
41
38
|
"peerDependencies": {
|
|
42
39
|
"typescript": ">=5.0.0"
|
|
@@ -48,7 +45,6 @@
|
|
|
48
45
|
},
|
|
49
46
|
"scripts": {
|
|
50
47
|
"build": "rimraf dist && cross-env NODE_ENV=production rollup --config rollup.config.js",
|
|
51
|
-
"dev": "rollup --config rollup.config.js --watch",
|
|
52
48
|
"lint": "biome lint ./src",
|
|
53
49
|
"typecheck": "tsc"
|
|
54
50
|
}
|