@serwist/recipes 9.0.0-preview.2 → 9.0.0-preview.21

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,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 function googleFontsCache(options?: GoogleFontCacheOptions): void;
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":"AAaA,MAAM,WAAW,sBAAsB;IACrC;;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,iBAAS,gBAAgB,CAAC,OAAO,GAAE,sBAA2B,GAAG,IAAI,CA8BpE;AAED,OAAO,EAAE,gBAAgB,EAAE,CAAC"}
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"}
@@ -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,6 +36,5 @@ export interface ImageCacheOptions {
30
36
  *
31
37
  * @param options
32
38
  */
33
- declare function imageCache(options?: ImageCacheOptions): void;
34
- export { imageCache };
39
+ export declare const imageCache: ({ router, cacheName, matchCallback, maxAgeSeconds, maxEntries, plugins, warmCache, }?: ImageCacheOptions) => void;
35
40
  //# sourceMappingURL=imageCache.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"imageCache.d.ts","sourceRoot":"","sources":["../src/imageCache.ts"],"names":[],"mappings":"AASA,OAAO,KAAK,EAAE,kBAAkB,EAA6B,aAAa,EAAE,MAAM,eAAe,CAAC;AAOlG,MAAM,WAAW,iBAAiB;IAChC;;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,iBAAS,UAAU,CAAC,OAAO,GAAE,iBAAsB,GAAG,IAAI,CA+BzD;AAED,OAAO,EAAE,UAAU,EAAE,CAAC"}
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/cacheable-response';
2
- import { ExpirationPlugin } from '@serwist/expiration';
3
- import { registerRoute, setCatchHandler } from '@serwist/routing';
4
- import { StaleWhileRevalidate, CacheFirst, NetworkFirst } from '@serwist/strategies';
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
- function googleFontsCache(options = {}) {
8
- const sheetCacheName = `${options.cachePrefix || "google-fonts"}-stylesheets`;
9
- const fontCacheName = `${options.cachePrefix || "google-fonts"}-webfonts`;
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
- registerRoute(({ url })=>url.origin === "https://fonts.gstatic.com", new CacheFirst({
16
- cacheName: fontCacheName,
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
- function warmStrategyCache(options) {
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
- function imageCache(options = {}) {
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 @@ function imageCache(options = {}) {
60
49
  cacheName,
61
50
  plugins
62
51
  });
63
- registerRoute(matchCallback, strategy);
64
- if (options.warmCache) {
52
+ router.registerCapture(matchCallback, strategy);
53
+ if (warmCache) {
65
54
  warmStrategyCache({
66
- urls: options.warmCache,
55
+ urls: warmCache,
67
56
  strategy
68
57
  });
69
58
  }
70
- }
59
+ };
71
60
 
72
- function offlineFallback(options = {}) {
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,28 +75,23 @@ function 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 !== false) {
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 !== false) {
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();
104
90
  };
105
91
  setCatchHandler(handler);
106
- }
92
+ };
107
93
 
108
- function pageCache(options = {}) {
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
- registerRoute(matchCallback, strategy);
126
- if (options.warmCache) {
106
+ router.registerCapture(matchCallback, strategy);
107
+ if (warmCache) {
127
108
  warmStrategyCache({
128
- urls: options.warmCache,
109
+ urls: warmCache,
129
110
  strategy
130
111
  });
131
112
  }
132
- }
113
+ };
133
114
 
134
- function staticResourceCache(options = {}) {
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
- registerRoute(matchCallback, strategy);
150
- if (options.warmCache) {
126
+ router.registerCapture(matchCallback, strategy);
127
+ if (warmCache) {
151
128
  warmStrategyCache({
152
- urls: options.warmCache,
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,6 +24,5 @@ export interface OfflineFallbackOptions {
18
24
 
19
25
  * @param options
20
26
  */
21
- declare function offlineFallback(options?: OfflineFallbackOptions): void;
22
- export { offlineFallback };
27
+ export declare const offlineFallback: ({ precacheController, pageFallback, imageFallback, fontFallback, }?: OfflineFallbackOptions) => void;
23
28
  //# sourceMappingURL=offlineFallback.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"offlineFallback.d.ts","sourceRoot":"","sources":["../src/offlineFallback.ts"],"names":[],"mappings":"AAYA,MAAM,WAAW,sBAAsB;IACrC;;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,iBAAS,eAAe,CAAC,OAAO,GAAE,sBAA2B,GAAG,IAAI,CAwCnE;AAED,OAAO,EAAE,eAAe,EAAE,CAAC"}
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"}
@@ -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 function pageCache(options?: PageCacheOptions): void;
32
- export { pageCache };
37
+ export declare const pageCache: ({ router, cacheName, matchCallback, networkTimeoutSeconds, plugins, warmCache, }?: PageCacheOptions) => void;
33
38
  //# sourceMappingURL=pageCache.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"pageCache.d.ts","sourceRoot":"","sources":["../src/pageCache.ts"],"names":[],"mappings":"AASA,OAAO,KAAK,EAAE,kBAAkB,EAA6B,aAAa,EAAE,MAAM,eAAe,CAAC;AAMlG,MAAM,WAAW,gBAAgB;IAC/B;;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,iBAAS,SAAS,CAAC,OAAO,GAAE,gBAAqB,GAAG,IAAI,CA0BvD;AAED,OAAO,EAAE,SAAS,EAAE,CAAC"}
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 function staticResourceCache(options?: StaticResourceOptions): void;
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":"AASA,OAAO,KAAK,EAAE,kBAAkB,EAA6B,aAAa,EAAE,MAAM,eAAe,CAAC;AAMlG,MAAM,WAAW,qBAAqB;IACpC;;;;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,iBAAS,mBAAmB,CAAC,OAAO,GAAE,qBAA0B,GAAG,IAAI,CAwBtE;AAED,OAAO,EAAE,mBAAmB,EAAE,CAAC"}
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 function warmStrategyCache(options: WarmStrategyCacheOptions): void;
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,qBAAqB,CAAC;AAEpD,MAAM,WAAW,wBAAwB;IACvC;;OAEG;IACH,IAAI,EAAE,MAAM,EAAE,CAAC;IACf;;OAEG;IACH,QAAQ,EAAE,QAAQ,CAAC;CACpB;AAKD;;GAEG;AACH,iBAAS,iBAAiB,CAAC,OAAO,EAAE,wBAAwB,GAAG,IAAI,CAYlE;AAED,OAAO,EAAE,iBAAiB,EAAE,CAAC"}
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.2",
3
+ "version": "9.0.0-preview.21",
4
4
  "type": "module",
5
5
  "description": "A service worker helper library to manage common request and caching patterns",
6
6
  "files": [
@@ -17,8 +17,8 @@
17
17
  ],
18
18
  "author": "Google's Web DevRel Team, Serwist's Team",
19
19
  "license": "MIT",
20
- "repository": "serwist/serwist",
21
- "bugs": "https://github.com/serwist/serwist/issues",
20
+ "repository": "https://gitlab.com/serwist/serwist",
21
+ "bugs": "https://gitlab.com/serwist/serwist/issues",
22
22
  "homepage": "https://serwist.pages.dev",
23
23
  "main": "./dist/index.js",
24
24
  "types": "./dist/index.d.ts",
@@ -30,17 +30,13 @@
30
30
  "./package.json": "./package.json"
31
31
  },
32
32
  "dependencies": {
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/precaching": "9.0.0-preview.2",
37
- "@serwist/routing": "9.0.0-preview.2",
38
- "@serwist/strategies": "9.0.0-preview.2"
33
+ "@serwist/core": "9.0.0-preview.21",
34
+ "@serwist/sw": "9.0.0-preview.21"
39
35
  },
40
36
  "devDependencies": {
41
- "rollup": "4.9.6",
42
- "typescript": "5.4.0-dev.20240206",
43
- "@serwist/constants": "9.0.0-preview.2"
37
+ "rollup": "4.13.0",
38
+ "typescript": "5.5.0-dev.20240323",
39
+ "@serwist/constants": "9.0.0-preview.21"
44
40
  },
45
41
  "peerDependencies": {
46
42
  "typescript": ">=5.0.0"
@@ -6,12 +6,16 @@
6
6
  https://opensource.org/licenses/MIT.
7
7
  */
8
8
 
9
- import { CacheableResponsePlugin } from "@serwist/cacheable-response";
10
- import { ExpirationPlugin } from "@serwist/expiration";
11
- import { registerRoute } from "@serwist/routing";
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
- function googleFontsCache(options: GoogleFontCacheOptions = {}): void {
35
- const sheetCacheName = `${options.cachePrefix || "google-fonts"}-stylesheets`;
36
- const fontCacheName = `${options.cachePrefix || "google-fonts"}-webfonts`;
37
- const maxAgeSeconds = options.maxAgeSeconds || 60 * 60 * 24 * 365;
38
- const maxEntries = options.maxEntries || 30;
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
- registerRoute(
45
+ router.registerCapture(
42
46
  ({ url }) => url.origin === "https://fonts.googleapis.com",
43
47
  new StaleWhileRevalidate({
44
- cacheName: sheetCacheName,
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
- registerRoute(
53
+ router.registerCapture(
50
54
  ({ url }) => url.origin === "https://fonts.gstatic.com",
51
55
  new CacheFirst({
52
- cacheName: fontCacheName,
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/expiration";
12
- import { registerRoute } from "@serwist/routing";
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
- function imageCache(options: ImageCacheOptions = {}): void {
50
- const defaultMatchCallback = ({ request }: RouteMatchCallbackOptions) => request.destination === "image";
51
-
52
- const cacheName = options.cacheName || "images";
53
- const matchCallback = options.matchCallback || defaultMatchCallback;
54
- const maxAgeSeconds = options.maxAgeSeconds || 30 * 24 * 60 * 60;
55
- const maxEntries = options.maxEntries || 60;
56
- const plugins = options.plugins || [];
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,12 +75,10 @@ function imageCache(options: ImageCacheOptions = {}): void {
71
75
  plugins,
72
76
  });
73
77
 
74
- registerRoute(matchCallback, strategy);
78
+ router.registerCapture(matchCallback, strategy);
75
79
 
76
80
  // Warms the cache
77
- if (options.warmCache) {
78
- warmStrategyCache({ urls: options.warmCache, strategy });
81
+ if (warmCache) {
82
+ warmStrategyCache({ urls: warmCache, strategy });
79
83
  }
80
- }
81
-
82
- export { imageCache };
84
+ };
@@ -7,10 +7,15 @@
7
7
  */
8
8
 
9
9
  import type { RouteHandler, RouteHandlerCallbackOptions } from "@serwist/core";
10
- import { matchPrecache } from "@serwist/precaching";
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
- function offlineFallback(options: OfflineFallbackOptions = {}): void {
38
- const pageFallback = options.pageFallback || "offline.html";
39
- const imageFallback = options.imageFallback || false;
40
- const fontFallback = options.fontFallback || false;
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 @@ function 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 !== false) {
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 !== false) {
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
 
@@ -74,6 +80,4 @@ function offlineFallback(options: OfflineFallbackOptions = {}): void {
74
80
  };
75
81
 
76
82
  setCatchHandler(handler);
77
- }
78
-
79
- export { offlineFallback };
83
+ };
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 { registerRoute } from "@serwist/routing";
12
- import { NetworkFirst } from "@serwist/strategies";
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
- function pageCache(options: PageCacheOptions = {}): void {
47
- const defaultMatchCallback = ({ request }: RouteMatchCallbackOptions) => request.mode === "navigate";
48
-
49
- const cacheName = options.cacheName || "pages";
50
- const matchCallback = options.matchCallback || defaultMatchCallback;
51
- const networkTimeoutSeconds = options.networkTimeoutSeconds || 3;
52
- const plugins = options.plugins || [];
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
- registerRoute(matchCallback, strategy);
71
+ router.registerCapture(matchCallback, strategy);
67
72
 
68
73
  // Warms the cache
69
- if (options.warmCache) {
70
- warmStrategyCache({ urls: options.warmCache, strategy });
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 { registerRoute } from "@serwist/routing";
12
- import { StaleWhileRevalidate } from "@serwist/strategies";
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
- function staticResourceCache(options: StaticResourceOptions = {}): void {
45
- const defaultMatchCallback = ({ request }: RouteMatchCallbackOptions) =>
46
- request.destination === "style" || request.destination === "script" || request.destination === "worker";
47
-
48
- const cacheName = options.cacheName || "static-resources";
49
- const matchCallback = options.matchCallback || defaultMatchCallback;
50
- const plugins = options.plugins || [];
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
- registerRoute(matchCallback, strategy);
67
+ router.registerCapture(matchCallback, strategy);
63
68
 
64
69
  // Warms the cache
65
- if (options.warmCache) {
66
- warmStrategyCache({ urls: options.warmCache, strategy });
70
+ if (warmCache) {
71
+ warmStrategyCache({ urls: warmCache, strategy });
67
72
  }
68
- }
69
-
70
- export { staticResourceCache };
73
+ };
@@ -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
- function warmStrategyCache(options: WarmStrategyCacheOptions): void {
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
+ };