@serwist/recipes 9.0.0-preview.8 → 9.0.0

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,9 @@
1
+ import { Serwist } from "serwist";
1
2
  export interface GoogleFontCacheOptions {
3
+ /**
4
+ * Your `Serwist` instance.
5
+ */
6
+ serwist: Serwist;
2
7
  /**
3
8
  * Cache prefix for caching stylesheets and webfonts. Defaults to google-fonts.
4
9
  */
@@ -17,6 +22,5 @@ export interface GoogleFontCacheOptions {
17
22
  *
18
23
  * @param options
19
24
  */
20
- declare function googleFontsCache(options?: GoogleFontCacheOptions): void;
21
- export { googleFontsCache };
25
+ export declare const googleFontsCache: ({ serwist, cachePrefix, maxAgeSeconds, maxEntries, }: GoogleFontCacheOptions) => void;
22
26
  //# 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":"AAOA,OAAO,EAAyD,OAAO,EAAwB,MAAM,SAAS,CAAC;AAE/G,MAAM,WAAW,sBAAsB;IACrC;;OAEG;IACH,OAAO,EAAE,OAAO,CAAC;IACjB;;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,KAAG,IAyB3B,CAAC"}
@@ -1,5 +1,10 @@
1
- import type { RouteMatchCallback, SerwistPlugin } from "@serwist/core";
1
+ import type { RouteMatchCallback, SerwistPlugin } from "serwist";
2
+ import { Serwist } from "serwist";
2
3
  export interface ImageCacheOptions {
4
+ /**
5
+ * Your `Serwist` instance.
6
+ */
7
+ serwist: Serwist;
3
8
  /**
4
9
  * Name for cache. Defaults to images.
5
10
  */
@@ -30,5 +35,5 @@ export interface ImageCacheOptions {
30
35
  *
31
36
  * @param options
32
37
  */
33
- export declare const imageCache: (options?: ImageCacheOptions) => void;
38
+ export declare const imageCache: ({ serwist, cacheName, matchCallback, maxAgeSeconds, maxEntries, plugins, warmCache, }: ImageCacheOptions) => void;
34
39
  //# 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,eAAO,MAAM,UAAU,aAAa,iBAAiB,KAAQ,IA+B5D,CAAC"}
1
+ {"version":3,"file":"imageCache.d.ts","sourceRoot":"","sources":["../src/imageCache.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAAE,kBAAkB,EAA6B,aAAa,EAAE,MAAM,SAAS,CAAC;AAC5F,OAAO,EAAyD,OAAO,EAAE,MAAM,SAAS,CAAC;AAGzF,MAAM,WAAW,iBAAiB;IAChC;;OAEG;IACH,OAAO,EAAE,OAAO,CAAC;IACjB;;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,KAAG,IAwBtB,CAAC"}
package/dist/index.js CHANGED
@@ -1,19 +1,11 @@
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 { StaleWhileRevalidate, CacheFirst, CacheableResponsePlugin, ExpirationPlugin, NetworkFirst } from 'serwist';
6
2
 
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
3
+ const googleFontsCache = ({ serwist, cachePrefix = "google-fonts", maxAgeSeconds = 60 * 60 * 24 * 365, maxEntries = 30 })=>{
4
+ serwist.registerCapture(({ url })=>url.origin === "https://fonts.googleapis.com", new StaleWhileRevalidate({
5
+ cacheName: `${cachePrefix}-stylesheets`
14
6
  }));
15
- registerRoute(({ url })=>url.origin === "https://fonts.gstatic.com", new CacheFirst({
16
- cacheName: fontCacheName,
7
+ serwist.registerCapture(({ url })=>url.origin === "https://fonts.gstatic.com", new CacheFirst({
8
+ cacheName: `${cachePrefix}-webfonts`,
17
9
  plugins: [
18
10
  new CacheableResponsePlugin({
19
11
  statuses: [
@@ -27,9 +19,9 @@ function googleFontsCache(options = {}) {
27
19
  })
28
20
  ]
29
21
  }));
30
- }
22
+ };
31
23
 
32
- function warmStrategyCache(options) {
24
+ const warmStrategyCache = (options)=>{
33
25
  self.addEventListener("install", (event)=>{
34
26
  const done = options.urls.map((path)=>options.strategy.handleAll({
35
27
  event,
@@ -37,15 +29,9 @@ function warmStrategyCache(options) {
37
29
  })[1]);
38
30
  event.waitUntil(Promise.all(done));
39
31
  });
40
- }
32
+ };
41
33
 
42
- const 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 || [];
34
+ const imageCache = ({ serwist, cacheName = "images", matchCallback = ({ request })=>request.destination === "image", maxAgeSeconds = 30 * 24 * 60 * 60, maxEntries = 60, plugins = [], warmCache })=>{
49
35
  plugins.push(new CacheableResponsePlugin({
50
36
  statuses: [
51
37
  0,
@@ -60,19 +46,16 @@ const imageCache = (options = {})=>{
60
46
  cacheName,
61
47
  plugins
62
48
  });
63
- registerRoute(matchCallback, strategy);
64
- if (options.warmCache) {
49
+ serwist.registerCapture(matchCallback, strategy);
50
+ if (warmCache) {
65
51
  warmStrategyCache({
66
- urls: options.warmCache,
52
+ urls: warmCache,
67
53
  strategy
68
54
  });
69
55
  }
70
56
  };
71
57
 
72
- const offlineFallback = (options = {})=>{
73
- const pageFallback = options.pageFallback || "offline.html";
74
- const imageFallback = options.imageFallback || false;
75
- const fontFallback = options.fontFallback || false;
58
+ const offlineFallback = ({ serwist, pageFallback = "offline.html", imageFallback, fontFallback })=>{
76
59
  self.addEventListener("install", (event)=>{
77
60
  const files = [
78
61
  pageFallback
@@ -89,28 +72,23 @@ const offlineFallback = (options = {})=>{
89
72
  const dest = options.request.destination;
90
73
  const cache = await self.caches.open("serwist-offline-fallbacks");
91
74
  if (dest === "document") {
92
- const match = await matchPrecache(pageFallback) || await cache.match(pageFallback);
75
+ const match = await serwist.matchPrecache(pageFallback) || await cache.match(pageFallback);
93
76
  return match || Response.error();
94
77
  }
95
- if (dest === "image" && imageFallback !== false) {
96
- const match = await matchPrecache(imageFallback) || await cache.match(imageFallback);
78
+ if (dest === "image" && imageFallback !== undefined) {
79
+ const match = await serwist.matchPrecache(imageFallback) || await cache.match(imageFallback);
97
80
  return match || Response.error();
98
81
  }
99
- if (dest === "font" && fontFallback !== false) {
100
- const match = await matchPrecache(fontFallback) || await cache.match(fontFallback);
82
+ if (dest === "font" && fontFallback !== undefined) {
83
+ const match = await serwist.matchPrecache(fontFallback) || await cache.match(fontFallback);
101
84
  return match || Response.error();
102
85
  }
103
86
  return Response.error();
104
87
  };
105
- setCatchHandler(handler);
88
+ serwist.setCatchHandler(handler);
106
89
  };
107
90
 
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 || [];
91
+ const pageCache = ({ serwist, cacheName = "pages", matchCallback = ({ request })=>request.mode === "navigate", networkTimeoutSeconds = 3, plugins = [], warmCache })=>{
114
92
  plugins.push(new CacheableResponsePlugin({
115
93
  statuses: [
116
94
  0,
@@ -122,20 +100,16 @@ function pageCache(options = {}) {
122
100
  cacheName,
123
101
  plugins
124
102
  });
125
- registerRoute(matchCallback, strategy);
126
- if (options.warmCache) {
103
+ serwist.registerCapture(matchCallback, strategy);
104
+ if (warmCache) {
127
105
  warmStrategyCache({
128
- urls: options.warmCache,
106
+ urls: warmCache,
129
107
  strategy
130
108
  });
131
109
  }
132
- }
110
+ };
133
111
 
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 || [];
112
+ const staticResourceCache = ({ serwist, cacheName = "static-resources", matchCallback = ({ request })=>request.destination === "style" || request.destination === "script" || request.destination === "worker", plugins = [], warmCache })=>{
139
113
  plugins.push(new CacheableResponsePlugin({
140
114
  statuses: [
141
115
  0,
@@ -146,13 +120,13 @@ function staticResourceCache(options = {}) {
146
120
  cacheName,
147
121
  plugins
148
122
  });
149
- registerRoute(matchCallback, strategy);
150
- if (options.warmCache) {
123
+ serwist.registerCapture(matchCallback, strategy);
124
+ if (warmCache) {
151
125
  warmStrategyCache({
152
- urls: options.warmCache,
126
+ urls: warmCache,
153
127
  strategy
154
128
  });
155
129
  }
156
- }
130
+ };
157
131
 
158
132
  export { googleFontsCache, imageCache, offlineFallback, pageCache, staticResourceCache, warmStrategyCache };
@@ -1,4 +1,9 @@
1
+ import type { Serwist } from "serwist";
1
2
  export interface OfflineFallbackOptions {
3
+ /**
4
+ * Your `Serwist` instance.
5
+ */
6
+ serwist: Serwist;
2
7
  /**
3
8
  * Precache name to match for page fallbacks. Defaults to offline.html.
4
9
  */
@@ -18,5 +23,5 @@ export interface OfflineFallbackOptions {
18
23
 
19
24
  * @param options
20
25
  */
21
- export declare const offlineFallback: (options?: OfflineFallbackOptions) => void;
26
+ export declare const offlineFallback: ({ serwist, pageFallback, imageFallback, fontFallback }: OfflineFallbackOptions) => void;
22
27
  //# 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,eAAO,MAAM,eAAe,aAAa,sBAAsB,KAAQ,IAwCtE,CAAC"}
1
+ {"version":3,"file":"offlineFallback.d.ts","sourceRoot":"","sources":["../src/offlineFallback.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAA6C,OAAO,EAAE,MAAM,SAAS,CAAC;AAElF,MAAM,WAAW,sBAAsB;IACrC;;OAEG;IACH,OAAO,EAAE,OAAO,CAAC;IACjB;;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,2DAA6E,sBAAsB,KAAG,IAoCjI,CAAC"}
@@ -1,5 +1,10 @@
1
- import type { RouteMatchCallback, SerwistPlugin } from "@serwist/core";
1
+ import type { RouteMatchCallback, SerwistPlugin } from "serwist";
2
+ import { Serwist } from "serwist";
2
3
  export interface PageCacheOptions {
4
+ /**
5
+ * Your `Serwist` instance.
6
+ */
7
+ serwist: Serwist;
3
8
  /**
4
9
  * Name for cache. Defaults to pages.
5
10
  */
@@ -28,6 +33,5 @@ export interface PageCacheOptions {
28
33
  *
29
34
  * @param options
30
35
  */
31
- declare function pageCache(options?: PageCacheOptions): void;
32
- export { pageCache };
36
+ export declare const pageCache: ({ serwist, cacheName, matchCallback, networkTimeoutSeconds, plugins, warmCache, }: PageCacheOptions) => void;
33
37
  //# 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,SAAS,CAAC;AAC5F,OAAO,EAAyC,OAAO,EAAE,MAAM,SAAS,CAAC;AAGzE,MAAM,WAAW,gBAAgB;IAC/B;;OAEG;IACH,OAAO,EAAE,OAAO,CAAC;IACjB;;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,KAAG,IAoBrB,CAAC"}
@@ -1,5 +1,10 @@
1
- import type { RouteMatchCallback, SerwistPlugin } from "@serwist/core";
1
+ import type { RouteMatchCallback, SerwistPlugin } from "serwist";
2
+ import { Serwist } from "serwist";
2
3
  export interface StaticResourceOptions {
4
+ /**
5
+ * Your `Serwist` instance.
6
+ */
7
+ serwist: Serwist;
3
8
  /**
4
9
  * Name for cache.
5
10
  *
@@ -26,6 +31,5 @@ export interface StaticResourceOptions {
26
31
  *
27
32
  * @param options
28
33
  */
29
- declare function staticResourceCache(options?: StaticResourceOptions): void;
30
- export { staticResourceCache };
34
+ export declare const staticResourceCache: ({ serwist, cacheName, matchCallback, plugins, warmCache, }: StaticResourceOptions) => void;
31
35
  //# 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,SAAS,CAAC;AAC5F,OAAO,EAA2B,OAAO,EAAwB,MAAM,SAAS,CAAC;AAGjF,MAAM,WAAW,qBAAqB;IACpC;;OAEG;IACH,OAAO,EAAE,OAAO,CAAC;IACjB;;;;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,KAAG,IAkB1B,CAAC"}
@@ -1,4 +1,4 @@
1
- import type { Strategy } from "@serwist/strategies";
1
+ import type { Strategy } from "serwist";
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,SAAS,CAAC;AAExC,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.8",
3
+ "version": "9.0.0",
4
4
  "type": "module",
5
5
  "description": "A service worker helper library to manage common request and caching patterns",
6
6
  "files": [
@@ -17,7 +17,7 @@
17
17
  ],
18
18
  "author": "Google's Web DevRel Team, Serwist's Team",
19
19
  "license": "MIT",
20
- "repository": "serwist/serwist",
20
+ "repository": "https://github.com/serwist/serwist",
21
21
  "bugs": "https://github.com/serwist/serwist/issues",
22
22
  "homepage": "https://serwist.pages.dev",
23
23
  "main": "./dist/index.js",
@@ -30,17 +30,12 @@
30
30
  "./package.json": "./package.json"
31
31
  },
32
32
  "dependencies": {
33
- "@serwist/cacheable-response": "9.0.0-preview.8",
34
- "@serwist/core": "9.0.0-preview.8",
35
- "@serwist/expiration": "9.0.0-preview.8",
36
- "@serwist/precaching": "9.0.0-preview.8",
37
- "@serwist/routing": "9.0.0-preview.8",
38
- "@serwist/strategies": "9.0.0-preview.8"
33
+ "serwist": "9.0.0"
39
34
  },
40
35
  "devDependencies": {
41
- "rollup": "4.9.6",
42
- "typescript": "5.4.0-dev.20240206",
43
- "@serwist/constants": "9.0.0-preview.8"
36
+ "rollup": "4.14.3",
37
+ "typescript": "5.5.0-dev.20240415",
38
+ "@serwist/configs": "9.0.0"
44
39
  },
45
40
  "peerDependencies": {
46
41
  "typescript": ">=5.0.0"
@@ -5,13 +5,13 @@
5
5
  license that can be found in the LICENSE file or at
6
6
  https://opensource.org/licenses/MIT.
7
7
  */
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";
8
+ import { CacheFirst, CacheableResponsePlugin, ExpirationPlugin, Serwist, StaleWhileRevalidate } from "serwist";
13
9
 
14
10
  export interface GoogleFontCacheOptions {
11
+ /**
12
+ * Your `Serwist` instance.
13
+ */
14
+ serwist: Serwist;
15
15
  /**
16
16
  * Cache prefix for caching stylesheets and webfonts. Defaults to google-fonts.
17
17
  */
@@ -31,25 +31,25 @@ export interface GoogleFontCacheOptions {
31
31
  *
32
32
  * @param options
33
33
  */
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
-
34
+ export const googleFontsCache = ({
35
+ serwist,
36
+ cachePrefix = "google-fonts",
37
+ maxAgeSeconds = 60 * 60 * 24 * 365,
38
+ maxEntries = 30,
39
+ }: GoogleFontCacheOptions): void => {
40
40
  // Cache the Google Fonts stylesheets with a stale-while-revalidate strategy.
41
- registerRoute(
41
+ serwist.registerCapture(
42
42
  ({ url }) => url.origin === "https://fonts.googleapis.com",
43
43
  new StaleWhileRevalidate({
44
- cacheName: sheetCacheName,
44
+ cacheName: `${cachePrefix}-stylesheets`,
45
45
  }),
46
46
  );
47
47
 
48
48
  // Cache the underlying font files with a cache-first strategy for 1 year.
49
- registerRoute(
49
+ serwist.registerCapture(
50
50
  ({ url }) => url.origin === "https://fonts.gstatic.com",
51
51
  new CacheFirst({
52
- cacheName: fontCacheName,
52
+ cacheName: `${cachePrefix}-webfonts`,
53
53
  plugins: [
54
54
  new CacheableResponsePlugin({
55
55
  statuses: [0, 200],
@@ -61,6 +61,4 @@ function googleFontsCache(options: GoogleFontCacheOptions = {}): void {
61
61
  ],
62
62
  }),
63
63
  );
64
- }
65
-
66
- export { googleFontsCache };
64
+ };
package/src/imageCache.ts CHANGED
@@ -6,15 +6,15 @@
6
6
  https://opensource.org/licenses/MIT.
7
7
  */
8
8
 
9
- import { CacheableResponsePlugin } from "@serwist/cacheable-response";
10
- 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
-
9
+ import type { RouteMatchCallback, RouteMatchCallbackOptions, SerwistPlugin } from "serwist";
10
+ import { CacheFirst, CacheableResponsePlugin, ExpirationPlugin, Serwist } from "serwist";
15
11
  import { warmStrategyCache } from "./warmStrategyCache.js";
16
12
 
17
13
  export interface ImageCacheOptions {
14
+ /**
15
+ * Your `Serwist` instance.
16
+ */
17
+ serwist: Serwist;
18
18
  /**
19
19
  * Name for cache. Defaults to images.
20
20
  */
@@ -46,14 +46,15 @@ export interface ImageCacheOptions {
46
46
  *
47
47
  * @param options
48
48
  */
49
- export const 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 || [];
49
+ export const imageCache = ({
50
+ serwist,
51
+ cacheName = "images",
52
+ matchCallback = ({ request }: RouteMatchCallbackOptions) => request.destination === "image",
53
+ maxAgeSeconds = 30 * 24 * 60 * 60,
54
+ maxEntries = 60,
55
+ plugins = [],
56
+ warmCache,
57
+ }: ImageCacheOptions): void => {
57
58
  plugins.push(
58
59
  new CacheableResponsePlugin({
59
60
  statuses: [0, 200],
@@ -71,10 +72,10 @@ export const imageCache = (options: ImageCacheOptions = {}): void => {
71
72
  plugins,
72
73
  });
73
74
 
74
- registerRoute(matchCallback, strategy);
75
+ serwist.registerCapture(matchCallback, strategy);
75
76
 
76
77
  // Warms the cache
77
- if (options.warmCache) {
78
- warmStrategyCache({ urls: options.warmCache, strategy });
78
+ if (warmCache) {
79
+ warmStrategyCache({ urls: warmCache, strategy });
79
80
  }
80
81
  };
@@ -6,11 +6,13 @@
6
6
  https://opensource.org/licenses/MIT.
7
7
  */
8
8
 
9
- import type { RouteHandler, RouteHandlerCallbackOptions } from "@serwist/core";
10
- import { matchPrecache } from "@serwist/precaching";
11
- import { setCatchHandler } from "@serwist/routing";
9
+ import type { RouteHandler, RouteHandlerCallbackOptions, Serwist } from "serwist";
12
10
 
13
11
  export interface OfflineFallbackOptions {
12
+ /**
13
+ * Your `Serwist` instance.
14
+ */
15
+ serwist: Serwist;
14
16
  /**
15
17
  * Precache name to match for page fallbacks. Defaults to offline.html.
16
18
  */
@@ -34,11 +36,7 @@ declare let self: ServiceWorkerGlobalScope;
34
36
 
35
37
  * @param options
36
38
  */
37
- export const offlineFallback = (options: OfflineFallbackOptions = {}): void => {
38
- const pageFallback = options.pageFallback || "offline.html";
39
- const imageFallback = options.imageFallback || false;
40
- const fontFallback = options.fontFallback || false;
41
-
39
+ export const offlineFallback = ({ serwist, pageFallback = "offline.html", imageFallback, fontFallback }: OfflineFallbackOptions): void => {
42
40
  self.addEventListener("install", (event) => {
43
41
  const files = [pageFallback];
44
42
  if (imageFallback) {
@@ -56,22 +54,22 @@ export const offlineFallback = (options: OfflineFallbackOptions = {}): void => {
56
54
  const cache = await self.caches.open("serwist-offline-fallbacks");
57
55
 
58
56
  if (dest === "document") {
59
- const match = (await matchPrecache(pageFallback)) || (await cache.match(pageFallback));
57
+ const match = (await serwist.matchPrecache(pageFallback)) || (await cache.match(pageFallback));
60
58
  return match || Response.error();
61
59
  }
62
60
 
63
- if (dest === "image" && imageFallback !== false) {
64
- const match = (await matchPrecache(imageFallback)) || (await cache.match(imageFallback));
61
+ if (dest === "image" && imageFallback !== undefined) {
62
+ const match = (await serwist.matchPrecache(imageFallback)) || (await cache.match(imageFallback));
65
63
  return match || Response.error();
66
64
  }
67
65
 
68
- if (dest === "font" && fontFallback !== false) {
69
- const match = (await matchPrecache(fontFallback)) || (await cache.match(fontFallback));
66
+ if (dest === "font" && fontFallback !== undefined) {
67
+ const match = (await serwist.matchPrecache(fontFallback)) || (await cache.match(fontFallback));
70
68
  return match || Response.error();
71
69
  }
72
70
 
73
71
  return Response.error();
74
72
  };
75
73
 
76
- setCatchHandler(handler);
74
+ serwist.setCatchHandler(handler);
77
75
  };
package/src/pageCache.ts CHANGED
@@ -6,14 +6,15 @@
6
6
  https://opensource.org/licenses/MIT.
7
7
  */
8
8
 
9
- import { CacheableResponsePlugin } from "@serwist/cacheable-response";
10
- import type { RouteMatchCallback, RouteMatchCallbackOptions, SerwistPlugin } from "@serwist/core";
11
- import { registerRoute } from "@serwist/routing";
12
- import { NetworkFirst } from "@serwist/strategies";
13
-
9
+ import type { RouteMatchCallback, RouteMatchCallbackOptions, SerwistPlugin } from "serwist";
10
+ import { CacheableResponsePlugin, NetworkFirst, Serwist } from "serwist";
14
11
  import { warmStrategyCache } from "./warmStrategyCache.js";
15
12
 
16
13
  export interface PageCacheOptions {
14
+ /**
15
+ * Your `Serwist` instance.
16
+ */
17
+ serwist: Serwist;
17
18
  /**
18
19
  * Name for cache. Defaults to pages.
19
20
  */
@@ -43,13 +44,14 @@ export interface PageCacheOptions {
43
44
  *
44
45
  * @param options
45
46
  */
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 || [];
47
+ export const pageCache = ({
48
+ serwist,
49
+ cacheName = "pages",
50
+ matchCallback = ({ request }: RouteMatchCallbackOptions) => request.mode === "navigate",
51
+ networkTimeoutSeconds = 3,
52
+ plugins = [],
53
+ warmCache,
54
+ }: PageCacheOptions): void => {
53
55
  plugins.push(
54
56
  new CacheableResponsePlugin({
55
57
  statuses: [0, 200],
@@ -63,12 +65,10 @@ function pageCache(options: PageCacheOptions = {}): void {
63
65
  });
64
66
 
65
67
  // Registers the route
66
- registerRoute(matchCallback, strategy);
68
+ serwist.registerCapture(matchCallback, strategy);
67
69
 
68
70
  // Warms the cache
69
- if (options.warmCache) {
70
- warmStrategyCache({ urls: options.warmCache, strategy });
71
+ if (warmCache) {
72
+ warmStrategyCache({ urls: warmCache, strategy });
71
73
  }
72
- }
73
-
74
- export { pageCache };
74
+ };
@@ -6,14 +6,15 @@
6
6
  https://opensource.org/licenses/MIT.
7
7
  */
8
8
 
9
- import { CacheableResponsePlugin } from "@serwist/cacheable-response";
10
- import type { RouteMatchCallback, RouteMatchCallbackOptions, SerwistPlugin } from "@serwist/core";
11
- import { registerRoute } from "@serwist/routing";
12
- import { StaleWhileRevalidate } from "@serwist/strategies";
13
-
9
+ import type { RouteMatchCallback, RouteMatchCallbackOptions, SerwistPlugin } from "serwist";
10
+ import { CacheableResponsePlugin, Serwist, StaleWhileRevalidate } from "serwist";
14
11
  import { warmStrategyCache } from "./warmStrategyCache.js";
15
12
 
16
13
  export interface StaticResourceOptions {
14
+ /**
15
+ * Your `Serwist` instance.
16
+ */
17
+ serwist: Serwist;
17
18
  /**
18
19
  * Name for cache.
19
20
  *
@@ -41,13 +42,14 @@ export interface StaticResourceOptions {
41
42
  *
42
43
  * @param options
43
44
  */
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 || [];
45
+ export const staticResourceCache = ({
46
+ serwist,
47
+ cacheName = "static-resources",
48
+ matchCallback = ({ request }: RouteMatchCallbackOptions) =>
49
+ request.destination === "style" || request.destination === "script" || request.destination === "worker",
50
+ plugins = [],
51
+ warmCache,
52
+ }: StaticResourceOptions): void => {
51
53
  plugins.push(
52
54
  new CacheableResponsePlugin({
53
55
  statuses: [0, 200],
@@ -59,12 +61,10 @@ function staticResourceCache(options: StaticResourceOptions = {}): void {
59
61
  plugins,
60
62
  });
61
63
 
62
- registerRoute(matchCallback, strategy);
64
+ serwist.registerCapture(matchCallback, strategy);
63
65
 
64
66
  // Warms the cache
65
- if (options.warmCache) {
66
- warmStrategyCache({ urls: options.warmCache, strategy });
67
+ if (warmCache) {
68
+ warmStrategyCache({ urls: warmCache, strategy });
67
69
  }
68
- }
69
-
70
- export { staticResourceCache };
70
+ };
@@ -1,4 +1,4 @@
1
- import type { Strategy } from "@serwist/strategies";
1
+ import type { Strategy } from "serwist";
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
+ };