@serwist/sw 9.0.0-preview.2 → 9.0.0-preview.3

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.
@@ -1,5 +1,7 @@
1
1
  /**
2
2
  * Disables Serwist's logging in development mode.
3
+ *
4
+ * @see https://serwist.pages.dev/docs/sw/disable-dev-logs
3
5
  */
4
6
  export declare const disableDevLogs: () => void;
5
7
  //# sourceMappingURL=disableDevLogs.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"disableDevLogs.d.ts","sourceRoot":"","sources":["../src/disableDevLogs.ts"],"names":[],"mappings":"AAIA;;GAEG;AACH,eAAO,MAAM,cAAc,YAE1B,CAAC"}
1
+ {"version":3,"file":"disableDevLogs.d.ts","sourceRoot":"","sources":["../src/disableDevLogs.ts"],"names":[],"mappings":"AAIA;;;;GAIG;AACH,eAAO,MAAM,cAAc,QAAO,IAEjC,CAAC"}
@@ -1,28 +1,13 @@
1
1
  import type { HandlerDidErrorCallbackParam } from "@serwist/core";
2
2
  import type { PrecacheRouteOptions } from "@serwist/precaching";
3
+ import type { PrecacheFallbackEntry } from "@serwist/precaching";
3
4
  import type { RuntimeCaching } from "./types.js";
4
5
  export type FallbackMatcher = (_: HandlerDidErrorCallbackParam) => boolean;
5
- export interface FallbackEntry {
6
- /**
7
- * The matcher, which checks whether the fallback entry can be used
8
- * for a Request.
9
- */
10
- matcher: FallbackMatcher;
11
- /**
12
- * The fallback URL.
13
- */
14
- url: URL | string;
6
+ export interface FallbackEntry extends PrecacheFallbackEntry {
15
7
  /**
16
8
  * The revision used for precaching.
17
9
  */
18
10
  revision: string;
19
- /**
20
- * How the Response in the cache should be matched.
21
- *
22
- * @default
23
- * { ignoreSearch: true }
24
- */
25
- cacheMatchOptions?: MultiCacheQueryOptions;
26
11
  }
27
12
  export interface FallbacksOptions {
28
13
  /**
@@ -41,10 +26,14 @@ export interface FallbacksOptions {
41
26
  }
42
27
  /**
43
28
  * Precaches routes so that they can be used as a fallback when
44
- * a `RuntimeCaching` handler fails.
29
+ * a Strategy fails to generate a response.
30
+ *
31
+ * Note: This function mutates `runtimeCaching`!
32
+ *
33
+ * @see https://serwist.pages.dev/docs/sw/fallbacks
45
34
  * @param options
46
35
  * @returns The modified `RuntimeCaching` array. Using this value
47
- * is not needed, as it is simply the provided array.
36
+ * is not needed, as it is simply the array in `options`.
48
37
  */
49
38
  export declare const fallbacks: ({ runtimeCaching, entries, precacheOptions }: FallbacksOptions) => RuntimeCaching[];
50
39
  //# sourceMappingURL=fallbacks.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"fallbacks.d.ts","sourceRoot":"","sources":["../src/fallbacks.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,4BAA4B,EAAE,MAAM,eAAe,CAAC;AAClE,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,qBAAqB,CAAC;AAGhE,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAEjD,MAAM,MAAM,eAAe,GAAG,CAAC,CAAC,EAAE,4BAA4B,KAAK,OAAO,CAAC;AAE3E,MAAM,WAAW,aAAa;IAC5B;;;OAGG;IACH,OAAO,EAAE,eAAe,CAAC;IACzB;;OAEG;IACH,GAAG,EAAE,GAAG,GAAG,MAAM,CAAC;IAClB;;OAEG;IACH,QAAQ,EAAE,MAAM,CAAC;IACjB;;;;;OAKG;IACH,iBAAiB,CAAC,EAAE,sBAAsB,CAAC;CAC5C;AAED,MAAM,WAAW,gBAAgB;IAC/B;;OAEG;IACH,cAAc,EAAE,cAAc,EAAE,CAAC;IACjC;;OAEG;IACH,OAAO,EAAE,aAAa,EAAE,CAAC;IACzB;;;OAGG;IACH,eAAe,CAAC,EAAE,oBAAoB,CAAC;CACxC;AAED;;;;;;GAMG;AACH,eAAO,MAAM,SAAS,iDAAkD,gBAAgB,KAAG,cAAc,EA4BxG,CAAC"}
1
+ {"version":3,"file":"fallbacks.d.ts","sourceRoot":"","sources":["../src/fallbacks.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,4BAA4B,EAAE,MAAM,eAAe,CAAC;AAClE,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,qBAAqB,CAAC;AAEhE,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,qBAAqB,CAAC;AAGjE,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAEjD,MAAM,MAAM,eAAe,GAAG,CAAC,CAAC,EAAE,4BAA4B,KAAK,OAAO,CAAC;AAE3E,MAAM,WAAW,aAAc,SAAQ,qBAAqB;IAC1D;;OAEG;IACH,QAAQ,EAAE,MAAM,CAAC;CAClB;AAED,MAAM,WAAW,gBAAgB;IAC/B;;OAEG;IACH,cAAc,EAAE,cAAc,EAAE,CAAC;IACjC;;OAEG;IACH,OAAO,EAAE,aAAa,EAAE,CAAC;IACzB;;;OAGG;IACH,eAAe,CAAC,EAAE,oBAAoB,CAAC;CACxC;AAED;;;;;;;;;;GAUG;AACH,eAAO,MAAM,SAAS,iDAAkD,gBAAgB,KAAG,cAAc,EAoBxG,CAAC"}
@@ -35,6 +35,8 @@ export type HandlePrecachingOptions = {
35
35
  });
36
36
  /**
37
37
  * Handles a list of precache entries and cleans up outdated caches.
38
+ *
39
+ * @see https://serwist.pages.dev/docs/sw/handlePrecaching
38
40
  * @param options
39
41
  */
40
42
  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;;;;OAIG;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;;;GAGG;AACH,eAAO,MAAM,gBAAgB,4EAAqF,uBAAuB,SAoBxI,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,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;;;;OAIG;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"}
package/dist/index.js CHANGED
@@ -1,44 +1,24 @@
1
- import { precacheAndRoute, cleanupOutdatedCaches, createHandlerBoundToURL, PrecacheFallbackPlugin } from '@serwist/precaching';
1
+ import { precacheAndRoute, PrecacheFallbackPlugin, cleanupOutdatedCaches, createHandlerBoundToURL } from '@serwist/precaching';
2
+ import { Strategy } from '@serwist/strategies';
2
3
  import { registerRoute, NavigationRoute } from '@serwist/routing';
3
4
  import { setCacheNameDetails, clientsClaim } from '@serwist/core';
4
- import { logger } from '@serwist/core/internal';
5
5
  import { initialize } from '@serwist/google-analytics/initialize';
6
6
  import { enable } from '@serwist/navigation-preload';
7
- import { BackgroundSyncPlugin } from '@serwist/background-sync';
8
- import { BroadcastUpdatePlugin } from '@serwist/broadcast-update';
9
- import { CacheableResponsePlugin } from '@serwist/cacheable-response';
10
- import { ExpirationPlugin } from '@serwist/expiration';
11
- import { RangeRequestsPlugin } from '@serwist/range-requests';
12
- import { CacheFirst, CacheOnly, NetworkFirst, NetworkOnly, StaleWhileRevalidate } from '@serwist/strategies';
7
+ import { logger } from '@serwist/core/internal';
13
8
 
14
9
  const disableDevLogs = ()=>{
15
10
  self.__WB_DISABLE_DEV_LOGS = true;
16
11
  };
17
12
 
18
13
  const fallbacks = ({ runtimeCaching, entries, precacheOptions })=>{
19
- precacheAndRoute(entries.map(({ url, revision })=>({
20
- url: typeof url === "string" ? url : url.toString(),
21
- revision
22
- })), precacheOptions);
23
- runtimeCaching = runtimeCaching.map((cacheEntry)=>{
24
- if (!cacheEntry.options || cacheEntry.options.precacheFallback || cacheEntry.options.plugins?.some((plugin)=>"handlerDidError" in plugin)) {
25
- return cacheEntry;
26
- }
27
- if (!cacheEntry.options.plugins) {
28
- cacheEntry.options.plugins = [];
14
+ precacheAndRoute(entries, precacheOptions);
15
+ const fallbackPlugin = new PrecacheFallbackPlugin({
16
+ fallbackUrls: entries
17
+ });
18
+ runtimeCaching.forEach((cacheEntry)=>{
19
+ if (cacheEntry.handler instanceof Strategy && !cacheEntry.handler.plugins.some((plugin)=>"handlerDidError" in plugin)) {
20
+ cacheEntry.handler.plugins.push(fallbackPlugin);
29
21
  }
30
- cacheEntry.options.plugins.push({
31
- async handlerDidError (info) {
32
- for (const { matcher, url, cacheMatchOptions = {
33
- ignoreSearch: true
34
- } } of entries){
35
- if (matcher(info)) {
36
- return caches.match(url, cacheMatchOptions);
37
- }
38
- }
39
- return Response.error();
40
- }
41
- });
42
22
  return cacheEntry;
43
23
  });
44
24
  return runtimeCaching;
@@ -57,52 +37,20 @@ const handlePrecaching = ({ precacheEntries, precacheOptions, cleanupOutdatedCac
57
37
  }
58
38
  };
59
39
 
60
- const nonNullable = (value)=>value !== null && value !== undefined;
61
-
62
- const HANDLER_NAME_TO_METHOD = {
63
- CacheFirst,
64
- CacheOnly,
65
- NetworkFirst,
66
- NetworkOnly,
67
- StaleWhileRevalidate
68
- };
69
40
  const registerRuntimeCaching = (...runtimeCachingList)=>{
70
41
  if (!("__WB_FORCE_RUNTIME_CACHING" in globalThis)) {
71
42
  self.__WB_FORCE_RUNTIME_CACHING = false;
72
43
  }
73
44
  if (!self.__WB_FORCE_RUNTIME_CACHING && process.env.NODE_ENV !== "production") {
74
45
  logger.log("registerRuntimeCaching is disabled in development mode.");
75
- return;
76
- }
77
- for (const rcEntry of runtimeCachingList){
78
- if (typeof rcEntry.handler === "string") {
79
- const { cacheName, networkTimeoutSeconds, fetchOptions, matchOptions, plugins, backgroundSync, broadcastUpdate, cacheableResponse, expiration, precacheFallback, rangeRequests } = rcEntry.options;
80
- const Handler = HANDLER_NAME_TO_METHOD[rcEntry.handler];
81
- registerRoute(rcEntry.urlPattern, new Handler({
82
- cacheName: cacheName ?? undefined,
83
- networkTimeoutSeconds,
84
- fetchOptions,
85
- matchOptions,
86
- plugins: [
87
- ...plugins ?? [],
88
- backgroundSync && new BackgroundSyncPlugin(backgroundSync.name, backgroundSync.options),
89
- broadcastUpdate && new BroadcastUpdatePlugin({
90
- channelName: broadcastUpdate.channelName,
91
- ...broadcastUpdate.options
92
- }),
93
- cacheableResponse && new CacheableResponsePlugin(cacheableResponse),
94
- expiration && new ExpirationPlugin(expiration),
95
- precacheFallback && new PrecacheFallbackPlugin(precacheFallback),
96
- rangeRequests ? new RangeRequestsPlugin() : undefined
97
- ].filter(nonNullable)
98
- }), rcEntry.method);
99
- } else {
100
- registerRoute(rcEntry.urlPattern, rcEntry.handler, rcEntry.method);
46
+ } else {
47
+ for (const entry of runtimeCachingList){
48
+ registerRoute(entry.urlPattern, entry.handler, entry.method);
101
49
  }
102
50
  }
103
51
  };
104
52
 
105
- const installSerwist = ({ precacheEntries, precacheOptions, cleanupOutdatedCaches, skipWaiting = false, importScripts, navigationPreload = false, cacheId, clientsClaim: clientsClaim$1 = false, runtimeCaching, offlineAnalyticsConfig, disableDevLogs: disableDevLogs$1, fallbacks: fallbacks$1, ...options })=>{
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 })=>{
106
54
  if (!!importScripts && importScripts.length > 0) self.importScripts(...importScripts);
107
55
  if (navigationPreload) enable();
108
56
  if (cacheId !== undefined) {
@@ -138,7 +86,7 @@ const installSerwist = ({ precacheEntries, precacheOptions, cleanupOutdatedCache
138
86
  logger.log("runtimeCaching and fallbacks are disabled in development mode.");
139
87
  } else {
140
88
  if (fallbacks$1 !== undefined) {
141
- runtimeCaching = fallbacks({
89
+ fallbacks({
142
90
  ...fallbacks$1,
143
91
  runtimeCaching
144
92
  });
@@ -3,14 +3,61 @@ import type { FallbacksOptions } from "./fallbacks.js";
3
3
  import { type HandlePrecachingOptions } from "./handlePrecaching.js";
4
4
  import type { RuntimeCaching } from "./types.js";
5
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
+ */
6
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
+ */
7
18
  importScripts?: string[];
19
+ /**
20
+ * Enables Navigation Preload if it is supported.
21
+ *
22
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerRegistration/navigationPreload
23
+ */
8
24
  navigationPreload?: boolean;
25
+ /**
26
+ * Modifies the prefix of the default cache names used by Serwist packages.
27
+ */
9
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
+ */
10
35
  clientsClaim?: boolean;
36
+ /**
37
+ * A list of caching strategies.
38
+ *
39
+ * @see https://serwist.pages.dev/docs/sw/register-runtime-caching
40
+ */
11
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
+ */
12
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
+ */
13
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
+ */
14
61
  fallbacks?: Omit<FallbacksOptions, "runtimeCaching">;
15
62
  };
16
63
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"installSerwist.d.ts","sourceRoot":"","sources":["../src/installSerwist.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,KAAK,gCAAgC,EAAc,MAAM,sCAAsC,CAAC;AAIzG,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AAEvD,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,WAAW,CAAC,EAAE,OAAO,CAAC;IACtB,aAAa,CAAC,EAAE,MAAM,EAAE,CAAC;IACzB,iBAAiB,CAAC,EAAE,OAAO,CAAC;IAC5B,OAAO,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IAC7B,YAAY,CAAC,EAAE,OAAO,CAAC;IACvB,cAAc,CAAC,EAAE,cAAc,EAAE,CAAC;IAClC,sBAAsB,CAAC,EAAE,gCAAgC,GAAG,OAAO,CAAC;IACpE,cAAc,CAAC,EAAE,OAAO,CAAC;IACzB,SAAS,CAAC,EAAE,IAAI,CAAC,gBAAgB,EAAE,gBAAgB,CAAC,CAAC;CACtD,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,cAAc,qNAexB,qBAAqB,SA4DvB,CAAC"}
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"}
@@ -2,6 +2,8 @@ import type { RuntimeCaching } from "./types.js";
2
2
  /**
3
3
  * Registers caching strategies to a singleton Router instance. It is a simple
4
4
  * syntatic sugar for `@serwist/routing.registerRoute`.
5
+ *
6
+ * @see https://serwist.pages.dev/docs/sw/registerRuntimeCaching
5
7
  * @param runtimeCachingList
6
8
  * @returns
7
9
  */
@@ -1 +1 @@
1
- {"version":3,"file":"registerRuntimeCaching.d.ts","sourceRoot":"","sources":["../src/registerRuntimeCaching.ts"],"names":[],"mappings":"AAWA,OAAO,KAAK,EAAE,cAAc,EAAgB,MAAM,YAAY,CAAC;AAY/D;;;;;GAKG;AACH,eAAO,MAAM,sBAAsB,0BAA2B,cAAc,EAAE,SAsD7E,CAAC"}
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"}
package/dist/types.d.ts CHANGED
@@ -1,19 +1,6 @@
1
- import type { QueueOptions } from "@serwist/background-sync";
2
- import type { BroadcastCacheUpdateOptions } from "@serwist/broadcast-update";
3
- import type { CacheableResponseOptions } from "@serwist/cacheable-response";
4
- import type { RouteHandler, RouteMatchCallback, SerwistPlugin } from "@serwist/core";
5
- import type { ExpirationPluginOptions } from "@serwist/expiration";
1
+ import type { RouteHandler, RouteMatchCallback } from "@serwist/core";
6
2
  import type { HTTPMethod } from "@serwist/routing";
7
- export type StrategyName = "CacheFirst" | "CacheOnly" | "NetworkFirst" | "NetworkOnly" | "StaleWhileRevalidate";
8
3
  export interface RuntimeCaching {
9
- /**
10
- * This determines how the runtime route will generate a response.
11
- * To use one of the built-in `@serwist/strategies`, provide its name,
12
- * like `'NetworkFirst'`.
13
- * Alternatively, this can be a `@serwist/core.RouteHandler` callback
14
- * function with custom response logic.
15
- */
16
- handler: RouteHandler | StrategyName;
17
4
  /**
18
5
  * The HTTP method to match against. The default value of `'GET'` is normally
19
6
  * sufficient, unless you explicitly need to match `'POST'`, `'PUT'`, or
@@ -21,80 +8,6 @@ export interface RuntimeCaching {
21
8
  * @default "GET"
22
9
  */
23
10
  method?: HTTPMethod;
24
- options?: {
25
- /**
26
- * Configuring this will add a
27
- * `@serwist/background-sync.BackgroundSyncPlugin` instance to the
28
- * `@serwist/strategies` configured in `handler`.
29
- */
30
- backgroundSync?: {
31
- name: string;
32
- options?: QueueOptions;
33
- };
34
- /**
35
- * Configuring this will add a
36
- * `@serwist/broadcast-update.BroadcastUpdatePlugin` instance to the
37
- * `@serwist/strategies` configured in `handler`.
38
- */
39
- broadcastUpdate?: {
40
- channelName?: string;
41
- options: BroadcastCacheUpdateOptions;
42
- };
43
- /**
44
- * Configuring this will add a
45
- * `@serwist/cacheable-response.CacheableResponsePlugin` instance to
46
- * the `@serwist/strategies` configured in `handler`.
47
- */
48
- cacheableResponse?: CacheableResponseOptions;
49
- /**
50
- * If provided, this will set the `cacheName` property of the
51
- * `@serwist/strategies` configured in `handler`.
52
- */
53
- cacheName?: string | null;
54
- /**
55
- * Configuring this will add a
56
- * `@serwist/expiration.ExpirationPlugin` instance to
57
- * the `@serwist/strategies` configured in `handler`.
58
- */
59
- expiration?: ExpirationPluginOptions;
60
- /**
61
- * If provided, this will set the `networkTimeoutSeconds` property of the
62
- * `@serwist/strategies` configured in `handler`. Note that only
63
- * `'NetworkFirst'` and `'NetworkOnly'` support `networkTimeoutSeconds`.
64
- */
65
- networkTimeoutSeconds?: number;
66
- /**
67
- * Configuring this allows the use of one or more Serwist plugins that
68
- * don't have "shortcut" options (like `expiration` for
69
- * `@serwist/expiration.ExpirationPlugin`). The plugins provided here
70
- * will be added to the `@serwist/strategies` configured in `handler`.
71
- */
72
- plugins?: SerwistPlugin[];
73
- /**
74
- * Configuring this will add a
75
- * `@serwist/precaching.PrecacheFallbackPlugin` instance to
76
- * the `@serwist/strategies` configured in `handler`.
77
- */
78
- precacheFallback?: {
79
- fallbackURL: string;
80
- };
81
- /**
82
- * Enabling this will add a
83
- * `@serwist/range-requests.RangeRequestsPlugin` instance to
84
- * the `@serwist/strategies` configured in `handler`.
85
- */
86
- rangeRequests?: boolean;
87
- /**
88
- * Configuring this will pass along the `fetchOptions` value to
89
- * the `@serwist/strategies` configured in `handler`.
90
- */
91
- fetchOptions?: RequestInit;
92
- /**
93
- * Configuring this will pass along the `matchOptions` value to
94
- * the `@serwist/strategies` configured in `handler`.
95
- */
96
- matchOptions?: CacheQueryOptions;
97
- };
98
11
  /**
99
12
  * This match criteria determines whether the configured handler will
100
13
  * generate a response for any requests that don't match one of the precached
@@ -106,5 +19,11 @@ export interface RuntimeCaching {
106
19
  * `@serwist/core.RouteMatchCallback` function for greatest flexibility.
107
20
  */
108
21
  urlPattern: RegExp | string | RouteMatchCallback;
22
+ /**
23
+ * This determines how the runtime route will generate a response. It
24
+ * can be a `@serwist/core.RouteHandler` callback function with custom
25
+ * response logic.
26
+ */
27
+ handler: RouteHandler;
109
28
  }
110
29
  //# sourceMappingURL=types.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,0BAA0B,CAAC;AAC7D,OAAO,KAAK,EAAE,2BAA2B,EAAE,MAAM,2BAA2B,CAAC;AAC7E,OAAO,KAAK,EAAE,wBAAwB,EAAE,MAAM,6BAA6B,CAAC;AAC5E,OAAO,KAAK,EAAE,YAAY,EAAE,kBAAkB,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC;AACrF,OAAO,KAAK,EAAE,uBAAuB,EAAE,MAAM,qBAAqB,CAAC;AACnE,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAEnD,MAAM,MAAM,YAAY,GAAG,YAAY,GAAG,WAAW,GAAG,cAAc,GAAG,aAAa,GAAG,sBAAsB,CAAC;AAEhH,MAAM,WAAW,cAAc;IAC7B;;;;;;OAMG;IACH,OAAO,EAAE,YAAY,GAAG,YAAY,CAAC;IACrC;;;;;OAKG;IACH,MAAM,CAAC,EAAE,UAAU,CAAC;IACpB,OAAO,CAAC,EAAE;QACR;;;;WAIG;QACH,cAAc,CAAC,EAAE;YACf,IAAI,EAAE,MAAM,CAAC;YACb,OAAO,CAAC,EAAE,YAAY,CAAC;SACxB,CAAC;QACF;;;;WAIG;QACH,eAAe,CAAC,EAAE;YAGhB,WAAW,CAAC,EAAE,MAAM,CAAC;YACrB,OAAO,EAAE,2BAA2B,CAAC;SACtC,CAAC;QACF;;;;WAIG;QACH,iBAAiB,CAAC,EAAE,wBAAwB,CAAC;QAC7C;;;WAGG;QACH,SAAS,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;QAC1B;;;;WAIG;QACH,UAAU,CAAC,EAAE,uBAAuB,CAAC;QACrC;;;;WAIG;QACH,qBAAqB,CAAC,EAAE,MAAM,CAAC;QAC/B;;;;;WAKG;QACH,OAAO,CAAC,EAAE,aAAa,EAAE,CAAC;QAC1B;;;;WAIG;QACH,gBAAgB,CAAC,EAAE;YACjB,WAAW,EAAE,MAAM,CAAC;SACrB,CAAC;QACF;;;;WAIG;QACH,aAAa,CAAC,EAAE,OAAO,CAAC;QACxB;;;WAGG;QACH,YAAY,CAAC,EAAE,WAAW,CAAC;QAC3B;;;WAGG;QACH,YAAY,CAAC,EAAE,iBAAiB,CAAC;KAClC,CAAC;IACF;;;;;;;;;OASG;IACH,UAAU,EAAE,MAAM,GAAG,MAAM,GAAG,kBAAkB,CAAC;CAClD"}
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,kBAAkB,EAAE,MAAM,eAAe,CAAC;AACtE,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAEnD,MAAM,WAAW,cAAc;IAC7B;;;;;OAKG;IACH,MAAM,CAAC,EAAE,UAAU,CAAC;IACpB;;;;;;;;;OASG;IACH,UAAU,EAAE,MAAM,GAAG,MAAM,GAAG,kBAAkB,CAAC;IACjD;;;;OAIG;IACH,OAAO,EAAE,YAAY,CAAC;CACvB"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@serwist/sw",
3
- "version": "9.0.0-preview.2",
3
+ "version": "9.0.0-preview.3",
4
4
  "type": "module",
5
5
  "description": "This module makes it easy to get started with the Serwist service worker libraries.",
6
6
  "files": [
@@ -28,23 +28,23 @@
28
28
  "./package.json": "./package.json"
29
29
  },
30
30
  "dependencies": {
31
- "@serwist/background-sync": "9.0.0-preview.2",
32
- "@serwist/broadcast-update": "9.0.0-preview.2",
33
- "@serwist/cacheable-response": "9.0.0-preview.2",
34
- "@serwist/core": "9.0.0-preview.2",
35
- "@serwist/expiration": "9.0.0-preview.2",
36
- "@serwist/google-analytics": "9.0.0-preview.2",
37
- "@serwist/navigation-preload": "9.0.0-preview.2",
38
- "@serwist/precaching": "9.0.0-preview.2",
39
- "@serwist/range-requests": "9.0.0-preview.2",
40
- "@serwist/routing": "9.0.0-preview.2",
41
- "@serwist/strategies": "9.0.0-preview.2"
31
+ "@serwist/background-sync": "9.0.0-preview.3",
32
+ "@serwist/broadcast-update": "9.0.0-preview.3",
33
+ "@serwist/cacheable-response": "9.0.0-preview.3",
34
+ "@serwist/core": "9.0.0-preview.3",
35
+ "@serwist/expiration": "9.0.0-preview.3",
36
+ "@serwist/google-analytics": "9.0.0-preview.3",
37
+ "@serwist/navigation-preload": "9.0.0-preview.3",
38
+ "@serwist/precaching": "9.0.0-preview.3",
39
+ "@serwist/range-requests": "9.0.0-preview.3",
40
+ "@serwist/routing": "9.0.0-preview.3",
41
+ "@serwist/strategies": "9.0.0-preview.3"
42
42
  },
43
43
  "devDependencies": {
44
44
  "rollup": "4.9.6",
45
45
  "typescript": "5.4.0-dev.20240206",
46
- "@serwist/constants": "9.0.0-preview.2",
47
- "@serwist/utils": "9.0.0-preview.2"
46
+ "@serwist/constants": "9.0.0-preview.3",
47
+ "@serwist/utils": "9.0.0-preview.3"
48
48
  },
49
49
  "peerDependencies": {
50
50
  "typescript": ">=5.0.0"
@@ -4,7 +4,9 @@ declare const self: WorkerGlobalScope & {
4
4
 
5
5
  /**
6
6
  * Disables Serwist's logging in development mode.
7
+ *
8
+ * @see https://serwist.pages.dev/docs/sw/disable-dev-logs
7
9
  */
8
- export const disableDevLogs = () => {
10
+ export const disableDevLogs = (): void => {
9
11
  self.__WB_DISABLE_DEV_LOGS = true;
10
12
  };
package/src/fallbacks.ts CHANGED
@@ -1,32 +1,18 @@
1
1
  import type { HandlerDidErrorCallbackParam } from "@serwist/core";
2
2
  import type { PrecacheRouteOptions } from "@serwist/precaching";
3
- import { precacheAndRoute } from "@serwist/precaching";
3
+ import { PrecacheFallbackPlugin, precacheAndRoute } from "@serwist/precaching";
4
+ import type { PrecacheFallbackEntry } from "@serwist/precaching";
4
5
 
6
+ import { Strategy } from "@serwist/strategies";
5
7
  import type { RuntimeCaching } from "./types.js";
6
8
 
7
9
  export type FallbackMatcher = (_: HandlerDidErrorCallbackParam) => boolean;
8
10
 
9
- export interface FallbackEntry {
10
- /**
11
- * The matcher, which checks whether the fallback entry can be used
12
- * for a Request.
13
- */
14
- matcher: FallbackMatcher;
15
- /**
16
- * The fallback URL.
17
- */
18
- url: URL | string;
11
+ export interface FallbackEntry extends PrecacheFallbackEntry {
19
12
  /**
20
13
  * The revision used for precaching.
21
14
  */
22
15
  revision: string;
23
- /**
24
- * How the Response in the cache should be matched.
25
- *
26
- * @default
27
- * { ignoreSearch: true }
28
- */
29
- cacheMatchOptions?: MultiCacheQueryOptions;
30
16
  }
31
17
 
32
18
  export interface FallbacksOptions {
@@ -46,38 +32,34 @@ export interface FallbacksOptions {
46
32
  }
47
33
 
48
34
  /**
49
- * Precaches routes so that they can be used as a fallback when
50
- * a `RuntimeCaching` handler fails.
35
+ * Precaches routes so that they can be used as a fallback when
36
+ * a Strategy fails to generate a response.
37
+ *
38
+ * Note: This function mutates `runtimeCaching`!
39
+ *
40
+ * @see https://serwist.pages.dev/docs/sw/fallbacks
51
41
  * @param options
52
- * @returns The modified `RuntimeCaching` array. Using this value
53
- * is not needed, as it is simply the provided array.
42
+ * @returns The modified `RuntimeCaching` array. Using this value
43
+ * is not needed, as it is simply the array in `options`.
54
44
  */
55
45
  export const fallbacks = ({ runtimeCaching, entries, precacheOptions }: FallbacksOptions): RuntimeCaching[] => {
56
- precacheAndRoute(
57
- entries.map(({ url, revision }) => ({ url: typeof url === "string" ? url : url.toString(), revision })),
58
- precacheOptions,
59
- );
60
- runtimeCaching = runtimeCaching.map((cacheEntry) => {
61
- if (!cacheEntry.options || cacheEntry.options.precacheFallback || cacheEntry.options.plugins?.some((plugin) => "handlerDidError" in plugin)) {
62
- return cacheEntry;
63
- }
46
+ precacheAndRoute(entries, precacheOptions);
64
47
 
65
- if (!cacheEntry.options.plugins) {
66
- cacheEntry.options.plugins = [];
67
- }
48
+ const fallbackPlugin = new PrecacheFallbackPlugin({
49
+ fallbackUrls: entries,
50
+ });
68
51
 
69
- cacheEntry.options.plugins.push({
70
- async handlerDidError(info) {
71
- for (const { matcher, url, cacheMatchOptions = { ignoreSearch: true } } of entries) {
72
- if (matcher(info)) {
73
- return caches.match(url, cacheMatchOptions);
74
- }
75
- }
76
- return Response.error();
77
- },
78
- });
52
+ runtimeCaching.forEach((cacheEntry) => {
53
+ if (
54
+ cacheEntry.handler instanceof Strategy &&
55
+ // PrecacheFallbackPlugin also has `handlerDidError`, so we don't need to check for its instances.
56
+ !cacheEntry.handler.plugins.some((plugin) => "handlerDidError" in plugin)
57
+ ) {
58
+ cacheEntry.handler.plugins.push(fallbackPlugin);
59
+ }
79
60
 
80
61
  return cacheEntry;
81
62
  });
63
+
82
64
  return runtimeCaching;
83
65
  };
@@ -13,7 +13,7 @@ export type HandlePrecachingOptions = {
13
13
  precacheOptions?: PrecacheRouteOptions;
14
14
  /**
15
15
  * Whether outdated caches should be removed.
16
- *
16
+ *
17
17
  * @default false
18
18
  */
19
19
  cleanupOutdatedCaches?: boolean;
@@ -40,9 +40,11 @@ export type HandlePrecachingOptions = {
40
40
 
41
41
  /**
42
42
  * Handles a list of precache entries and cleans up outdated caches.
43
+ *
44
+ * @see https://serwist.pages.dev/docs/sw/handlePrecaching
43
45
  * @param options
44
46
  */
45
- export const handlePrecaching = ({ precacheEntries, precacheOptions, cleanupOutdatedCaches = false, ...options }: HandlePrecachingOptions) => {
47
+ export const handlePrecaching = ({ precacheEntries, precacheOptions, cleanupOutdatedCaches = false, ...options }: HandlePrecachingOptions): void => {
46
48
  if (!!precacheEntries && precacheEntries.length > 0) {
47
49
  /**
48
50
  * The precacheAndRoute() method efficiently caches and responds to
@@ -1,11 +1,11 @@
1
1
  import { clientsClaim as clientsClaimImpl, setCacheNameDetails } from "@serwist/core";
2
- import { logger } from "@serwist/core/internal";
3
2
  import { type GoogleAnalyticsInitializeOptions, initialize } from "@serwist/google-analytics/initialize";
4
3
  import { enable } from "@serwist/navigation-preload";
5
4
 
5
+ import { logger } from "@serwist/core/internal";
6
6
  import { disableDevLogs as disableDevLogsImpl } from "./disableDevLogs.js";
7
- import type { FallbacksOptions } from "./fallbacks.js";
8
7
  import { fallbacks as fallbacksImpl } from "./fallbacks.js";
8
+ import type { FallbacksOptions } from "./fallbacks.js";
9
9
  import { type HandlePrecachingOptions, handlePrecaching } from "./handlePrecaching.js";
10
10
  import { registerRuntimeCaching } from "./registerRuntimeCaching.js";
11
11
  import type { RuntimeCaching } from "./types.js";
@@ -13,14 +13,61 @@ import type { RuntimeCaching } from "./types.js";
13
13
  declare const self: ServiceWorkerGlobalScope;
14
14
 
15
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
+ */
16
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
+ */
17
28
  importScripts?: string[];
29
+ /**
30
+ * Enables Navigation Preload if it is supported.
31
+ *
32
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerRegistration/navigationPreload
33
+ */
18
34
  navigationPreload?: boolean;
35
+ /**
36
+ * Modifies the prefix of the default cache names used by Serwist packages.
37
+ */
19
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
+ */
20
45
  clientsClaim?: boolean;
46
+ /**
47
+ * A list of caching strategies.
48
+ *
49
+ * @see https://serwist.pages.dev/docs/sw/register-runtime-caching
50
+ */
21
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
+ */
22
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
+ */
23
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
+ */
24
71
  fallbacks?: Omit<FallbacksOptions, "runtimeCaching">;
25
72
  };
26
73
 
@@ -40,10 +87,10 @@ export const installSerwist = ({
40
87
  clientsClaim = false,
41
88
  runtimeCaching,
42
89
  offlineAnalyticsConfig,
43
- disableDevLogs,
90
+ disableDevLogs = false,
44
91
  fallbacks,
45
92
  ...options
46
- }: InstallSerwistOptions) => {
93
+ }: InstallSerwistOptions): void => {
47
94
  if (!!importScripts && importScripts.length > 0) self.importScripts(...importScripts);
48
95
 
49
96
  if (navigationPreload) enable();
@@ -85,10 +132,7 @@ export const installSerwist = ({
85
132
  logger.log("runtimeCaching and fallbacks are disabled in development mode.");
86
133
  } else {
87
134
  if (fallbacks !== undefined) {
88
- runtimeCaching = fallbacksImpl({
89
- ...fallbacks,
90
- runtimeCaching,
91
- });
135
+ fallbacksImpl({ ...fallbacks, runtimeCaching });
92
136
  }
93
137
  registerRuntimeCaching(...runtimeCaching);
94
138
  }
@@ -1,84 +1,28 @@
1
- import { BackgroundSyncPlugin } from "@serwist/background-sync";
2
- import { BroadcastUpdatePlugin } from "@serwist/broadcast-update";
3
- import { CacheableResponsePlugin } from "@serwist/cacheable-response";
4
1
  import { logger } from "@serwist/core/internal";
5
- import { ExpirationPlugin } from "@serwist/expiration";
6
- import { PrecacheFallbackPlugin } from "@serwist/precaching";
7
- import { RangeRequestsPlugin } from "@serwist/range-requests";
8
2
  import { registerRoute } from "@serwist/routing";
9
- import { CacheFirst, CacheOnly, NetworkFirst, NetworkOnly, StaleWhileRevalidate } from "@serwist/strategies";
10
- import { nonNullable } from "@serwist/utils";
11
3
 
12
- import type { RuntimeCaching, StrategyName } from "./types.js";
4
+ import type { RuntimeCaching } from "./types.js";
13
5
 
14
6
  declare const self: ServiceWorkerGlobalScope;
15
7
 
16
- const HANDLER_NAME_TO_METHOD = {
17
- CacheFirst,
18
- CacheOnly,
19
- NetworkFirst,
20
- NetworkOnly,
21
- StaleWhileRevalidate,
22
- } satisfies Record<StrategyName, unknown>;
23
-
24
8
  /**
25
9
  * Registers caching strategies to a singleton Router instance. It is a simple
26
10
  * syntatic sugar for `@serwist/routing.registerRoute`.
11
+ *
12
+ * @see https://serwist.pages.dev/docs/sw/registerRuntimeCaching
27
13
  * @param runtimeCachingList
28
14
  * @returns
29
15
  */
30
- export const registerRuntimeCaching = (...runtimeCachingList: RuntimeCaching[]) => {
16
+ export const registerRuntimeCaching = (...runtimeCachingList: RuntimeCaching[]): void => {
31
17
  if (!("__WB_FORCE_RUNTIME_CACHING" in globalThis)) {
32
18
  self.__WB_FORCE_RUNTIME_CACHING = false;
33
19
  }
34
20
 
35
21
  if (!self.__WB_FORCE_RUNTIME_CACHING && process.env.NODE_ENV !== "production") {
36
22
  logger.log("registerRuntimeCaching is disabled in development mode.");
37
- return;
38
- }
39
-
40
- for (const rcEntry of runtimeCachingList) {
41
- if (typeof rcEntry.handler === "string") {
42
- const {
43
- cacheName,
44
- networkTimeoutSeconds,
45
- fetchOptions,
46
- matchOptions,
47
- plugins,
48
- backgroundSync,
49
- broadcastUpdate,
50
- cacheableResponse,
51
- expiration,
52
- precacheFallback,
53
- rangeRequests,
54
- } = rcEntry.options!; // entry.options is always defined when entry.handler is of type 'string'.
55
- const Handler = HANDLER_NAME_TO_METHOD[rcEntry.handler];
56
- registerRoute(
57
- rcEntry.urlPattern,
58
- new Handler({
59
- cacheName: cacheName ?? undefined,
60
- networkTimeoutSeconds,
61
- fetchOptions,
62
- matchOptions,
63
- plugins: [
64
- ...(plugins ?? []),
65
- backgroundSync && new BackgroundSyncPlugin(backgroundSync.name, backgroundSync.options),
66
- broadcastUpdate &&
67
- new BroadcastUpdatePlugin({
68
- // @ts-expect-error weird...
69
- channelName: broadcastUpdate.channelName,
70
- ...broadcastUpdate.options,
71
- }),
72
- cacheableResponse && new CacheableResponsePlugin(cacheableResponse),
73
- expiration && new ExpirationPlugin(expiration),
74
- precacheFallback && new PrecacheFallbackPlugin(precacheFallback),
75
- rangeRequests ? new RangeRequestsPlugin() : undefined,
76
- ].filter(nonNullable),
77
- }),
78
- rcEntry.method,
79
- );
80
- } else {
81
- registerRoute(rcEntry.urlPattern, rcEntry.handler, rcEntry.method);
23
+ } else {
24
+ for (const entry of runtimeCachingList) {
25
+ registerRoute(entry.urlPattern, entry.handler, entry.method);
82
26
  }
83
27
  }
84
28
  };
package/src/types.ts CHANGED
@@ -1,21 +1,7 @@
1
- import type { QueueOptions } from "@serwist/background-sync";
2
- import type { BroadcastCacheUpdateOptions } from "@serwist/broadcast-update";
3
- import type { CacheableResponseOptions } from "@serwist/cacheable-response";
4
- import type { RouteHandler, RouteMatchCallback, SerwistPlugin } from "@serwist/core";
5
- import type { ExpirationPluginOptions } from "@serwist/expiration";
1
+ import type { RouteHandler, RouteMatchCallback } from "@serwist/core";
6
2
  import type { HTTPMethod } from "@serwist/routing";
7
3
 
8
- export type StrategyName = "CacheFirst" | "CacheOnly" | "NetworkFirst" | "NetworkOnly" | "StaleWhileRevalidate";
9
-
10
4
  export interface RuntimeCaching {
11
- /**
12
- * This determines how the runtime route will generate a response.
13
- * To use one of the built-in `@serwist/strategies`, provide its name,
14
- * like `'NetworkFirst'`.
15
- * Alternatively, this can be a `@serwist/core.RouteHandler` callback
16
- * function with custom response logic.
17
- */
18
- handler: RouteHandler | StrategyName;
19
5
  /**
20
6
  * The HTTP method to match against. The default value of `'GET'` is normally
21
7
  * sufficient, unless you explicitly need to match `'POST'`, `'PUT'`, or
@@ -23,82 +9,6 @@ export interface RuntimeCaching {
23
9
  * @default "GET"
24
10
  */
25
11
  method?: HTTPMethod;
26
- options?: {
27
- /**
28
- * Configuring this will add a
29
- * `@serwist/background-sync.BackgroundSyncPlugin` instance to the
30
- * `@serwist/strategies` configured in `handler`.
31
- */
32
- backgroundSync?: {
33
- name: string;
34
- options?: QueueOptions;
35
- };
36
- /**
37
- * Configuring this will add a
38
- * `@serwist/broadcast-update.BroadcastUpdatePlugin` instance to the
39
- * `@serwist/strategies` configured in `handler`.
40
- */
41
- broadcastUpdate?: {
42
- // TODO: This option is ignored since we switched to using postMessage().
43
- // Remove it in the next major release.
44
- channelName?: string;
45
- options: BroadcastCacheUpdateOptions;
46
- };
47
- /**
48
- * Configuring this will add a
49
- * `@serwist/cacheable-response.CacheableResponsePlugin` instance to
50
- * the `@serwist/strategies` configured in `handler`.
51
- */
52
- cacheableResponse?: CacheableResponseOptions;
53
- /**
54
- * If provided, this will set the `cacheName` property of the
55
- * `@serwist/strategies` configured in `handler`.
56
- */
57
- cacheName?: string | null;
58
- /**
59
- * Configuring this will add a
60
- * `@serwist/expiration.ExpirationPlugin` instance to
61
- * the `@serwist/strategies` configured in `handler`.
62
- */
63
- expiration?: ExpirationPluginOptions;
64
- /**
65
- * If provided, this will set the `networkTimeoutSeconds` property of the
66
- * `@serwist/strategies` configured in `handler`. Note that only
67
- * `'NetworkFirst'` and `'NetworkOnly'` support `networkTimeoutSeconds`.
68
- */
69
- networkTimeoutSeconds?: number;
70
- /**
71
- * Configuring this allows the use of one or more Serwist plugins that
72
- * don't have "shortcut" options (like `expiration` for
73
- * `@serwist/expiration.ExpirationPlugin`). The plugins provided here
74
- * will be added to the `@serwist/strategies` configured in `handler`.
75
- */
76
- plugins?: SerwistPlugin[];
77
- /**
78
- * Configuring this will add a
79
- * `@serwist/precaching.PrecacheFallbackPlugin` instance to
80
- * the `@serwist/strategies` configured in `handler`.
81
- */
82
- precacheFallback?: {
83
- fallbackURL: string;
84
- };
85
- /**
86
- * Enabling this will add a
87
- * `@serwist/range-requests.RangeRequestsPlugin` instance to
88
- * the `@serwist/strategies` configured in `handler`.
89
- */
90
- rangeRequests?: boolean;
91
- /**
92
- * Configuring this will pass along the `fetchOptions` value to
93
- * the `@serwist/strategies` configured in `handler`.
94
- */
95
- fetchOptions?: RequestInit;
96
- /**
97
- * Configuring this will pass along the `matchOptions` value to
98
- * the `@serwist/strategies` configured in `handler`.
99
- */
100
- matchOptions?: CacheQueryOptions;
101
- };
102
12
  /**
103
13
  * This match criteria determines whether the configured handler will
104
14
  * generate a response for any requests that don't match one of the precached
@@ -110,4 +20,10 @@ export interface RuntimeCaching {
110
20
  * `@serwist/core.RouteMatchCallback` function for greatest flexibility.
111
21
  */
112
22
  urlPattern: RegExp | string | RouteMatchCallback;
23
+ /**
24
+ * This determines how the runtime route will generate a response. It
25
+ * can be a `@serwist/core.RouteHandler` callback function with custom
26
+ * response logic.
27
+ */
28
+ handler: RouteHandler;
113
29
  }