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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,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 type HandlePrecachingOptions = {
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: string;
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/handlePrecaching
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,MAAM,uBAAuB,GAAG;IACpC;;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;CACjC,GAAG,CACA;IACE;;;OAGG;IACH,gBAAgB,EAAE,MAAM,CAAC;IACzB;;OAEG;IACH,yBAAyB,CAAC,EAAE,MAAM,EAAE,CAAC;IACrC;;;OAGG;IACH,wBAAwB,CAAC,EAAE,MAAM,EAAE,CAAC;CACrC,GACD;IAAE,gBAAgB,CAAC,EAAE,KAAK,CAAA;CAAE,CAC/B,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,gBAAgB,4EAAqF,uBAAuB,KAAG,IAoB3I,CAAC"}
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 as SerwistOptions, RuntimeCaching };
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
@@ -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;AAC/F,YAAY,EAAE,aAAa,EAAE,eAAe,EAAE,gBAAgB,EAAE,uBAAuB,EAAE,qBAAqB,IAAI,cAAc,EAAE,cAAc,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,10 +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';
7
8
  import { logger } from '@serwist/core/internal';
9
+ import { getOrCreatePrecacheController } from '@serwist/precaching/internal';
8
10
 
9
11
  const disableDevLogs = ()=>{
10
12
  self.__WB_DISABLE_DEV_LOGS = true;
@@ -24,6 +26,64 @@ const fallbacks = ({ runtimeCaching, entries, precacheOptions })=>{
24
26
  return runtimeCaching;
25
27
  };
26
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
+
27
87
  const handlePrecaching = ({ precacheEntries, precacheOptions, cleanupOutdatedCaches: cleanupOutdatedCaches$1 = false, ...options })=>{
28
88
  if (!!precacheEntries && precacheEntries.length > 0) {
29
89
  precacheAndRoute(precacheEntries, precacheOptions);
@@ -37,71 +97,21 @@ const handlePrecaching = ({ precacheEntries, precacheOptions, cleanupOutdatedCac
37
97
  }
38
98
  };
39
99
 
40
- const registerRuntimeCaching = (...runtimeCachingList)=>{
41
- if (!("__WB_FORCE_RUNTIME_CACHING" in globalThis)) {
42
- self.__WB_FORCE_RUNTIME_CACHING = false;
43
- }
44
- if (!self.__WB_FORCE_RUNTIME_CACHING && process.env.NODE_ENV !== "production") {
45
- logger.log("registerRuntimeCaching is disabled in development mode.");
46
- } else {
47
- for (const entry of runtimeCachingList){
48
- registerRoute(entry.matcher, entry.handler, entry.method);
49
- }
100
+ const installSerwist = (options)=>{
101
+ if (process.env.NODE_ENV !== "production") {
102
+ logger.warn("'installSerwist' has been deprecated. Please migrate to 'new Serwist().install()'.");
50
103
  }
104
+ const serwist = new Serwist({
105
+ precacheController: getOrCreatePrecacheController(),
106
+ router: getOrCreateDefaultRouter()
107
+ });
108
+ serwist.install(options);
51
109
  };
52
110
 
53
- const installSerwist = ({ precacheEntries, precacheOptions, cleanupOutdatedCaches, skipWaiting = false, importScripts, navigationPreload = false, cacheId, clientsClaim: clientsClaim$1 = false, runtimeCaching, offlineAnalyticsConfig, disableDevLogs: disableDevLogs$1 = false, fallbacks: fallbacks$1, ...options })=>{
54
- if (!!importScripts && importScripts.length > 0) self.importScripts(...importScripts);
55
- if (navigationPreload) enable();
56
- if (cacheId !== undefined) {
57
- setCacheNameDetails({
58
- prefix: cacheId
59
- });
60
- }
61
- if (skipWaiting) {
62
- self.skipWaiting();
63
- } else {
64
- self.addEventListener("message", (event)=>{
65
- if (event.data && event.data.type === "SKIP_WAITING") {
66
- self.skipWaiting();
67
- }
68
- });
69
- }
70
- if (clientsClaim$1) clientsClaim();
71
- handlePrecaching({
72
- precacheEntries,
73
- precacheOptions,
74
- cleanupOutdatedCaches,
75
- ...options.navigateFallback && {
76
- navigateFallback: options.navigateFallback,
77
- navigateFallbackAllowlist: options.navigateFallbackAllowlist,
78
- navigateFallbackDenylist: options.navigateFallbackDenylist
79
- }
80
- });
81
- if (offlineAnalyticsConfig !== undefined) {
82
- if (typeof offlineAnalyticsConfig === "boolean") {
83
- offlineAnalyticsConfig && initialize();
84
- } else {
85
- initialize(offlineAnalyticsConfig);
86
- }
87
- }
88
- if (runtimeCaching !== undefined) {
89
- if (!("__WB_FORCE_RUNTIME_CACHING" in globalThis)) {
90
- self.__WB_FORCE_RUNTIME_CACHING = false;
91
- }
92
- if (!self.__WB_FORCE_RUNTIME_CACHING && process.env.NODE_ENV !== "production") {
93
- logger.log("runtimeCaching and fallbacks are disabled in development mode.");
94
- } else {
95
- if (fallbacks$1 !== undefined) {
96
- fallbacks({
97
- ...fallbacks$1,
98
- runtimeCaching
99
- });
100
- }
101
- registerRuntimeCaching(...runtimeCaching);
102
- }
111
+ const registerRuntimeCaching = (...runtimeCachingList)=>{
112
+ for (const entry of runtimeCachingList){
113
+ registerRoute(entry.matcher, entry.handler, entry.method);
103
114
  }
104
- if (disableDevLogs$1) disableDevLogs();
105
115
  };
106
116
 
107
- export { disableDevLogs, fallbacks, handlePrecaching, installSerwist, registerRuntimeCaching };
117
+ export { Serwist, disableDevLogs, fallbacks, handlePrecaching, installSerwist, registerRuntimeCaching };
@@ -1,7 +1,7 @@
1
1
  import type { QueueEntry, QueueOptions } from "@serwist/background-sync";
2
2
  import { BackgroundSyncPlugin, Queue, QueueStore, StorableRequest } from "@serwist/background-sync";
3
3
  import type { BroadcastCacheUpdateOptions, BroadcastMessage, BroadcastPayload, BroadcastPayloadGenerator } from "@serwist/broadcast-update";
4
- import { defaultHeadersToCheck as BROADCAST_UPDATE_DEFAULT_HEADERS, CACHE_UPDATED_MESSAGE_META as BROADCAST_UPDATE_MESSAGE_META, CACHE_UPDATED_MESSAGE_TYPE as BROADCAST_UPDATE_MESSAGE_TYPE, BroadcastCacheUpdate, BroadcastUpdatePlugin, responsesAreSame } from "@serwist/broadcast-update";
4
+ import { BroadcastCacheUpdate, BroadcastUpdatePlugin, CACHE_UPDATED_MESSAGE_META as BROADCAST_UPDATE_MESSAGE_META, CACHE_UPDATED_MESSAGE_TYPE as BROADCAST_UPDATE_MESSAGE_TYPE, defaultHeadersToCheck as BROADCAST_UPDATE_DEFAULT_HEADERS, responsesAreSame } from "@serwist/broadcast-update";
5
5
  import type { CacheableResponseOptions } from "@serwist/cacheable-response";
6
6
  import { CacheableResponse, CacheableResponsePlugin } from "@serwist/cacheable-response";
7
7
  import type { ExpirationPluginOptions } from "@serwist/expiration";
@@ -1 +1 @@
1
- {"version":3,"file":"index.plugins.d.ts","sourceRoot":"","sources":["../src/index.plugins.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,YAAY,EAAE,MAAM,0BAA0B,CAAC;AACzE,OAAO,EAAE,oBAAoB,EAAE,KAAK,EAAE,UAAU,EAAE,eAAe,EAAE,MAAM,0BAA0B,CAAC;AACpG,OAAO,KAAK,EAAE,2BAA2B,EAAE,gBAAgB,EAAE,gBAAgB,EAAE,yBAAyB,EAAE,MAAM,2BAA2B,CAAC;AAC5I,OAAO,EACL,qBAAqB,IAAI,gCAAgC,EACzD,0BAA0B,IAAI,6BAA6B,EAC3D,0BAA0B,IAAI,6BAA6B,EAC3D,oBAAoB,EACpB,qBAAqB,EACrB,gBAAgB,EACjB,MAAM,2BAA2B,CAAC;AACnC,OAAO,KAAK,EAAE,wBAAwB,EAAE,MAAM,6BAA6B,CAAC;AAC5E,OAAO,EAAE,iBAAiB,EAAE,uBAAuB,EAAE,MAAM,6BAA6B,CAAC;AACzF,OAAO,KAAK,EAAE,uBAAuB,EAAE,MAAM,qBAAqB,CAAC;AACnE,OAAO,EAAE,eAAe,EAAE,gBAAgB,EAAE,MAAM,qBAAqB,CAAC;AACxE,OAAO,EAAE,mBAAmB,EAAE,qBAAqB,EAAE,MAAM,yBAAyB,CAAC;AAErF,OAAO,EACL,oBAAoB,EACpB,KAAK,IAAI,mBAAmB,EAC5B,UAAU,IAAI,wBAAwB,EACtC,eAAe,EACf,oBAAoB,EACpB,qBAAqB,EACrB,gBAAgB,EAChB,6BAA6B,EAC7B,6BAA6B,EAC7B,gCAAgC,EAChC,iBAAiB,EACjB,uBAAuB,EACvB,eAAe,EACf,gBAAgB,EAChB,qBAAqB,EACrB,mBAAmB,GACpB,CAAC;AAEF,YAAY,EACV,YAAY,IAAI,0BAA0B,EAC1C,UAAU,IAAI,wBAAwB,EACtC,2BAA2B,EAC3B,gBAAgB,EAChB,yBAAyB,EACzB,gBAAgB,EAChB,wBAAwB,EACxB,uBAAuB,GACxB,CAAC"}
1
+ {"version":3,"file":"index.plugins.d.ts","sourceRoot":"","sources":["../src/index.plugins.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,YAAY,EAAE,MAAM,0BAA0B,CAAC;AACzE,OAAO,EAAE,oBAAoB,EAAE,KAAK,EAAE,UAAU,EAAE,eAAe,EAAE,MAAM,0BAA0B,CAAC;AACpG,OAAO,KAAK,EAAE,2BAA2B,EAAE,gBAAgB,EAAE,gBAAgB,EAAE,yBAAyB,EAAE,MAAM,2BAA2B,CAAC;AAC5I,OAAO,EACL,oBAAoB,EACpB,qBAAqB,EACrB,0BAA0B,IAAI,6BAA6B,EAC3D,0BAA0B,IAAI,6BAA6B,EAC3D,qBAAqB,IAAI,gCAAgC,EACzD,gBAAgB,EACjB,MAAM,2BAA2B,CAAC;AACnC,OAAO,KAAK,EAAE,wBAAwB,EAAE,MAAM,6BAA6B,CAAC;AAC5E,OAAO,EAAE,iBAAiB,EAAE,uBAAuB,EAAE,MAAM,6BAA6B,CAAC;AACzF,OAAO,KAAK,EAAE,uBAAuB,EAAE,MAAM,qBAAqB,CAAC;AACnE,OAAO,EAAE,eAAe,EAAE,gBAAgB,EAAE,MAAM,qBAAqB,CAAC;AACxE,OAAO,EAAE,mBAAmB,EAAE,qBAAqB,EAAE,MAAM,yBAAyB,CAAC;AAErF,OAAO,EACL,oBAAoB,EACpB,KAAK,IAAI,mBAAmB,EAC5B,UAAU,IAAI,wBAAwB,EACtC,eAAe,EACf,oBAAoB,EACpB,qBAAqB,EACrB,gBAAgB,EAChB,6BAA6B,EAC7B,6BAA6B,EAC7B,gCAAgC,EAChC,iBAAiB,EACjB,uBAAuB,EACvB,eAAe,EACf,gBAAgB,EAChB,qBAAqB,EACrB,mBAAmB,GACpB,CAAC;AAEF,YAAY,EACV,YAAY,IAAI,0BAA0B,EAC1C,UAAU,IAAI,wBAAwB,EACtC,2BAA2B,EAC3B,gBAAgB,EAChB,yBAAyB,EACzB,gBAAgB,EAChB,wBAAwB,EACxB,uBAAuB,GACxB,CAAC"}
@@ -1,68 +1,15 @@
1
- import { type GoogleAnalyticsInitializeOptions } from "@serwist/google-analytics/initialize";
2
- import type { FallbacksOptions } from "./fallbacks.js";
3
- import { type HandlePrecachingOptions } from "./handlePrecaching.js";
4
- import type { RuntimeCaching } from "./types.js";
5
- export type InstallSerwistOptions = HandlePrecachingOptions & {
6
- /**
7
- * Forces the waiting service worker to become the active one.
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: ({ precacheEntries, precacheOptions, cleanupOutdatedCaches, skipWaiting, importScripts, navigationPreload, cacheId, clientsClaim, runtimeCaching, offlineAnalyticsConfig, disableDevLogs, fallbacks, ...options }: InstallSerwistOptions) => void;
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":"AACA,OAAO,EAAE,KAAK,gCAAgC,EAAc,MAAM,sCAAsC,CAAC;AAMzG,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AACvD,OAAO,EAAE,KAAK,uBAAuB,EAAoB,MAAM,uBAAuB,CAAC;AAEvF,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAIjD,MAAM,MAAM,qBAAqB,GAAG,uBAAuB,GAAG;IAC5D;;;;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;;;;;OAKG;IACH,cAAc,CAAC,EAAE,OAAO,CAAC;IACzB;;;;;;OAMG;IACH,SAAS,CAAC,EAAE,IAAI,CAAC,gBAAgB,EAAE,gBAAgB,CAAC,CAAC;CACtD,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,cAAc,qNAexB,qBAAqB,KAAG,IAyD1B,CAAC"}
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/registerRuntimeCaching
6
+ * @see https://serwist.pages.dev/docs/sw/register-runtime-caching
7
7
  * @param runtimeCachingList
8
8
  * @returns
9
9
  */
@@ -1 +1 @@
1
- {"version":3,"file":"registerRuntimeCaching.d.ts","sourceRoot":"","sources":["../src/registerRuntimeCaching.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAIjD;;;;;;;GAOG;AACH,eAAO,MAAM,sBAAsB,0BAA2B,cAAc,EAAE,KAAG,IAYhF,CAAC"}
1
+ {"version":3,"file":"registerRuntimeCaching.d.ts","sourceRoot":"","sources":["../src/registerRuntimeCaching.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAEjD;;;;;;;GAOG;AACH,eAAO,MAAM,sBAAsB,0BAA2B,cAAc,EAAE,KAAG,IAIhF,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@serwist/sw",
3
- "version": "9.0.0-preview.15",
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.15",
50
- "@serwist/broadcast-update": "9.0.0-preview.15",
51
- "@serwist/cacheable-response": "9.0.0-preview.15",
52
- "@serwist/core": "9.0.0-preview.15",
53
- "@serwist/expiration": "9.0.0-preview.15",
54
- "@serwist/google-analytics": "9.0.0-preview.15",
55
- "@serwist/navigation-preload": "9.0.0-preview.15",
56
- "@serwist/precaching": "9.0.0-preview.15",
57
- "@serwist/range-requests": "9.0.0-preview.15",
58
- "@serwist/routing": "9.0.0-preview.15",
59
- "@serwist/strategies": "9.0.0-preview.15"
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.15",
65
- "@serwist/utils": "9.0.0-preview.15"
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
+ }
@@ -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 type HandlePrecachingOptions = {
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
- * An URL that should point to a HTML file with which navigation requests for URLs that aren't
24
- * precached will be fulfilled.
25
- */
26
- navigateFallback: string;
27
- /**
28
- * URLs that should be allowed to use the `navigateFallback` handler.
29
- */
30
- navigateFallbackAllowlist?: RegExp[];
31
- /**
32
- * URLs that should not be allowed to use the `navigateFallback` handler. This takes precedence
33
- * over `navigateFallbackAllowlist`.
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/handlePrecaching
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 => {
@@ -2,11 +2,11 @@ import type { QueueEntry, QueueOptions } from "@serwist/background-sync";
2
2
  import { BackgroundSyncPlugin, Queue, QueueStore, StorableRequest } from "@serwist/background-sync";
3
3
  import type { BroadcastCacheUpdateOptions, BroadcastMessage, BroadcastPayload, BroadcastPayloadGenerator } from "@serwist/broadcast-update";
4
4
  import {
5
- defaultHeadersToCheck as BROADCAST_UPDATE_DEFAULT_HEADERS,
6
- CACHE_UPDATED_MESSAGE_META as BROADCAST_UPDATE_MESSAGE_META,
7
- CACHE_UPDATED_MESSAGE_TYPE as BROADCAST_UPDATE_MESSAGE_TYPE,
8
5
  BroadcastCacheUpdate,
9
6
  BroadcastUpdatePlugin,
7
+ CACHE_UPDATED_MESSAGE_META as BROADCAST_UPDATE_MESSAGE_META,
8
+ CACHE_UPDATED_MESSAGE_TYPE as BROADCAST_UPDATE_MESSAGE_TYPE,
9
+ defaultHeadersToCheck as BROADCAST_UPDATE_DEFAULT_HEADERS,
10
10
  responsesAreSame,
11
11
  } from "@serwist/broadcast-update";
12
12
  import type { CacheableResponseOptions } from "@serwist/cacheable-response";
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 { FallbackEntry, FallbackMatcher, FallbacksOptions, HandlePrecachingOptions, InstallSerwistOptions as SerwistOptions, RuntimeCaching };
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
+ };
@@ -1,150 +1,28 @@
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
-
5
1
  import { logger } from "@serwist/core/internal";
6
- import { disableDevLogs as disableDevLogsImpl } from "./disableDevLogs.js";
7
- import { fallbacks as fallbacksImpl } from "./fallbacks.js";
8
- import type { FallbacksOptions } from "./fallbacks.js";
9
- import { type HandlePrecachingOptions, handlePrecaching } from "./handlePrecaching.js";
10
- import { registerRuntimeCaching } from "./registerRuntimeCaching.js";
11
- import type { RuntimeCaching } from "./types.js";
12
-
13
- declare const self: ServiceWorkerGlobalScope;
2
+ import { getOrCreatePrecacheController } from "@serwist/precaching/internal";
3
+ import { getOrCreateDefaultRouter } from "@serwist/routing/internal";
4
+ import { Serwist, type SerwistInstallOptions } from "./Serwist.js";
14
5
 
15
- export type InstallSerwistOptions = HandlePrecachingOptions & {
16
- /**
17
- * Forces the waiting service worker to become the active one.
18
- *
19
- * @see https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerGlobalScope/skipWaiting
20
- */
21
- skipWaiting?: boolean;
22
- /**
23
- * Imports external scripts. They are executed in the order they
24
- * are passed.
25
- *
26
- * @see https://developer.mozilla.org/en-US/docs/Web/API/WorkerGlobalScope/importScripts
27
- */
28
- importScripts?: string[];
29
- /**
30
- * Enables navigation preloading if it is supported.
31
- *
32
- * @see https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerRegistration/navigationPreload
33
- */
34
- navigationPreload?: boolean;
35
- /**
36
- * Modifies the prefix of the default cache names used by Serwist packages.
37
- */
38
- cacheId?: string | undefined;
39
- /**
40
- * Claims any currently available clients once the service worker
41
- * becomes active. This is normally used in conjunction with `skipWaiting()`.
42
- *
43
- * @default false
44
- */
45
- clientsClaim?: boolean;
46
- /**
47
- * A list of caching strategies.
48
- *
49
- * @see https://serwist.pages.dev/docs/sw/register-runtime-caching
50
- */
51
- runtimeCaching?: RuntimeCaching[];
52
- /**
53
- * Your configuration for `@serwist/google-analytics`. This plugin is
54
- * only initialized when this option is not `undefined` or `false`.
55
- */
56
- offlineAnalyticsConfig?: GoogleAnalyticsInitializeOptions | boolean;
57
- /**
58
- * Disables Serwist's logging in development mode.
59
- *
60
- * @default false
61
- * @see https://serwist.pages.dev/docs/sw/disable-dev-logs
62
- */
63
- disableDevLogs?: boolean;
64
- /**
65
- * Precaches routes so that they can be used as a fallback when
66
- * a Strategy fails to generate a response.
67
- * Note: This option mutates `runtimeCaching`!
68
- *
69
- * @see https://serwist.pages.dev/docs/sw/fallbacks
70
- */
71
- fallbacks?: Omit<FallbacksOptions, "runtimeCaching">;
72
- };
6
+ /**
7
+ * Options for `installSerwist`.
8
+ *
9
+ * @deprecated
10
+ */
11
+ export type InstallSerwistOptions = SerwistInstallOptions;
73
12
 
74
13
  /**
75
14
  * Abstracts away the core APIs of Serwist.
15
+ *
76
16
  * @param options - `installSerwist` options.
17
+ * @deprecated
77
18
  */
78
- export const installSerwist = ({
79
- precacheEntries,
80
- precacheOptions,
81
- cleanupOutdatedCaches,
82
-
83
- skipWaiting = false,
84
- importScripts,
85
- navigationPreload = false,
86
- cacheId,
87
- clientsClaim = false,
88
- runtimeCaching,
89
- offlineAnalyticsConfig,
90
- disableDevLogs = false,
91
- fallbacks,
92
- ...options
93
- }: InstallSerwistOptions): void => {
94
- if (!!importScripts && importScripts.length > 0) self.importScripts(...importScripts);
95
-
96
- if (navigationPreload) enable();
97
-
98
- if (cacheId !== undefined) {
99
- setCacheNameDetails({
100
- prefix: cacheId,
101
- });
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()'.");
102
22
  }
103
-
104
- if (skipWaiting) {
105
- self.skipWaiting();
106
- } else {
107
- self.addEventListener("message", (event) => {
108
- if (event.data && event.data.type === "SKIP_WAITING") {
109
- self.skipWaiting();
110
- }
111
- });
112
- }
113
-
114
- if (clientsClaim) clientsClaimImpl();
115
-
116
- handlePrecaching({
117
- precacheEntries,
118
- precacheOptions,
119
- cleanupOutdatedCaches,
120
- ...(options.navigateFallback && {
121
- navigateFallback: options.navigateFallback,
122
- navigateFallbackAllowlist: options.navigateFallbackAllowlist,
123
- navigateFallbackDenylist: options.navigateFallbackDenylist,
124
- }),
23
+ const serwist = new Serwist({
24
+ precacheController: getOrCreatePrecacheController(),
25
+ router: getOrCreateDefaultRouter(),
125
26
  });
126
-
127
- if (offlineAnalyticsConfig !== undefined) {
128
- if (typeof offlineAnalyticsConfig === "boolean") {
129
- offlineAnalyticsConfig && initialize();
130
- } else {
131
- initialize(offlineAnalyticsConfig);
132
- }
133
- }
134
-
135
- if (runtimeCaching !== undefined) {
136
- if (!("__WB_FORCE_RUNTIME_CACHING" in globalThis)) {
137
- self.__WB_FORCE_RUNTIME_CACHING = false;
138
- }
139
- if (!self.__WB_FORCE_RUNTIME_CACHING && process.env.NODE_ENV !== "production") {
140
- logger.log("runtimeCaching and fallbacks are disabled in development mode.");
141
- } else {
142
- if (fallbacks !== undefined) {
143
- fallbacksImpl({ ...fallbacks, runtimeCaching });
144
- }
145
- registerRuntimeCaching(...runtimeCaching);
146
- }
147
- }
148
-
149
- if (disableDevLogs) disableDevLogsImpl();
27
+ serwist.install(options);
150
28
  };
@@ -1,28 +1,17 @@
1
- import { logger } from "@serwist/core/internal";
2
1
  import { registerRoute } from "@serwist/routing";
3
2
 
4
3
  import type { RuntimeCaching } from "./types.js";
5
4
 
6
- declare const self: ServiceWorkerGlobalScope;
7
-
8
5
  /**
9
6
  * Registers caching strategies to a singleton Router instance. It is a simple
10
7
  * syntatic sugar for `@serwist/routing.registerRoute`.
11
8
  *
12
- * @see https://serwist.pages.dev/docs/sw/registerRuntimeCaching
9
+ * @see https://serwist.pages.dev/docs/sw/register-runtime-caching
13
10
  * @param runtimeCachingList
14
11
  * @returns
15
12
  */
16
13
  export const registerRuntimeCaching = (...runtimeCachingList: RuntimeCaching[]): void => {
17
- if (!("__WB_FORCE_RUNTIME_CACHING" in globalThis)) {
18
- self.__WB_FORCE_RUNTIME_CACHING = false;
19
- }
20
-
21
- if (!self.__WB_FORCE_RUNTIME_CACHING && process.env.NODE_ENV !== "production") {
22
- logger.log("registerRuntimeCaching is disabled in development mode.");
23
- } else {
24
- for (const entry of runtimeCachingList) {
25
- registerRoute(entry.matcher, entry.handler, entry.method);
26
- }
14
+ for (const entry of runtimeCachingList) {
15
+ registerRoute(entry.matcher, entry.handler, entry.method);
27
16
  }
28
17
  };