@serwist/precaching 9.0.0-preview.15 → 9.0.0-preview.17

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.
@@ -0,0 +1,436 @@
1
+ import { privateCacheNames, logger, getFriendlyURL, SerwistError, assert, waitUntil } from '@serwist/core/internal';
2
+ import { copyResponse } from '@serwist/core';
3
+ import { Strategy } from '@serwist/strategies';
4
+
5
+ const parallel = async (limit, array, func)=>{
6
+ const work = array.map((item, index)=>({
7
+ index,
8
+ item
9
+ }));
10
+ const processor = async (res)=>{
11
+ const results = [];
12
+ while(true){
13
+ const next = work.pop();
14
+ if (!next) {
15
+ return res(results);
16
+ }
17
+ const result = await func(next.item);
18
+ results.push({
19
+ result: result,
20
+ index: next.index
21
+ });
22
+ }
23
+ };
24
+ const queues = Array.from({
25
+ length: limit
26
+ }, ()=>new Promise(processor));
27
+ const results = (await Promise.all(queues)).flat().sort((a, b)=>a.index < b.index ? -1 : 1).map((res)=>res.result);
28
+ return results;
29
+ };
30
+
31
+ class PrecacheStrategy extends Strategy {
32
+ _fallbackToNetwork;
33
+ static defaultPrecacheCacheabilityPlugin = {
34
+ async cacheWillUpdate ({ response }) {
35
+ if (!response || response.status >= 400) {
36
+ return null;
37
+ }
38
+ return response;
39
+ }
40
+ };
41
+ static copyRedirectedCacheableResponsesPlugin = {
42
+ async cacheWillUpdate ({ response }) {
43
+ return response.redirected ? await copyResponse(response) : response;
44
+ }
45
+ };
46
+ constructor(options = {}){
47
+ options.cacheName = privateCacheNames.getPrecacheName(options.cacheName);
48
+ super(options);
49
+ this._fallbackToNetwork = options.fallbackToNetwork === false ? false : true;
50
+ this.plugins.push(PrecacheStrategy.copyRedirectedCacheableResponsesPlugin);
51
+ }
52
+ async _handle(request, handler) {
53
+ const response = await handler.cacheMatch(request);
54
+ if (response) {
55
+ return response;
56
+ }
57
+ if (handler.event && handler.event.type === "install") {
58
+ return await this._handleInstall(request, handler);
59
+ }
60
+ return await this._handleFetch(request, handler);
61
+ }
62
+ async _handleFetch(request, handler) {
63
+ let response = undefined;
64
+ const params = handler.params || {};
65
+ if (this._fallbackToNetwork) {
66
+ if (process.env.NODE_ENV !== "production") {
67
+ logger.warn(`The precached response for ${getFriendlyURL(request.url)} in ${this.cacheName} was not found. Falling back to the network.`);
68
+ }
69
+ const integrityInManifest = params.integrity;
70
+ const integrityInRequest = request.integrity;
71
+ const noIntegrityConflict = !integrityInRequest || integrityInRequest === integrityInManifest;
72
+ response = await handler.fetch(new Request(request, {
73
+ integrity: request.mode !== "no-cors" ? integrityInRequest || integrityInManifest : undefined
74
+ }));
75
+ if (integrityInManifest && noIntegrityConflict && request.mode !== "no-cors") {
76
+ this._useDefaultCacheabilityPluginIfNeeded();
77
+ const wasCached = await handler.cachePut(request, response.clone());
78
+ if (process.env.NODE_ENV !== "production") {
79
+ if (wasCached) {
80
+ logger.log(`A response for ${getFriendlyURL(request.url)} was used to "repair" the precache.`);
81
+ }
82
+ }
83
+ }
84
+ } else {
85
+ throw new SerwistError("missing-precache-entry", {
86
+ cacheName: this.cacheName,
87
+ url: request.url
88
+ });
89
+ }
90
+ if (process.env.NODE_ENV !== "production") {
91
+ const cacheKey = params.cacheKey || await handler.getCacheKey(request, "read");
92
+ logger.groupCollapsed(`Precaching is responding to: ${getFriendlyURL(request.url)}`);
93
+ logger.log(`Serving the precached url: ${getFriendlyURL(cacheKey instanceof Request ? cacheKey.url : cacheKey)}`);
94
+ logger.groupCollapsed("View request details here.");
95
+ logger.log(request);
96
+ logger.groupEnd();
97
+ logger.groupCollapsed("View response details here.");
98
+ logger.log(response);
99
+ logger.groupEnd();
100
+ logger.groupEnd();
101
+ }
102
+ return response;
103
+ }
104
+ async _handleInstall(request, handler) {
105
+ this._useDefaultCacheabilityPluginIfNeeded();
106
+ const response = await handler.fetch(request);
107
+ const wasCached = await handler.cachePut(request, response.clone());
108
+ if (!wasCached) {
109
+ throw new SerwistError("bad-precaching-response", {
110
+ url: request.url,
111
+ status: response.status
112
+ });
113
+ }
114
+ return response;
115
+ }
116
+ _useDefaultCacheabilityPluginIfNeeded() {
117
+ let defaultPluginIndex = null;
118
+ let cacheWillUpdatePluginCount = 0;
119
+ for (const [index, plugin] of this.plugins.entries()){
120
+ if (plugin === PrecacheStrategy.copyRedirectedCacheableResponsesPlugin) {
121
+ continue;
122
+ }
123
+ if (plugin === PrecacheStrategy.defaultPrecacheCacheabilityPlugin) {
124
+ defaultPluginIndex = index;
125
+ }
126
+ if (plugin.cacheWillUpdate) {
127
+ cacheWillUpdatePluginCount++;
128
+ }
129
+ }
130
+ if (cacheWillUpdatePluginCount === 0) {
131
+ this.plugins.push(PrecacheStrategy.defaultPrecacheCacheabilityPlugin);
132
+ } else if (cacheWillUpdatePluginCount > 1 && defaultPluginIndex !== null) {
133
+ this.plugins.splice(defaultPluginIndex, 1);
134
+ }
135
+ }
136
+ }
137
+
138
+ class PrecacheCacheKeyPlugin {
139
+ _precacheController;
140
+ constructor({ precacheController }){
141
+ this._precacheController = precacheController;
142
+ }
143
+ cacheKeyWillBeUsed = async ({ request, params })=>{
144
+ const cacheKey = params?.cacheKey || this._precacheController.getCacheKeyForURL(request.url);
145
+ return cacheKey ? new Request(cacheKey, {
146
+ headers: request.headers
147
+ }) : request;
148
+ };
149
+ }
150
+
151
+ class PrecacheInstallReportPlugin {
152
+ updatedURLs = [];
153
+ notUpdatedURLs = [];
154
+ handlerWillStart = async ({ request, state })=>{
155
+ if (state) {
156
+ state.originalRequest = request;
157
+ }
158
+ };
159
+ cachedResponseWillBeUsed = async ({ event, state, cachedResponse })=>{
160
+ if (event.type === "install") {
161
+ if (state?.originalRequest && state.originalRequest instanceof Request) {
162
+ const url = state.originalRequest.url;
163
+ if (cachedResponse) {
164
+ this.notUpdatedURLs.push(url);
165
+ } else {
166
+ this.updatedURLs.push(url);
167
+ }
168
+ }
169
+ }
170
+ return cachedResponse;
171
+ };
172
+ }
173
+
174
+ const REVISION_SEARCH_PARAM = "__WB_REVISION__";
175
+ function createCacheKey(entry) {
176
+ if (!entry) {
177
+ throw new SerwistError("add-to-cache-list-unexpected-type", {
178
+ entry
179
+ });
180
+ }
181
+ if (typeof entry === "string") {
182
+ const urlObject = new URL(entry, location.href);
183
+ return {
184
+ cacheKey: urlObject.href,
185
+ url: urlObject.href
186
+ };
187
+ }
188
+ const { revision, url } = entry;
189
+ if (!url) {
190
+ throw new SerwistError("add-to-cache-list-unexpected-type", {
191
+ entry
192
+ });
193
+ }
194
+ if (!revision) {
195
+ const urlObject = new URL(url, location.href);
196
+ return {
197
+ cacheKey: urlObject.href,
198
+ url: urlObject.href
199
+ };
200
+ }
201
+ const cacheKeyURL = new URL(url, location.href);
202
+ const originalURL = new URL(url, location.href);
203
+ cacheKeyURL.searchParams.set(REVISION_SEARCH_PARAM, revision);
204
+ return {
205
+ cacheKey: cacheKeyURL.href,
206
+ url: originalURL.href
207
+ };
208
+ }
209
+
210
+ const logGroup = (groupTitle, deletedURLs)=>{
211
+ logger.groupCollapsed(groupTitle);
212
+ for (const url of deletedURLs){
213
+ logger.log(url);
214
+ }
215
+ logger.groupEnd();
216
+ };
217
+ function printCleanupDetails(deletedURLs) {
218
+ const deletionCount = deletedURLs.length;
219
+ if (deletionCount > 0) {
220
+ logger.groupCollapsed(`During precaching cleanup, ${deletionCount} cached request${deletionCount === 1 ? " was" : "s were"} deleted.`);
221
+ logGroup("Deleted Cache Requests", deletedURLs);
222
+ logger.groupEnd();
223
+ }
224
+ }
225
+
226
+ function _nestedGroup(groupTitle, urls) {
227
+ if (urls.length === 0) {
228
+ return;
229
+ }
230
+ logger.groupCollapsed(groupTitle);
231
+ for (const url of urls){
232
+ logger.log(url);
233
+ }
234
+ logger.groupEnd();
235
+ }
236
+ function printInstallDetails(urlsToPrecache, urlsAlreadyPrecached) {
237
+ const precachedCount = urlsToPrecache.length;
238
+ const alreadyPrecachedCount = urlsAlreadyPrecached.length;
239
+ if (precachedCount || alreadyPrecachedCount) {
240
+ let message = `Precaching ${precachedCount} file${precachedCount === 1 ? "" : "s"}.`;
241
+ if (alreadyPrecachedCount > 0) {
242
+ message += ` ${alreadyPrecachedCount} ` + `file${alreadyPrecachedCount === 1 ? " is" : "s are"} already cached.`;
243
+ }
244
+ logger.groupCollapsed(message);
245
+ _nestedGroup("View newly precached URLs.", urlsToPrecache);
246
+ _nestedGroup("View previously precached URLs.", urlsAlreadyPrecached);
247
+ logger.groupEnd();
248
+ }
249
+ }
250
+
251
+ class PrecacheController {
252
+ _installAndActiveListenersAdded;
253
+ _concurrentPrecaching;
254
+ _strategy;
255
+ _urlsToCacheKeys = new Map();
256
+ _urlsToCacheModes = new Map();
257
+ _cacheKeysToIntegrities = new Map();
258
+ constructor({ cacheName, plugins = [], fallbackToNetwork = true, concurrentPrecaching } = {}){
259
+ this._strategy = new PrecacheStrategy({
260
+ cacheName: privateCacheNames.getPrecacheName(cacheName),
261
+ plugins: [
262
+ ...plugins,
263
+ new PrecacheCacheKeyPlugin({
264
+ precacheController: this
265
+ })
266
+ ],
267
+ fallbackToNetwork
268
+ });
269
+ this._concurrentPrecaching = concurrentPrecaching;
270
+ this.install = this.install.bind(this);
271
+ this.activate = this.activate.bind(this);
272
+ }
273
+ get strategy() {
274
+ return this._strategy;
275
+ }
276
+ precache(entries) {
277
+ this.addToCacheList(entries);
278
+ if (!this._installAndActiveListenersAdded) {
279
+ self.addEventListener("install", this.install);
280
+ self.addEventListener("activate", this.activate);
281
+ this._installAndActiveListenersAdded = true;
282
+ }
283
+ }
284
+ addToCacheList(entries) {
285
+ if (process.env.NODE_ENV !== "production") {
286
+ assert.isArray(entries, {
287
+ moduleName: "@serwist/precaching",
288
+ className: "PrecacheController",
289
+ funcName: "addToCacheList",
290
+ paramName: "entries"
291
+ });
292
+ }
293
+ const urlsToWarnAbout = [];
294
+ for (const entry of entries){
295
+ if (typeof entry === "string") {
296
+ urlsToWarnAbout.push(entry);
297
+ } else if (entry && !entry.integrity && entry.revision === undefined) {
298
+ urlsToWarnAbout.push(entry.url);
299
+ }
300
+ const { cacheKey, url } = createCacheKey(entry);
301
+ const cacheMode = typeof entry !== "string" && entry.revision ? "reload" : "default";
302
+ if (this._urlsToCacheKeys.has(url) && this._urlsToCacheKeys.get(url) !== cacheKey) {
303
+ throw new SerwistError("add-to-cache-list-conflicting-entries", {
304
+ firstEntry: this._urlsToCacheKeys.get(url),
305
+ secondEntry: cacheKey
306
+ });
307
+ }
308
+ if (typeof entry !== "string" && entry.integrity) {
309
+ if (this._cacheKeysToIntegrities.has(cacheKey) && this._cacheKeysToIntegrities.get(cacheKey) !== entry.integrity) {
310
+ throw new SerwistError("add-to-cache-list-conflicting-integrities", {
311
+ url
312
+ });
313
+ }
314
+ this._cacheKeysToIntegrities.set(cacheKey, entry.integrity);
315
+ }
316
+ this._urlsToCacheKeys.set(url, cacheKey);
317
+ this._urlsToCacheModes.set(url, cacheMode);
318
+ if (urlsToWarnAbout.length > 0) {
319
+ const warningMessage = `Serwist is precaching URLs without revision info: ${urlsToWarnAbout.join(", ")}\nThis is generally NOT safe. Learn more at https://bit.ly/wb-precache`;
320
+ if (process.env.NODE_ENV === "production") {
321
+ console.warn(warningMessage);
322
+ } else {
323
+ logger.warn(warningMessage);
324
+ }
325
+ }
326
+ }
327
+ }
328
+ install(event) {
329
+ return waitUntil(event, async ()=>{
330
+ const installReportPlugin = new PrecacheInstallReportPlugin();
331
+ this.strategy.plugins.push(installReportPlugin);
332
+ let concurrents = this._concurrentPrecaching;
333
+ if (concurrents === undefined) {
334
+ if (!("__WB_CONCURRENT_PRECACHING" in globalThis)) {
335
+ self.__WB_CONCURRENT_PRECACHING = 1;
336
+ }
337
+ concurrents = self.__WB_CONCURRENT_PRECACHING;
338
+ }
339
+ await parallel(concurrents, Array.from(this._urlsToCacheKeys.entries()), async ([url, cacheKey])=>{
340
+ const integrity = this._cacheKeysToIntegrities.get(cacheKey);
341
+ const cacheMode = this._urlsToCacheModes.get(url);
342
+ const request = new Request(url, {
343
+ integrity,
344
+ cache: cacheMode,
345
+ credentials: "same-origin"
346
+ });
347
+ await Promise.all(this.strategy.handleAll({
348
+ event,
349
+ request,
350
+ url: new URL(request.url),
351
+ params: {
352
+ cacheKey
353
+ }
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
+ export { PrecacheController as P, PrecacheStrategy as a, getOrCreatePrecacheController as g };
@@ -0,0 +1,3 @@
1
+ import { getOrCreatePrecacheController } from "./utils/getOrCreatePrecacheController.js";
2
+ export { getOrCreatePrecacheController };
3
+ //# sourceMappingURL=index.internal.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.internal.d.ts","sourceRoot":"","sources":["../src/index.internal.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,6BAA6B,EAAE,MAAM,0CAA0C,CAAC;AAEzF,OAAO,EAAE,6BAA6B,EAAE,CAAC"}
@@ -0,0 +1,4 @@
1
+ export { g as getOrCreatePrecacheController } from './chunks/getOrCreatePrecacheController.js';
2
+ import '@serwist/core/internal';
3
+ import '@serwist/core';
4
+ import '@serwist/strategies';
package/dist/index.js CHANGED
@@ -1,438 +1,9 @@
1
- import { privateCacheNames, logger, getFriendlyURL, SerwistError, assert, waitUntil } from '@serwist/core/internal';
2
- import { copyResponse } from '@serwist/core';
3
- import { Strategy } from '@serwist/strategies';
1
+ import { g as getOrCreatePrecacheController } from './chunks/getOrCreatePrecacheController.js';
2
+ export { P as PrecacheController, a as PrecacheStrategy } from './chunks/getOrCreatePrecacheController.js';
3
+ import { logger, getFriendlyURL, privateCacheNames } from '@serwist/core/internal';
4
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
- event,
350
- request,
351
- url: new URL(request.url),
352
- params: {
353
- cacheKey
354
- }
355
- }));
356
- });
357
- const { updatedURLs, notUpdatedURLs } = installReportPlugin;
358
- if (process.env.NODE_ENV !== "production") {
359
- printInstallDetails(updatedURLs, notUpdatedURLs);
360
- }
361
- return {
362
- updatedURLs,
363
- notUpdatedURLs
364
- };
365
- });
366
- }
367
- activate(event) {
368
- return waitUntil(event, async ()=>{
369
- const cache = await self.caches.open(this.strategy.cacheName);
370
- const currentlyCachedRequests = await cache.keys();
371
- const expectedCacheKeys = new Set(this._urlsToCacheKeys.values());
372
- const deletedCacheRequests = [];
373
- for (const request of currentlyCachedRequests){
374
- if (!expectedCacheKeys.has(request.url)) {
375
- await cache.delete(request);
376
- deletedCacheRequests.push(request.url);
377
- }
378
- }
379
- if (process.env.NODE_ENV !== "production") {
380
- printCleanupDetails(deletedCacheRequests);
381
- }
382
- return {
383
- deletedCacheRequests
384
- };
385
- });
386
- }
387
- getURLsToCacheKeys() {
388
- return this._urlsToCacheKeys;
389
- }
390
- getCachedURLs() {
391
- return [
392
- ...this._urlsToCacheKeys.keys()
393
- ];
394
- }
395
- getCacheKeyForURL(url) {
396
- const urlObject = new URL(url, location.href);
397
- return this._urlsToCacheKeys.get(urlObject.href);
398
- }
399
- getIntegrityForCacheKey(cacheKey) {
400
- return this._cacheKeysToIntegrities.get(cacheKey);
401
- }
402
- async matchPrecache(request) {
403
- const url = request instanceof Request ? request.url : request;
404
- const cacheKey = this.getCacheKeyForURL(url);
405
- if (cacheKey) {
406
- const cache = await self.caches.open(this.strategy.cacheName);
407
- return cache.match(cacheKey);
408
- }
409
- return undefined;
410
- }
411
- createHandlerBoundToURL(url) {
412
- const cacheKey = this.getCacheKeyForURL(url);
413
- if (!cacheKey) {
414
- throw new SerwistError("non-precached-url", {
415
- url
416
- });
417
- }
418
- return (options)=>{
419
- options.request = new Request(url);
420
- options.params = {
421
- cacheKey,
422
- ...options.params
423
- };
424
- return this.strategy.handle(options);
425
- };
426
- }
427
- }
428
-
429
- let precacheController;
430
- const getOrCreatePrecacheController = ()=>{
431
- if (!precacheController) {
432
- precacheController = new PrecacheController();
433
- }
434
- return precacheController;
435
- };
5
+ import '@serwist/core';
6
+ import '@serwist/strategies';
436
7
 
437
8
  class PrecacheFallbackPlugin {
438
9
  _fallbackUrls;
@@ -581,4 +152,4 @@ const precacheAndRoute = (entries, options)=>{
581
152
  addRoute(options);
582
153
  };
583
154
 
584
- export { PrecacheController, PrecacheFallbackPlugin, PrecacheRoute, PrecacheStrategy, addPlugins, addRoute, cleanupOutdatedCaches, createHandlerBoundToURL, getCacheKeyForURL, matchPrecache, precache, precacheAndRoute };
155
+ export { PrecacheFallbackPlugin, PrecacheRoute, addPlugins, addRoute, cleanupOutdatedCaches, createHandlerBoundToURL, getCacheKeyForURL, matchPrecache, precache, precacheAndRoute };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@serwist/precaching",
3
- "version": "9.0.0-preview.15",
3
+ "version": "9.0.0-preview.17",
4
4
  "type": "module",
5
5
  "description": "A module that efficiently precaches assets.",
6
6
  "files": [
@@ -20,23 +20,34 @@
20
20
  "homepage": "https://serwist.pages.dev",
21
21
  "main": "./dist/index.js",
22
22
  "types": "./dist/index.d.ts",
23
+ "typesVersions": {
24
+ "*": {
25
+ "internal": [
26
+ "./dist/index.internal.d.ts"
27
+ ]
28
+ }
29
+ },
23
30
  "exports": {
24
31
  ".": {
25
32
  "types": "./dist/index.d.ts",
26
33
  "default": "./dist/index.js"
27
34
  },
35
+ "./internal": {
36
+ "types": "./dist/index.internal.d.ts",
37
+ "default": "./dist/index.internal.js"
38
+ },
28
39
  "./package.json": "./package.json"
29
40
  },
30
41
  "dependencies": {
31
- "@serwist/routing": "9.0.0-preview.15",
32
- "@serwist/core": "9.0.0-preview.15",
33
- "@serwist/strategies": "9.0.0-preview.15"
42
+ "@serwist/core": "9.0.0-preview.17",
43
+ "@serwist/routing": "9.0.0-preview.17",
44
+ "@serwist/strategies": "9.0.0-preview.17"
34
45
  },
35
46
  "devDependencies": {
36
47
  "rollup": "4.13.0",
37
48
  "typescript": "5.5.0-dev.20240323",
38
- "@serwist/constants": "9.0.0-preview.15",
39
- "@serwist/utils": "9.0.0-preview.15"
49
+ "@serwist/constants": "9.0.0-preview.17",
50
+ "@serwist/utils": "9.0.0-preview.17"
40
51
  },
41
52
  "peerDependencies": {
42
53
  "typescript": ">=5.0.0"
@@ -123,7 +123,7 @@ export class PrecacheController {
123
123
  // See https://github.com/GoogleChrome/workbox/issues/2259
124
124
  if (typeof entry === "string") {
125
125
  urlsToWarnAbout.push(entry);
126
- } else if (entry && entry.revision === undefined) {
126
+ } else if (entry && !entry.integrity && entry.revision === undefined) {
127
127
  urlsToWarnAbout.push(entry.url);
128
128
  }
129
129
 
@@ -0,0 +1,3 @@
1
+ import { getOrCreatePrecacheController } from "./utils/getOrCreatePrecacheController.js";
2
+
3
+ export { getOrCreatePrecacheController };