@serwist/precaching 8.4.0 → 8.4.2
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/PrecacheRoute.d.ts +1 -1
- package/dist/index.cjs +96 -96
- package/dist/index.d.cts +4 -4
- package/dist/index.d.ts +4 -4
- package/dist/index.js +96 -96
- package/dist/utils/generateURLVariations.d.ts +1 -1
- package/dist/utils/getCacheKeyForURL.d.ts +1 -1
- package/package.json +6 -6
package/dist/PrecacheRoute.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Route } from "@serwist/routing";
|
|
2
|
-
import type { PrecacheRouteOptions } from "./_types.js";
|
|
3
2
|
import type { PrecacheController } from "./PrecacheController.js";
|
|
3
|
+
import type { PrecacheRouteOptions } from "./_types.js";
|
|
4
4
|
/**
|
|
5
5
|
* A subclass of `@serwist/routing.Route` that takes a
|
|
6
6
|
* `@serwist/precaching.PrecacheController`
|
package/dist/index.cjs
CHANGED
|
@@ -60,12 +60,12 @@ var routing = require('@serwist/routing');
|
|
|
60
60
|
return await this._handleFetch(request, handler);
|
|
61
61
|
}
|
|
62
62
|
async _handleFetch(request, handler) {
|
|
63
|
-
let response;
|
|
63
|
+
let response = undefined;
|
|
64
64
|
const params = handler.params || {};
|
|
65
65
|
// Fallback to the network if we're configured to do so.
|
|
66
66
|
if (this._fallbackToNetwork) {
|
|
67
67
|
if (process.env.NODE_ENV !== "production") {
|
|
68
|
-
internal.logger.warn(`The precached response for
|
|
68
|
+
internal.logger.warn(`The precached response for ${internal.getFriendlyURL(request.url)} in ${this.cacheName} was not found. Falling back to the network.`);
|
|
69
69
|
}
|
|
70
70
|
const integrityInManifest = params.integrity;
|
|
71
71
|
const integrityInRequest = request.integrity;
|
|
@@ -87,7 +87,7 @@ var routing = require('@serwist/routing');
|
|
|
87
87
|
const wasCached = await handler.cachePut(request, response.clone());
|
|
88
88
|
if (process.env.NODE_ENV !== "production") {
|
|
89
89
|
if (wasCached) {
|
|
90
|
-
internal.logger.log(`A response for ${internal.getFriendlyURL(request.url)}
|
|
90
|
+
internal.logger.log(`A response for ${internal.getFriendlyURL(request.url)} was used to "repair" the precache.`);
|
|
91
91
|
}
|
|
92
92
|
}
|
|
93
93
|
}
|
|
@@ -103,12 +103,12 @@ var routing = require('@serwist/routing');
|
|
|
103
103
|
const cacheKey = params.cacheKey || await handler.getCacheKey(request, "read");
|
|
104
104
|
// Serwist is going to handle the route.
|
|
105
105
|
// print the routing details to the console.
|
|
106
|
-
internal.logger.groupCollapsed(`Precaching is responding to:
|
|
106
|
+
internal.logger.groupCollapsed(`Precaching is responding to: ${internal.getFriendlyURL(request.url)}`);
|
|
107
107
|
internal.logger.log(`Serving the precached url: ${internal.getFriendlyURL(cacheKey instanceof Request ? cacheKey.url : cacheKey)}`);
|
|
108
|
-
internal.logger.groupCollapsed(
|
|
108
|
+
internal.logger.groupCollapsed("View request details here.");
|
|
109
109
|
internal.logger.log(request);
|
|
110
110
|
internal.logger.groupEnd();
|
|
111
|
-
internal.logger.groupCollapsed(
|
|
111
|
+
internal.logger.groupCollapsed("View response details here.");
|
|
112
112
|
internal.logger.log(response);
|
|
113
113
|
internal.logger.groupEnd();
|
|
114
114
|
internal.logger.groupEnd();
|
|
@@ -183,56 +183,6 @@ var routing = require('@serwist/routing');
|
|
|
183
183
|
}
|
|
184
184
|
}
|
|
185
185
|
|
|
186
|
-
// Name of the search parameter used to store revision info.
|
|
187
|
-
const REVISION_SEARCH_PARAM = "__WB_REVISION__";
|
|
188
|
-
/**
|
|
189
|
-
* Converts a manifest entry into a versioned URL suitable for precaching.
|
|
190
|
-
*
|
|
191
|
-
* @param entry
|
|
192
|
-
* @returns A URL with versioning info.
|
|
193
|
-
*
|
|
194
|
-
* @private
|
|
195
|
-
*/ function createCacheKey(entry) {
|
|
196
|
-
if (!entry) {
|
|
197
|
-
throw new internal.SerwistError("add-to-cache-list-unexpected-type", {
|
|
198
|
-
entry
|
|
199
|
-
});
|
|
200
|
-
}
|
|
201
|
-
// If a precache manifest entry is a string, it's assumed to be a versioned
|
|
202
|
-
// URL, like '/app.abcd1234.js'. Return as-is.
|
|
203
|
-
if (typeof entry === "string") {
|
|
204
|
-
const urlObject = new URL(entry, location.href);
|
|
205
|
-
return {
|
|
206
|
-
cacheKey: urlObject.href,
|
|
207
|
-
url: urlObject.href
|
|
208
|
-
};
|
|
209
|
-
}
|
|
210
|
-
const { revision, url } = entry;
|
|
211
|
-
if (!url) {
|
|
212
|
-
throw new internal.SerwistError("add-to-cache-list-unexpected-type", {
|
|
213
|
-
entry
|
|
214
|
-
});
|
|
215
|
-
}
|
|
216
|
-
// If there's just a URL and no revision, then it's also assumed to be a
|
|
217
|
-
// versioned URL.
|
|
218
|
-
if (!revision) {
|
|
219
|
-
const urlObject = new URL(url, location.href);
|
|
220
|
-
return {
|
|
221
|
-
cacheKey: urlObject.href,
|
|
222
|
-
url: urlObject.href
|
|
223
|
-
};
|
|
224
|
-
}
|
|
225
|
-
// Otherwise, construct a properly versioned URL using the custom Serwist
|
|
226
|
-
// search parameter along with the revision info.
|
|
227
|
-
const cacheKeyURL = new URL(url, location.href);
|
|
228
|
-
const originalURL = new URL(url, location.href);
|
|
229
|
-
cacheKeyURL.searchParams.set(REVISION_SEARCH_PARAM, revision);
|
|
230
|
-
return {
|
|
231
|
-
cacheKey: cacheKeyURL.href,
|
|
232
|
-
url: originalURL.href
|
|
233
|
-
};
|
|
234
|
-
}
|
|
235
|
-
|
|
236
186
|
/*
|
|
237
187
|
Copyright 2020 Google LLC
|
|
238
188
|
|
|
@@ -280,7 +230,7 @@ const REVISION_SEARCH_PARAM = "__WB_REVISION__";
|
|
|
280
230
|
};
|
|
281
231
|
cachedResponseWillBeUsed = async ({ event, state, cachedResponse })=>{
|
|
282
232
|
if (event.type === "install") {
|
|
283
|
-
if (state
|
|
233
|
+
if (state?.originalRequest && state.originalRequest instanceof Request) {
|
|
284
234
|
// TODO: `state` should never be undefined...
|
|
285
235
|
const url = state.originalRequest.url;
|
|
286
236
|
if (cachedResponse) {
|
|
@@ -294,6 +244,56 @@ const REVISION_SEARCH_PARAM = "__WB_REVISION__";
|
|
|
294
244
|
};
|
|
295
245
|
}
|
|
296
246
|
|
|
247
|
+
// Name of the search parameter used to store revision info.
|
|
248
|
+
const REVISION_SEARCH_PARAM = "__WB_REVISION__";
|
|
249
|
+
/**
|
|
250
|
+
* Converts a manifest entry into a versioned URL suitable for precaching.
|
|
251
|
+
*
|
|
252
|
+
* @param entry
|
|
253
|
+
* @returns A URL with versioning info.
|
|
254
|
+
*
|
|
255
|
+
* @private
|
|
256
|
+
*/ function createCacheKey(entry) {
|
|
257
|
+
if (!entry) {
|
|
258
|
+
throw new internal.SerwistError("add-to-cache-list-unexpected-type", {
|
|
259
|
+
entry
|
|
260
|
+
});
|
|
261
|
+
}
|
|
262
|
+
// If a precache manifest entry is a string, it's assumed to be a versioned
|
|
263
|
+
// URL, like '/app.abcd1234.js'. Return as-is.
|
|
264
|
+
if (typeof entry === "string") {
|
|
265
|
+
const urlObject = new URL(entry, location.href);
|
|
266
|
+
return {
|
|
267
|
+
cacheKey: urlObject.href,
|
|
268
|
+
url: urlObject.href
|
|
269
|
+
};
|
|
270
|
+
}
|
|
271
|
+
const { revision, url } = entry;
|
|
272
|
+
if (!url) {
|
|
273
|
+
throw new internal.SerwistError("add-to-cache-list-unexpected-type", {
|
|
274
|
+
entry
|
|
275
|
+
});
|
|
276
|
+
}
|
|
277
|
+
// If there's just a URL and no revision, then it's also assumed to be a
|
|
278
|
+
// versioned URL.
|
|
279
|
+
if (!revision) {
|
|
280
|
+
const urlObject = new URL(url, location.href);
|
|
281
|
+
return {
|
|
282
|
+
cacheKey: urlObject.href,
|
|
283
|
+
url: urlObject.href
|
|
284
|
+
};
|
|
285
|
+
}
|
|
286
|
+
// Otherwise, construct a properly versioned URL using the custom Serwist
|
|
287
|
+
// search parameter along with the revision info.
|
|
288
|
+
const cacheKeyURL = new URL(url, location.href);
|
|
289
|
+
const originalURL = new URL(url, location.href);
|
|
290
|
+
cacheKeyURL.searchParams.set(REVISION_SEARCH_PARAM, revision);
|
|
291
|
+
return {
|
|
292
|
+
cacheKey: cacheKeyURL.href,
|
|
293
|
+
url: originalURL.href
|
|
294
|
+
};
|
|
295
|
+
}
|
|
296
|
+
|
|
297
297
|
/**
|
|
298
298
|
* @param groupTitle
|
|
299
299
|
* @param deletedURLs
|
|
@@ -312,7 +312,7 @@ const REVISION_SEARCH_PARAM = "__WB_REVISION__";
|
|
|
312
312
|
*/ function printCleanupDetails(deletedURLs) {
|
|
313
313
|
const deletionCount = deletedURLs.length;
|
|
314
314
|
if (deletionCount > 0) {
|
|
315
|
-
internal.logger.groupCollapsed(`During precaching cleanup,
|
|
315
|
+
internal.logger.groupCollapsed(`During precaching cleanup, ${deletionCount} cached request${deletionCount === 1 ? " was" : "s were"} deleted.`);
|
|
316
316
|
logGroup("Deleted Cache Requests", deletedURLs);
|
|
317
317
|
internal.logger.groupEnd();
|
|
318
318
|
}
|
|
@@ -346,8 +346,8 @@ const REVISION_SEARCH_PARAM = "__WB_REVISION__";
|
|
|
346
346
|
message += ` ${alreadyPrecachedCount} ` + `file${alreadyPrecachedCount === 1 ? " is" : "s are"} already cached.`;
|
|
347
347
|
}
|
|
348
348
|
internal.logger.groupCollapsed(message);
|
|
349
|
-
_nestedGroup(
|
|
350
|
-
_nestedGroup(
|
|
349
|
+
_nestedGroup("View newly precached URLs.", urlsToPrecache);
|
|
350
|
+
_nestedGroup("View previously precached URLs.", urlsAlreadyPrecached);
|
|
351
351
|
internal.logger.groupEnd();
|
|
352
352
|
}
|
|
353
353
|
}
|
|
@@ -442,7 +442,7 @@ const REVISION_SEARCH_PARAM = "__WB_REVISION__";
|
|
|
442
442
|
this._urlsToCacheKeys.set(url, cacheKey);
|
|
443
443
|
this._urlsToCacheModes.set(url, cacheMode);
|
|
444
444
|
if (urlsToWarnAbout.length > 0) {
|
|
445
|
-
const warningMessage = `Serwist is precaching URLs without revision
|
|
445
|
+
const warningMessage = `Serwist is precaching URLs without revision info: ${urlsToWarnAbout.join(", ")}\nThis is generally NOT safe. Learn more at https://bit.ly/wb-precache`;
|
|
446
446
|
if (process.env.NODE_ENV === "production") {
|
|
447
447
|
// Use console directly to display this warning without bloating
|
|
448
448
|
// bundle sizes by pulling in all of the logger codebase in prod.
|
|
@@ -626,12 +626,31 @@ let precacheController;
|
|
|
626
626
|
};
|
|
627
627
|
|
|
628
628
|
/**
|
|
629
|
-
*
|
|
629
|
+
* `PrecacheFallbackPlugin` allows you to specify an "offline fallback"
|
|
630
|
+
* response to be used when a given strategy is unable to generate a response.
|
|
630
631
|
*
|
|
631
|
-
*
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
632
|
+
* It does this by intercepting the `handlerDidError` plugin callback
|
|
633
|
+
* and returning a precached response, taking the expected revision parameter
|
|
634
|
+
* into account automatically.
|
|
635
|
+
*
|
|
636
|
+
* Unless you explicitly pass in a `PrecacheController` instance to the
|
|
637
|
+
* constructor, the default instance will be used. Generally speaking, most
|
|
638
|
+
* developers will end up using the default.
|
|
639
|
+
*/ class PrecacheFallbackPlugin {
|
|
640
|
+
_fallbackURL;
|
|
641
|
+
_precacheController;
|
|
642
|
+
/**
|
|
643
|
+
* Constructs a new PrecacheFallbackPlugin with the associated fallbackURL.
|
|
644
|
+
*
|
|
645
|
+
* @param config
|
|
646
|
+
*/ constructor({ fallbackURL, precacheController }){
|
|
647
|
+
this._fallbackURL = fallbackURL;
|
|
648
|
+
this._precacheController = precacheController || getOrCreatePrecacheController();
|
|
649
|
+
}
|
|
650
|
+
/**
|
|
651
|
+
* @returns The precache response for the fallback URL.
|
|
652
|
+
* @private
|
|
653
|
+
*/ handlerDidError = ()=>this._precacheController.matchPrecache(this._fallbackURL);
|
|
635
654
|
}
|
|
636
655
|
|
|
637
656
|
/*
|
|
@@ -724,7 +743,7 @@ let precacheController;
|
|
|
724
743
|
}
|
|
725
744
|
}
|
|
726
745
|
if (process.env.NODE_ENV !== "production") {
|
|
727
|
-
internal.logger.debug(`Precaching did not find a match for
|
|
746
|
+
internal.logger.debug(`Precaching did not find a match for ${internal.getFriendlyURL(request.url)}`);
|
|
728
747
|
}
|
|
729
748
|
return;
|
|
730
749
|
};
|
|
@@ -732,6 +751,15 @@ let precacheController;
|
|
|
732
751
|
}
|
|
733
752
|
}
|
|
734
753
|
|
|
754
|
+
/**
|
|
755
|
+
* Adds plugins to the precaching strategy.
|
|
756
|
+
*
|
|
757
|
+
* @param plugins
|
|
758
|
+
*/ function addPlugins(plugins) {
|
|
759
|
+
const precacheController = getOrCreatePrecacheController();
|
|
760
|
+
precacheController.strategy.plugins.push(...plugins);
|
|
761
|
+
}
|
|
762
|
+
|
|
735
763
|
/**
|
|
736
764
|
* Add a `fetch` listener to the service worker that will
|
|
737
765
|
* respond to
|
|
@@ -791,7 +819,7 @@ const SUBSTRING_TO_FIND = "-precache-";
|
|
|
791
819
|
event.waitUntil(deleteOutdatedCaches(cacheName).then((cachesDeleted)=>{
|
|
792
820
|
if (process.env.NODE_ENV !== "production") {
|
|
793
821
|
if (cachesDeleted.length > 0) {
|
|
794
|
-
internal.logger.log(
|
|
822
|
+
internal.logger.log("The following out-of-date precaches were cleaned up " + "automatically:", cachesDeleted);
|
|
795
823
|
}
|
|
796
824
|
}
|
|
797
825
|
}));
|
|
@@ -888,34 +916,6 @@ const SUBSTRING_TO_FIND = "-precache-";
|
|
|
888
916
|
addRoute(options);
|
|
889
917
|
}
|
|
890
918
|
|
|
891
|
-
/**
|
|
892
|
-
* `PrecacheFallbackPlugin` allows you to specify an "offline fallback"
|
|
893
|
-
* response to be used when a given strategy is unable to generate a response.
|
|
894
|
-
*
|
|
895
|
-
* It does this by intercepting the `handlerDidError` plugin callback
|
|
896
|
-
* and returning a precached response, taking the expected revision parameter
|
|
897
|
-
* into account automatically.
|
|
898
|
-
*
|
|
899
|
-
* Unless you explicitly pass in a `PrecacheController` instance to the
|
|
900
|
-
* constructor, the default instance will be used. Generally speaking, most
|
|
901
|
-
* developers will end up using the default.
|
|
902
|
-
*/ class PrecacheFallbackPlugin {
|
|
903
|
-
_fallbackURL;
|
|
904
|
-
_precacheController;
|
|
905
|
-
/**
|
|
906
|
-
* Constructs a new PrecacheFallbackPlugin with the associated fallbackURL.
|
|
907
|
-
*
|
|
908
|
-
* @param config
|
|
909
|
-
*/ constructor({ fallbackURL, precacheController }){
|
|
910
|
-
this._fallbackURL = fallbackURL;
|
|
911
|
-
this._precacheController = precacheController || getOrCreatePrecacheController();
|
|
912
|
-
}
|
|
913
|
-
/**
|
|
914
|
-
* @returns The precache response for the fallback URL.
|
|
915
|
-
* @private
|
|
916
|
-
*/ handlerDidError = ()=>this._precacheController.matchPrecache(this._fallbackURL);
|
|
917
|
-
}
|
|
918
|
-
|
|
919
919
|
exports.PrecacheController = PrecacheController;
|
|
920
920
|
exports.PrecacheFallbackPlugin = PrecacheFallbackPlugin;
|
|
921
921
|
exports.PrecacheRoute = PrecacheRoute;
|
package/dist/index.d.cts
CHANGED
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
import { PrecacheController } from "./PrecacheController.js";
|
|
2
|
+
import { PrecacheFallbackPlugin } from "./PrecacheFallbackPlugin.js";
|
|
3
|
+
import { PrecacheRoute } from "./PrecacheRoute.js";
|
|
4
|
+
import { PrecacheStrategy } from "./PrecacheStrategy.js";
|
|
1
5
|
import { addPlugins } from "./addPlugins.js";
|
|
2
6
|
import { addRoute } from "./addRoute.js";
|
|
3
7
|
import { cleanupOutdatedCaches } from "./cleanupOutdatedCaches.js";
|
|
@@ -6,10 +10,6 @@ import { getCacheKeyForURL } from "./getCacheKeyForURL.js";
|
|
|
6
10
|
import { matchPrecache } from "./matchPrecache.js";
|
|
7
11
|
import { precache } from "./precache.js";
|
|
8
12
|
import { precacheAndRoute } from "./precacheAndRoute.js";
|
|
9
|
-
import { PrecacheController } from "./PrecacheController.js";
|
|
10
|
-
import { PrecacheFallbackPlugin } from "./PrecacheFallbackPlugin.js";
|
|
11
|
-
import { PrecacheRoute } from "./PrecacheRoute.js";
|
|
12
|
-
import { PrecacheStrategy } from "./PrecacheStrategy.js";
|
|
13
13
|
/**
|
|
14
14
|
* Most consumers of this module will want to use the
|
|
15
15
|
* `@serwist/precaching.precacheAndRoute`
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
import { PrecacheController } from "./PrecacheController.js";
|
|
2
|
+
import { PrecacheFallbackPlugin } from "./PrecacheFallbackPlugin.js";
|
|
3
|
+
import { PrecacheRoute } from "./PrecacheRoute.js";
|
|
4
|
+
import { PrecacheStrategy } from "./PrecacheStrategy.js";
|
|
1
5
|
import { addPlugins } from "./addPlugins.js";
|
|
2
6
|
import { addRoute } from "./addRoute.js";
|
|
3
7
|
import { cleanupOutdatedCaches } from "./cleanupOutdatedCaches.js";
|
|
@@ -6,10 +10,6 @@ import { getCacheKeyForURL } from "./getCacheKeyForURL.js";
|
|
|
6
10
|
import { matchPrecache } from "./matchPrecache.js";
|
|
7
11
|
import { precache } from "./precache.js";
|
|
8
12
|
import { precacheAndRoute } from "./precacheAndRoute.js";
|
|
9
|
-
import { PrecacheController } from "./PrecacheController.js";
|
|
10
|
-
import { PrecacheFallbackPlugin } from "./PrecacheFallbackPlugin.js";
|
|
11
|
-
import { PrecacheRoute } from "./PrecacheRoute.js";
|
|
12
|
-
import { PrecacheStrategy } from "./PrecacheStrategy.js";
|
|
13
13
|
/**
|
|
14
14
|
* Most consumers of this module will want to use the
|
|
15
15
|
* `@serwist/precaching.precacheAndRoute`
|
package/dist/index.js
CHANGED
|
@@ -58,12 +58,12 @@ import { Route, registerRoute } from '@serwist/routing';
|
|
|
58
58
|
return await this._handleFetch(request, handler);
|
|
59
59
|
}
|
|
60
60
|
async _handleFetch(request, handler) {
|
|
61
|
-
let response;
|
|
61
|
+
let response = undefined;
|
|
62
62
|
const params = handler.params || {};
|
|
63
63
|
// Fallback to the network if we're configured to do so.
|
|
64
64
|
if (this._fallbackToNetwork) {
|
|
65
65
|
if (process.env.NODE_ENV !== "production") {
|
|
66
|
-
logger.warn(`The precached response for
|
|
66
|
+
logger.warn(`The precached response for ${getFriendlyURL(request.url)} in ${this.cacheName} was not found. Falling back to the network.`);
|
|
67
67
|
}
|
|
68
68
|
const integrityInManifest = params.integrity;
|
|
69
69
|
const integrityInRequest = request.integrity;
|
|
@@ -85,7 +85,7 @@ import { Route, registerRoute } from '@serwist/routing';
|
|
|
85
85
|
const wasCached = await handler.cachePut(request, response.clone());
|
|
86
86
|
if (process.env.NODE_ENV !== "production") {
|
|
87
87
|
if (wasCached) {
|
|
88
|
-
logger.log(`A response for ${getFriendlyURL(request.url)}
|
|
88
|
+
logger.log(`A response for ${getFriendlyURL(request.url)} was used to "repair" the precache.`);
|
|
89
89
|
}
|
|
90
90
|
}
|
|
91
91
|
}
|
|
@@ -101,12 +101,12 @@ import { Route, registerRoute } from '@serwist/routing';
|
|
|
101
101
|
const cacheKey = params.cacheKey || await handler.getCacheKey(request, "read");
|
|
102
102
|
// Serwist is going to handle the route.
|
|
103
103
|
// print the routing details to the console.
|
|
104
|
-
logger.groupCollapsed(`Precaching is responding to:
|
|
104
|
+
logger.groupCollapsed(`Precaching is responding to: ${getFriendlyURL(request.url)}`);
|
|
105
105
|
logger.log(`Serving the precached url: ${getFriendlyURL(cacheKey instanceof Request ? cacheKey.url : cacheKey)}`);
|
|
106
|
-
logger.groupCollapsed(
|
|
106
|
+
logger.groupCollapsed("View request details here.");
|
|
107
107
|
logger.log(request);
|
|
108
108
|
logger.groupEnd();
|
|
109
|
-
logger.groupCollapsed(
|
|
109
|
+
logger.groupCollapsed("View response details here.");
|
|
110
110
|
logger.log(response);
|
|
111
111
|
logger.groupEnd();
|
|
112
112
|
logger.groupEnd();
|
|
@@ -181,56 +181,6 @@ import { Route, registerRoute } from '@serwist/routing';
|
|
|
181
181
|
}
|
|
182
182
|
}
|
|
183
183
|
|
|
184
|
-
// Name of the search parameter used to store revision info.
|
|
185
|
-
const REVISION_SEARCH_PARAM = "__WB_REVISION__";
|
|
186
|
-
/**
|
|
187
|
-
* Converts a manifest entry into a versioned URL suitable for precaching.
|
|
188
|
-
*
|
|
189
|
-
* @param entry
|
|
190
|
-
* @returns A URL with versioning info.
|
|
191
|
-
*
|
|
192
|
-
* @private
|
|
193
|
-
*/ function createCacheKey(entry) {
|
|
194
|
-
if (!entry) {
|
|
195
|
-
throw new SerwistError("add-to-cache-list-unexpected-type", {
|
|
196
|
-
entry
|
|
197
|
-
});
|
|
198
|
-
}
|
|
199
|
-
// If a precache manifest entry is a string, it's assumed to be a versioned
|
|
200
|
-
// URL, like '/app.abcd1234.js'. Return as-is.
|
|
201
|
-
if (typeof entry === "string") {
|
|
202
|
-
const urlObject = new URL(entry, location.href);
|
|
203
|
-
return {
|
|
204
|
-
cacheKey: urlObject.href,
|
|
205
|
-
url: urlObject.href
|
|
206
|
-
};
|
|
207
|
-
}
|
|
208
|
-
const { revision, url } = entry;
|
|
209
|
-
if (!url) {
|
|
210
|
-
throw new SerwistError("add-to-cache-list-unexpected-type", {
|
|
211
|
-
entry
|
|
212
|
-
});
|
|
213
|
-
}
|
|
214
|
-
// If there's just a URL and no revision, then it's also assumed to be a
|
|
215
|
-
// versioned URL.
|
|
216
|
-
if (!revision) {
|
|
217
|
-
const urlObject = new URL(url, location.href);
|
|
218
|
-
return {
|
|
219
|
-
cacheKey: urlObject.href,
|
|
220
|
-
url: urlObject.href
|
|
221
|
-
};
|
|
222
|
-
}
|
|
223
|
-
// Otherwise, construct a properly versioned URL using the custom Serwist
|
|
224
|
-
// search parameter along with the revision info.
|
|
225
|
-
const cacheKeyURL = new URL(url, location.href);
|
|
226
|
-
const originalURL = new URL(url, location.href);
|
|
227
|
-
cacheKeyURL.searchParams.set(REVISION_SEARCH_PARAM, revision);
|
|
228
|
-
return {
|
|
229
|
-
cacheKey: cacheKeyURL.href,
|
|
230
|
-
url: originalURL.href
|
|
231
|
-
};
|
|
232
|
-
}
|
|
233
|
-
|
|
234
184
|
/*
|
|
235
185
|
Copyright 2020 Google LLC
|
|
236
186
|
|
|
@@ -278,7 +228,7 @@ const REVISION_SEARCH_PARAM = "__WB_REVISION__";
|
|
|
278
228
|
};
|
|
279
229
|
cachedResponseWillBeUsed = async ({ event, state, cachedResponse })=>{
|
|
280
230
|
if (event.type === "install") {
|
|
281
|
-
if (state
|
|
231
|
+
if (state?.originalRequest && state.originalRequest instanceof Request) {
|
|
282
232
|
// TODO: `state` should never be undefined...
|
|
283
233
|
const url = state.originalRequest.url;
|
|
284
234
|
if (cachedResponse) {
|
|
@@ -292,6 +242,56 @@ const REVISION_SEARCH_PARAM = "__WB_REVISION__";
|
|
|
292
242
|
};
|
|
293
243
|
}
|
|
294
244
|
|
|
245
|
+
// Name of the search parameter used to store revision info.
|
|
246
|
+
const REVISION_SEARCH_PARAM = "__WB_REVISION__";
|
|
247
|
+
/**
|
|
248
|
+
* Converts a manifest entry into a versioned URL suitable for precaching.
|
|
249
|
+
*
|
|
250
|
+
* @param entry
|
|
251
|
+
* @returns A URL with versioning info.
|
|
252
|
+
*
|
|
253
|
+
* @private
|
|
254
|
+
*/ function createCacheKey(entry) {
|
|
255
|
+
if (!entry) {
|
|
256
|
+
throw new SerwistError("add-to-cache-list-unexpected-type", {
|
|
257
|
+
entry
|
|
258
|
+
});
|
|
259
|
+
}
|
|
260
|
+
// If a precache manifest entry is a string, it's assumed to be a versioned
|
|
261
|
+
// URL, like '/app.abcd1234.js'. Return as-is.
|
|
262
|
+
if (typeof entry === "string") {
|
|
263
|
+
const urlObject = new URL(entry, location.href);
|
|
264
|
+
return {
|
|
265
|
+
cacheKey: urlObject.href,
|
|
266
|
+
url: urlObject.href
|
|
267
|
+
};
|
|
268
|
+
}
|
|
269
|
+
const { revision, url } = entry;
|
|
270
|
+
if (!url) {
|
|
271
|
+
throw new SerwistError("add-to-cache-list-unexpected-type", {
|
|
272
|
+
entry
|
|
273
|
+
});
|
|
274
|
+
}
|
|
275
|
+
// If there's just a URL and no revision, then it's also assumed to be a
|
|
276
|
+
// versioned URL.
|
|
277
|
+
if (!revision) {
|
|
278
|
+
const urlObject = new URL(url, location.href);
|
|
279
|
+
return {
|
|
280
|
+
cacheKey: urlObject.href,
|
|
281
|
+
url: urlObject.href
|
|
282
|
+
};
|
|
283
|
+
}
|
|
284
|
+
// Otherwise, construct a properly versioned URL using the custom Serwist
|
|
285
|
+
// search parameter along with the revision info.
|
|
286
|
+
const cacheKeyURL = new URL(url, location.href);
|
|
287
|
+
const originalURL = new URL(url, location.href);
|
|
288
|
+
cacheKeyURL.searchParams.set(REVISION_SEARCH_PARAM, revision);
|
|
289
|
+
return {
|
|
290
|
+
cacheKey: cacheKeyURL.href,
|
|
291
|
+
url: originalURL.href
|
|
292
|
+
};
|
|
293
|
+
}
|
|
294
|
+
|
|
295
295
|
/**
|
|
296
296
|
* @param groupTitle
|
|
297
297
|
* @param deletedURLs
|
|
@@ -310,7 +310,7 @@ const REVISION_SEARCH_PARAM = "__WB_REVISION__";
|
|
|
310
310
|
*/ function printCleanupDetails(deletedURLs) {
|
|
311
311
|
const deletionCount = deletedURLs.length;
|
|
312
312
|
if (deletionCount > 0) {
|
|
313
|
-
logger.groupCollapsed(`During precaching cleanup,
|
|
313
|
+
logger.groupCollapsed(`During precaching cleanup, ${deletionCount} cached request${deletionCount === 1 ? " was" : "s were"} deleted.`);
|
|
314
314
|
logGroup("Deleted Cache Requests", deletedURLs);
|
|
315
315
|
logger.groupEnd();
|
|
316
316
|
}
|
|
@@ -344,8 +344,8 @@ const REVISION_SEARCH_PARAM = "__WB_REVISION__";
|
|
|
344
344
|
message += ` ${alreadyPrecachedCount} ` + `file${alreadyPrecachedCount === 1 ? " is" : "s are"} already cached.`;
|
|
345
345
|
}
|
|
346
346
|
logger.groupCollapsed(message);
|
|
347
|
-
_nestedGroup(
|
|
348
|
-
_nestedGroup(
|
|
347
|
+
_nestedGroup("View newly precached URLs.", urlsToPrecache);
|
|
348
|
+
_nestedGroup("View previously precached URLs.", urlsAlreadyPrecached);
|
|
349
349
|
logger.groupEnd();
|
|
350
350
|
}
|
|
351
351
|
}
|
|
@@ -440,7 +440,7 @@ const REVISION_SEARCH_PARAM = "__WB_REVISION__";
|
|
|
440
440
|
this._urlsToCacheKeys.set(url, cacheKey);
|
|
441
441
|
this._urlsToCacheModes.set(url, cacheMode);
|
|
442
442
|
if (urlsToWarnAbout.length > 0) {
|
|
443
|
-
const warningMessage = `Serwist is precaching URLs without revision
|
|
443
|
+
const warningMessage = `Serwist is precaching URLs without revision info: ${urlsToWarnAbout.join(", ")}\nThis is generally NOT safe. Learn more at https://bit.ly/wb-precache`;
|
|
444
444
|
if (process.env.NODE_ENV === "production") {
|
|
445
445
|
// Use console directly to display this warning without bloating
|
|
446
446
|
// bundle sizes by pulling in all of the logger codebase in prod.
|
|
@@ -624,12 +624,31 @@ let precacheController;
|
|
|
624
624
|
};
|
|
625
625
|
|
|
626
626
|
/**
|
|
627
|
-
*
|
|
627
|
+
* `PrecacheFallbackPlugin` allows you to specify an "offline fallback"
|
|
628
|
+
* response to be used when a given strategy is unable to generate a response.
|
|
628
629
|
*
|
|
629
|
-
*
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
630
|
+
* It does this by intercepting the `handlerDidError` plugin callback
|
|
631
|
+
* and returning a precached response, taking the expected revision parameter
|
|
632
|
+
* into account automatically.
|
|
633
|
+
*
|
|
634
|
+
* Unless you explicitly pass in a `PrecacheController` instance to the
|
|
635
|
+
* constructor, the default instance will be used. Generally speaking, most
|
|
636
|
+
* developers will end up using the default.
|
|
637
|
+
*/ class PrecacheFallbackPlugin {
|
|
638
|
+
_fallbackURL;
|
|
639
|
+
_precacheController;
|
|
640
|
+
/**
|
|
641
|
+
* Constructs a new PrecacheFallbackPlugin with the associated fallbackURL.
|
|
642
|
+
*
|
|
643
|
+
* @param config
|
|
644
|
+
*/ constructor({ fallbackURL, precacheController }){
|
|
645
|
+
this._fallbackURL = fallbackURL;
|
|
646
|
+
this._precacheController = precacheController || getOrCreatePrecacheController();
|
|
647
|
+
}
|
|
648
|
+
/**
|
|
649
|
+
* @returns The precache response for the fallback URL.
|
|
650
|
+
* @private
|
|
651
|
+
*/ handlerDidError = ()=>this._precacheController.matchPrecache(this._fallbackURL);
|
|
633
652
|
}
|
|
634
653
|
|
|
635
654
|
/*
|
|
@@ -722,7 +741,7 @@ let precacheController;
|
|
|
722
741
|
}
|
|
723
742
|
}
|
|
724
743
|
if (process.env.NODE_ENV !== "production") {
|
|
725
|
-
logger.debug(`Precaching did not find a match for
|
|
744
|
+
logger.debug(`Precaching did not find a match for ${getFriendlyURL(request.url)}`);
|
|
726
745
|
}
|
|
727
746
|
return;
|
|
728
747
|
};
|
|
@@ -730,6 +749,15 @@ let precacheController;
|
|
|
730
749
|
}
|
|
731
750
|
}
|
|
732
751
|
|
|
752
|
+
/**
|
|
753
|
+
* Adds plugins to the precaching strategy.
|
|
754
|
+
*
|
|
755
|
+
* @param plugins
|
|
756
|
+
*/ function addPlugins(plugins) {
|
|
757
|
+
const precacheController = getOrCreatePrecacheController();
|
|
758
|
+
precacheController.strategy.plugins.push(...plugins);
|
|
759
|
+
}
|
|
760
|
+
|
|
733
761
|
/**
|
|
734
762
|
* Add a `fetch` listener to the service worker that will
|
|
735
763
|
* respond to
|
|
@@ -789,7 +817,7 @@ const SUBSTRING_TO_FIND = "-precache-";
|
|
|
789
817
|
event.waitUntil(deleteOutdatedCaches(cacheName).then((cachesDeleted)=>{
|
|
790
818
|
if (process.env.NODE_ENV !== "production") {
|
|
791
819
|
if (cachesDeleted.length > 0) {
|
|
792
|
-
logger.log(
|
|
820
|
+
logger.log("The following out-of-date precaches were cleaned up " + "automatically:", cachesDeleted);
|
|
793
821
|
}
|
|
794
822
|
}
|
|
795
823
|
}));
|
|
@@ -886,32 +914,4 @@ const SUBSTRING_TO_FIND = "-precache-";
|
|
|
886
914
|
addRoute(options);
|
|
887
915
|
}
|
|
888
916
|
|
|
889
|
-
/**
|
|
890
|
-
* `PrecacheFallbackPlugin` allows you to specify an "offline fallback"
|
|
891
|
-
* response to be used when a given strategy is unable to generate a response.
|
|
892
|
-
*
|
|
893
|
-
* It does this by intercepting the `handlerDidError` plugin callback
|
|
894
|
-
* and returning a precached response, taking the expected revision parameter
|
|
895
|
-
* into account automatically.
|
|
896
|
-
*
|
|
897
|
-
* Unless you explicitly pass in a `PrecacheController` instance to the
|
|
898
|
-
* constructor, the default instance will be used. Generally speaking, most
|
|
899
|
-
* developers will end up using the default.
|
|
900
|
-
*/ class PrecacheFallbackPlugin {
|
|
901
|
-
_fallbackURL;
|
|
902
|
-
_precacheController;
|
|
903
|
-
/**
|
|
904
|
-
* Constructs a new PrecacheFallbackPlugin with the associated fallbackURL.
|
|
905
|
-
*
|
|
906
|
-
* @param config
|
|
907
|
-
*/ constructor({ fallbackURL, precacheController }){
|
|
908
|
-
this._fallbackURL = fallbackURL;
|
|
909
|
-
this._precacheController = precacheController || getOrCreatePrecacheController();
|
|
910
|
-
}
|
|
911
|
-
/**
|
|
912
|
-
* @returns The precache response for the fallback URL.
|
|
913
|
-
* @private
|
|
914
|
-
*/ handlerDidError = ()=>this._precacheController.matchPrecache(this._fallbackURL);
|
|
915
|
-
}
|
|
916
|
-
|
|
917
917
|
export { PrecacheController, PrecacheFallbackPlugin, PrecacheRoute, PrecacheStrategy, addPlugins, addRoute, cleanupOutdatedCaches, createHandlerBoundToURL, getCacheKeyForURL, matchPrecache, precache, precacheAndRoute };
|
|
@@ -8,4 +8,4 @@ import type { PrecacheRouteOptions } from "../_types.js";
|
|
|
8
8
|
*
|
|
9
9
|
* @private
|
|
10
10
|
*/
|
|
11
|
-
export declare function generateURLVariations(url: string, { ignoreURLParametersMatching, directoryIndex, cleanURLs, urlManipulation }?: PrecacheRouteOptions): Generator<string, void, unknown>;
|
|
11
|
+
export declare function generateURLVariations(url: string, { ignoreURLParametersMatching, directoryIndex, cleanURLs, urlManipulation, }?: PrecacheRouteOptions): Generator<string, void, unknown>;
|
|
@@ -10,4 +10,4 @@ import type { PrecacheRouteOptions } from "../_types.js";
|
|
|
10
10
|
*
|
|
11
11
|
* @private
|
|
12
12
|
*/
|
|
13
|
-
export declare const getCacheKeyForURL: (url: string, options: PrecacheRouteOptions) => string |
|
|
13
|
+
export declare const getCacheKeyForURL: (url: string, options: PrecacheRouteOptions) => string | undefined;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@serwist/precaching",
|
|
3
|
-
"version": "8.4.
|
|
3
|
+
"version": "8.4.2",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "This module efficiently precaches assets.",
|
|
6
6
|
"files": [
|
|
@@ -35,18 +35,18 @@
|
|
|
35
35
|
"./package.json": "./package.json"
|
|
36
36
|
},
|
|
37
37
|
"dependencies": {
|
|
38
|
-
"@serwist/core": "8.4.
|
|
39
|
-
"@serwist/routing": "8.4.
|
|
40
|
-
"@serwist/strategies": "8.4.
|
|
38
|
+
"@serwist/core": "8.4.2",
|
|
39
|
+
"@serwist/routing": "8.4.2",
|
|
40
|
+
"@serwist/strategies": "8.4.2"
|
|
41
41
|
},
|
|
42
42
|
"devDependencies": {
|
|
43
43
|
"rollup": "4.9.1",
|
|
44
|
-
"@serwist/constants": "8.4.
|
|
44
|
+
"@serwist/constants": "8.4.2"
|
|
45
45
|
},
|
|
46
46
|
"scripts": {
|
|
47
47
|
"build": "rimraf dist && cross-env NODE_ENV=production rollup --config rollup.config.js",
|
|
48
48
|
"dev": "rollup --config rollup.config.js --watch",
|
|
49
|
-
"lint": "
|
|
49
|
+
"lint": "biome lint ./src",
|
|
50
50
|
"typecheck": "tsc"
|
|
51
51
|
}
|
|
52
52
|
}
|