@serwist/recipes 9.0.0-preview.19 → 9.0.0-preview.20
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/googleFontsCache.d.ts +7 -2
- package/dist/googleFontsCache.d.ts.map +1 -1
- package/dist/imageCache.d.ts +7 -1
- package/dist/imageCache.d.ts.map +1 -1
- package/dist/index.js +32 -55
- package/dist/offlineFallback.d.ts +7 -1
- package/dist/offlineFallback.d.ts.map +1 -1
- package/dist/pageCache.d.ts +7 -2
- package/dist/pageCache.d.ts.map +1 -1
- package/dist/staticResourceCache.d.ts +7 -2
- package/dist/staticResourceCache.d.ts.map +1 -1
- package/dist/warmStrategyCache.d.ts +2 -3
- package/dist/warmStrategyCache.d.ts.map +1 -1
- package/package.json +4 -8
- package/src/googleFontsCache.ts +19 -17
- package/src/imageCache.ts +20 -16
- package/src/offlineFallback.ts +18 -12
- package/src/pageCache.ts +20 -17
- package/src/staticResourceCache.ts +20 -17
- package/src/warmStrategyCache.ts +3 -5
@@ -1,4 +1,10 @@
|
|
1
|
+
import { type Router } from "@serwist/sw/routing";
|
1
2
|
export interface GoogleFontCacheOptions {
|
3
|
+
/**
|
4
|
+
* An optional `Router` instance. If not provided, the singleton `Router`
|
5
|
+
* will be used.
|
6
|
+
*/
|
7
|
+
router?: Router;
|
2
8
|
/**
|
3
9
|
* Cache prefix for caching stylesheets and webfonts. Defaults to google-fonts.
|
4
10
|
*/
|
@@ -17,6 +23,5 @@ export interface GoogleFontCacheOptions {
|
|
17
23
|
*
|
18
24
|
* @param options
|
19
25
|
*/
|
20
|
-
declare
|
21
|
-
export { googleFontsCache };
|
26
|
+
export declare const googleFontsCache: ({ router, cachePrefix, maxAgeSeconds, maxEntries, }?: GoogleFontCacheOptions) => void;
|
22
27
|
//# sourceMappingURL=googleFontsCache.d.ts.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"googleFontsCache.d.ts","sourceRoot":"","sources":["../src/googleFontsCache.ts"],"names":[],"mappings":"
|
1
|
+
{"version":3,"file":"googleFontsCache.d.ts","sourceRoot":"","sources":["../src/googleFontsCache.ts"],"names":[],"mappings":"AASA,OAAO,EAAE,KAAK,MAAM,EAAsB,MAAM,qBAAqB,CAAC;AAGtE,MAAM,WAAW,sBAAsB;IACrC;;;OAGG;IACH,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB;;OAEG;IACH,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB;;OAEG;IACH,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB;;OAEG;IACH,UAAU,CAAC,EAAE,MAAM,CAAC;CACrB;AAED;;;;GAIG;AACH,eAAO,MAAM,gBAAgB,yDAK1B,sBAAsB,KAAQ,IAyBhC,CAAC"}
|
package/dist/imageCache.d.ts
CHANGED
@@ -1,5 +1,11 @@
|
|
1
1
|
import type { RouteMatchCallback, SerwistPlugin } from "@serwist/core";
|
2
|
+
import { type Router } from "@serwist/sw/routing";
|
2
3
|
export interface ImageCacheOptions {
|
4
|
+
/**
|
5
|
+
* An optional `Router` instance. If not provided, the singleton `Router`
|
6
|
+
* will be used.
|
7
|
+
*/
|
8
|
+
router?: Router;
|
3
9
|
/**
|
4
10
|
* Name for cache. Defaults to images.
|
5
11
|
*/
|
@@ -30,5 +36,5 @@ export interface ImageCacheOptions {
|
|
30
36
|
*
|
31
37
|
* @param options
|
32
38
|
*/
|
33
|
-
export declare const imageCache: (
|
39
|
+
export declare const imageCache: ({ router, cacheName, matchCallback, maxAgeSeconds, maxEntries, plugins, warmCache, }?: ImageCacheOptions) => void;
|
34
40
|
//# sourceMappingURL=imageCache.d.ts.map
|
package/dist/imageCache.d.ts.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"imageCache.d.ts","sourceRoot":"","sources":["../src/imageCache.ts"],"names":[],"mappings":"
|
1
|
+
{"version":3,"file":"imageCache.d.ts","sourceRoot":"","sources":["../src/imageCache.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAAE,kBAAkB,EAA6B,aAAa,EAAE,MAAM,eAAe,CAAC;AAElG,OAAO,EAAE,KAAK,MAAM,EAAsB,MAAM,qBAAqB,CAAC;AAItE,MAAM,WAAW,iBAAiB;IAChC;;;OAGG;IACH,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB;;OAEG;IACH,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB;;OAEG;IACH,aAAa,CAAC,EAAE,kBAAkB,CAAC;IACnC;;OAEG;IACH,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB;;OAEG;IACH,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB;;OAEG;IACH,OAAO,CAAC,EAAE,aAAa,EAAE,CAAC;IAC1B;;OAEG;IACH,SAAS,CAAC,EAAE,MAAM,EAAE,CAAC;CACtB;AAED;;;;GAIG;AACH,eAAO,MAAM,UAAU,0FAQpB,iBAAiB,KAAQ,IAwB3B,CAAC"}
|
package/dist/index.js
CHANGED
@@ -1,19 +1,14 @@
|
|
1
|
-
import { CacheableResponsePlugin } from '@serwist/
|
2
|
-
import {
|
3
|
-
import {
|
4
|
-
import {
|
5
|
-
import { matchPrecache } from '@serwist/precaching';
|
1
|
+
import { CacheableResponsePlugin, ExpirationPlugin } from '@serwist/sw/plugins';
|
2
|
+
import { getSingletonRouter, setCatchHandler } from '@serwist/sw/routing';
|
3
|
+
import { StaleWhileRevalidate, CacheFirst, NetworkFirst } from '@serwist/sw/strategies';
|
4
|
+
import { getSingletonPrecacheController } from '@serwist/sw/precaching';
|
6
5
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
const maxAgeSeconds = options.maxAgeSeconds || 60 * 60 * 24 * 365;
|
11
|
-
const maxEntries = options.maxEntries || 30;
|
12
|
-
registerRoute(({ url })=>url.origin === "https://fonts.googleapis.com", new StaleWhileRevalidate({
|
13
|
-
cacheName: sheetCacheName
|
6
|
+
const googleFontsCache = ({ router = getSingletonRouter(), cachePrefix = "google-fonts", maxAgeSeconds = 60 * 60 * 24 * 365, maxEntries = 30 } = {})=>{
|
7
|
+
router.registerCapture(({ url })=>url.origin === "https://fonts.googleapis.com", new StaleWhileRevalidate({
|
8
|
+
cacheName: `${cachePrefix}-stylesheets`
|
14
9
|
}));
|
15
|
-
|
16
|
-
cacheName:
|
10
|
+
router.registerCapture(({ url })=>url.origin === "https://fonts.gstatic.com", new CacheFirst({
|
11
|
+
cacheName: `${cachePrefix}-webfonts`,
|
17
12
|
plugins: [
|
18
13
|
new CacheableResponsePlugin({
|
19
14
|
statuses: [
|
@@ -27,9 +22,9 @@ function googleFontsCache(options = {}) {
|
|
27
22
|
})
|
28
23
|
]
|
29
24
|
}));
|
30
|
-
}
|
25
|
+
};
|
31
26
|
|
32
|
-
|
27
|
+
const warmStrategyCache = (options)=>{
|
33
28
|
self.addEventListener("install", (event)=>{
|
34
29
|
const done = options.urls.map((path)=>options.strategy.handleAll({
|
35
30
|
event,
|
@@ -37,15 +32,9 @@ function warmStrategyCache(options) {
|
|
37
32
|
})[1]);
|
38
33
|
event.waitUntil(Promise.all(done));
|
39
34
|
});
|
40
|
-
}
|
35
|
+
};
|
41
36
|
|
42
|
-
const imageCache = (
|
43
|
-
const defaultMatchCallback = ({ request })=>request.destination === "image";
|
44
|
-
const cacheName = options.cacheName || "images";
|
45
|
-
const matchCallback = options.matchCallback || defaultMatchCallback;
|
46
|
-
const maxAgeSeconds = options.maxAgeSeconds || 30 * 24 * 60 * 60;
|
47
|
-
const maxEntries = options.maxEntries || 60;
|
48
|
-
const plugins = options.plugins || [];
|
37
|
+
const imageCache = ({ router = getSingletonRouter(), cacheName = "images", matchCallback = ({ request })=>request.destination === "image", maxAgeSeconds = 30 * 24 * 60 * 60, maxEntries = 60, plugins = [], warmCache } = {})=>{
|
49
38
|
plugins.push(new CacheableResponsePlugin({
|
50
39
|
statuses: [
|
51
40
|
0,
|
@@ -60,19 +49,16 @@ const imageCache = (options = {})=>{
|
|
60
49
|
cacheName,
|
61
50
|
plugins
|
62
51
|
});
|
63
|
-
|
64
|
-
if (
|
52
|
+
router.registerCapture(matchCallback, strategy);
|
53
|
+
if (warmCache) {
|
65
54
|
warmStrategyCache({
|
66
|
-
urls:
|
55
|
+
urls: warmCache,
|
67
56
|
strategy
|
68
57
|
});
|
69
58
|
}
|
70
59
|
};
|
71
60
|
|
72
|
-
const offlineFallback = (
|
73
|
-
const pageFallback = options.pageFallback || "offline.html";
|
74
|
-
const imageFallback = options.imageFallback || false;
|
75
|
-
const fontFallback = options.fontFallback || false;
|
61
|
+
const offlineFallback = ({ precacheController = getSingletonPrecacheController(), pageFallback = "offline.html", imageFallback, fontFallback } = {})=>{
|
76
62
|
self.addEventListener("install", (event)=>{
|
77
63
|
const files = [
|
78
64
|
pageFallback
|
@@ -89,15 +75,15 @@ const offlineFallback = (options = {})=>{
|
|
89
75
|
const dest = options.request.destination;
|
90
76
|
const cache = await self.caches.open("serwist-offline-fallbacks");
|
91
77
|
if (dest === "document") {
|
92
|
-
const match = await matchPrecache(pageFallback) || await cache.match(pageFallback);
|
78
|
+
const match = await precacheController.matchPrecache(pageFallback) || await cache.match(pageFallback);
|
93
79
|
return match || Response.error();
|
94
80
|
}
|
95
|
-
if (dest === "image" && imageFallback !==
|
96
|
-
const match = await matchPrecache(imageFallback) || await cache.match(imageFallback);
|
81
|
+
if (dest === "image" && imageFallback !== undefined) {
|
82
|
+
const match = await precacheController.matchPrecache(imageFallback) || await cache.match(imageFallback);
|
97
83
|
return match || Response.error();
|
98
84
|
}
|
99
|
-
if (dest === "font" && fontFallback !==
|
100
|
-
const match = await matchPrecache(fontFallback) || await cache.match(fontFallback);
|
85
|
+
if (dest === "font" && fontFallback !== undefined) {
|
86
|
+
const match = await precacheController.matchPrecache(fontFallback) || await cache.match(fontFallback);
|
101
87
|
return match || Response.error();
|
102
88
|
}
|
103
89
|
return Response.error();
|
@@ -105,12 +91,7 @@ const offlineFallback = (options = {})=>{
|
|
105
91
|
setCatchHandler(handler);
|
106
92
|
};
|
107
93
|
|
108
|
-
|
109
|
-
const defaultMatchCallback = ({ request })=>request.mode === "navigate";
|
110
|
-
const cacheName = options.cacheName || "pages";
|
111
|
-
const matchCallback = options.matchCallback || defaultMatchCallback;
|
112
|
-
const networkTimeoutSeconds = options.networkTimeoutSeconds || 3;
|
113
|
-
const plugins = options.plugins || [];
|
94
|
+
const pageCache = ({ router = getSingletonRouter(), cacheName = "pages", matchCallback = ({ request })=>request.mode === "navigate", networkTimeoutSeconds = 3, plugins = [], warmCache } = {})=>{
|
114
95
|
plugins.push(new CacheableResponsePlugin({
|
115
96
|
statuses: [
|
116
97
|
0,
|
@@ -122,20 +103,16 @@ function pageCache(options = {}) {
|
|
122
103
|
cacheName,
|
123
104
|
plugins
|
124
105
|
});
|
125
|
-
|
126
|
-
if (
|
106
|
+
router.registerCapture(matchCallback, strategy);
|
107
|
+
if (warmCache) {
|
127
108
|
warmStrategyCache({
|
128
|
-
urls:
|
109
|
+
urls: warmCache,
|
129
110
|
strategy
|
130
111
|
});
|
131
112
|
}
|
132
|
-
}
|
113
|
+
};
|
133
114
|
|
134
|
-
|
135
|
-
const defaultMatchCallback = ({ request })=>request.destination === "style" || request.destination === "script" || request.destination === "worker";
|
136
|
-
const cacheName = options.cacheName || "static-resources";
|
137
|
-
const matchCallback = options.matchCallback || defaultMatchCallback;
|
138
|
-
const plugins = options.plugins || [];
|
115
|
+
const staticResourceCache = ({ router = getSingletonRouter(), cacheName = "static-resources", matchCallback = ({ request })=>request.destination === "style" || request.destination === "script" || request.destination === "worker", plugins = [], warmCache } = {})=>{
|
139
116
|
plugins.push(new CacheableResponsePlugin({
|
140
117
|
statuses: [
|
141
118
|
0,
|
@@ -146,13 +123,13 @@ function staticResourceCache(options = {}) {
|
|
146
123
|
cacheName,
|
147
124
|
plugins
|
148
125
|
});
|
149
|
-
|
150
|
-
if (
|
126
|
+
router.registerCapture(matchCallback, strategy);
|
127
|
+
if (warmCache) {
|
151
128
|
warmStrategyCache({
|
152
|
-
urls:
|
129
|
+
urls: warmCache,
|
153
130
|
strategy
|
154
131
|
});
|
155
132
|
}
|
156
|
-
}
|
133
|
+
};
|
157
134
|
|
158
135
|
export { googleFontsCache, imageCache, offlineFallback, pageCache, staticResourceCache, warmStrategyCache };
|
@@ -1,4 +1,10 @@
|
|
1
|
+
import { type PrecacheController } from "@serwist/sw/precaching";
|
1
2
|
export interface OfflineFallbackOptions {
|
3
|
+
/**
|
4
|
+
* An optional `PrecacheController` instance. If not provided, the singleton
|
5
|
+
* `PrecacheController` will be used.
|
6
|
+
*/
|
7
|
+
precacheController?: PrecacheController;
|
2
8
|
/**
|
3
9
|
* Precache name to match for page fallbacks. Defaults to offline.html.
|
4
10
|
*/
|
@@ -18,5 +24,5 @@ export interface OfflineFallbackOptions {
|
|
18
24
|
|
19
25
|
* @param options
|
20
26
|
*/
|
21
|
-
export declare const offlineFallback: (
|
27
|
+
export declare const offlineFallback: ({ precacheController, pageFallback, imageFallback, fontFallback, }?: OfflineFallbackOptions) => void;
|
22
28
|
//# sourceMappingURL=offlineFallback.d.ts.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"offlineFallback.d.ts","sourceRoot":"","sources":["../src/offlineFallback.ts"],"names":[],"mappings":"
|
1
|
+
{"version":3,"file":"offlineFallback.d.ts","sourceRoot":"","sources":["../src/offlineFallback.ts"],"names":[],"mappings":"AASA,OAAO,EAAE,KAAK,kBAAkB,EAAkC,MAAM,wBAAwB,CAAC;AAGjG,MAAM,WAAW,sBAAsB;IACrC;;;OAGG;IACH,kBAAkB,CAAC,EAAE,kBAAkB,CAAC;IACxC;;OAEG;IACH,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB;;OAEG;IACH,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB;;OAEG;IACH,YAAY,CAAC,EAAE,MAAM,CAAC;CACvB;AAKD;;;;;GAKG;AACH,eAAO,MAAM,eAAe,wEAKzB,sBAAsB,KAAQ,IAoChC,CAAC"}
|
package/dist/pageCache.d.ts
CHANGED
@@ -1,5 +1,11 @@
|
|
1
1
|
import type { RouteMatchCallback, SerwistPlugin } from "@serwist/core";
|
2
|
+
import { type Router } from "@serwist/sw/routing";
|
2
3
|
export interface PageCacheOptions {
|
4
|
+
/**
|
5
|
+
* An optional `Router` instance. If not provided, the singleton `Router`
|
6
|
+
* will be used.
|
7
|
+
*/
|
8
|
+
router?: Router;
|
3
9
|
/**
|
4
10
|
* Name for cache. Defaults to pages.
|
5
11
|
*/
|
@@ -28,6 +34,5 @@ export interface PageCacheOptions {
|
|
28
34
|
*
|
29
35
|
* @param options
|
30
36
|
*/
|
31
|
-
declare
|
32
|
-
export { pageCache };
|
37
|
+
export declare const pageCache: ({ router, cacheName, matchCallback, networkTimeoutSeconds, plugins, warmCache, }?: PageCacheOptions) => void;
|
33
38
|
//# sourceMappingURL=pageCache.d.ts.map
|
package/dist/pageCache.d.ts.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"pageCache.d.ts","sourceRoot":"","sources":["../src/pageCache.ts"],"names":[],"mappings":"
|
1
|
+
{"version":3,"file":"pageCache.d.ts","sourceRoot":"","sources":["../src/pageCache.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAAE,kBAAkB,EAA6B,aAAa,EAAE,MAAM,eAAe,CAAC;AAElG,OAAO,EAAE,KAAK,MAAM,EAAsB,MAAM,qBAAqB,CAAC;AAItE,MAAM,WAAW,gBAAgB;IAC/B;;;OAGG;IACH,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB;;OAEG;IACH,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB;;OAEG;IACH,aAAa,CAAC,EAAE,kBAAkB,CAAC;IACnC;;;;OAIG;IACH,qBAAqB,CAAC,EAAE,MAAM,CAAC;IAC/B;;OAEG;IACH,OAAO,CAAC,EAAE,aAAa,EAAE,CAAC;IAC1B;;OAEG;IACH,SAAS,CAAC,EAAE,MAAM,EAAE,CAAC;CACtB;AAED;;;;GAIG;AACH,eAAO,MAAM,SAAS,sFAOnB,gBAAgB,KAAQ,IAoB1B,CAAC"}
|
@@ -1,5 +1,11 @@
|
|
1
1
|
import type { RouteMatchCallback, SerwistPlugin } from "@serwist/core";
|
2
|
+
import { type Router } from "@serwist/sw/routing";
|
2
3
|
export interface StaticResourceOptions {
|
4
|
+
/**
|
5
|
+
* An optional `Router` instance. If not provided, the singleton `Router`
|
6
|
+
* will be used.
|
7
|
+
*/
|
8
|
+
router?: Router;
|
3
9
|
/**
|
4
10
|
* Name for cache.
|
5
11
|
*
|
@@ -26,6 +32,5 @@ export interface StaticResourceOptions {
|
|
26
32
|
*
|
27
33
|
* @param options
|
28
34
|
*/
|
29
|
-
declare
|
30
|
-
export { staticResourceCache };
|
35
|
+
export declare const staticResourceCache: ({ router, cacheName, matchCallback, plugins, warmCache, }?: StaticResourceOptions) => void;
|
31
36
|
//# sourceMappingURL=staticResourceCache.d.ts.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"staticResourceCache.d.ts","sourceRoot":"","sources":["../src/staticResourceCache.ts"],"names":[],"mappings":"
|
1
|
+
{"version":3,"file":"staticResourceCache.d.ts","sourceRoot":"","sources":["../src/staticResourceCache.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAAE,kBAAkB,EAA6B,aAAa,EAAE,MAAM,eAAe,CAAC;AAElG,OAAO,EAAE,KAAK,MAAM,EAAsB,MAAM,qBAAqB,CAAC;AAItE,MAAM,WAAW,qBAAqB;IACpC;;;OAGG;IACH,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB;;;;OAIG;IACH,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB;;;;OAIG;IACH,aAAa,CAAC,EAAE,kBAAkB,CAAC;IACnC;;OAEG;IACH,OAAO,CAAC,EAAE,aAAa,EAAE,CAAC;IAC1B;;OAEG;IACH,SAAS,CAAC,EAAE,MAAM,EAAE,CAAC;CACtB;AAED;;;;GAIG;AACH,eAAO,MAAM,mBAAmB,+DAO7B,qBAAqB,KAAQ,IAkB/B,CAAC"}
|
@@ -1,4 +1,4 @@
|
|
1
|
-
import type { Strategy } from "@serwist/strategies";
|
1
|
+
import type { Strategy } from "@serwist/sw/strategies";
|
2
2
|
export interface WarmStrategyCacheOptions {
|
3
3
|
/**
|
4
4
|
* Paths to warm the strategy's cache with.
|
@@ -12,6 +12,5 @@ export interface WarmStrategyCacheOptions {
|
|
12
12
|
/**
|
13
13
|
* @param options
|
14
14
|
*/
|
15
|
-
declare
|
16
|
-
export { warmStrategyCache };
|
15
|
+
export declare const warmStrategyCache: (options: WarmStrategyCacheOptions) => void;
|
17
16
|
//# sourceMappingURL=warmStrategyCache.d.ts.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"warmStrategyCache.d.ts","sourceRoot":"","sources":["../src/warmStrategyCache.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,
|
1
|
+
{"version":3,"file":"warmStrategyCache.d.ts","sourceRoot":"","sources":["../src/warmStrategyCache.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,wBAAwB,CAAC;AAEvD,MAAM,WAAW,wBAAwB;IACvC;;OAEG;IACH,IAAI,EAAE,MAAM,EAAE,CAAC;IACf;;OAEG;IACH,QAAQ,EAAE,QAAQ,CAAC;CACpB;AAKD;;GAEG;AACH,eAAO,MAAM,iBAAiB,YAAa,wBAAwB,KAAG,IAYrE,CAAC"}
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@serwist/recipes",
|
3
|
-
"version": "9.0.0-preview.
|
3
|
+
"version": "9.0.0-preview.20",
|
4
4
|
"type": "module",
|
5
5
|
"description": "A service worker helper library to manage common request and caching patterns",
|
6
6
|
"files": [
|
@@ -30,17 +30,13 @@
|
|
30
30
|
"./package.json": "./package.json"
|
31
31
|
},
|
32
32
|
"dependencies": {
|
33
|
-
"@serwist/
|
34
|
-
"@serwist/
|
35
|
-
"@serwist/expiration": "9.0.0-preview.19",
|
36
|
-
"@serwist/precaching": "9.0.0-preview.19",
|
37
|
-
"@serwist/routing": "9.0.0-preview.19",
|
38
|
-
"@serwist/strategies": "9.0.0-preview.19"
|
33
|
+
"@serwist/core": "9.0.0-preview.20",
|
34
|
+
"@serwist/sw": "9.0.0-preview.20"
|
39
35
|
},
|
40
36
|
"devDependencies": {
|
41
37
|
"rollup": "4.13.0",
|
42
38
|
"typescript": "5.5.0-dev.20240323",
|
43
|
-
"@serwist/constants": "9.0.0-preview.
|
39
|
+
"@serwist/constants": "9.0.0-preview.20"
|
44
40
|
},
|
45
41
|
"peerDependencies": {
|
46
42
|
"typescript": ">=5.0.0"
|
package/src/googleFontsCache.ts
CHANGED
@@ -6,12 +6,16 @@
|
|
6
6
|
https://opensource.org/licenses/MIT.
|
7
7
|
*/
|
8
8
|
|
9
|
-
import { CacheableResponsePlugin } from "@serwist/
|
10
|
-
import {
|
11
|
-
import {
|
12
|
-
import { CacheFirst, StaleWhileRevalidate } from "@serwist/strategies";
|
9
|
+
import { CacheableResponsePlugin, ExpirationPlugin } from "@serwist/sw/plugins";
|
10
|
+
import { type Router, getSingletonRouter } from "@serwist/sw/routing";
|
11
|
+
import { CacheFirst, StaleWhileRevalidate } from "@serwist/sw/strategies";
|
13
12
|
|
14
13
|
export interface GoogleFontCacheOptions {
|
14
|
+
/**
|
15
|
+
* An optional `Router` instance. If not provided, the singleton `Router`
|
16
|
+
* will be used.
|
17
|
+
*/
|
18
|
+
router?: Router;
|
15
19
|
/**
|
16
20
|
* Cache prefix for caching stylesheets and webfonts. Defaults to google-fonts.
|
17
21
|
*/
|
@@ -31,25 +35,25 @@ export interface GoogleFontCacheOptions {
|
|
31
35
|
*
|
32
36
|
* @param options
|
33
37
|
*/
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
38
|
+
export const googleFontsCache = ({
|
39
|
+
router = getSingletonRouter(),
|
40
|
+
cachePrefix = "google-fonts",
|
41
|
+
maxAgeSeconds = 60 * 60 * 24 * 365,
|
42
|
+
maxEntries = 30,
|
43
|
+
}: GoogleFontCacheOptions = {}): void => {
|
40
44
|
// Cache the Google Fonts stylesheets with a stale-while-revalidate strategy.
|
41
|
-
|
45
|
+
router.registerCapture(
|
42
46
|
({ url }) => url.origin === "https://fonts.googleapis.com",
|
43
47
|
new StaleWhileRevalidate({
|
44
|
-
cacheName:
|
48
|
+
cacheName: `${cachePrefix}-stylesheets`,
|
45
49
|
}),
|
46
50
|
);
|
47
51
|
|
48
52
|
// Cache the underlying font files with a cache-first strategy for 1 year.
|
49
|
-
|
53
|
+
router.registerCapture(
|
50
54
|
({ url }) => url.origin === "https://fonts.gstatic.com",
|
51
55
|
new CacheFirst({
|
52
|
-
cacheName:
|
56
|
+
cacheName: `${cachePrefix}-webfonts`,
|
53
57
|
plugins: [
|
54
58
|
new CacheableResponsePlugin({
|
55
59
|
statuses: [0, 200],
|
@@ -61,6 +65,4 @@ function googleFontsCache(options: GoogleFontCacheOptions = {}): void {
|
|
61
65
|
],
|
62
66
|
}),
|
63
67
|
);
|
64
|
-
}
|
65
|
-
|
66
|
-
export { googleFontsCache };
|
68
|
+
};
|
package/src/imageCache.ts
CHANGED
@@ -6,15 +6,18 @@
|
|
6
6
|
https://opensource.org/licenses/MIT.
|
7
7
|
*/
|
8
8
|
|
9
|
-
import { CacheableResponsePlugin } from "@serwist/cacheable-response";
|
10
9
|
import type { RouteMatchCallback, RouteMatchCallbackOptions, SerwistPlugin } from "@serwist/core";
|
11
|
-
import { ExpirationPlugin } from "@serwist/
|
12
|
-
import {
|
13
|
-
import { CacheFirst } from "@serwist/strategies";
|
14
|
-
|
10
|
+
import { CacheableResponsePlugin, ExpirationPlugin } from "@serwist/sw/plugins";
|
11
|
+
import { type Router, getSingletonRouter } from "@serwist/sw/routing";
|
12
|
+
import { CacheFirst } from "@serwist/sw/strategies";
|
15
13
|
import { warmStrategyCache } from "./warmStrategyCache.js";
|
16
14
|
|
17
15
|
export interface ImageCacheOptions {
|
16
|
+
/**
|
17
|
+
* An optional `Router` instance. If not provided, the singleton `Router`
|
18
|
+
* will be used.
|
19
|
+
*/
|
20
|
+
router?: Router;
|
18
21
|
/**
|
19
22
|
* Name for cache. Defaults to images.
|
20
23
|
*/
|
@@ -46,14 +49,15 @@ export interface ImageCacheOptions {
|
|
46
49
|
*
|
47
50
|
* @param options
|
48
51
|
*/
|
49
|
-
export const imageCache = (
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
52
|
+
export const imageCache = ({
|
53
|
+
router = getSingletonRouter(),
|
54
|
+
cacheName = "images",
|
55
|
+
matchCallback = ({ request }: RouteMatchCallbackOptions) => request.destination === "image",
|
56
|
+
maxAgeSeconds = 30 * 24 * 60 * 60,
|
57
|
+
maxEntries = 60,
|
58
|
+
plugins = [],
|
59
|
+
warmCache,
|
60
|
+
}: ImageCacheOptions = {}): void => {
|
57
61
|
plugins.push(
|
58
62
|
new CacheableResponsePlugin({
|
59
63
|
statuses: [0, 200],
|
@@ -71,10 +75,10 @@ export const imageCache = (options: ImageCacheOptions = {}): void => {
|
|
71
75
|
plugins,
|
72
76
|
});
|
73
77
|
|
74
|
-
|
78
|
+
router.registerCapture(matchCallback, strategy);
|
75
79
|
|
76
80
|
// Warms the cache
|
77
|
-
if (
|
78
|
-
warmStrategyCache({ urls:
|
81
|
+
if (warmCache) {
|
82
|
+
warmStrategyCache({ urls: warmCache, strategy });
|
79
83
|
}
|
80
84
|
};
|
package/src/offlineFallback.ts
CHANGED
@@ -7,10 +7,15 @@
|
|
7
7
|
*/
|
8
8
|
|
9
9
|
import type { RouteHandler, RouteHandlerCallbackOptions } from "@serwist/core";
|
10
|
-
import {
|
11
|
-
import { setCatchHandler } from "@serwist/routing";
|
10
|
+
import { type PrecacheController, getSingletonPrecacheController } from "@serwist/sw/precaching";
|
11
|
+
import { setCatchHandler } from "@serwist/sw/routing";
|
12
12
|
|
13
13
|
export interface OfflineFallbackOptions {
|
14
|
+
/**
|
15
|
+
* An optional `PrecacheController` instance. If not provided, the singleton
|
16
|
+
* `PrecacheController` will be used.
|
17
|
+
*/
|
18
|
+
precacheController?: PrecacheController;
|
14
19
|
/**
|
15
20
|
* Precache name to match for page fallbacks. Defaults to offline.html.
|
16
21
|
*/
|
@@ -34,11 +39,12 @@ declare let self: ServiceWorkerGlobalScope;
|
|
34
39
|
|
35
40
|
* @param options
|
36
41
|
*/
|
37
|
-
export const offlineFallback = (
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
+
export const offlineFallback = ({
|
43
|
+
precacheController = getSingletonPrecacheController(),
|
44
|
+
pageFallback = "offline.html",
|
45
|
+
imageFallback,
|
46
|
+
fontFallback,
|
47
|
+
}: OfflineFallbackOptions = {}): void => {
|
42
48
|
self.addEventListener("install", (event) => {
|
43
49
|
const files = [pageFallback];
|
44
50
|
if (imageFallback) {
|
@@ -56,17 +62,17 @@ export const offlineFallback = (options: OfflineFallbackOptions = {}): void => {
|
|
56
62
|
const cache = await self.caches.open("serwist-offline-fallbacks");
|
57
63
|
|
58
64
|
if (dest === "document") {
|
59
|
-
const match = (await matchPrecache(pageFallback)) || (await cache.match(pageFallback));
|
65
|
+
const match = (await precacheController.matchPrecache(pageFallback)) || (await cache.match(pageFallback));
|
60
66
|
return match || Response.error();
|
61
67
|
}
|
62
68
|
|
63
|
-
if (dest === "image" && imageFallback !==
|
64
|
-
const match = (await matchPrecache(imageFallback)) || (await cache.match(imageFallback));
|
69
|
+
if (dest === "image" && imageFallback !== undefined) {
|
70
|
+
const match = (await precacheController.matchPrecache(imageFallback)) || (await cache.match(imageFallback));
|
65
71
|
return match || Response.error();
|
66
72
|
}
|
67
73
|
|
68
|
-
if (dest === "font" && fontFallback !==
|
69
|
-
const match = (await matchPrecache(fontFallback)) || (await cache.match(fontFallback));
|
74
|
+
if (dest === "font" && fontFallback !== undefined) {
|
75
|
+
const match = (await precacheController.matchPrecache(fontFallback)) || (await cache.match(fontFallback));
|
70
76
|
return match || Response.error();
|
71
77
|
}
|
72
78
|
|
package/src/pageCache.ts
CHANGED
@@ -6,14 +6,18 @@
|
|
6
6
|
https://opensource.org/licenses/MIT.
|
7
7
|
*/
|
8
8
|
|
9
|
-
import { CacheableResponsePlugin } from "@serwist/cacheable-response";
|
10
9
|
import type { RouteMatchCallback, RouteMatchCallbackOptions, SerwistPlugin } from "@serwist/core";
|
11
|
-
import {
|
12
|
-
import {
|
13
|
-
|
10
|
+
import { CacheableResponsePlugin } from "@serwist/sw/plugins";
|
11
|
+
import { type Router, getSingletonRouter } from "@serwist/sw/routing";
|
12
|
+
import { NetworkFirst } from "@serwist/sw/strategies";
|
14
13
|
import { warmStrategyCache } from "./warmStrategyCache.js";
|
15
14
|
|
16
15
|
export interface PageCacheOptions {
|
16
|
+
/**
|
17
|
+
* An optional `Router` instance. If not provided, the singleton `Router`
|
18
|
+
* will be used.
|
19
|
+
*/
|
20
|
+
router?: Router;
|
17
21
|
/**
|
18
22
|
* Name for cache. Defaults to pages.
|
19
23
|
*/
|
@@ -43,13 +47,14 @@ export interface PageCacheOptions {
|
|
43
47
|
*
|
44
48
|
* @param options
|
45
49
|
*/
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
50
|
+
export const pageCache = ({
|
51
|
+
router = getSingletonRouter(),
|
52
|
+
cacheName = "pages",
|
53
|
+
matchCallback = ({ request }: RouteMatchCallbackOptions) => request.mode === "navigate",
|
54
|
+
networkTimeoutSeconds = 3,
|
55
|
+
plugins = [],
|
56
|
+
warmCache,
|
57
|
+
}: PageCacheOptions = {}): void => {
|
53
58
|
plugins.push(
|
54
59
|
new CacheableResponsePlugin({
|
55
60
|
statuses: [0, 200],
|
@@ -63,12 +68,10 @@ function pageCache(options: PageCacheOptions = {}): void {
|
|
63
68
|
});
|
64
69
|
|
65
70
|
// Registers the route
|
66
|
-
|
71
|
+
router.registerCapture(matchCallback, strategy);
|
67
72
|
|
68
73
|
// Warms the cache
|
69
|
-
if (
|
70
|
-
warmStrategyCache({ urls:
|
74
|
+
if (warmCache) {
|
75
|
+
warmStrategyCache({ urls: warmCache, strategy });
|
71
76
|
}
|
72
|
-
}
|
73
|
-
|
74
|
-
export { pageCache };
|
77
|
+
};
|
@@ -6,14 +6,18 @@
|
|
6
6
|
https://opensource.org/licenses/MIT.
|
7
7
|
*/
|
8
8
|
|
9
|
-
import { CacheableResponsePlugin } from "@serwist/cacheable-response";
|
10
9
|
import type { RouteMatchCallback, RouteMatchCallbackOptions, SerwistPlugin } from "@serwist/core";
|
11
|
-
import {
|
12
|
-
import {
|
13
|
-
|
10
|
+
import { CacheableResponsePlugin } from "@serwist/sw/plugins";
|
11
|
+
import { type Router, getSingletonRouter } from "@serwist/sw/routing";
|
12
|
+
import { StaleWhileRevalidate } from "@serwist/sw/strategies";
|
14
13
|
import { warmStrategyCache } from "./warmStrategyCache.js";
|
15
14
|
|
16
15
|
export interface StaticResourceOptions {
|
16
|
+
/**
|
17
|
+
* An optional `Router` instance. If not provided, the singleton `Router`
|
18
|
+
* will be used.
|
19
|
+
*/
|
20
|
+
router?: Router;
|
17
21
|
/**
|
18
22
|
* Name for cache.
|
19
23
|
*
|
@@ -41,13 +45,14 @@ export interface StaticResourceOptions {
|
|
41
45
|
*
|
42
46
|
* @param options
|
43
47
|
*/
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
48
|
+
export const staticResourceCache = ({
|
49
|
+
router = getSingletonRouter(),
|
50
|
+
cacheName = "static-resources",
|
51
|
+
matchCallback = ({ request }: RouteMatchCallbackOptions) =>
|
52
|
+
request.destination === "style" || request.destination === "script" || request.destination === "worker",
|
53
|
+
plugins = [],
|
54
|
+
warmCache,
|
55
|
+
}: StaticResourceOptions = {}): void => {
|
51
56
|
plugins.push(
|
52
57
|
new CacheableResponsePlugin({
|
53
58
|
statuses: [0, 200],
|
@@ -59,12 +64,10 @@ function staticResourceCache(options: StaticResourceOptions = {}): void {
|
|
59
64
|
plugins,
|
60
65
|
});
|
61
66
|
|
62
|
-
|
67
|
+
router.registerCapture(matchCallback, strategy);
|
63
68
|
|
64
69
|
// Warms the cache
|
65
|
-
if (
|
66
|
-
warmStrategyCache({ urls:
|
70
|
+
if (warmCache) {
|
71
|
+
warmStrategyCache({ urls: warmCache, strategy });
|
67
72
|
}
|
68
|
-
}
|
69
|
-
|
70
|
-
export { staticResourceCache };
|
73
|
+
};
|
package/src/warmStrategyCache.ts
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
import type { Strategy } from "@serwist/strategies";
|
1
|
+
import type { Strategy } from "@serwist/sw/strategies";
|
2
2
|
|
3
3
|
export interface WarmStrategyCacheOptions {
|
4
4
|
/**
|
@@ -17,7 +17,7 @@ declare let self: ServiceWorkerGlobalScope;
|
|
17
17
|
/**
|
18
18
|
* @param options
|
19
19
|
*/
|
20
|
-
|
20
|
+
export const warmStrategyCache = (options: WarmStrategyCacheOptions): void => {
|
21
21
|
self.addEventListener("install", (event) => {
|
22
22
|
const done = options.urls.map(
|
23
23
|
(path) =>
|
@@ -29,6 +29,4 @@ function warmStrategyCache(options: WarmStrategyCacheOptions): void {
|
|
29
29
|
|
30
30
|
event.waitUntil(Promise.all(done));
|
31
31
|
});
|
32
|
-
}
|
33
|
-
|
34
|
-
export { warmStrategyCache };
|
32
|
+
};
|