@serwist/sw 9.0.0-preview.1 → 9.0.0-preview.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/disableDevLogs.d.ts +5 -0
- package/dist/disableDevLogs.d.ts.map +1 -1
- package/dist/fallbacks.d.ts +23 -17
- package/dist/fallbacks.d.ts.map +1 -1
- package/dist/handlePrecaching.d.ts +30 -1
- package/dist/handlePrecaching.d.ts.map +1 -1
- package/dist/index.js +19 -71
- package/dist/installSerwist.d.ts +47 -0
- package/dist/installSerwist.d.ts.map +1 -1
- package/dist/registerRuntimeCaching.d.ts +8 -0
- package/dist/registerRuntimeCaching.d.ts.map +1 -1
- package/dist/types.d.ts +9 -90
- package/dist/types.d.ts.map +1 -1
- package/package.json +15 -15
- package/src/disableDevLogs.ts +7 -4
- package/src/fallbacks.ts +39 -40
- package/src/handlePrecaching.ts +40 -10
- package/src/installSerwist.ts +53 -9
- package/src/registerRuntimeCaching.ts +14 -64
- package/src/types.ts +9 -93
package/dist/disableDevLogs.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"disableDevLogs.d.ts","sourceRoot":"","sources":["../src/disableDevLogs.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"disableDevLogs.d.ts","sourceRoot":"","sources":["../src/disableDevLogs.ts"],"names":[],"mappings":"AAEA;;;;GAIG;AACH,eAAO,MAAM,cAAc,QAAO,IAEjC,CAAC"}
|
package/dist/fallbacks.d.ts
CHANGED
|
@@ -1,33 +1,39 @@
|
|
|
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 {
|
|
13
|
+
/**
|
|
14
|
+
* Your previous `RuntimeCaching` array.
|
|
15
|
+
*/
|
|
28
16
|
runtimeCaching: RuntimeCaching[];
|
|
17
|
+
/**
|
|
18
|
+
* A list of fallback entries.
|
|
19
|
+
*/
|
|
29
20
|
entries: FallbackEntry[];
|
|
21
|
+
/**
|
|
22
|
+
* Precache options that will be used for your
|
|
23
|
+
* fallback entries.
|
|
24
|
+
*/
|
|
30
25
|
precacheOptions?: PrecacheRouteOptions;
|
|
31
26
|
}
|
|
27
|
+
/**
|
|
28
|
+
* Precaches routes so that they can be used as a fallback when
|
|
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
|
|
34
|
+
* @param options
|
|
35
|
+
* @returns The modified `RuntimeCaching` array. Using this value
|
|
36
|
+
* is not needed, as it is simply the array in `options`.
|
|
37
|
+
*/
|
|
32
38
|
export declare const fallbacks: ({ runtimeCaching, entries, precacheOptions }: FallbacksOptions) => RuntimeCaching[];
|
|
33
39
|
//# sourceMappingURL=fallbacks.d.ts.map
|
package/dist/fallbacks.d.ts.map
CHANGED
|
@@ -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;
|
|
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"}
|
|
@@ -1,14 +1,43 @@
|
|
|
1
1
|
import type { PrecacheEntry, PrecacheRouteOptions } from "@serwist/precaching";
|
|
2
2
|
export type HandlePrecachingOptions = {
|
|
3
|
+
/**
|
|
4
|
+
* A list of fallback entries.
|
|
5
|
+
*/
|
|
3
6
|
precacheEntries?: (PrecacheEntry | string)[];
|
|
7
|
+
/**
|
|
8
|
+
* Precache options for the provided entries.
|
|
9
|
+
*/
|
|
4
10
|
precacheOptions?: PrecacheRouteOptions;
|
|
11
|
+
/**
|
|
12
|
+
* Whether outdated caches should be removed.
|
|
13
|
+
*
|
|
14
|
+
* @default false
|
|
15
|
+
*/
|
|
5
16
|
cleanupOutdatedCaches?: boolean;
|
|
6
17
|
} & ({
|
|
18
|
+
/**
|
|
19
|
+
* An URL that should point to a HTML
|
|
20
|
+
* file with which navigation requests for URLs that aren't precached will be fulfilled.
|
|
21
|
+
* For more complex cases, consider `@serwist/sw.fallbacks`instead.
|
|
22
|
+
*/
|
|
7
23
|
navigateFallback: string;
|
|
24
|
+
/**
|
|
25
|
+
* URLs that should be allowed to use the `navigateFallback` handler.
|
|
26
|
+
*/
|
|
8
27
|
navigateFallbackAllowlist?: RegExp[];
|
|
28
|
+
/**
|
|
29
|
+
* URLs that should not be allowed to use the `navigateFallback` handler. This takes precedence
|
|
30
|
+
* over `navigateFallbackAllowlist`.
|
|
31
|
+
*/
|
|
9
32
|
navigateFallbackDenylist?: RegExp[];
|
|
10
33
|
} | {
|
|
11
34
|
navigateFallback?: never;
|
|
12
35
|
});
|
|
13
|
-
|
|
36
|
+
/**
|
|
37
|
+
* Handles a list of precache entries and cleans up outdated caches.
|
|
38
|
+
*
|
|
39
|
+
* @see https://serwist.pages.dev/docs/sw/handlePrecaching
|
|
40
|
+
* @param options
|
|
41
|
+
*/
|
|
42
|
+
export declare const handlePrecaching: ({ precacheEntries, precacheOptions, cleanupOutdatedCaches, ...options }: HandlePrecachingOptions) => void;
|
|
14
43
|
//# sourceMappingURL=handlePrecaching.d.ts.map
|
|
@@ -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,eAAe,CAAC,EAAE,CAAC,aAAa,GAAG,MAAM,CAAC,EAAE,CAAC;IAC7C,eAAe,CAAC,EAAE,oBAAoB,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,54 +1,34 @@
|
|
|
1
|
-
import { precacheAndRoute, cleanupOutdatedCaches, createHandlerBoundToURL
|
|
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 {
|
|
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
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
runtimeCaching
|
|
24
|
-
if (
|
|
25
|
-
|
|
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;
|
|
45
25
|
};
|
|
46
26
|
|
|
47
|
-
const handlePrecaching = ({ precacheEntries, precacheOptions, cleanupOutdatedCaches:
|
|
27
|
+
const handlePrecaching = ({ precacheEntries, precacheOptions, cleanupOutdatedCaches: cleanupOutdatedCaches$1 = false, ...options })=>{
|
|
48
28
|
if (!!precacheEntries && precacheEntries.length > 0) {
|
|
49
29
|
precacheAndRoute(precacheEntries, precacheOptions);
|
|
50
30
|
}
|
|
51
|
-
if (
|
|
31
|
+
if (cleanupOutdatedCaches$1) cleanupOutdatedCaches();
|
|
52
32
|
if (options.navigateFallback) {
|
|
53
33
|
registerRoute(new NavigationRoute(createHandlerBoundToURL(options.navigateFallback), {
|
|
54
34
|
allowlist: options.navigateFallbackAllowlist,
|
|
@@ -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
|
-
logger.
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
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);
|
|
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);
|
|
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) {
|
|
@@ -135,10 +83,10 @@ const installSerwist = ({ precacheEntries, precacheOptions, cleanupOutdatedCache
|
|
|
135
83
|
self.__WB_FORCE_RUNTIME_CACHING = false;
|
|
136
84
|
}
|
|
137
85
|
if (!self.__WB_FORCE_RUNTIME_CACHING && process.env.NODE_ENV !== "production") {
|
|
138
|
-
logger.
|
|
86
|
+
logger.log("runtimeCaching and fallbacks are disabled in development mode.");
|
|
139
87
|
} else {
|
|
140
88
|
if (fallbacks$1 !== undefined) {
|
|
141
|
-
|
|
89
|
+
fallbacks({
|
|
142
90
|
...fallbacks$1,
|
|
143
91
|
runtimeCaching
|
|
144
92
|
});
|
package/dist/installSerwist.d.ts
CHANGED
|
@@ -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":"
|
|
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,3 +1,11 @@
|
|
|
1
1
|
import type { RuntimeCaching } from "./types.js";
|
|
2
|
+
/**
|
|
3
|
+
* Registers caching strategies to a singleton Router instance. It is a simple
|
|
4
|
+
* syntatic sugar for `@serwist/routing.registerRoute`.
|
|
5
|
+
*
|
|
6
|
+
* @see https://serwist.pages.dev/docs/sw/registerRuntimeCaching
|
|
7
|
+
* @param runtimeCachingList
|
|
8
|
+
* @returns
|
|
9
|
+
*/
|
|
2
10
|
export declare const registerRuntimeCaching: (...runtimeCachingList: RuntimeCaching[]) => void;
|
|
3
11
|
//# sourceMappingURL=registerRuntimeCaching.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"registerRuntimeCaching.d.ts","sourceRoot":"","sources":["../src/registerRuntimeCaching.ts"],"names":[],"mappings":"
|
|
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 {
|
|
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,90 +8,22 @@ 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
|
|
101
14
|
* URLs. If multiple `RuntimeCaching` routes are defined, then the first one
|
|
102
|
-
* whose `
|
|
15
|
+
* whose `matcher` matches will be the one that responds.
|
|
103
16
|
*
|
|
104
17
|
* This value directly maps to the first parameter passed to
|
|
105
18
|
* `@serwist/routing.registerRoute`. It's recommended to use a
|
|
106
19
|
* `@serwist/core.RouteMatchCallback` function for greatest flexibility.
|
|
107
20
|
*/
|
|
108
|
-
|
|
21
|
+
matcher: 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
|
package/dist/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,
|
|
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,OAAO,EAAE,MAAM,GAAG,MAAM,GAAG,kBAAkB,CAAC;IAC9C;;;;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.
|
|
3
|
+
"version": "9.0.0-preview.10",
|
|
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.
|
|
32
|
-
"@serwist/broadcast-update": "9.0.0-preview.
|
|
33
|
-
"@serwist/cacheable-response": "9.0.0-preview.
|
|
34
|
-
"@serwist/core": "9.0.0-preview.
|
|
35
|
-
"@serwist/expiration": "9.0.0-preview.
|
|
36
|
-
"@serwist/google-analytics": "9.0.0-preview.
|
|
37
|
-
"@serwist/navigation-preload": "9.0.0-preview.
|
|
38
|
-
"@serwist/precaching": "9.0.0-preview.
|
|
39
|
-
"@serwist/range-requests": "9.0.0-preview.
|
|
40
|
-
"@serwist/routing": "9.0.0-preview.
|
|
41
|
-
"@serwist/strategies": "9.0.0-preview.
|
|
31
|
+
"@serwist/background-sync": "9.0.0-preview.10",
|
|
32
|
+
"@serwist/broadcast-update": "9.0.0-preview.10",
|
|
33
|
+
"@serwist/cacheable-response": "9.0.0-preview.10",
|
|
34
|
+
"@serwist/core": "9.0.0-preview.10",
|
|
35
|
+
"@serwist/expiration": "9.0.0-preview.10",
|
|
36
|
+
"@serwist/google-analytics": "9.0.0-preview.10",
|
|
37
|
+
"@serwist/navigation-preload": "9.0.0-preview.10",
|
|
38
|
+
"@serwist/precaching": "9.0.0-preview.10",
|
|
39
|
+
"@serwist/range-requests": "9.0.0-preview.10",
|
|
40
|
+
"@serwist/routing": "9.0.0-preview.10",
|
|
41
|
+
"@serwist/strategies": "9.0.0-preview.10"
|
|
42
42
|
},
|
|
43
43
|
"devDependencies": {
|
|
44
44
|
"rollup": "4.9.6",
|
|
45
|
-
"typescript": "5.4.0-dev.
|
|
46
|
-
"@serwist/constants": "9.0.0-preview.
|
|
47
|
-
"@serwist/utils": "9.0.0-preview.
|
|
45
|
+
"typescript": "5.4.0-dev.20240206",
|
|
46
|
+
"@serwist/constants": "9.0.0-preview.10",
|
|
47
|
+
"@serwist/utils": "9.0.0-preview.10"
|
|
48
48
|
},
|
|
49
49
|
"peerDependencies": {
|
|
50
50
|
"typescript": ">=5.0.0"
|
package/src/disableDevLogs.ts
CHANGED
|
@@ -1,7 +1,10 @@
|
|
|
1
|
-
declare const self:
|
|
2
|
-
__WB_DISABLE_DEV_LOGS: boolean;
|
|
3
|
-
};
|
|
1
|
+
declare const self: ServiceWorkerGlobalScope;
|
|
4
2
|
|
|
5
|
-
|
|
3
|
+
/**
|
|
4
|
+
* Disables Serwist's logging in development mode.
|
|
5
|
+
*
|
|
6
|
+
* @see https://serwist.pages.dev/docs/sw/disable-dev-logs
|
|
7
|
+
*/
|
|
8
|
+
export const disableDevLogs = (): void => {
|
|
6
9
|
self.__WB_DISABLE_DEV_LOGS = true;
|
|
7
10
|
};
|
package/src/fallbacks.ts
CHANGED
|
@@ -1,66 +1,65 @@
|
|
|
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 {
|
|
19
|
+
/**
|
|
20
|
+
* Your previous `RuntimeCaching` array.
|
|
21
|
+
*/
|
|
33
22
|
runtimeCaching: RuntimeCaching[];
|
|
23
|
+
/**
|
|
24
|
+
* A list of fallback entries.
|
|
25
|
+
*/
|
|
34
26
|
entries: FallbackEntry[];
|
|
27
|
+
/**
|
|
28
|
+
* Precache options that will be used for your
|
|
29
|
+
* fallback entries.
|
|
30
|
+
*/
|
|
35
31
|
precacheOptions?: PrecacheRouteOptions;
|
|
36
32
|
}
|
|
37
33
|
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
34
|
+
/**
|
|
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
|
|
41
|
+
* @param options
|
|
42
|
+
* @returns The modified `RuntimeCaching` array. Using this value
|
|
43
|
+
* is not needed, as it is simply the array in `options`.
|
|
44
|
+
*/
|
|
45
|
+
export const fallbacks = ({ runtimeCaching, entries, precacheOptions }: FallbacksOptions): RuntimeCaching[] => {
|
|
46
|
+
precacheAndRoute(entries, precacheOptions);
|
|
47
47
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
48
|
+
const fallbackPlugin = new PrecacheFallbackPlugin({
|
|
49
|
+
fallbackUrls: entries,
|
|
50
|
+
});
|
|
51
51
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
},
|
|
61
|
-
});
|
|
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
|
+
}
|
|
62
60
|
|
|
63
61
|
return cacheEntry;
|
|
64
62
|
});
|
|
63
|
+
|
|
65
64
|
return runtimeCaching;
|
|
66
65
|
};
|
package/src/handlePrecaching.ts
CHANGED
|
@@ -1,20 +1,50 @@
|
|
|
1
1
|
import type { PrecacheEntry, PrecacheRouteOptions } from "@serwist/precaching";
|
|
2
|
-
import { cleanupOutdatedCaches, createHandlerBoundToURL, precacheAndRoute } from "@serwist/precaching";
|
|
2
|
+
import { cleanupOutdatedCaches as cleanupOutdatedCachesImpl, createHandlerBoundToURL, precacheAndRoute } from "@serwist/precaching";
|
|
3
3
|
import { NavigationRoute, registerRoute } from "@serwist/routing";
|
|
4
4
|
|
|
5
5
|
export type HandlePrecachingOptions = {
|
|
6
|
+
/**
|
|
7
|
+
* A list of fallback entries.
|
|
8
|
+
*/
|
|
6
9
|
precacheEntries?: (PrecacheEntry | string)[];
|
|
10
|
+
/**
|
|
11
|
+
* Precache options for the provided entries.
|
|
12
|
+
*/
|
|
7
13
|
precacheOptions?: PrecacheRouteOptions;
|
|
8
|
-
|
|
14
|
+
/**
|
|
15
|
+
* Whether outdated caches should be removed.
|
|
16
|
+
*
|
|
17
|
+
* @default false
|
|
18
|
+
*/
|
|
9
19
|
cleanupOutdatedCaches?: boolean;
|
|
10
|
-
} & (
|
|
20
|
+
} & (
|
|
21
|
+
| {
|
|
22
|
+
/**
|
|
23
|
+
* An URL that should point to a HTML
|
|
24
|
+
* file with which navigation requests for URLs that aren't precached will be fulfilled.
|
|
25
|
+
* For more complex cases, consider `@serwist/sw.fallbacks`instead.
|
|
26
|
+
*/
|
|
27
|
+
navigateFallback: string;
|
|
28
|
+
/**
|
|
29
|
+
* URLs that should be allowed to use the `navigateFallback` handler.
|
|
30
|
+
*/
|
|
31
|
+
navigateFallbackAllowlist?: RegExp[];
|
|
32
|
+
/**
|
|
33
|
+
* URLs that should not be allowed to use the `navigateFallback` handler. This takes precedence
|
|
34
|
+
* over `navigateFallbackAllowlist`.
|
|
35
|
+
*/
|
|
36
|
+
navigateFallbackDenylist?: RegExp[];
|
|
37
|
+
}
|
|
38
|
+
| { navigateFallback?: never }
|
|
39
|
+
);
|
|
11
40
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
41
|
+
/**
|
|
42
|
+
* Handles a list of precache entries and cleans up outdated caches.
|
|
43
|
+
*
|
|
44
|
+
* @see https://serwist.pages.dev/docs/sw/handlePrecaching
|
|
45
|
+
* @param options
|
|
46
|
+
*/
|
|
47
|
+
export const handlePrecaching = ({ precacheEntries, precacheOptions, cleanupOutdatedCaches = false, ...options }: HandlePrecachingOptions): void => {
|
|
18
48
|
if (!!precacheEntries && precacheEntries.length > 0) {
|
|
19
49
|
/**
|
|
20
50
|
* The precacheAndRoute() method efficiently caches and responds to
|
|
@@ -24,7 +54,7 @@ export const handlePrecaching = ({
|
|
|
24
54
|
precacheAndRoute(precacheEntries, precacheOptions);
|
|
25
55
|
}
|
|
26
56
|
|
|
27
|
-
if (
|
|
57
|
+
if (cleanupOutdatedCaches) cleanupOutdatedCachesImpl();
|
|
28
58
|
|
|
29
59
|
if (options.navigateFallback) {
|
|
30
60
|
registerRoute(
|
package/src/installSerwist.ts
CHANGED
|
@@ -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();
|
|
@@ -82,13 +129,10 @@ export const installSerwist = ({
|
|
|
82
129
|
self.__WB_FORCE_RUNTIME_CACHING = false;
|
|
83
130
|
}
|
|
84
131
|
if (!self.__WB_FORCE_RUNTIME_CACHING && process.env.NODE_ENV !== "production") {
|
|
85
|
-
logger.
|
|
132
|
+
logger.log("runtimeCaching and fallbacks are disabled in development mode.");
|
|
86
133
|
} else {
|
|
87
134
|
if (fallbacks !== undefined) {
|
|
88
|
-
|
|
89
|
-
...fallbacks,
|
|
90
|
-
runtimeCaching,
|
|
91
|
-
});
|
|
135
|
+
fallbacksImpl({ ...fallbacks, runtimeCaching });
|
|
92
136
|
}
|
|
93
137
|
registerRuntimeCaching(...runtimeCaching);
|
|
94
138
|
}
|
|
@@ -1,78 +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
|
|
4
|
+
import type { RuntimeCaching } from "./types.js";
|
|
13
5
|
|
|
14
6
|
declare const self: ServiceWorkerGlobalScope;
|
|
15
7
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
export const registerRuntimeCaching = (...runtimeCachingList: RuntimeCaching[]) => {
|
|
8
|
+
/**
|
|
9
|
+
* Registers caching strategies to a singleton Router instance. It is a simple
|
|
10
|
+
* syntatic sugar for `@serwist/routing.registerRoute`.
|
|
11
|
+
*
|
|
12
|
+
* @see https://serwist.pages.dev/docs/sw/registerRuntimeCaching
|
|
13
|
+
* @param runtimeCachingList
|
|
14
|
+
* @returns
|
|
15
|
+
*/
|
|
16
|
+
export const registerRuntimeCaching = (...runtimeCachingList: RuntimeCaching[]): void => {
|
|
25
17
|
if (!("__WB_FORCE_RUNTIME_CACHING" in globalThis)) {
|
|
26
18
|
self.__WB_FORCE_RUNTIME_CACHING = false;
|
|
27
19
|
}
|
|
28
20
|
|
|
29
21
|
if (!self.__WB_FORCE_RUNTIME_CACHING && process.env.NODE_ENV !== "production") {
|
|
30
|
-
logger.
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
for (const rcEntry of runtimeCachingList) {
|
|
35
|
-
if (typeof rcEntry.handler === "string") {
|
|
36
|
-
const {
|
|
37
|
-
cacheName,
|
|
38
|
-
networkTimeoutSeconds,
|
|
39
|
-
fetchOptions,
|
|
40
|
-
matchOptions,
|
|
41
|
-
plugins,
|
|
42
|
-
backgroundSync,
|
|
43
|
-
broadcastUpdate,
|
|
44
|
-
cacheableResponse,
|
|
45
|
-
expiration,
|
|
46
|
-
precacheFallback,
|
|
47
|
-
rangeRequests,
|
|
48
|
-
} = rcEntry.options!; // entry.options is always defined when entry.handler is of type 'string'.
|
|
49
|
-
const Handler = HANDLER_NAME_TO_METHOD[rcEntry.handler];
|
|
50
|
-
registerRoute(
|
|
51
|
-
rcEntry.urlPattern,
|
|
52
|
-
new Handler({
|
|
53
|
-
cacheName: cacheName ?? undefined,
|
|
54
|
-
networkTimeoutSeconds,
|
|
55
|
-
fetchOptions,
|
|
56
|
-
matchOptions,
|
|
57
|
-
plugins: [
|
|
58
|
-
...(plugins ?? []),
|
|
59
|
-
backgroundSync && new BackgroundSyncPlugin(backgroundSync.name, backgroundSync.options),
|
|
60
|
-
broadcastUpdate &&
|
|
61
|
-
new BroadcastUpdatePlugin({
|
|
62
|
-
// @ts-expect-error weird...
|
|
63
|
-
channelName: broadcastUpdate.channelName,
|
|
64
|
-
...broadcastUpdate.options,
|
|
65
|
-
}),
|
|
66
|
-
cacheableResponse && new CacheableResponsePlugin(cacheableResponse),
|
|
67
|
-
expiration && new ExpirationPlugin(expiration),
|
|
68
|
-
precacheFallback && new PrecacheFallbackPlugin(precacheFallback),
|
|
69
|
-
rangeRequests ? new RangeRequestsPlugin() : undefined,
|
|
70
|
-
].filter(nonNullable),
|
|
71
|
-
}),
|
|
72
|
-
rcEntry.method,
|
|
73
|
-
);
|
|
74
|
-
} else {
|
|
75
|
-
registerRoute(rcEntry.urlPattern, rcEntry.handler, rcEntry.method);
|
|
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);
|
|
76
26
|
}
|
|
77
27
|
}
|
|
78
28
|
};
|
package/src/types.ts
CHANGED
|
@@ -1,21 +1,7 @@
|
|
|
1
|
-
import type {
|
|
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,91 +9,21 @@ 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
|
|
105
15
|
* URLs. If multiple `RuntimeCaching` routes are defined, then the first one
|
|
106
|
-
* whose `
|
|
16
|
+
* whose `matcher` matches will be the one that responds.
|
|
107
17
|
*
|
|
108
18
|
* This value directly maps to the first parameter passed to
|
|
109
19
|
* `@serwist/routing.registerRoute`. It's recommended to use a
|
|
110
20
|
* `@serwist/core.RouteMatchCallback` function for greatest flexibility.
|
|
111
21
|
*/
|
|
112
|
-
|
|
22
|
+
matcher: 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
|
}
|