serwist 10.0.0-preview.13 → 10.0.0-preview.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/chunks/waitUntil.js +24 -1
- package/dist/index.internal.js +2 -25
- package/dist/index.js +6 -3
- package/dist/lib/constants.d.ts +1 -1
- package/dist/lib/constants.d.ts.map +1 -1
- package/dist/lib/extensions/precache/extension.d.ts.map +1 -1
- package/dist/lib/extensions/precache/options.d.ts +1 -1
- package/dist/lib/types.d.ts +93 -2
- package/dist/lib/types.d.ts.map +1 -1
- package/dist/utils/cleanupOutdatedCaches.d.ts +7 -0
- package/dist/utils/cleanupOutdatedCaches.d.ts.map +1 -1
- package/package.json +4 -4
- package/src/lib/constants.ts +1 -1
- package/src/lib/extensions/precache/extension.ts +10 -2
- package/src/lib/types.ts +109 -9
- package/src/utils/cleanupOutdatedCaches.ts +16 -9
package/dist/chunks/waitUntil.js
CHANGED
|
@@ -401,6 +401,29 @@ function timeout(ms) {
|
|
|
401
401
|
return new Promise((resolve)=>setTimeout(resolve, ms));
|
|
402
402
|
}
|
|
403
403
|
|
|
404
|
+
const SUBSTRING_TO_FIND = "-precache-";
|
|
405
|
+
const deleteOutdatedCaches = async (currentPrecacheName, substringToFind = SUBSTRING_TO_FIND)=>{
|
|
406
|
+
const cacheNames = await self.caches.keys();
|
|
407
|
+
const cacheNamesToDelete = cacheNames.filter((cacheName)=>{
|
|
408
|
+
return cacheName.includes(substringToFind) && cacheName.includes(self.registration.scope) && cacheName !== currentPrecacheName;
|
|
409
|
+
});
|
|
410
|
+
await Promise.all(cacheNamesToDelete.map((cacheName)=>self.caches.delete(cacheName)));
|
|
411
|
+
return cacheNamesToDelete;
|
|
412
|
+
};
|
|
413
|
+
|
|
414
|
+
const deleteOutdatedPrecaches = (cacheName)=>deleteOutdatedCaches(cacheName).then((cachesDeleted)=>{
|
|
415
|
+
if (process.env.NODE_ENV !== "production") {
|
|
416
|
+
if (cachesDeleted.length > 0) {
|
|
417
|
+
logger.log("The following out-of-date precaches were cleaned up automatically:", cachesDeleted);
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
});
|
|
421
|
+
const cleanupOutdatedCaches = (cacheName)=>{
|
|
422
|
+
self.addEventListener("activate", (event)=>{
|
|
423
|
+
event.waitUntil(deleteOutdatedPrecaches(cacheNames.getPrecacheName(cacheName)));
|
|
424
|
+
});
|
|
425
|
+
};
|
|
426
|
+
|
|
404
427
|
let supportStatus;
|
|
405
428
|
function canConstructResponseFromBodyStream() {
|
|
406
429
|
if (supportStatus === undefined) {
|
|
@@ -453,4 +476,4 @@ const waitUntil = (event, asyncFn)=>{
|
|
|
453
476
|
return returnPromise;
|
|
454
477
|
};
|
|
455
478
|
|
|
456
|
-
export { Deferred as D, SerwistError as S, cacheMatchIgnoreParams as a, canConstructResponseFromBodyStream as b, cacheNames as c,
|
|
479
|
+
export { Deferred as D, SerwistError as S, cacheMatchIgnoreParams as a, canConstructResponseFromBodyStream as b, cacheNames as c, deleteOutdatedPrecaches as d, executeQuotaErrorCallbacks as e, finalAssertExports as f, getFriendlyURL as g, clientsClaim as h, cleanupOutdatedCaches as i, logger as l, quotaErrorCallbacks as q, resultingClientExists as r, timeout as t, waitUntil as w };
|
package/dist/index.internal.js
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
export { D as Deferred, S as SerwistError, f as assert, a as cacheMatchIgnoreParams, b as canConstructResponseFromBodyStream, d as clientsClaim, e as executeQuotaErrorCallbacks, g as getFriendlyURL, r as resultingClientExists, t as timeout, w as waitUntil } from './chunks/waitUntil.js';
|
|
1
|
+
export { D as Deferred, S as SerwistError, f as assert, a as cacheMatchIgnoreParams, b as canConstructResponseFromBodyStream, i as cleanupOutdatedCaches, h as clientsClaim, e as executeQuotaErrorCallbacks, g as getFriendlyURL, l as logger, c as privateCacheNames, r as resultingClientExists, t as timeout, w as waitUntil } from './chunks/waitUntil.js';
|
|
3
2
|
|
|
4
3
|
let supportStatus;
|
|
5
4
|
function canConstructReadableStream() {
|
|
@@ -16,30 +15,8 @@ function canConstructReadableStream() {
|
|
|
16
15
|
return supportStatus;
|
|
17
16
|
}
|
|
18
17
|
|
|
19
|
-
const SUBSTRING_TO_FIND = "-precache-";
|
|
20
|
-
const deleteOutdatedCaches = async (currentPrecacheName, substringToFind = SUBSTRING_TO_FIND)=>{
|
|
21
|
-
const cacheNames = await self.caches.keys();
|
|
22
|
-
const cacheNamesToDelete = cacheNames.filter((cacheName)=>{
|
|
23
|
-
return cacheName.includes(substringToFind) && cacheName.includes(self.registration.scope) && cacheName !== currentPrecacheName;
|
|
24
|
-
});
|
|
25
|
-
await Promise.all(cacheNamesToDelete.map((cacheName)=>self.caches.delete(cacheName)));
|
|
26
|
-
return cacheNamesToDelete;
|
|
27
|
-
};
|
|
28
|
-
|
|
29
|
-
const cleanupOutdatedCaches = (cacheName)=>{
|
|
30
|
-
self.addEventListener("activate", (event)=>{
|
|
31
|
-
event.waitUntil(deleteOutdatedCaches(cacheNames.getPrecacheName(cacheName)).then((cachesDeleted)=>{
|
|
32
|
-
if (process.env.NODE_ENV !== "production") {
|
|
33
|
-
if (cachesDeleted.length > 0) {
|
|
34
|
-
logger.log("The following out-of-date precaches were cleaned up automatically:", cachesDeleted);
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
}));
|
|
38
|
-
});
|
|
39
|
-
};
|
|
40
|
-
|
|
41
18
|
function dontWaitFor(promise) {
|
|
42
19
|
void promise.then(()=>{});
|
|
43
20
|
}
|
|
44
21
|
|
|
45
|
-
export { canConstructReadableStream,
|
|
22
|
+
export { canConstructReadableStream, dontWaitFor };
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { c as cacheNames$1, l as logger, f as finalAssertExports, S as SerwistError, g as getFriendlyURL, D as Deferred, t as timeout, a as cacheMatchIgnoreParams, e as executeQuotaErrorCallbacks, b as canConstructResponseFromBodyStream, q as quotaErrorCallbacks, w as waitUntil,
|
|
1
|
+
import { c as cacheNames$1, l as logger, f as finalAssertExports, S as SerwistError, g as getFriendlyURL, D as Deferred, t as timeout, a as cacheMatchIgnoreParams, e as executeQuotaErrorCallbacks, b as canConstructResponseFromBodyStream, q as quotaErrorCallbacks, d as deleteOutdatedPrecaches, w as waitUntil, h as clientsClaim, r as resultingClientExists } from './chunks/waitUntil.js';
|
|
2
2
|
import { openDB, deleteDB } from 'idb';
|
|
3
3
|
import { parallel } from '@serwist/utils';
|
|
4
4
|
|
|
@@ -2004,10 +2004,10 @@ class Precache {
|
|
|
2004
2004
|
_options;
|
|
2005
2005
|
_routeOptions;
|
|
2006
2006
|
constructor(precacheOptions){
|
|
2007
|
-
const { entries, strategyOptions, routeOptions, extensionOptions
|
|
2007
|
+
const { entries, strategyOptions, routeOptions, extensionOptions } = parsePrecacheOptions(this, precacheOptions);
|
|
2008
2008
|
this.addToCacheList(entries);
|
|
2009
2009
|
this._strategy = new PrecacheStrategy(strategyOptions);
|
|
2010
|
-
this._options =
|
|
2010
|
+
this._options = extensionOptions;
|
|
2011
2011
|
this._routeOptions = routeOptions;
|
|
2012
2012
|
}
|
|
2013
2013
|
get strategy() {
|
|
@@ -2105,6 +2105,9 @@ class Precache {
|
|
|
2105
2105
|
if (process.env.NODE_ENV !== "production") {
|
|
2106
2106
|
printCleanupDetails(deletedCacheRequests);
|
|
2107
2107
|
}
|
|
2108
|
+
if (this._options.cleanupOutdatedCaches) {
|
|
2109
|
+
await deleteOutdatedPrecaches(this._strategy.cacheName);
|
|
2110
|
+
}
|
|
2108
2111
|
}
|
|
2109
2112
|
getUrlsToPrecacheKeys() {
|
|
2110
2113
|
return this._urlsToCacheKeys;
|
package/dist/lib/constants.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export type HTTPMethod = "DELETE" | "GET" | "HEAD" | "PATCH" | "POST" | "PUT";
|
|
1
|
+
export type HTTPMethod = "DELETE" | "GET" | "HEAD" | "PATCH" | "POST" | "PUT" | "OPTIONS";
|
|
2
2
|
/**
|
|
3
3
|
* The default HTTP method, 'GET', used when there's no specific method
|
|
4
4
|
* configured for a route.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../../src/lib/constants.ts"],"names":[],"mappings":"AAQA,MAAM,MAAM,UAAU,GAAG,QAAQ,GAAG,KAAK,GAAG,MAAM,GAAG,OAAO,GAAG,MAAM,GAAG,KAAK,CAAC;
|
|
1
|
+
{"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../../src/lib/constants.ts"],"names":[],"mappings":"AAQA,MAAM,MAAM,UAAU,GAAG,QAAQ,GAAG,KAAK,GAAG,MAAM,GAAG,OAAO,GAAG,MAAM,GAAG,KAAK,GAAG,SAAS,CAAC;AAE1F;;;;;GAKG;AACH,eAAO,MAAM,aAAa,QAA6B,CAAC;AAExD;;;;GAIG;AACH,eAAO,MAAM,YAAY,0DAA2E,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"extension.d.ts","sourceRoot":"","sources":["../../../../src/lib/extensions/precache/extension.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAC5C,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAC;
|
|
1
|
+
{"version":3,"file":"extension.d.ts","sourceRoot":"","sources":["../../../../src/lib/extensions/precache/extension.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAC5C,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAC;AAUnD,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,8BAA8B,CAAC;AAC7D,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,gBAAgB,CAAC;AAG3D,OAAO,EAAiB,KAAK,oBAAoB,EAAE,MAAM,YAAY,CAAC;AACtE,OAAO,KAAK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AAE,GAAG,IAAI;IAcvC,OAAO,CAAC,EAAE,KAAK,EAAE,EAAE;QAAE,KAAK,EAAE,eAAe,CAAA;KAAE,GAAG,OAAO,CAAC,IAAI,CAAC;IA8B7D,QAAQ,IAAI,OAAO,CAAC,IAAI,CAAC;IA0B/B;;;;;OAKG;IACH,qBAAqB,IAAI,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC;IAI5C;;;;;OAKG;IACH,gBAAgB,IAAI,MAAM,EAAE;IAI5B;;;;;;;;OAQG;IACH,oBAAoB,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS;IAKrD;;;;OAIG;IACH,0BAA0B,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS;IAIhE;;;;;;;;;;;;;;;;;OAiBG;IACG,aAAa,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,GAAG,OAAO,CAAC,QAAQ,GAAG,SAAS,CAAC;IAU7E;;;;;;OAMG;IACH,uBAAuB,CAAC,GAAG,EAAE,MAAM,GAAG,oBAAoB;CAY3D"}
|
|
@@ -4,7 +4,7 @@ export declare const parsePrecacheOptions: (controller: Precache, { entries, cac
|
|
|
4
4
|
entries: (string | import("./extension.js").PrecacheEntry)[];
|
|
5
5
|
strategyOptions: {
|
|
6
6
|
cacheName: string;
|
|
7
|
-
plugins: (import("../../types.js").
|
|
7
|
+
plugins: (import("../../types.js").SerwistPlugin | PrecacheCacheKeyPlugin)[];
|
|
8
8
|
fetchOptions: RequestInit | undefined;
|
|
9
9
|
matchOptions: CacheQueryOptions | undefined;
|
|
10
10
|
fallbackToNetwork: boolean | undefined;
|
package/dist/lib/types.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { MaybePromise, Prettify } from "@serwist/utils";
|
|
2
2
|
import type { PrecacheEntry, PrecacheExtensionOptions, PrecacheOptions, PrecacheRouteOptions } from "#lib/extensions/index.js";
|
|
3
3
|
import type { HTTPMethod } from "./constants.js";
|
|
4
|
-
export type { MaybePromise as PromiseOrNot };
|
|
4
|
+
export type { MaybePromise as PromiseOrNot, StrategyPlugin as SerwistPlugin, StrategyPluginCallbackParam as SerwistPluginCallbackParam, PrecacheEntry, PrecacheRouteOptions, PrecacheExtensionOptions, PrecacheOptions, };
|
|
5
5
|
export interface MapLikeObject {
|
|
6
6
|
[key: string]: any;
|
|
7
7
|
}
|
|
@@ -291,5 +291,96 @@ export interface CleanupResult {
|
|
|
291
291
|
export type UrlManipulation = ({ url }: {
|
|
292
292
|
url: URL;
|
|
293
293
|
}) => URL[];
|
|
294
|
-
|
|
294
|
+
/**
|
|
295
|
+
* Represents the condition object that defines which resources should match a rule.
|
|
296
|
+
* Based on the MDN documentation for `InstallEvent.addRoutes()`.
|
|
297
|
+
*/
|
|
298
|
+
export interface RequestRuleCondition {
|
|
299
|
+
/**
|
|
300
|
+
* A condition object defining conditions that must explicitly NOT be met to match the rule.
|
|
301
|
+
* Conditions defined inside a `not` condition are mutually exclusive with other conditions.
|
|
302
|
+
*/
|
|
303
|
+
not?: RequestRuleCondition;
|
|
304
|
+
/**
|
|
305
|
+
* An array of condition objects. One set of these defined conditions must be met to match the rule.
|
|
306
|
+
* Conditions defined inside an `or` condition are mutually exclusive with other conditions.
|
|
307
|
+
* Cannot be combined with other condition types.
|
|
308
|
+
*/
|
|
309
|
+
or?: RequestRuleCondition[];
|
|
310
|
+
/**
|
|
311
|
+
* A string representing the HTTP method a request should be sent by for it to match the rule.
|
|
312
|
+
*
|
|
313
|
+
* @example "GET", "POST", "PUT", "DELETE", "HEAD", "OPTIONS", "PATCH"
|
|
314
|
+
*/
|
|
315
|
+
requestMethod?: HTTPMethod;
|
|
316
|
+
/**
|
|
317
|
+
* A string representing the mode a request should have for it to match the rule.
|
|
318
|
+
*
|
|
319
|
+
* @example "same-origin", "no-cors", "cors"
|
|
320
|
+
*/
|
|
321
|
+
requestMode?: RequestMode;
|
|
322
|
+
/**
|
|
323
|
+
* A string representing the destination of a request, i.e., what content type should be requested.
|
|
324
|
+
*
|
|
325
|
+
* @example "audio", "document", "script", "worker"
|
|
326
|
+
*/
|
|
327
|
+
requestDestination?: RequestDestination;
|
|
328
|
+
/**
|
|
329
|
+
* An enumerated value representing the required running status of the service worker for a request to match the rule.
|
|
330
|
+
*/
|
|
331
|
+
runningStatus?: "running" | "not-running";
|
|
332
|
+
/**
|
|
333
|
+
* A URLPattern instance, or a URLPattern constructor input pattern representing the URLs that match the rule.
|
|
334
|
+
* Regular expression capturing groups are not allowed, so `URLPattern.hasRegExpGroups` must be `false`.
|
|
335
|
+
*/
|
|
336
|
+
urlPattern?: URLPattern | URLPatternInit;
|
|
337
|
+
}
|
|
338
|
+
/**
|
|
339
|
+
* Represents the source from which matching resources will be loaded.
|
|
340
|
+
* Can be an enumerated value or an object specifying a named cache.
|
|
341
|
+
*/
|
|
342
|
+
export type RequestRuleSource = "cache" | "fetch-event" | "network" | "race-network-and-fetch-handler" | {
|
|
343
|
+
cacheName: string;
|
|
344
|
+
};
|
|
345
|
+
/**
|
|
346
|
+
* Represents a single router rule configuration.
|
|
347
|
+
* Each rule contains a condition (optional) and a source (required).
|
|
348
|
+
*/
|
|
349
|
+
export interface RequestRule {
|
|
350
|
+
/**
|
|
351
|
+
* An object defining one or more conditions that specify which resources should match this rule.
|
|
352
|
+
* If multiple properties are used, a resource must meet all specified conditions to match the rule.
|
|
353
|
+
*/
|
|
354
|
+
condition?: RequestRuleCondition;
|
|
355
|
+
/**
|
|
356
|
+
* An enumerated value or an object specifying the source from which matching resources will be loaded.
|
|
357
|
+
*/
|
|
358
|
+
source: RequestRuleSource;
|
|
359
|
+
}
|
|
360
|
+
/**
|
|
361
|
+
* Types based on MDN documentation
|
|
362
|
+
* https://developer.mozilla.org/en-US/docs/Web/API/InstallEvent/addRoutes
|
|
363
|
+
*
|
|
364
|
+
* Extends the standard `ExtendableEvent` interface to include the `addRoutes` method
|
|
365
|
+
* for configuring static routes during the service worker's installation.
|
|
366
|
+
*
|
|
367
|
+
* The `addRoutes()` method specifies one or more static routes, which define rules
|
|
368
|
+
* for fetching specified resources that will be used even before service worker startup.
|
|
369
|
+
* This allows bypassing a service worker in cases where you always want to fetch
|
|
370
|
+
* a resource from the network or a browser cache, avoiding the performance overhead
|
|
371
|
+
* of unnecessary service worker cycles.
|
|
372
|
+
*/
|
|
373
|
+
export interface InstallEvent extends ExtendableEvent {
|
|
374
|
+
/**
|
|
375
|
+
* Specifies one or more static routes for fetching resources.
|
|
376
|
+
*
|
|
377
|
+
* @param requestRules A single object, or an array of one or more objects, representing rules
|
|
378
|
+
* for how certain resources should be fetched.
|
|
379
|
+
* @returns A Promise that fulfills with `undefined`.
|
|
380
|
+
* @throws {TypeError} Thrown if one or more of the rules objects is invalid, or has a source
|
|
381
|
+
* value of `"fetch-event"` when the associated service worker does not have
|
|
382
|
+
* a `fetch` event handler. Also thrown if you try to combine `or` with another condition type.
|
|
383
|
+
*/
|
|
384
|
+
addRoutes?(requestRules: RequestRule | RequestRule[]): Promise<void>;
|
|
385
|
+
}
|
|
295
386
|
//# sourceMappingURL=types.d.ts.map
|
package/dist/lib/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/lib/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAC7D,OAAO,KAAK,EAAE,aAAa,EAAE,wBAAwB,EAAE,eAAe,EAAE,oBAAoB,EAAE,MAAM,0BAA0B,CAAC;AAC/H,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAC;AAIjD,YAAY,
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/lib/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAC7D,OAAO,KAAK,EAAE,aAAa,EAAE,wBAAwB,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AAW,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;CACtE"}
|
|
@@ -1,3 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cleans up incompatible precaches that were created by older versions
|
|
3
|
+
* of Serwist.
|
|
4
|
+
* @param cacheName
|
|
5
|
+
* @returns
|
|
6
|
+
*/
|
|
7
|
+
export declare const deleteOutdatedPrecaches: (cacheName: string) => Promise<void>;
|
|
1
8
|
/**
|
|
2
9
|
* Adds an `activate` event listener which will clean up incompatible
|
|
3
10
|
* precaches that were created by older versions of Serwist.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cleanupOutdatedCaches.d.ts","sourceRoot":"","sources":["../../src/utils/cleanupOutdatedCaches.ts"],"names":[],"mappings":"AAcA;;;GAGG;AACH,eAAO,MAAM,qBAAqB,GAAI,YAAY,MAAM,KAAG,
|
|
1
|
+
{"version":3,"file":"cleanupOutdatedCaches.d.ts","sourceRoot":"","sources":["../../src/utils/cleanupOutdatedCaches.ts"],"names":[],"mappings":"AAcA;;;;;GAKG;AACH,eAAO,MAAM,uBAAuB,GAAI,WAAW,MAAM,kBAOrD,CAAC;AAEL;;;GAGG;AACH,eAAO,MAAM,qBAAqB,GAAI,YAAY,MAAM,KAAG,IAI1D,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "serwist",
|
|
3
|
-
"version": "10.0.0-preview.
|
|
3
|
+
"version": "10.0.0-preview.14",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"sideEffects": false,
|
|
6
6
|
"description": "A Swiss Army knife for service workers.",
|
|
@@ -66,12 +66,12 @@
|
|
|
66
66
|
},
|
|
67
67
|
"dependencies": {
|
|
68
68
|
"idb": "8.0.3",
|
|
69
|
-
"@serwist/utils": "10.0.0-preview.
|
|
69
|
+
"@serwist/utils": "10.0.0-preview.14"
|
|
70
70
|
},
|
|
71
71
|
"devDependencies": {
|
|
72
|
-
"rollup": "4.
|
|
72
|
+
"rollup": "4.49.0",
|
|
73
73
|
"typescript": "5.9.2",
|
|
74
|
-
"@serwist/configs": "10.0.0-preview.
|
|
74
|
+
"@serwist/configs": "10.0.0-preview.14"
|
|
75
75
|
},
|
|
76
76
|
"peerDependencies": {
|
|
77
77
|
"typescript": ">=5.0.0"
|
package/src/lib/constants.ts
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
https://opensource.org/licenses/MIT.
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
|
-
export type HTTPMethod = "DELETE" | "GET" | "HEAD" | "PATCH" | "POST" | "PUT";
|
|
9
|
+
export type HTTPMethod = "DELETE" | "GET" | "HEAD" | "PATCH" | "POST" | "PUT" | "OPTIONS";
|
|
10
10
|
|
|
11
11
|
/**
|
|
12
12
|
* The default HTTP method, 'GET', used when there's no specific method
|
|
@@ -3,6 +3,7 @@ import type { Serwist } from "#lib/core.js";
|
|
|
3
3
|
import type { Extension } from "#lib/extension.js";
|
|
4
4
|
import { registerRoute } from "#lib/functions/router.js";
|
|
5
5
|
import { assert } from "#utils/assert.js";
|
|
6
|
+
import { deleteOutdatedPrecaches } from "#utils/cleanupOutdatedCaches.js";
|
|
6
7
|
import { createCacheKey } from "#utils/createCacheKey.js";
|
|
7
8
|
import { logger } from "#utils/logger.js";
|
|
8
9
|
import { printCleanupDetails } from "#utils/printCleanupDetails.js";
|
|
@@ -74,10 +75,10 @@ export class Precache implements Extension {
|
|
|
74
75
|
* @param options
|
|
75
76
|
*/
|
|
76
77
|
constructor(precacheOptions: PrecacheOptions) {
|
|
77
|
-
const { entries, strategyOptions, routeOptions, extensionOptions
|
|
78
|
+
const { entries, strategyOptions, routeOptions, extensionOptions } = parsePrecacheOptions(this, precacheOptions);
|
|
78
79
|
this.addToCacheList(entries);
|
|
79
80
|
this._strategy = new PrecacheStrategy(strategyOptions);
|
|
80
|
-
this._options =
|
|
81
|
+
this._options = extensionOptions;
|
|
81
82
|
this._routeOptions = routeOptions;
|
|
82
83
|
}
|
|
83
84
|
|
|
@@ -211,6 +212,13 @@ export class Precache implements Extension {
|
|
|
211
212
|
if (process.env.NODE_ENV !== "production") {
|
|
212
213
|
printCleanupDetails(deletedCacheRequests);
|
|
213
214
|
}
|
|
215
|
+
|
|
216
|
+
// This deletes caches whose keys contain `-precache-` but
|
|
217
|
+
// are different from `this._strategy.cacheName`. Functionally
|
|
218
|
+
// equivalent to `cleanupOutdatedCaches`.
|
|
219
|
+
if (this._options.cleanupOutdatedCaches) {
|
|
220
|
+
await deleteOutdatedPrecaches(this._strategy.cacheName);
|
|
221
|
+
}
|
|
214
222
|
}
|
|
215
223
|
|
|
216
224
|
/**
|
package/src/lib/types.ts
CHANGED
|
@@ -4,7 +4,15 @@ import type { HTTPMethod } from "./constants.js";
|
|
|
4
4
|
import type { Route } from "./route.js";
|
|
5
5
|
import type { Serwist } from "./serwist.js";
|
|
6
6
|
|
|
7
|
-
export type {
|
|
7
|
+
export type {
|
|
8
|
+
MaybePromise as PromiseOrNot,
|
|
9
|
+
StrategyPlugin as SerwistPlugin,
|
|
10
|
+
StrategyPluginCallbackParam as SerwistPluginCallbackParam,
|
|
11
|
+
PrecacheEntry,
|
|
12
|
+
PrecacheRouteOptions,
|
|
13
|
+
PrecacheExtensionOptions,
|
|
14
|
+
PrecacheOptions,
|
|
15
|
+
};
|
|
8
16
|
|
|
9
17
|
export interface MapLikeObject {
|
|
10
18
|
[key: string]: any;
|
|
@@ -334,11 +342,103 @@ export interface CleanupResult {
|
|
|
334
342
|
|
|
335
343
|
export type UrlManipulation = ({ url }: { url: URL }) => URL[];
|
|
336
344
|
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
+
/**
|
|
346
|
+
* Represents the condition object that defines which resources should match a rule.
|
|
347
|
+
* Based on the MDN documentation for `InstallEvent.addRoutes()`.
|
|
348
|
+
*/
|
|
349
|
+
export interface RequestRuleCondition {
|
|
350
|
+
/**
|
|
351
|
+
* A condition object defining conditions that must explicitly NOT be met to match the rule.
|
|
352
|
+
* Conditions defined inside a `not` condition are mutually exclusive with other conditions.
|
|
353
|
+
*/
|
|
354
|
+
not?: RequestRuleCondition;
|
|
355
|
+
|
|
356
|
+
/**
|
|
357
|
+
* An array of condition objects. One set of these defined conditions must be met to match the rule.
|
|
358
|
+
* Conditions defined inside an `or` condition are mutually exclusive with other conditions.
|
|
359
|
+
* Cannot be combined with other condition types.
|
|
360
|
+
*/
|
|
361
|
+
or?: RequestRuleCondition[];
|
|
362
|
+
|
|
363
|
+
/**
|
|
364
|
+
* A string representing the HTTP method a request should be sent by for it to match the rule.
|
|
365
|
+
*
|
|
366
|
+
* @example "GET", "POST", "PUT", "DELETE", "HEAD", "OPTIONS", "PATCH"
|
|
367
|
+
*/
|
|
368
|
+
requestMethod?: HTTPMethod;
|
|
369
|
+
|
|
370
|
+
/**
|
|
371
|
+
* A string representing the mode a request should have for it to match the rule.
|
|
372
|
+
*
|
|
373
|
+
* @example "same-origin", "no-cors", "cors"
|
|
374
|
+
*/
|
|
375
|
+
requestMode?: RequestMode;
|
|
376
|
+
|
|
377
|
+
/**
|
|
378
|
+
* A string representing the destination of a request, i.e., what content type should be requested.
|
|
379
|
+
*
|
|
380
|
+
* @example "audio", "document", "script", "worker"
|
|
381
|
+
*/
|
|
382
|
+
requestDestination?: RequestDestination;
|
|
383
|
+
|
|
384
|
+
/**
|
|
385
|
+
* An enumerated value representing the required running status of the service worker for a request to match the rule.
|
|
386
|
+
*/
|
|
387
|
+
runningStatus?: "running" | "not-running";
|
|
388
|
+
|
|
389
|
+
/**
|
|
390
|
+
* A URLPattern instance, or a URLPattern constructor input pattern representing the URLs that match the rule.
|
|
391
|
+
* Regular expression capturing groups are not allowed, so `URLPattern.hasRegExpGroups` must be `false`.
|
|
392
|
+
*/
|
|
393
|
+
urlPattern?: URLPattern | URLPatternInit;
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
/**
|
|
397
|
+
* Represents the source from which matching resources will be loaded.
|
|
398
|
+
* Can be an enumerated value or an object specifying a named cache.
|
|
399
|
+
*/
|
|
400
|
+
export type RequestRuleSource = "cache" | "fetch-event" | "network" | "race-network-and-fetch-handler" | { cacheName: string };
|
|
401
|
+
|
|
402
|
+
/**
|
|
403
|
+
* Represents a single router rule configuration.
|
|
404
|
+
* Each rule contains a condition (optional) and a source (required).
|
|
405
|
+
*/
|
|
406
|
+
export interface RequestRule {
|
|
407
|
+
/**
|
|
408
|
+
* An object defining one or more conditions that specify which resources should match this rule.
|
|
409
|
+
* If multiple properties are used, a resource must meet all specified conditions to match the rule.
|
|
410
|
+
*/
|
|
411
|
+
condition?: RequestRuleCondition;
|
|
412
|
+
|
|
413
|
+
/**
|
|
414
|
+
* An enumerated value or an object specifying the source from which matching resources will be loaded.
|
|
415
|
+
*/
|
|
416
|
+
source: RequestRuleSource;
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
/**
|
|
420
|
+
* Types based on MDN documentation
|
|
421
|
+
* https://developer.mozilla.org/en-US/docs/Web/API/InstallEvent/addRoutes
|
|
422
|
+
*
|
|
423
|
+
* Extends the standard `ExtendableEvent` interface to include the `addRoutes` method
|
|
424
|
+
* for configuring static routes during the service worker's installation.
|
|
425
|
+
*
|
|
426
|
+
* The `addRoutes()` method specifies one or more static routes, which define rules
|
|
427
|
+
* for fetching specified resources that will be used even before service worker startup.
|
|
428
|
+
* This allows bypassing a service worker in cases where you always want to fetch
|
|
429
|
+
* a resource from the network or a browser cache, avoiding the performance overhead
|
|
430
|
+
* of unnecessary service worker cycles.
|
|
431
|
+
*/
|
|
432
|
+
export interface InstallEvent extends ExtendableEvent {
|
|
433
|
+
/**
|
|
434
|
+
* Specifies one or more static routes for fetching resources.
|
|
435
|
+
*
|
|
436
|
+
* @param requestRules A single object, or an array of one or more objects, representing rules
|
|
437
|
+
* for how certain resources should be fetched.
|
|
438
|
+
* @returns A Promise that fulfills with `undefined`.
|
|
439
|
+
* @throws {TypeError} Thrown if one or more of the rules objects is invalid, or has a source
|
|
440
|
+
* value of `"fetch-event"` when the associated service worker does not have
|
|
441
|
+
* a `fetch` event handler. Also thrown if you try to combine `or` with another condition type.
|
|
442
|
+
*/
|
|
443
|
+
addRoutes?(requestRules: RequestRule | RequestRule[]): Promise<void>;
|
|
444
|
+
}
|
|
@@ -12,20 +12,27 @@ import { logger } from "./logger.js";
|
|
|
12
12
|
|
|
13
13
|
declare const self: ServiceWorkerGlobalScope;
|
|
14
14
|
|
|
15
|
+
/**
|
|
16
|
+
* Cleans up incompatible precaches that were created by older versions
|
|
17
|
+
* of Serwist.
|
|
18
|
+
* @param cacheName
|
|
19
|
+
* @returns
|
|
20
|
+
*/
|
|
21
|
+
export const deleteOutdatedPrecaches = (cacheName: string) =>
|
|
22
|
+
deleteOutdatedCaches(cacheName).then((cachesDeleted) => {
|
|
23
|
+
if (process.env.NODE_ENV !== "production") {
|
|
24
|
+
if (cachesDeleted.length > 0) {
|
|
25
|
+
logger.log("The following out-of-date precaches were cleaned up automatically:", cachesDeleted);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
});
|
|
29
|
+
|
|
15
30
|
/**
|
|
16
31
|
* Adds an `activate` event listener which will clean up incompatible
|
|
17
32
|
* precaches that were created by older versions of Serwist.
|
|
18
33
|
*/
|
|
19
34
|
export const cleanupOutdatedCaches = (cacheName?: string): void => {
|
|
20
35
|
self.addEventListener("activate", (event: ExtendableEvent) => {
|
|
21
|
-
event.waitUntil(
|
|
22
|
-
deleteOutdatedCaches(privateCacheNames.getPrecacheName(cacheName)).then((cachesDeleted) => {
|
|
23
|
-
if (process.env.NODE_ENV !== "production") {
|
|
24
|
-
if (cachesDeleted.length > 0) {
|
|
25
|
-
logger.log("The following out-of-date precaches were cleaned up automatically:", cachesDeleted);
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
}),
|
|
29
|
-
);
|
|
36
|
+
event.waitUntil(deleteOutdatedPrecaches(privateCacheNames.getPrecacheName(cacheName)));
|
|
30
37
|
});
|
|
31
38
|
};
|