@serwist/sw 8.0.5 → 8.1.1

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,32 @@
1
+ import type { RuntimeCaching } from "@serwist/build";
2
+ import type { HandlerDidErrorCallbackParam } from "@serwist/core";
3
+ import type { PrecacheRouteOptions } from "@serwist/precaching";
4
+ 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;
15
+ /**
16
+ * The revision used for precaching.
17
+ */
18
+ revision: string;
19
+ /**
20
+ * How the Response in the cache should be matched.
21
+ *
22
+ * @default
23
+ * { ignoreSearch: true }
24
+ */
25
+ cacheMatchOptions?: MultiCacheQueryOptions;
26
+ }
27
+ export interface FallbacksOptions {
28
+ runtimeCaching: RuntimeCaching[];
29
+ entries: FallbackEntry[];
30
+ precacheOptions?: PrecacheRouteOptions;
31
+ }
32
+ export declare const fallbacks: ({ runtimeCaching, entries, precacheOptions }: FallbacksOptions) => RuntimeCaching[];
package/dist/index.d.ts CHANGED
@@ -1,6 +1,8 @@
1
1
  import type { RuntimeCaching } from "@serwist/build";
2
2
  import { type GoogleAnalyticsInitializeOptions } from "@serwist/google-analytics/initialize";
3
3
  import { disableDevLogs } from "./disableDevLogs.js";
4
+ import type { FallbackEntry, FallbackMatcher, FallbacksOptions } from "./fallbacks.js";
5
+ import { fallbacks } from "./fallbacks.js";
4
6
  import { handlePrecaching, type HandlePrecachingOptions } from "./handlePrecaching.js";
5
7
  import { registerRuntimeCaching } from "./registerRuntimeCaching.js";
6
8
  export type SerwistOptions = HandlePrecachingOptions & {
@@ -12,6 +14,8 @@ export type SerwistOptions = HandlePrecachingOptions & {
12
14
  runtimeCaching?: RuntimeCaching[];
13
15
  offlineAnalyticsConfig?: GoogleAnalyticsInitializeOptions | boolean;
14
16
  disableDevLogs?: boolean;
17
+ fallbacks?: Omit<FallbacksOptions, "runtimeCaching">;
15
18
  };
16
- export declare const installSerwist: ({ precacheEntries, precacheOptions, cleanupOutdatedCaches, skipWaiting: shouldSkipWaiting, importScripts: scriptsToImport, navigationPreload, cacheId, clientsClaim: shouldClaimClients, runtimeCaching, offlineAnalyticsConfig, disableDevLogs: shouldDisableDevLogs, ...options }: SerwistOptions) => void;
17
- export { disableDevLogs, handlePrecaching, registerRuntimeCaching };
19
+ export declare const installSerwist: ({ precacheEntries, precacheOptions, cleanupOutdatedCaches, skipWaiting: shouldSkipWaiting, importScripts: scriptsToImport, navigationPreload, cacheId, clientsClaim: shouldClaimClients, runtimeCaching, offlineAnalyticsConfig, disableDevLogs: shouldDisableDevLogs, fallbacks: fallbacksOptions, ...options }: SerwistOptions) => void;
20
+ export { disableDevLogs, fallbacks, handlePrecaching, registerRuntimeCaching };
21
+ export type { FallbackEntry, FallbackMatcher, FallbacksOptions, HandlePrecachingOptions };
package/dist/index.js CHANGED
@@ -14,6 +14,35 @@ const disableDevLogs = ()=>{
14
14
  self.__WB_DISABLE_DEV_LOGS = true;
15
15
  };
16
16
 
17
+ const fallbacks = ({ runtimeCaching, entries, precacheOptions })=>{
18
+ precacheAndRoute(entries.map(({ url, revision })=>({
19
+ url: typeof url === "string" ? url : url.toString(),
20
+ revision
21
+ })), precacheOptions);
22
+ runtimeCaching = runtimeCaching.map((cacheEntry)=>{
23
+ if (!cacheEntry.options || cacheEntry.options.precacheFallback || cacheEntry.options.plugins?.some((plugin)=>"handlerDidError" in plugin)) {
24
+ return cacheEntry;
25
+ }
26
+ if (!cacheEntry.options.plugins) {
27
+ cacheEntry.options.plugins = [];
28
+ }
29
+ cacheEntry.options.plugins.push({
30
+ async handlerDidError (info) {
31
+ for (const { matcher, url, cacheMatchOptions = {
32
+ ignoreSearch: true
33
+ } } of entries){
34
+ if (matcher(info)) {
35
+ return caches.match(url, cacheMatchOptions);
36
+ }
37
+ }
38
+ return Response.error();
39
+ }
40
+ });
41
+ return cacheEntry;
42
+ });
43
+ return runtimeCaching;
44
+ };
45
+
17
46
  const handlePrecaching = ({ precacheEntries, precacheOptions, cleanupOutdatedCaches: shouldCleanupOutdatedCaches = false, ...options })=>{
18
47
  if (!!precacheEntries && precacheEntries.length > 0) {
19
48
  /**
@@ -70,7 +99,7 @@ const registerRuntimeCaching = (...runtimeCachingList)=>{
70
99
  }
71
100
  };
72
101
 
73
- const installSerwist = ({ precacheEntries, precacheOptions, cleanupOutdatedCaches, skipWaiting: shouldSkipWaiting = false, importScripts: scriptsToImport, navigationPreload = false, cacheId, clientsClaim: shouldClaimClients = false, runtimeCaching, offlineAnalyticsConfig, disableDevLogs: shouldDisableDevLogs, ...options })=>{
102
+ const installSerwist = ({ precacheEntries, precacheOptions, cleanupOutdatedCaches, skipWaiting: shouldSkipWaiting = false, importScripts: scriptsToImport, navigationPreload = false, cacheId, clientsClaim: shouldClaimClients = false, runtimeCaching, offlineAnalyticsConfig, disableDevLogs: shouldDisableDevLogs, fallbacks: fallbacksOptions, ...options })=>{
74
103
  if (!!scriptsToImport && scriptsToImport.length > 0) self.importScripts(...scriptsToImport);
75
104
  if (navigationPreload) enable();
76
105
  if (cacheId !== undefined) setCacheNameDetails({
@@ -96,7 +125,15 @@ const installSerwist = ({ precacheEntries, precacheOptions, cleanupOutdatedCache
96
125
  navigateFallbackDenylist: options.navigateFallbackDenylist
97
126
  }
98
127
  });
99
- if (runtimeCaching !== undefined) registerRuntimeCaching(...runtimeCaching);
128
+ if (runtimeCaching !== undefined) {
129
+ if (fallbacksOptions !== undefined) {
130
+ runtimeCaching = fallbacks({
131
+ ...fallbacksOptions,
132
+ runtimeCaching
133
+ });
134
+ }
135
+ registerRuntimeCaching(...runtimeCaching);
136
+ }
100
137
  if (offlineAnalyticsConfig !== undefined) {
101
138
  if (typeof offlineAnalyticsConfig === "boolean") {
102
139
  offlineAnalyticsConfig && initialize();
@@ -107,4 +144,4 @@ const installSerwist = ({ precacheEntries, precacheOptions, cleanupOutdatedCache
107
144
  if (shouldDisableDevLogs) disableDevLogs();
108
145
  };
109
146
 
110
- export { disableDevLogs, handlePrecaching, installSerwist, registerRuntimeCaching };
147
+ export { disableDevLogs, fallbacks, handlePrecaching, installSerwist, registerRuntimeCaching };
@@ -16,6 +16,35 @@ const disableDevLogs = ()=>{
16
16
  self.__WB_DISABLE_DEV_LOGS = true;
17
17
  };
18
18
 
19
+ const fallbacks = ({ runtimeCaching, entries, precacheOptions })=>{
20
+ precaching.precacheAndRoute(entries.map(({ url, revision })=>({
21
+ url: typeof url === "string" ? url : url.toString(),
22
+ revision
23
+ })), precacheOptions);
24
+ runtimeCaching = runtimeCaching.map((cacheEntry)=>{
25
+ if (!cacheEntry.options || cacheEntry.options.precacheFallback || cacheEntry.options.plugins?.some((plugin)=>"handlerDidError" in plugin)) {
26
+ return cacheEntry;
27
+ }
28
+ if (!cacheEntry.options.plugins) {
29
+ cacheEntry.options.plugins = [];
30
+ }
31
+ cacheEntry.options.plugins.push({
32
+ async handlerDidError (info) {
33
+ for (const { matcher, url, cacheMatchOptions = {
34
+ ignoreSearch: true
35
+ } } of entries){
36
+ if (matcher(info)) {
37
+ return caches.match(url, cacheMatchOptions);
38
+ }
39
+ }
40
+ return Response.error();
41
+ }
42
+ });
43
+ return cacheEntry;
44
+ });
45
+ return runtimeCaching;
46
+ };
47
+
19
48
  const handlePrecaching = ({ precacheEntries, precacheOptions, cleanupOutdatedCaches: shouldCleanupOutdatedCaches = false, ...options })=>{
20
49
  if (!!precacheEntries && precacheEntries.length > 0) {
21
50
  /**
@@ -72,7 +101,7 @@ const registerRuntimeCaching = (...runtimeCachingList)=>{
72
101
  }
73
102
  };
74
103
 
75
- const installSerwist = ({ precacheEntries, precacheOptions, cleanupOutdatedCaches, skipWaiting: shouldSkipWaiting = false, importScripts: scriptsToImport, navigationPreload: navigationPreload$1 = false, cacheId, clientsClaim: shouldClaimClients = false, runtimeCaching, offlineAnalyticsConfig, disableDevLogs: shouldDisableDevLogs, ...options })=>{
104
+ const installSerwist = ({ precacheEntries, precacheOptions, cleanupOutdatedCaches, skipWaiting: shouldSkipWaiting = false, importScripts: scriptsToImport, navigationPreload: navigationPreload$1 = false, cacheId, clientsClaim: shouldClaimClients = false, runtimeCaching, offlineAnalyticsConfig, disableDevLogs: shouldDisableDevLogs, fallbacks: fallbacksOptions, ...options })=>{
76
105
  if (!!scriptsToImport && scriptsToImport.length > 0) self.importScripts(...scriptsToImport);
77
106
  if (navigationPreload$1) navigationPreload.enable();
78
107
  if (cacheId !== undefined) core.setCacheNameDetails({
@@ -98,7 +127,15 @@ const installSerwist = ({ precacheEntries, precacheOptions, cleanupOutdatedCache
98
127
  navigateFallbackDenylist: options.navigateFallbackDenylist
99
128
  }
100
129
  });
101
- if (runtimeCaching !== undefined) registerRuntimeCaching(...runtimeCaching);
130
+ if (runtimeCaching !== undefined) {
131
+ if (fallbacksOptions !== undefined) {
132
+ runtimeCaching = fallbacks({
133
+ ...fallbacksOptions,
134
+ runtimeCaching
135
+ });
136
+ }
137
+ registerRuntimeCaching(...runtimeCaching);
138
+ }
102
139
  if (offlineAnalyticsConfig !== undefined) {
103
140
  if (typeof offlineAnalyticsConfig === "boolean") {
104
141
  offlineAnalyticsConfig && initialize.initialize();
@@ -110,6 +147,7 @@ const installSerwist = ({ precacheEntries, precacheOptions, cleanupOutdatedCache
110
147
  };
111
148
 
112
149
  exports.disableDevLogs = disableDevLogs;
150
+ exports.fallbacks = fallbacks;
113
151
  exports.handlePrecaching = handlePrecaching;
114
152
  exports.installSerwist = installSerwist;
115
153
  exports.registerRuntimeCaching = registerRuntimeCaching;
@@ -1,6 +1,8 @@
1
1
  import type { RuntimeCaching } from "@serwist/build";
2
2
  import { type GoogleAnalyticsInitializeOptions } from "@serwist/google-analytics/initialize";
3
3
  import { disableDevLogs } from "./disableDevLogs.js";
4
+ import type { FallbackEntry, FallbackMatcher, FallbacksOptions } from "./fallbacks.js";
5
+ import { fallbacks } from "./fallbacks.js";
4
6
  import { handlePrecaching, type HandlePrecachingOptions } from "./handlePrecaching.js";
5
7
  import { registerRuntimeCaching } from "./registerRuntimeCaching.js";
6
8
  export type SerwistOptions = HandlePrecachingOptions & {
@@ -12,6 +14,8 @@ export type SerwistOptions = HandlePrecachingOptions & {
12
14
  runtimeCaching?: RuntimeCaching[];
13
15
  offlineAnalyticsConfig?: GoogleAnalyticsInitializeOptions | boolean;
14
16
  disableDevLogs?: boolean;
17
+ fallbacks?: Omit<FallbacksOptions, "runtimeCaching">;
15
18
  };
16
- export declare const installSerwist: ({ precacheEntries, precacheOptions, cleanupOutdatedCaches, skipWaiting: shouldSkipWaiting, importScripts: scriptsToImport, navigationPreload, cacheId, clientsClaim: shouldClaimClients, runtimeCaching, offlineAnalyticsConfig, disableDevLogs: shouldDisableDevLogs, ...options }: SerwistOptions) => void;
17
- export { disableDevLogs, handlePrecaching, registerRuntimeCaching };
19
+ export declare const installSerwist: ({ precacheEntries, precacheOptions, cleanupOutdatedCaches, skipWaiting: shouldSkipWaiting, importScripts: scriptsToImport, navigationPreload, cacheId, clientsClaim: shouldClaimClients, runtimeCaching, offlineAnalyticsConfig, disableDevLogs: shouldDisableDevLogs, fallbacks: fallbacksOptions, ...options }: SerwistOptions) => void;
20
+ export { disableDevLogs, fallbacks, handlePrecaching, registerRuntimeCaching };
21
+ export type { FallbackEntry, FallbackMatcher, FallbacksOptions, HandlePrecachingOptions };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@serwist/sw",
3
- "version": "8.0.5",
3
+ "version": "8.1.1",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "description": "This module makes it easy to get started with the Serwist service worker libraries.",
@@ -35,22 +35,22 @@
35
35
  "./package.json": "./package.json"
36
36
  },
37
37
  "dependencies": {
38
- "@serwist/background-sync": "8.0.5",
39
- "@serwist/broadcast-update": "8.0.5",
40
- "@serwist/cacheable-response": "8.0.5",
41
- "@serwist/core": "8.0.5",
42
- "@serwist/expiration": "8.0.5",
43
- "@serwist/google-analytics": "8.0.5",
44
- "@serwist/navigation-preload": "8.0.5",
45
- "@serwist/precaching": "8.0.5",
46
- "@serwist/range-requests": "8.0.5",
47
- "@serwist/routing": "8.0.5",
48
- "@serwist/strategies": "8.0.5"
38
+ "@serwist/background-sync": "8.1.1",
39
+ "@serwist/broadcast-update": "8.1.1",
40
+ "@serwist/cacheable-response": "8.1.1",
41
+ "@serwist/core": "8.1.1",
42
+ "@serwist/expiration": "8.1.1",
43
+ "@serwist/google-analytics": "8.1.1",
44
+ "@serwist/navigation-preload": "8.1.1",
45
+ "@serwist/precaching": "8.1.1",
46
+ "@serwist/range-requests": "8.1.1",
47
+ "@serwist/routing": "8.1.1",
48
+ "@serwist/strategies": "8.1.1"
49
49
  },
50
50
  "devDependencies": {
51
- "rollup": "3.28.1",
52
- "@serwist/build": "8.0.5",
53
- "@serwist/constants": "8.0.5"
51
+ "rollup": "4.9.1",
52
+ "@serwist/build": "8.1.1",
53
+ "@serwist/constants": "8.1.1"
54
54
  },
55
55
  "scripts": {
56
56
  "build": "rimraf dist && cross-env NODE_ENV=production rollup --config rollup.config.js",