@serwist/sw 9.0.0-preview.16 → 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.
- package/dist/Serwist.d.ts +93 -0
- package/dist/Serwist.d.ts.map +1 -0
- package/dist/handlePrecaching.d.ts +4 -7
- package/dist/handlePrecaching.d.ts.map +1 -1
- package/dist/index.d.ts +3 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +76 -51
- package/dist/installSerwist.d.ts +10 -63
- package/dist/installSerwist.d.ts.map +1 -1
- package/dist/registerRuntimeCaching.d.ts +1 -1
- package/package.json +14 -14
- package/src/Serwist.ts +182 -0
- package/src/handlePrecaching.ts +17 -21
- package/src/index.ts +12 -2
- package/src/installSerwist.ts +19 -133
- package/src/registerRuntimeCaching.ts +1 -1
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { type GoogleAnalyticsInitializeOptions } from "@serwist/google-analytics/initialize";
|
|
2
|
+
import { PrecacheController } from "@serwist/precaching";
|
|
3
|
+
import { Router } from "@serwist/routing";
|
|
4
|
+
import type { FallbacksOptions } from "./fallbacks.js";
|
|
5
|
+
import { type HandlePrecachingOptions } from "./handlePrecaching.js";
|
|
6
|
+
import type { RuntimeCaching } from "./types.js";
|
|
7
|
+
export interface SerwistOptions {
|
|
8
|
+
/**
|
|
9
|
+
* The precache controller that will be used to perform efficient
|
|
10
|
+
* precaching of assets.
|
|
11
|
+
* @default
|
|
12
|
+
* ```js
|
|
13
|
+
* new PrecacheController()
|
|
14
|
+
* ```
|
|
15
|
+
* @see https://serwist.pages.dev/docs/sw/precache-controller
|
|
16
|
+
*/
|
|
17
|
+
precacheController?: PrecacheController;
|
|
18
|
+
/**
|
|
19
|
+
* The router that will be used to process a `FetchEvent`, responding
|
|
20
|
+
* with a `Response` if a matching route exists.
|
|
21
|
+
*
|
|
22
|
+
* @default
|
|
23
|
+
* ```js
|
|
24
|
+
* new Router()
|
|
25
|
+
* ```
|
|
26
|
+
* @see https://serwist.pages.dev/docs/sw/router
|
|
27
|
+
*/
|
|
28
|
+
router?: Router;
|
|
29
|
+
}
|
|
30
|
+
export interface SerwistInstallOptions extends HandlePrecachingOptions {
|
|
31
|
+
/**
|
|
32
|
+
* Forces the waiting service worker to become the active one.
|
|
33
|
+
*
|
|
34
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerGlobalScope/skipWaiting
|
|
35
|
+
*/
|
|
36
|
+
skipWaiting?: boolean;
|
|
37
|
+
/**
|
|
38
|
+
* Imports external scripts. They are executed in the order they
|
|
39
|
+
* are passed.
|
|
40
|
+
*
|
|
41
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/API/WorkerGlobalScope/importScripts
|
|
42
|
+
*/
|
|
43
|
+
importScripts?: string[];
|
|
44
|
+
/**
|
|
45
|
+
* Enables navigation preloading if it is supported.
|
|
46
|
+
*
|
|
47
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerRegistration/navigationPreload
|
|
48
|
+
*/
|
|
49
|
+
navigationPreload?: boolean;
|
|
50
|
+
/**
|
|
51
|
+
* Modifies the prefix of the default cache names used by Serwist packages.
|
|
52
|
+
*/
|
|
53
|
+
cacheId?: string | undefined;
|
|
54
|
+
/**
|
|
55
|
+
* Claims any currently available clients once the service worker
|
|
56
|
+
* becomes active. This is normally used in conjunction with `skipWaiting()`.
|
|
57
|
+
*
|
|
58
|
+
* @default false
|
|
59
|
+
*/
|
|
60
|
+
clientsClaim?: boolean;
|
|
61
|
+
/**
|
|
62
|
+
* A list of caching strategies.
|
|
63
|
+
*
|
|
64
|
+
* @see https://serwist.pages.dev/docs/sw/runtime-caching
|
|
65
|
+
*/
|
|
66
|
+
runtimeCaching?: RuntimeCaching[];
|
|
67
|
+
/**
|
|
68
|
+
* Your configuration for `@serwist/google-analytics`. This plugin is
|
|
69
|
+
* only initialized when this option is not `undefined` or `false`.
|
|
70
|
+
*/
|
|
71
|
+
offlineAnalyticsConfig?: GoogleAnalyticsInitializeOptions | boolean;
|
|
72
|
+
/**
|
|
73
|
+
* Disables Serwist's logging in development mode.
|
|
74
|
+
*
|
|
75
|
+
* @default falseOmit<SerwistOptions, "precacheController">
|
|
76
|
+
*/
|
|
77
|
+
disableDevLogs?: boolean;
|
|
78
|
+
/**
|
|
79
|
+
* Precaches routes so that they can be used as a fallback when
|
|
80
|
+
* a Strategy fails to generate a response.
|
|
81
|
+
* Note: This option mutates `runtimeCaching`!
|
|
82
|
+
*
|
|
83
|
+
* @see https://serwist.pages.dev/docs/sw/fallbacks
|
|
84
|
+
*/
|
|
85
|
+
fallbacks?: Omit<FallbacksOptions, "runtimeCaching">;
|
|
86
|
+
}
|
|
87
|
+
export declare class Serwist {
|
|
88
|
+
private _precacheController;
|
|
89
|
+
private _router;
|
|
90
|
+
constructor({ precacheController, router }?: SerwistOptions);
|
|
91
|
+
install({ precacheEntries, precacheOptions, cleanupOutdatedCaches, navigateFallback, navigateFallbackAllowlist, navigateFallbackDenylist, skipWaiting, importScripts, navigationPreload, cacheId, clientsClaim, runtimeCaching, offlineAnalyticsConfig, disableDevLogs, fallbacks, }: SerwistInstallOptions): void;
|
|
92
|
+
}
|
|
93
|
+
//# sourceMappingURL=Serwist.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Serwist.d.ts","sourceRoot":"","sources":["../src/Serwist.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,gCAAgC,EAAc,MAAM,sCAAsC,CAAC;AAEzG,OAAO,EAAE,kBAAkB,EAA8F,MAAM,qBAAqB,CAAC;AACrJ,OAAO,EAAmB,MAAM,EAAE,MAAM,kBAAkB,CAAC;AAI3D,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AACvD,OAAO,EAAE,KAAK,uBAAuB,EAAE,MAAM,uBAAuB,CAAC;AACrE,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAIjD,MAAM,WAAW,cAAc;IAC7B;;;;;;;;OAQG;IACH,kBAAkB,CAAC,EAAE,kBAAkB,CAAC;IACxC;;;;;;;;;OASG;IACH,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB;AAED,MAAM,WAAW,qBAAsB,SAAQ,uBAAuB;IACpE;;;;OAIG;IACH,WAAW,CAAC,EAAE,OAAO,CAAC;IACtB;;;;;OAKG;IACH,aAAa,CAAC,EAAE,MAAM,EAAE,CAAC;IACzB;;;;OAIG;IACH,iBAAiB,CAAC,EAAE,OAAO,CAAC;IAC5B;;OAEG;IACH,OAAO,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IAC7B;;;;;OAKG;IACH,YAAY,CAAC,EAAE,OAAO,CAAC;IACvB;;;;OAIG;IACH,cAAc,CAAC,EAAE,cAAc,EAAE,CAAC;IAClC;;;OAGG;IACH,sBAAsB,CAAC,EAAE,gCAAgC,GAAG,OAAO,CAAC;IACpE;;;;OAIG;IACH,cAAc,CAAC,EAAE,OAAO,CAAC;IACzB;;;;;;OAMG;IACH,SAAS,CAAC,EAAE,IAAI,CAAC,gBAAgB,EAAE,gBAAgB,CAAC,CAAC;CACtD;AAED,qBAAa,OAAO;IAClB,OAAO,CAAC,mBAAmB,CAAqB;IAChD,OAAO,CAAC,OAAO,CAAS;gBACZ,EAAE,kBAA6C,EAAE,MAAqB,EAAE,GAAE,cAAmB;IAIzG,OAAO,CAAC,EACN,eAAe,EACf,eAAe,EACf,qBAAqB,EACrB,gBAAgB,EAChB,yBAAyB,EACzB,wBAAwB,EACxB,WAAmB,EACnB,aAAa,EACb,iBAAyB,EACzB,OAAO,EACP,YAAoB,EACpB,cAAc,EACd,sBAAsB,EACtB,cAAsB,EACtB,SAAS,GACV,EAAE,qBAAqB;CA8DzB"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { PrecacheEntry, PrecacheRouteOptions } from "@serwist/precaching";
|
|
2
|
-
export
|
|
2
|
+
export interface HandlePrecachingOptions {
|
|
3
3
|
/**
|
|
4
4
|
* A list of fallback entries.
|
|
5
5
|
*/
|
|
@@ -14,12 +14,11 @@ export type HandlePrecachingOptions = {
|
|
|
14
14
|
* @default false
|
|
15
15
|
*/
|
|
16
16
|
cleanupOutdatedCaches?: boolean;
|
|
17
|
-
} & ({
|
|
18
17
|
/**
|
|
19
18
|
* An URL that should point to a HTML file with which navigation requests for URLs that aren't
|
|
20
19
|
* precached will be fulfilled.
|
|
21
20
|
*/
|
|
22
|
-
navigateFallback
|
|
21
|
+
navigateFallback?: string;
|
|
23
22
|
/**
|
|
24
23
|
* URLs that should be allowed to use the `navigateFallback` handler.
|
|
25
24
|
*/
|
|
@@ -29,13 +28,11 @@ export type HandlePrecachingOptions = {
|
|
|
29
28
|
* over `navigateFallbackAllowlist`.
|
|
30
29
|
*/
|
|
31
30
|
navigateFallbackDenylist?: RegExp[];
|
|
32
|
-
}
|
|
33
|
-
navigateFallback?: never;
|
|
34
|
-
});
|
|
31
|
+
}
|
|
35
32
|
/**
|
|
36
33
|
* Handles a list of precache entries and cleans up outdated caches.
|
|
37
34
|
*
|
|
38
|
-
* @see https://serwist.pages.dev/docs/sw/
|
|
35
|
+
* @see https://serwist.pages.dev/docs/sw/handle-precaching
|
|
39
36
|
* @param options
|
|
40
37
|
*/
|
|
41
38
|
export declare const handlePrecaching: ({ precacheEntries, precacheOptions, cleanupOutdatedCaches, ...options }: HandlePrecachingOptions) => void;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"handlePrecaching.d.ts","sourceRoot":"","sources":["../src/handlePrecaching.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,oBAAoB,EAAE,MAAM,qBAAqB,CAAC;AAI/E,MAAM,
|
|
1
|
+
{"version":3,"file":"handlePrecaching.d.ts","sourceRoot":"","sources":["../src/handlePrecaching.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,oBAAoB,EAAE,MAAM,qBAAqB,CAAC;AAI/E,MAAM,WAAW,uBAAuB;IACtC;;OAEG;IACH,eAAe,CAAC,EAAE,CAAC,aAAa,GAAG,MAAM,CAAC,EAAE,CAAC;IAC7C;;OAEG;IACH,eAAe,CAAC,EAAE,oBAAoB,CAAC;IACvC;;;;OAIG;IACH,qBAAqB,CAAC,EAAE,OAAO,CAAC;IAChC;;;OAGG;IACH,gBAAgB,CAAC,EAAE,MAAM,CAAC;IAC1B;;OAEG;IACH,yBAAyB,CAAC,EAAE,MAAM,EAAE,CAAC;IACrC;;;OAGG;IACH,wBAAwB,CAAC,EAAE,MAAM,EAAE,CAAC;CACrC;AAED;;;;;GAKG;AACH,eAAO,MAAM,gBAAgB,4EAAqF,uBAAuB,KAAG,IAoB3I,CAAC"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { Serwist, type SerwistOptions, type SerwistInstallOptions } from "./Serwist.js";
|
|
1
2
|
import { disableDevLogs } from "./disableDevLogs.js";
|
|
2
3
|
import type { FallbackEntry, FallbackMatcher, FallbacksOptions } from "./fallbacks.js";
|
|
3
4
|
import { fallbacks } from "./fallbacks.js";
|
|
@@ -5,6 +6,6 @@ import { type HandlePrecachingOptions, handlePrecaching } from "./handlePrecachi
|
|
|
5
6
|
import { type InstallSerwistOptions, installSerwist } from "./installSerwist.js";
|
|
6
7
|
import { registerRuntimeCaching } from "./registerRuntimeCaching.js";
|
|
7
8
|
import type { RuntimeCaching } from "./types.js";
|
|
8
|
-
export { disableDevLogs, fallbacks, handlePrecaching, installSerwist, registerRuntimeCaching };
|
|
9
|
-
export type { FallbackEntry, FallbackMatcher, FallbacksOptions, HandlePrecachingOptions, InstallSerwistOptions
|
|
9
|
+
export { disableDevLogs, fallbacks, handlePrecaching, installSerwist, Serwist, registerRuntimeCaching };
|
|
10
|
+
export type { FallbackEntry, FallbackMatcher, FallbacksOptions, HandlePrecachingOptions, InstallSerwistOptions, SerwistOptions, SerwistInstallOptions, RuntimeCaching, };
|
|
10
11
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AACrD,OAAO,KAAK,EAAE,aAAa,EAAE,eAAe,EAAE,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AACvF,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAC3C,OAAO,EAAE,KAAK,uBAAuB,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AACvF,OAAO,EAAE,KAAK,qBAAqB,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AACjF,OAAO,EAAE,sBAAsB,EAAE,MAAM,6BAA6B,CAAC;AACrE,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAEjD,OAAO,EAAE,cAAc,EAAE,SAAS,EAAE,gBAAgB,EAAE,cAAc,EAAE,sBAAsB,EAAE,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,KAAK,cAAc,EAAE,KAAK,qBAAqB,EAAE,MAAM,cAAc,CAAC;AACxF,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AACrD,OAAO,KAAK,EAAE,aAAa,EAAE,eAAe,EAAE,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AACvF,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAC3C,OAAO,EAAE,KAAK,uBAAuB,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AACvF,OAAO,EAAE,KAAK,qBAAqB,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AACjF,OAAO,EAAE,sBAAsB,EAAE,MAAM,6BAA6B,CAAC;AACrE,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAEjD,OAAO,EAAE,cAAc,EAAE,SAAS,EAAE,gBAAgB,EAAE,cAAc,EAAE,OAAO,EAAE,sBAAsB,EAAE,CAAC;AACxG,YAAY,EACV,aAAa,EACb,eAAe,EACf,gBAAgB,EAChB,uBAAuB,EACvB,qBAAqB,EACrB,cAAc,EACd,qBAAqB,EACrB,cAAc,GACf,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
|
-
import { precacheAndRoute, PrecacheFallbackPlugin, cleanupOutdatedCaches, createHandlerBoundToURL } from '@serwist/precaching';
|
|
2
|
-
import { Strategy } from '@serwist/strategies';
|
|
3
|
-
import { registerRoute, NavigationRoute } from '@serwist/routing';
|
|
4
1
|
import { setCacheNameDetails, clientsClaim } from '@serwist/core';
|
|
5
2
|
import { initialize } from '@serwist/google-analytics/initialize';
|
|
6
3
|
import { enable } from '@serwist/navigation-preload';
|
|
4
|
+
import { precacheAndRoute, PrecacheFallbackPlugin, PrecacheController, PrecacheRoute, cleanupOutdatedCaches, createHandlerBoundToURL } from '@serwist/precaching';
|
|
5
|
+
import { Router, NavigationRoute, registerRoute } from '@serwist/routing';
|
|
6
|
+
import { parseRoute, getOrCreateDefaultRouter } from '@serwist/routing/internal';
|
|
7
|
+
import { Strategy } from '@serwist/strategies';
|
|
8
|
+
import { logger } from '@serwist/core/internal';
|
|
9
|
+
import { getOrCreatePrecacheController } from '@serwist/precaching/internal';
|
|
7
10
|
|
|
8
11
|
const disableDevLogs = ()=>{
|
|
9
12
|
self.__WB_DISABLE_DEV_LOGS = true;
|
|
@@ -23,6 +26,64 @@ const fallbacks = ({ runtimeCaching, entries, precacheOptions })=>{
|
|
|
23
26
|
return runtimeCaching;
|
|
24
27
|
};
|
|
25
28
|
|
|
29
|
+
class Serwist {
|
|
30
|
+
_precacheController;
|
|
31
|
+
_router;
|
|
32
|
+
constructor({ precacheController = new PrecacheController(), router = new Router() } = {}){
|
|
33
|
+
this._precacheController = precacheController;
|
|
34
|
+
this._router = router;
|
|
35
|
+
}
|
|
36
|
+
install({ precacheEntries, precacheOptions, cleanupOutdatedCaches: cleanupOutdatedCaches$1, navigateFallback, navigateFallbackAllowlist, navigateFallbackDenylist, skipWaiting = false, importScripts, navigationPreload = false, cacheId, clientsClaim: clientsClaim$1 = false, runtimeCaching, offlineAnalyticsConfig, disableDevLogs: disableDevLogs$1 = false, fallbacks: fallbacks$1 }) {
|
|
37
|
+
if (!!importScripts && importScripts.length > 0) self.importScripts(...importScripts);
|
|
38
|
+
if (navigationPreload) enable();
|
|
39
|
+
if (cacheId !== undefined) {
|
|
40
|
+
setCacheNameDetails({
|
|
41
|
+
prefix: cacheId
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
if (skipWaiting) {
|
|
45
|
+
self.skipWaiting();
|
|
46
|
+
} else {
|
|
47
|
+
self.addEventListener("message", (event)=>{
|
|
48
|
+
if (event.data && event.data.type === "SKIP_WAITING") {
|
|
49
|
+
self.skipWaiting();
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
if (clientsClaim$1) clientsClaim();
|
|
54
|
+
if (!!precacheEntries && precacheEntries.length > 0) {
|
|
55
|
+
this._precacheController.precache(precacheEntries);
|
|
56
|
+
this._router.registerRoute(new PrecacheRoute(this._precacheController, precacheOptions));
|
|
57
|
+
if (cleanupOutdatedCaches$1) cleanupOutdatedCaches();
|
|
58
|
+
if (navigateFallback) {
|
|
59
|
+
this._router.registerRoute(new NavigationRoute(createHandlerBoundToURL(navigateFallback), {
|
|
60
|
+
allowlist: navigateFallbackAllowlist,
|
|
61
|
+
denylist: navigateFallbackDenylist
|
|
62
|
+
}));
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
if (offlineAnalyticsConfig !== undefined) {
|
|
66
|
+
if (typeof offlineAnalyticsConfig === "boolean") {
|
|
67
|
+
offlineAnalyticsConfig && initialize();
|
|
68
|
+
} else {
|
|
69
|
+
initialize(offlineAnalyticsConfig);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
if (runtimeCaching !== undefined) {
|
|
73
|
+
if (fallbacks$1 !== undefined) {
|
|
74
|
+
fallbacks({
|
|
75
|
+
...fallbacks$1,
|
|
76
|
+
runtimeCaching
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
for (const entry of runtimeCaching){
|
|
80
|
+
this._router.registerRoute(parseRoute(entry.matcher, entry.handler, entry.method));
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
if (disableDevLogs$1) disableDevLogs();
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
26
87
|
const handlePrecaching = ({ precacheEntries, precacheOptions, cleanupOutdatedCaches: cleanupOutdatedCaches$1 = false, ...options })=>{
|
|
27
88
|
if (!!precacheEntries && precacheEntries.length > 0) {
|
|
28
89
|
precacheAndRoute(precacheEntries, precacheOptions);
|
|
@@ -36,57 +97,21 @@ const handlePrecaching = ({ precacheEntries, precacheOptions, cleanupOutdatedCac
|
|
|
36
97
|
}
|
|
37
98
|
};
|
|
38
99
|
|
|
39
|
-
const
|
|
40
|
-
|
|
41
|
-
|
|
100
|
+
const installSerwist = (options)=>{
|
|
101
|
+
if (process.env.NODE_ENV !== "production") {
|
|
102
|
+
logger.warn("'installSerwist' has been deprecated. Please migrate to 'new Serwist().install()'.");
|
|
42
103
|
}
|
|
104
|
+
const serwist = new Serwist({
|
|
105
|
+
precacheController: getOrCreatePrecacheController(),
|
|
106
|
+
router: getOrCreateDefaultRouter()
|
|
107
|
+
});
|
|
108
|
+
serwist.install(options);
|
|
43
109
|
};
|
|
44
110
|
|
|
45
|
-
const
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
if (cacheId !== undefined) {
|
|
49
|
-
setCacheNameDetails({
|
|
50
|
-
prefix: cacheId
|
|
51
|
-
});
|
|
52
|
-
}
|
|
53
|
-
if (skipWaiting) {
|
|
54
|
-
self.skipWaiting();
|
|
55
|
-
} else {
|
|
56
|
-
self.addEventListener("message", (event)=>{
|
|
57
|
-
if (event.data && event.data.type === "SKIP_WAITING") {
|
|
58
|
-
self.skipWaiting();
|
|
59
|
-
}
|
|
60
|
-
});
|
|
61
|
-
}
|
|
62
|
-
if (clientsClaim$1) clientsClaim();
|
|
63
|
-
handlePrecaching({
|
|
64
|
-
precacheEntries,
|
|
65
|
-
precacheOptions,
|
|
66
|
-
cleanupOutdatedCaches,
|
|
67
|
-
...options.navigateFallback && {
|
|
68
|
-
navigateFallback: options.navigateFallback,
|
|
69
|
-
navigateFallbackAllowlist: options.navigateFallbackAllowlist,
|
|
70
|
-
navigateFallbackDenylist: options.navigateFallbackDenylist
|
|
71
|
-
}
|
|
72
|
-
});
|
|
73
|
-
if (offlineAnalyticsConfig !== undefined) {
|
|
74
|
-
if (typeof offlineAnalyticsConfig === "boolean") {
|
|
75
|
-
offlineAnalyticsConfig && initialize();
|
|
76
|
-
} else {
|
|
77
|
-
initialize(offlineAnalyticsConfig);
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
if (runtimeCaching !== undefined) {
|
|
81
|
-
if (fallbacks$1 !== undefined) {
|
|
82
|
-
fallbacks({
|
|
83
|
-
...fallbacks$1,
|
|
84
|
-
runtimeCaching
|
|
85
|
-
});
|
|
86
|
-
}
|
|
87
|
-
registerRuntimeCaching(...runtimeCaching);
|
|
111
|
+
const registerRuntimeCaching = (...runtimeCachingList)=>{
|
|
112
|
+
for (const entry of runtimeCachingList){
|
|
113
|
+
registerRoute(entry.matcher, entry.handler, entry.method);
|
|
88
114
|
}
|
|
89
|
-
if (disableDevLogs$1) disableDevLogs();
|
|
90
115
|
};
|
|
91
116
|
|
|
92
|
-
export { disableDevLogs, fallbacks, handlePrecaching, installSerwist, registerRuntimeCaching };
|
|
117
|
+
export { Serwist, disableDevLogs, fallbacks, handlePrecaching, installSerwist, registerRuntimeCaching };
|
package/dist/installSerwist.d.ts
CHANGED
|
@@ -1,68 +1,15 @@
|
|
|
1
|
-
import { type
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
*
|
|
9
|
-
* @see https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerGlobalScope/skipWaiting
|
|
10
|
-
*/
|
|
11
|
-
skipWaiting?: boolean;
|
|
12
|
-
/**
|
|
13
|
-
* Imports external scripts. They are executed in the order they
|
|
14
|
-
* are passed.
|
|
15
|
-
*
|
|
16
|
-
* @see https://developer.mozilla.org/en-US/docs/Web/API/WorkerGlobalScope/importScripts
|
|
17
|
-
*/
|
|
18
|
-
importScripts?: string[];
|
|
19
|
-
/**
|
|
20
|
-
* Enables navigation preloading if it is supported.
|
|
21
|
-
*
|
|
22
|
-
* @see https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerRegistration/navigationPreload
|
|
23
|
-
*/
|
|
24
|
-
navigationPreload?: boolean;
|
|
25
|
-
/**
|
|
26
|
-
* Modifies the prefix of the default cache names used by Serwist packages.
|
|
27
|
-
*/
|
|
28
|
-
cacheId?: string | undefined;
|
|
29
|
-
/**
|
|
30
|
-
* Claims any currently available clients once the service worker
|
|
31
|
-
* becomes active. This is normally used in conjunction with `skipWaiting()`.
|
|
32
|
-
*
|
|
33
|
-
* @default false
|
|
34
|
-
*/
|
|
35
|
-
clientsClaim?: boolean;
|
|
36
|
-
/**
|
|
37
|
-
* A list of caching strategies.
|
|
38
|
-
*
|
|
39
|
-
* @see https://serwist.pages.dev/docs/sw/register-runtime-caching
|
|
40
|
-
*/
|
|
41
|
-
runtimeCaching?: RuntimeCaching[];
|
|
42
|
-
/**
|
|
43
|
-
* Your configuration for `@serwist/google-analytics`. This plugin is
|
|
44
|
-
* only initialized when this option is not `undefined` or `false`.
|
|
45
|
-
*/
|
|
46
|
-
offlineAnalyticsConfig?: GoogleAnalyticsInitializeOptions | boolean;
|
|
47
|
-
/**
|
|
48
|
-
* Disables Serwist's logging in development mode.
|
|
49
|
-
*
|
|
50
|
-
* @default false
|
|
51
|
-
* @see https://serwist.pages.dev/docs/sw/disable-dev-logs
|
|
52
|
-
*/
|
|
53
|
-
disableDevLogs?: boolean;
|
|
54
|
-
/**
|
|
55
|
-
* Precaches routes so that they can be used as a fallback when
|
|
56
|
-
* a Strategy fails to generate a response.
|
|
57
|
-
* Note: This option mutates `runtimeCaching`!
|
|
58
|
-
*
|
|
59
|
-
* @see https://serwist.pages.dev/docs/sw/fallbacks
|
|
60
|
-
*/
|
|
61
|
-
fallbacks?: Omit<FallbacksOptions, "runtimeCaching">;
|
|
62
|
-
};
|
|
1
|
+
import { type SerwistInstallOptions } from "./Serwist.js";
|
|
2
|
+
/**
|
|
3
|
+
* Options for `installSerwist`.
|
|
4
|
+
*
|
|
5
|
+
* @deprecated
|
|
6
|
+
*/
|
|
7
|
+
export type InstallSerwistOptions = SerwistInstallOptions;
|
|
63
8
|
/**
|
|
64
9
|
* Abstracts away the core APIs of Serwist.
|
|
10
|
+
*
|
|
65
11
|
* @param options - `installSerwist` options.
|
|
12
|
+
* @deprecated
|
|
66
13
|
*/
|
|
67
|
-
export declare const installSerwist: (
|
|
14
|
+
export declare const installSerwist: (options: InstallSerwistOptions) => void;
|
|
68
15
|
//# sourceMappingURL=installSerwist.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"installSerwist.d.ts","sourceRoot":"","sources":["../src/installSerwist.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"installSerwist.d.ts","sourceRoot":"","sources":["../src/installSerwist.ts"],"names":[],"mappings":"AAGA,OAAO,EAAW,KAAK,qBAAqB,EAAE,MAAM,cAAc,CAAC;AAEnE;;;;GAIG;AACH,MAAM,MAAM,qBAAqB,GAAG,qBAAqB,CAAC;AAE1D;;;;;GAKG;AACH,eAAO,MAAM,cAAc,YAAa,qBAAqB,KAAG,IAS/D,CAAC"}
|
|
@@ -3,7 +3,7 @@ import type { RuntimeCaching } from "./types.js";
|
|
|
3
3
|
* Registers caching strategies to a singleton Router instance. It is a simple
|
|
4
4
|
* syntatic sugar for `@serwist/routing.registerRoute`.
|
|
5
5
|
*
|
|
6
|
-
* @see https://serwist.pages.dev/docs/sw/
|
|
6
|
+
* @see https://serwist.pages.dev/docs/sw/register-runtime-caching
|
|
7
7
|
* @param runtimeCachingList
|
|
8
8
|
* @returns
|
|
9
9
|
*/
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@serwist/sw",
|
|
3
|
-
"version": "9.0.0-preview.
|
|
3
|
+
"version": "9.0.0-preview.17",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "A module that makes it easy to get started with the Serwist service worker libraries.",
|
|
6
6
|
"files": [
|
|
@@ -46,23 +46,23 @@
|
|
|
46
46
|
"./package.json": "./package.json"
|
|
47
47
|
},
|
|
48
48
|
"dependencies": {
|
|
49
|
-
"@serwist/background-sync": "9.0.0-preview.
|
|
50
|
-
"@serwist/broadcast-update": "9.0.0-preview.
|
|
51
|
-
"@serwist/cacheable-response": "9.0.0-preview.
|
|
52
|
-
"@serwist/core": "9.0.0-preview.
|
|
53
|
-
"@serwist/expiration": "9.0.0-preview.
|
|
54
|
-
"@serwist/google-analytics": "9.0.0-preview.
|
|
55
|
-
"@serwist/navigation-preload": "9.0.0-preview.
|
|
56
|
-
"@serwist/precaching": "9.0.0-preview.
|
|
57
|
-
"@serwist/range-requests": "9.0.0-preview.
|
|
58
|
-
"@serwist/routing": "9.0.0-preview.
|
|
59
|
-
"@serwist/strategies": "9.0.0-preview.
|
|
49
|
+
"@serwist/background-sync": "9.0.0-preview.17",
|
|
50
|
+
"@serwist/broadcast-update": "9.0.0-preview.17",
|
|
51
|
+
"@serwist/cacheable-response": "9.0.0-preview.17",
|
|
52
|
+
"@serwist/core": "9.0.0-preview.17",
|
|
53
|
+
"@serwist/expiration": "9.0.0-preview.17",
|
|
54
|
+
"@serwist/google-analytics": "9.0.0-preview.17",
|
|
55
|
+
"@serwist/navigation-preload": "9.0.0-preview.17",
|
|
56
|
+
"@serwist/precaching": "9.0.0-preview.17",
|
|
57
|
+
"@serwist/range-requests": "9.0.0-preview.17",
|
|
58
|
+
"@serwist/routing": "9.0.0-preview.17",
|
|
59
|
+
"@serwist/strategies": "9.0.0-preview.17"
|
|
60
60
|
},
|
|
61
61
|
"devDependencies": {
|
|
62
62
|
"rollup": "4.13.0",
|
|
63
63
|
"typescript": "5.5.0-dev.20240323",
|
|
64
|
-
"@serwist/constants": "9.0.0-preview.
|
|
65
|
-
"@serwist/utils": "9.0.0-preview.
|
|
64
|
+
"@serwist/constants": "9.0.0-preview.17",
|
|
65
|
+
"@serwist/utils": "9.0.0-preview.17"
|
|
66
66
|
},
|
|
67
67
|
"peerDependencies": {
|
|
68
68
|
"typescript": ">=5.0.0"
|
package/src/Serwist.ts
ADDED
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
import { clientsClaim as clientsClaimImpl, setCacheNameDetails } from "@serwist/core";
|
|
2
|
+
import { type GoogleAnalyticsInitializeOptions, initialize } from "@serwist/google-analytics/initialize";
|
|
3
|
+
import { enable } from "@serwist/navigation-preload";
|
|
4
|
+
import { PrecacheController, PrecacheRoute, cleanupOutdatedCaches as cleanupOutdatedCachesImpl, createHandlerBoundToURL } from "@serwist/precaching";
|
|
5
|
+
import { NavigationRoute, Router } from "@serwist/routing";
|
|
6
|
+
import { parseRoute } from "@serwist/routing/internal";
|
|
7
|
+
import { disableDevLogs as disableDevLogsImpl } from "./disableDevLogs.js";
|
|
8
|
+
import { fallbacks as fallbacksImpl } from "./fallbacks.js";
|
|
9
|
+
import type { FallbacksOptions } from "./fallbacks.js";
|
|
10
|
+
import { type HandlePrecachingOptions } from "./handlePrecaching.js";
|
|
11
|
+
import type { RuntimeCaching } from "./types.js";
|
|
12
|
+
|
|
13
|
+
declare const self: ServiceWorkerGlobalScope;
|
|
14
|
+
|
|
15
|
+
export interface SerwistOptions {
|
|
16
|
+
/**
|
|
17
|
+
* The precache controller that will be used to perform efficient
|
|
18
|
+
* precaching of assets.
|
|
19
|
+
* @default
|
|
20
|
+
* ```js
|
|
21
|
+
* new PrecacheController()
|
|
22
|
+
* ```
|
|
23
|
+
* @see https://serwist.pages.dev/docs/sw/precache-controller
|
|
24
|
+
*/
|
|
25
|
+
precacheController?: PrecacheController;
|
|
26
|
+
/**
|
|
27
|
+
* The router that will be used to process a `FetchEvent`, responding
|
|
28
|
+
* with a `Response` if a matching route exists.
|
|
29
|
+
*
|
|
30
|
+
* @default
|
|
31
|
+
* ```js
|
|
32
|
+
* new Router()
|
|
33
|
+
* ```
|
|
34
|
+
* @see https://serwist.pages.dev/docs/sw/router
|
|
35
|
+
*/
|
|
36
|
+
router?: Router;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export interface SerwistInstallOptions extends HandlePrecachingOptions {
|
|
40
|
+
/**
|
|
41
|
+
* Forces the waiting service worker to become the active one.
|
|
42
|
+
*
|
|
43
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerGlobalScope/skipWaiting
|
|
44
|
+
*/
|
|
45
|
+
skipWaiting?: boolean;
|
|
46
|
+
/**
|
|
47
|
+
* Imports external scripts. They are executed in the order they
|
|
48
|
+
* are passed.
|
|
49
|
+
*
|
|
50
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/API/WorkerGlobalScope/importScripts
|
|
51
|
+
*/
|
|
52
|
+
importScripts?: string[];
|
|
53
|
+
/**
|
|
54
|
+
* Enables navigation preloading if it is supported.
|
|
55
|
+
*
|
|
56
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerRegistration/navigationPreload
|
|
57
|
+
*/
|
|
58
|
+
navigationPreload?: boolean;
|
|
59
|
+
/**
|
|
60
|
+
* Modifies the prefix of the default cache names used by Serwist packages.
|
|
61
|
+
*/
|
|
62
|
+
cacheId?: string | undefined;
|
|
63
|
+
/**
|
|
64
|
+
* Claims any currently available clients once the service worker
|
|
65
|
+
* becomes active. This is normally used in conjunction with `skipWaiting()`.
|
|
66
|
+
*
|
|
67
|
+
* @default false
|
|
68
|
+
*/
|
|
69
|
+
clientsClaim?: boolean;
|
|
70
|
+
/**
|
|
71
|
+
* A list of caching strategies.
|
|
72
|
+
*
|
|
73
|
+
* @see https://serwist.pages.dev/docs/sw/runtime-caching
|
|
74
|
+
*/
|
|
75
|
+
runtimeCaching?: RuntimeCaching[];
|
|
76
|
+
/**
|
|
77
|
+
* Your configuration for `@serwist/google-analytics`. This plugin is
|
|
78
|
+
* only initialized when this option is not `undefined` or `false`.
|
|
79
|
+
*/
|
|
80
|
+
offlineAnalyticsConfig?: GoogleAnalyticsInitializeOptions | boolean;
|
|
81
|
+
/**
|
|
82
|
+
* Disables Serwist's logging in development mode.
|
|
83
|
+
*
|
|
84
|
+
* @default falseOmit<SerwistOptions, "precacheController">
|
|
85
|
+
*/
|
|
86
|
+
disableDevLogs?: boolean;
|
|
87
|
+
/**
|
|
88
|
+
* Precaches routes so that they can be used as a fallback when
|
|
89
|
+
* a Strategy fails to generate a response.
|
|
90
|
+
* Note: This option mutates `runtimeCaching`!
|
|
91
|
+
*
|
|
92
|
+
* @see https://serwist.pages.dev/docs/sw/fallbacks
|
|
93
|
+
*/
|
|
94
|
+
fallbacks?: Omit<FallbacksOptions, "runtimeCaching">;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export class Serwist {
|
|
98
|
+
private _precacheController: PrecacheController;
|
|
99
|
+
private _router: Router;
|
|
100
|
+
constructor({ precacheController = new PrecacheController(), router = new Router() }: SerwistOptions = {}) {
|
|
101
|
+
this._precacheController = precacheController;
|
|
102
|
+
this._router = router;
|
|
103
|
+
}
|
|
104
|
+
install({
|
|
105
|
+
precacheEntries,
|
|
106
|
+
precacheOptions,
|
|
107
|
+
cleanupOutdatedCaches,
|
|
108
|
+
navigateFallback,
|
|
109
|
+
navigateFallbackAllowlist,
|
|
110
|
+
navigateFallbackDenylist,
|
|
111
|
+
skipWaiting = false,
|
|
112
|
+
importScripts,
|
|
113
|
+
navigationPreload = false,
|
|
114
|
+
cacheId,
|
|
115
|
+
clientsClaim = false,
|
|
116
|
+
runtimeCaching,
|
|
117
|
+
offlineAnalyticsConfig,
|
|
118
|
+
disableDevLogs = false,
|
|
119
|
+
fallbacks,
|
|
120
|
+
}: SerwistInstallOptions) {
|
|
121
|
+
if (!!importScripts && importScripts.length > 0) self.importScripts(...importScripts);
|
|
122
|
+
|
|
123
|
+
if (navigationPreload) enable();
|
|
124
|
+
|
|
125
|
+
if (cacheId !== undefined) {
|
|
126
|
+
setCacheNameDetails({
|
|
127
|
+
prefix: cacheId,
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
if (skipWaiting) {
|
|
132
|
+
self.skipWaiting();
|
|
133
|
+
} else {
|
|
134
|
+
self.addEventListener("message", (event) => {
|
|
135
|
+
if (event.data && event.data.type === "SKIP_WAITING") {
|
|
136
|
+
self.skipWaiting();
|
|
137
|
+
}
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
if (clientsClaim) clientsClaimImpl();
|
|
142
|
+
|
|
143
|
+
if (!!precacheEntries && precacheEntries.length > 0) {
|
|
144
|
+
/**
|
|
145
|
+
* The precacheAndRoute() method efficiently caches and responds to
|
|
146
|
+
* requests for URLs in the manifest.
|
|
147
|
+
* See https://goo.gl/S9QRab
|
|
148
|
+
*/
|
|
149
|
+
this._precacheController.precache(precacheEntries);
|
|
150
|
+
this._router.registerRoute(new PrecacheRoute(this._precacheController, precacheOptions));
|
|
151
|
+
|
|
152
|
+
if (cleanupOutdatedCaches) cleanupOutdatedCachesImpl();
|
|
153
|
+
|
|
154
|
+
if (navigateFallback) {
|
|
155
|
+
this._router.registerRoute(
|
|
156
|
+
new NavigationRoute(createHandlerBoundToURL(navigateFallback), {
|
|
157
|
+
allowlist: navigateFallbackAllowlist,
|
|
158
|
+
denylist: navigateFallbackDenylist,
|
|
159
|
+
}),
|
|
160
|
+
);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
if (offlineAnalyticsConfig !== undefined) {
|
|
165
|
+
if (typeof offlineAnalyticsConfig === "boolean") {
|
|
166
|
+
offlineAnalyticsConfig && initialize();
|
|
167
|
+
} else {
|
|
168
|
+
initialize(offlineAnalyticsConfig);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
if (runtimeCaching !== undefined) {
|
|
172
|
+
if (fallbacks !== undefined) {
|
|
173
|
+
fallbacksImpl({ ...fallbacks, runtimeCaching });
|
|
174
|
+
}
|
|
175
|
+
for (const entry of runtimeCaching) {
|
|
176
|
+
this._router.registerRoute(parseRoute(entry.matcher, entry.handler, entry.method));
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
if (disableDevLogs) disableDevLogsImpl();
|
|
181
|
+
}
|
|
182
|
+
}
|
package/src/handlePrecaching.ts
CHANGED
|
@@ -2,7 +2,7 @@ import type { PrecacheEntry, PrecacheRouteOptions } from "@serwist/precaching";
|
|
|
2
2
|
import { cleanupOutdatedCaches as cleanupOutdatedCachesImpl, createHandlerBoundToURL, precacheAndRoute } from "@serwist/precaching";
|
|
3
3
|
import { NavigationRoute, registerRoute } from "@serwist/routing";
|
|
4
4
|
|
|
5
|
-
export
|
|
5
|
+
export interface HandlePrecachingOptions {
|
|
6
6
|
/**
|
|
7
7
|
* A list of fallback entries.
|
|
8
8
|
*/
|
|
@@ -17,30 +17,26 @@ export type HandlePrecachingOptions = {
|
|
|
17
17
|
* @default false
|
|
18
18
|
*/
|
|
19
19
|
cleanupOutdatedCaches?: boolean;
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
navigateFallbackDenylist?: RegExp[];
|
|
36
|
-
}
|
|
37
|
-
| { navigateFallback?: never }
|
|
38
|
-
);
|
|
20
|
+
/**
|
|
21
|
+
* An URL that should point to a HTML file with which navigation requests for URLs that aren't
|
|
22
|
+
* precached will be fulfilled.
|
|
23
|
+
*/
|
|
24
|
+
navigateFallback?: string;
|
|
25
|
+
/**
|
|
26
|
+
* URLs that should be allowed to use the `navigateFallback` handler.
|
|
27
|
+
*/
|
|
28
|
+
navigateFallbackAllowlist?: RegExp[];
|
|
29
|
+
/**
|
|
30
|
+
* URLs that should not be allowed to use the `navigateFallback` handler. This takes precedence
|
|
31
|
+
* over `navigateFallbackAllowlist`.
|
|
32
|
+
*/
|
|
33
|
+
navigateFallbackDenylist?: RegExp[];
|
|
34
|
+
}
|
|
39
35
|
|
|
40
36
|
/**
|
|
41
37
|
* Handles a list of precache entries and cleans up outdated caches.
|
|
42
38
|
*
|
|
43
|
-
* @see https://serwist.pages.dev/docs/sw/
|
|
39
|
+
* @see https://serwist.pages.dev/docs/sw/handle-precaching
|
|
44
40
|
* @param options
|
|
45
41
|
*/
|
|
46
42
|
export const handlePrecaching = ({ precacheEntries, precacheOptions, cleanupOutdatedCaches = false, ...options }: HandlePrecachingOptions): void => {
|
package/src/index.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { Serwist, type SerwistOptions, type SerwistInstallOptions } from "./Serwist.js";
|
|
1
2
|
import { disableDevLogs } from "./disableDevLogs.js";
|
|
2
3
|
import type { FallbackEntry, FallbackMatcher, FallbacksOptions } from "./fallbacks.js";
|
|
3
4
|
import { fallbacks } from "./fallbacks.js";
|
|
@@ -6,5 +7,14 @@ import { type InstallSerwistOptions, installSerwist } from "./installSerwist.js"
|
|
|
6
7
|
import { registerRuntimeCaching } from "./registerRuntimeCaching.js";
|
|
7
8
|
import type { RuntimeCaching } from "./types.js";
|
|
8
9
|
|
|
9
|
-
export { disableDevLogs, fallbacks, handlePrecaching, installSerwist, registerRuntimeCaching };
|
|
10
|
-
export type {
|
|
10
|
+
export { disableDevLogs, fallbacks, handlePrecaching, installSerwist, Serwist, registerRuntimeCaching };
|
|
11
|
+
export type {
|
|
12
|
+
FallbackEntry,
|
|
13
|
+
FallbackMatcher,
|
|
14
|
+
FallbacksOptions,
|
|
15
|
+
HandlePrecachingOptions,
|
|
16
|
+
InstallSerwistOptions,
|
|
17
|
+
SerwistOptions,
|
|
18
|
+
SerwistInstallOptions,
|
|
19
|
+
RuntimeCaching,
|
|
20
|
+
};
|
package/src/installSerwist.ts
CHANGED
|
@@ -1,142 +1,28 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
1
|
+
import { logger } from "@serwist/core/internal";
|
|
2
|
+
import { getOrCreatePrecacheController } from "@serwist/precaching/internal";
|
|
3
|
+
import { getOrCreateDefaultRouter } from "@serwist/routing/internal";
|
|
4
|
+
import { Serwist, type SerwistInstallOptions } from "./Serwist.js";
|
|
4
5
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
declare const self: ServiceWorkerGlobalScope;
|
|
13
|
-
|
|
14
|
-
export type InstallSerwistOptions = HandlePrecachingOptions & {
|
|
15
|
-
/**
|
|
16
|
-
* Forces the waiting service worker to become the active one.
|
|
17
|
-
*
|
|
18
|
-
* @see https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerGlobalScope/skipWaiting
|
|
19
|
-
*/
|
|
20
|
-
skipWaiting?: boolean;
|
|
21
|
-
/**
|
|
22
|
-
* Imports external scripts. They are executed in the order they
|
|
23
|
-
* are passed.
|
|
24
|
-
*
|
|
25
|
-
* @see https://developer.mozilla.org/en-US/docs/Web/API/WorkerGlobalScope/importScripts
|
|
26
|
-
*/
|
|
27
|
-
importScripts?: string[];
|
|
28
|
-
/**
|
|
29
|
-
* Enables navigation preloading if it is supported.
|
|
30
|
-
*
|
|
31
|
-
* @see https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerRegistration/navigationPreload
|
|
32
|
-
*/
|
|
33
|
-
navigationPreload?: boolean;
|
|
34
|
-
/**
|
|
35
|
-
* Modifies the prefix of the default cache names used by Serwist packages.
|
|
36
|
-
*/
|
|
37
|
-
cacheId?: string | undefined;
|
|
38
|
-
/**
|
|
39
|
-
* Claims any currently available clients once the service worker
|
|
40
|
-
* becomes active. This is normally used in conjunction with `skipWaiting()`.
|
|
41
|
-
*
|
|
42
|
-
* @default false
|
|
43
|
-
*/
|
|
44
|
-
clientsClaim?: boolean;
|
|
45
|
-
/**
|
|
46
|
-
* A list of caching strategies.
|
|
47
|
-
*
|
|
48
|
-
* @see https://serwist.pages.dev/docs/sw/register-runtime-caching
|
|
49
|
-
*/
|
|
50
|
-
runtimeCaching?: RuntimeCaching[];
|
|
51
|
-
/**
|
|
52
|
-
* Your configuration for `@serwist/google-analytics`. This plugin is
|
|
53
|
-
* only initialized when this option is not `undefined` or `false`.
|
|
54
|
-
*/
|
|
55
|
-
offlineAnalyticsConfig?: GoogleAnalyticsInitializeOptions | boolean;
|
|
56
|
-
/**
|
|
57
|
-
* Disables Serwist's logging in development mode.
|
|
58
|
-
*
|
|
59
|
-
* @default false
|
|
60
|
-
* @see https://serwist.pages.dev/docs/sw/disable-dev-logs
|
|
61
|
-
*/
|
|
62
|
-
disableDevLogs?: boolean;
|
|
63
|
-
/**
|
|
64
|
-
* Precaches routes so that they can be used as a fallback when
|
|
65
|
-
* a Strategy fails to generate a response.
|
|
66
|
-
* Note: This option mutates `runtimeCaching`!
|
|
67
|
-
*
|
|
68
|
-
* @see https://serwist.pages.dev/docs/sw/fallbacks
|
|
69
|
-
*/
|
|
70
|
-
fallbacks?: Omit<FallbacksOptions, "runtimeCaching">;
|
|
71
|
-
};
|
|
6
|
+
/**
|
|
7
|
+
* Options for `installSerwist`.
|
|
8
|
+
*
|
|
9
|
+
* @deprecated
|
|
10
|
+
*/
|
|
11
|
+
export type InstallSerwistOptions = SerwistInstallOptions;
|
|
72
12
|
|
|
73
13
|
/**
|
|
74
14
|
* Abstracts away the core APIs of Serwist.
|
|
15
|
+
*
|
|
75
16
|
* @param options - `installSerwist` options.
|
|
17
|
+
* @deprecated
|
|
76
18
|
*/
|
|
77
|
-
export const installSerwist = ({
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
cleanupOutdatedCaches,
|
|
81
|
-
|
|
82
|
-
skipWaiting = false,
|
|
83
|
-
importScripts,
|
|
84
|
-
navigationPreload = false,
|
|
85
|
-
cacheId,
|
|
86
|
-
clientsClaim = false,
|
|
87
|
-
runtimeCaching,
|
|
88
|
-
offlineAnalyticsConfig,
|
|
89
|
-
disableDevLogs = false,
|
|
90
|
-
fallbacks,
|
|
91
|
-
...options
|
|
92
|
-
}: InstallSerwistOptions): void => {
|
|
93
|
-
if (!!importScripts && importScripts.length > 0) self.importScripts(...importScripts);
|
|
94
|
-
|
|
95
|
-
if (navigationPreload) enable();
|
|
96
|
-
|
|
97
|
-
if (cacheId !== undefined) {
|
|
98
|
-
setCacheNameDetails({
|
|
99
|
-
prefix: cacheId,
|
|
100
|
-
});
|
|
19
|
+
export const installSerwist = (options: InstallSerwistOptions): void => {
|
|
20
|
+
if (process.env.NODE_ENV !== "production") {
|
|
21
|
+
logger.warn("'installSerwist' has been deprecated. Please migrate to 'new Serwist().install()'.");
|
|
101
22
|
}
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
} else {
|
|
106
|
-
self.addEventListener("message", (event) => {
|
|
107
|
-
if (event.data && event.data.type === "SKIP_WAITING") {
|
|
108
|
-
self.skipWaiting();
|
|
109
|
-
}
|
|
110
|
-
});
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
if (clientsClaim) clientsClaimImpl();
|
|
114
|
-
|
|
115
|
-
handlePrecaching({
|
|
116
|
-
precacheEntries,
|
|
117
|
-
precacheOptions,
|
|
118
|
-
cleanupOutdatedCaches,
|
|
119
|
-
...(options.navigateFallback && {
|
|
120
|
-
navigateFallback: options.navigateFallback,
|
|
121
|
-
navigateFallbackAllowlist: options.navigateFallbackAllowlist,
|
|
122
|
-
navigateFallbackDenylist: options.navigateFallbackDenylist,
|
|
123
|
-
}),
|
|
23
|
+
const serwist = new Serwist({
|
|
24
|
+
precacheController: getOrCreatePrecacheController(),
|
|
25
|
+
router: getOrCreateDefaultRouter(),
|
|
124
26
|
});
|
|
125
|
-
|
|
126
|
-
if (offlineAnalyticsConfig !== undefined) {
|
|
127
|
-
if (typeof offlineAnalyticsConfig === "boolean") {
|
|
128
|
-
offlineAnalyticsConfig && initialize();
|
|
129
|
-
} else {
|
|
130
|
-
initialize(offlineAnalyticsConfig);
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
if (runtimeCaching !== undefined) {
|
|
135
|
-
if (fallbacks !== undefined) {
|
|
136
|
-
fallbacksImpl({ ...fallbacks, runtimeCaching });
|
|
137
|
-
}
|
|
138
|
-
registerRuntimeCaching(...runtimeCaching);
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
if (disableDevLogs) disableDevLogsImpl();
|
|
27
|
+
serwist.install(options);
|
|
142
28
|
};
|
|
@@ -6,7 +6,7 @@ import type { RuntimeCaching } from "./types.js";
|
|
|
6
6
|
* Registers caching strategies to a singleton Router instance. It is a simple
|
|
7
7
|
* syntatic sugar for `@serwist/routing.registerRoute`.
|
|
8
8
|
*
|
|
9
|
-
* @see https://serwist.pages.dev/docs/sw/
|
|
9
|
+
* @see https://serwist.pages.dev/docs/sw/register-runtime-caching
|
|
10
10
|
* @param runtimeCachingList
|
|
11
11
|
* @returns
|
|
12
12
|
*/
|