@serwist/recipes 8.4.4 → 9.0.0-preview.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -19,3 +19,4 @@ export interface GoogleFontCacheOptions {
19
19
  */
20
20
  declare function googleFontsCache(options?: GoogleFontCacheOptions): void;
21
21
  export { googleFontsCache };
22
+ //# sourceMappingURL=googleFontsCache.d.ts.map
@@ -0,0 +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"}
@@ -32,3 +32,4 @@ export interface ImageCacheOptions {
32
32
  */
33
33
  declare function imageCache(options?: ImageCacheOptions): void;
34
34
  export { imageCache };
35
+ //# sourceMappingURL=imageCache.d.ts.map
@@ -0,0 +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"}
package/dist/index.d.ts CHANGED
@@ -12,3 +12,4 @@ import type { WarmStrategyCacheOptions } from "./warmStrategyCache.js";
12
12
  import { warmStrategyCache } from "./warmStrategyCache.js";
13
13
  export { googleFontsCache, imageCache, offlineFallback, pageCache, staticResourceCache, warmStrategyCache };
14
14
  export type { GoogleFontCacheOptions, ImageCacheOptions, OfflineFallbackOptions, PageCacheOptions, StaticResourceOptions, WarmStrategyCacheOptions };
15
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,uBAAuB,CAAC;AACpE,OAAO,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AACzD,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,iBAAiB,CAAC;AACzD,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAC7C,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,sBAAsB,CAAC;AACnE,OAAO,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC;AACvD,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AACvD,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAC3C,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,0BAA0B,CAAC;AACtE,OAAO,EAAE,mBAAmB,EAAE,MAAM,0BAA0B,CAAC;AAC/D,OAAO,KAAK,EAAE,wBAAwB,EAAE,MAAM,wBAAwB,CAAC;AACvE,OAAO,EAAE,iBAAiB,EAAE,MAAM,wBAAwB,CAAC;AAE3D,OAAO,EAAE,gBAAgB,EAAE,UAAU,EAAE,eAAe,EAAE,SAAS,EAAE,mBAAmB,EAAE,iBAAiB,EAAE,CAAC;AAC5G,YAAY,EAAE,sBAAsB,EAAE,iBAAiB,EAAE,sBAAsB,EAAE,gBAAgB,EAAE,qBAAqB,EAAE,wBAAwB,EAAE,CAAC"}
package/dist/index.js CHANGED
@@ -4,20 +4,14 @@ import { registerRoute, setCatchHandler } from '@serwist/routing';
4
4
  import { StaleWhileRevalidate, CacheFirst, NetworkFirst } from '@serwist/strategies';
5
5
  import { matchPrecache } from '@serwist/precaching';
6
6
 
7
- /**
8
- * An implementation of the [Google fonts](https://developers.google.com/web/tools/workbox/guides/common-recipes#google_fonts) caching recipe.
9
- *
10
- * @param options
11
- */ function googleFontsCache(options = {}) {
7
+ function googleFontsCache(options = {}) {
12
8
  const sheetCacheName = `${options.cachePrefix || "google-fonts"}-stylesheets`;
13
9
  const fontCacheName = `${options.cachePrefix || "google-fonts"}-webfonts`;
14
10
  const maxAgeSeconds = options.maxAgeSeconds || 60 * 60 * 24 * 365;
15
11
  const maxEntries = options.maxEntries || 30;
16
- // Cache the Google Fonts stylesheets with a stale-while-revalidate strategy.
17
12
  registerRoute(({ url })=>url.origin === "https://fonts.googleapis.com", new StaleWhileRevalidate({
18
13
  cacheName: sheetCacheName
19
14
  }));
20
- // Cache the underlying font files with a cache-first strategy for 1 year.
21
15
  registerRoute(({ url })=>url.origin === "https://fonts.gstatic.com", new CacheFirst({
22
16
  cacheName: fontCacheName,
23
17
  plugins: [
@@ -35,9 +29,7 @@ import { matchPrecache } from '@serwist/precaching';
35
29
  }));
36
30
  }
37
31
 
38
- /**
39
- * @param options
40
- */ function warmStrategyCache(options) {
32
+ function warmStrategyCache(options) {
41
33
  self.addEventListener("install", (event)=>{
42
34
  const done = options.urls.map((path)=>options.strategy.handleAll({
43
35
  event,
@@ -47,11 +39,7 @@ import { matchPrecache } from '@serwist/precaching';
47
39
  });
48
40
  }
49
41
 
50
- /**
51
- * An implementation of the [image caching recipe](https://developers.google.com/web/tools/workbox/guides/common-recipes#caching_images).
52
- *
53
- * @param options
54
- */ function imageCache(options = {}) {
42
+ function imageCache(options = {}) {
55
43
  const defaultMatchCallback = ({ request })=>request.destination === "image";
56
44
  const cacheName = options.cacheName || "images";
57
45
  const matchCallback = options.matchCallback || defaultMatchCallback;
@@ -73,7 +61,6 @@ import { matchPrecache } from '@serwist/precaching';
73
61
  plugins
74
62
  });
75
63
  registerRoute(matchCallback, strategy);
76
- // Warms the cache
77
64
  if (options.warmCache) {
78
65
  warmStrategyCache({
79
66
  urls: options.warmCache,
@@ -82,12 +69,7 @@ import { matchPrecache } from '@serwist/precaching';
82
69
  }
83
70
  }
84
71
 
85
- /**
86
- * An implementation of the [comprehensive fallbacks recipe](https://developers.google.com/web/tools/workbox/guides/advanced-recipes#comprehensive_fallbacks).
87
- * Be sure to include the fallbacks in your precache injection.
88
-
89
- * @param options
90
- */ function offlineFallback(options = {}) {
72
+ function offlineFallback(options = {}) {
91
73
  const pageFallback = options.pageFallback || "offline.html";
92
74
  const imageFallback = options.imageFallback || false;
93
75
  const fontFallback = options.fontFallback || false;
@@ -123,11 +105,7 @@ import { matchPrecache } from '@serwist/precaching';
123
105
  setCatchHandler(handler);
124
106
  }
125
107
 
126
- /**
127
- * An implementation of a page caching recipe with a network timeout.
128
- *
129
- * @param options
130
- */ function pageCache(options = {}) {
108
+ function pageCache(options = {}) {
131
109
  const defaultMatchCallback = ({ request })=>request.mode === "navigate";
132
110
  const cacheName = options.cacheName || "pages";
133
111
  const matchCallback = options.matchCallback || defaultMatchCallback;
@@ -144,9 +122,7 @@ import { matchPrecache } from '@serwist/precaching';
144
122
  cacheName,
145
123
  plugins
146
124
  });
147
- // Registers the route
148
125
  registerRoute(matchCallback, strategy);
149
- // Warms the cache
150
126
  if (options.warmCache) {
151
127
  warmStrategyCache({
152
128
  urls: options.warmCache,
@@ -155,11 +131,7 @@ import { matchPrecache } from '@serwist/precaching';
155
131
  }
156
132
  }
157
133
 
158
- /**
159
- * An implementation of the [CSS and JavaScript files recipe](https://developers.google.com/web/tools/workbox/guides/common-recipes#cache_css_and_javascript_files).
160
- *
161
- * @param options
162
- */ function staticResourceCache(options = {}) {
134
+ function staticResourceCache(options = {}) {
163
135
  const defaultMatchCallback = ({ request })=>request.destination === "style" || request.destination === "script" || request.destination === "worker";
164
136
  const cacheName = options.cacheName || "static-resources";
165
137
  const matchCallback = options.matchCallback || defaultMatchCallback;
@@ -175,7 +147,6 @@ import { matchPrecache } from '@serwist/precaching';
175
147
  plugins
176
148
  });
177
149
  registerRoute(matchCallback, strategy);
178
- // Warms the cache
179
150
  if (options.warmCache) {
180
151
  warmStrategyCache({
181
152
  urls: options.warmCache,
@@ -20,3 +20,4 @@ export interface OfflineFallbackOptions {
20
20
  */
21
21
  declare function offlineFallback(options?: OfflineFallbackOptions): void;
22
22
  export { offlineFallback };
23
+ //# sourceMappingURL=offlineFallback.d.ts.map
@@ -0,0 +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"}
@@ -30,3 +30,4 @@ export interface PageCacheOptions {
30
30
  */
31
31
  declare function pageCache(options?: PageCacheOptions): void;
32
32
  export { pageCache };
33
+ //# sourceMappingURL=pageCache.d.ts.map
@@ -0,0 +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"}
@@ -28,3 +28,4 @@ export interface StaticResourceOptions {
28
28
  */
29
29
  declare function staticResourceCache(options?: StaticResourceOptions): void;
30
30
  export { staticResourceCache };
31
+ //# sourceMappingURL=staticResourceCache.d.ts.map
@@ -0,0 +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"}
@@ -14,3 +14,4 @@ export interface WarmStrategyCacheOptions {
14
14
  */
15
15
  declare function warmStrategyCache(options: WarmStrategyCacheOptions): void;
16
16
  export { warmStrategyCache };
17
+ //# sourceMappingURL=warmStrategyCache.d.ts.map
@@ -0,0 +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"}
package/package.json CHANGED
@@ -1,11 +1,11 @@
1
1
  {
2
2
  "name": "@serwist/recipes",
3
- "version": "8.4.4",
3
+ "version": "9.0.0-preview.1",
4
4
  "type": "module",
5
5
  "description": "A service worker helper library to manage common request and caching patterns",
6
6
  "files": [
7
- "dist",
8
- "!dist/**/dts"
7
+ "src",
8
+ "dist"
9
9
  ],
10
10
  "keywords": [
11
11
  "serwist",
@@ -20,33 +20,35 @@
20
20
  "repository": "serwist/serwist",
21
21
  "bugs": "https://github.com/serwist/serwist/issues",
22
22
  "homepage": "https://serwist.pages.dev",
23
- "module": "./dist/index.js",
24
- "main": "./dist/index.cjs",
23
+ "main": "./dist/index.js",
25
24
  "types": "./dist/index.d.ts",
26
25
  "exports": {
27
26
  ".": {
28
- "import": {
29
- "types": "./dist/index.d.ts",
30
- "default": "./dist/index.js"
31
- },
32
- "require": {
33
- "types": "./dist/index.d.cts",
34
- "default": "./dist/index.cjs"
35
- }
27
+ "types": "./dist/index.d.ts",
28
+ "default": "./dist/index.js"
36
29
  },
37
30
  "./package.json": "./package.json"
38
31
  },
39
32
  "dependencies": {
40
- "@serwist/cacheable-response": "8.4.4",
41
- "@serwist/core": "8.4.4",
42
- "@serwist/expiration": "8.4.4",
43
- "@serwist/precaching": "8.4.4",
44
- "@serwist/routing": "8.4.4",
45
- "@serwist/strategies": "8.4.4"
33
+ "@serwist/cacheable-response": "9.0.0-preview.1",
34
+ "@serwist/core": "9.0.0-preview.1",
35
+ "@serwist/expiration": "9.0.0-preview.1",
36
+ "@serwist/precaching": "9.0.0-preview.1",
37
+ "@serwist/routing": "9.0.0-preview.1",
38
+ "@serwist/strategies": "9.0.0-preview.1"
46
39
  },
47
40
  "devDependencies": {
48
- "rollup": "4.9.1",
49
- "@serwist/constants": "8.4.4"
41
+ "rollup": "4.9.6",
42
+ "typescript": "5.4.0-dev.20240203",
43
+ "@serwist/constants": "9.0.0-preview.1"
44
+ },
45
+ "peerDependencies": {
46
+ "typescript": ">=5.0.0"
47
+ },
48
+ "peerDependenciesMeta": {
49
+ "typescript": {
50
+ "optional": true
51
+ }
50
52
  },
51
53
  "scripts": {
52
54
  "build": "rimraf dist && cross-env NODE_ENV=production rollup --config rollup.config.js",
@@ -0,0 +1,66 @@
1
+ /*
2
+ Copyright 2020 Google LLC
3
+
4
+ Use of this source code is governed by an MIT-style
5
+ license that can be found in the LICENSE file or at
6
+ https://opensource.org/licenses/MIT.
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";
13
+
14
+ export interface GoogleFontCacheOptions {
15
+ /**
16
+ * Cache prefix for caching stylesheets and webfonts. Defaults to google-fonts.
17
+ */
18
+ cachePrefix?: string;
19
+ /**
20
+ * Maximum age, in seconds, that font entries will be cached for. Defaults to 1 year.
21
+ */
22
+ maxAgeSeconds?: number;
23
+ /**
24
+ * Maximum number of fonts that will be cached. Defaults to 30.
25
+ */
26
+ maxEntries?: number;
27
+ }
28
+
29
+ /**
30
+ * An implementation of the [Google fonts](https://developers.google.com/web/tools/workbox/guides/common-recipes#google_fonts) caching recipe.
31
+ *
32
+ * @param options
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
+
40
+ // Cache the Google Fonts stylesheets with a stale-while-revalidate strategy.
41
+ registerRoute(
42
+ ({ url }) => url.origin === "https://fonts.googleapis.com",
43
+ new StaleWhileRevalidate({
44
+ cacheName: sheetCacheName,
45
+ }),
46
+ );
47
+
48
+ // Cache the underlying font files with a cache-first strategy for 1 year.
49
+ registerRoute(
50
+ ({ url }) => url.origin === "https://fonts.gstatic.com",
51
+ new CacheFirst({
52
+ cacheName: fontCacheName,
53
+ plugins: [
54
+ new CacheableResponsePlugin({
55
+ statuses: [0, 200],
56
+ }),
57
+ new ExpirationPlugin({
58
+ maxAgeSeconds,
59
+ maxEntries,
60
+ }),
61
+ ],
62
+ }),
63
+ );
64
+ }
65
+
66
+ export { googleFontsCache };
@@ -0,0 +1,82 @@
1
+ /*
2
+ Copyright 2020 Google LLC
3
+
4
+ Use of this source code is governed by an MIT-style
5
+ license that can be found in the LICENSE file or at
6
+ https://opensource.org/licenses/MIT.
7
+ */
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
+
15
+ import { warmStrategyCache } from "./warmStrategyCache.js";
16
+
17
+ export interface ImageCacheOptions {
18
+ /**
19
+ * Name for cache. Defaults to images.
20
+ */
21
+ cacheName?: string;
22
+ /**
23
+ * Serwist callback function to call to match to. Defaults to request.destination === 'image'.
24
+ */
25
+ matchCallback?: RouteMatchCallback;
26
+ /**
27
+ * Maximum age, in seconds, that image entries will be cached for. Defaults to 30 days.
28
+ */
29
+ maxAgeSeconds?: number;
30
+ /**
31
+ * Maximum number of images that will be cached. Defaults to 60.
32
+ */
33
+ maxEntries?: number;
34
+ /**
35
+ * Additional plugins to use for this recipe.
36
+ */
37
+ plugins?: SerwistPlugin[];
38
+ /**
39
+ * Paths to call to use to warm this cache.
40
+ */
41
+ warmCache?: string[];
42
+ }
43
+
44
+ /**
45
+ * An implementation of the [image caching recipe](https://developers.google.com/web/tools/workbox/guides/common-recipes#caching_images).
46
+ *
47
+ * @param options
48
+ */
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 || [];
57
+ plugins.push(
58
+ new CacheableResponsePlugin({
59
+ statuses: [0, 200],
60
+ }),
61
+ );
62
+ plugins.push(
63
+ new ExpirationPlugin({
64
+ maxEntries,
65
+ maxAgeSeconds,
66
+ }),
67
+ );
68
+
69
+ const strategy = new CacheFirst({
70
+ cacheName,
71
+ plugins,
72
+ });
73
+
74
+ registerRoute(matchCallback, strategy);
75
+
76
+ // Warms the cache
77
+ if (options.warmCache) {
78
+ warmStrategyCache({ urls: options.warmCache, strategy });
79
+ }
80
+ }
81
+
82
+ export { imageCache };
@@ -1,3 +1,11 @@
1
+ /*
2
+ Copyright 2020 Google LLC
3
+
4
+ Use of this source code is governed by an MIT-style
5
+ license that can be found in the LICENSE file or at
6
+ https://opensource.org/licenses/MIT.
7
+ */
8
+
1
9
  import type { GoogleFontCacheOptions } from "./googleFontsCache.js";
2
10
  import { googleFontsCache } from "./googleFontsCache.js";
3
11
  import type { ImageCacheOptions } from "./imageCache.js";
@@ -10,5 +18,6 @@ import type { StaticResourceOptions } from "./staticResourceCache.js";
10
18
  import { staticResourceCache } from "./staticResourceCache.js";
11
19
  import type { WarmStrategyCacheOptions } from "./warmStrategyCache.js";
12
20
  import { warmStrategyCache } from "./warmStrategyCache.js";
21
+
13
22
  export { googleFontsCache, imageCache, offlineFallback, pageCache, staticResourceCache, warmStrategyCache };
14
23
  export type { GoogleFontCacheOptions, ImageCacheOptions, OfflineFallbackOptions, PageCacheOptions, StaticResourceOptions, WarmStrategyCacheOptions };
@@ -0,0 +1,79 @@
1
+ /*
2
+ Copyright 2020 Google LLC
3
+
4
+ Use of this source code is governed by an MIT-style
5
+ license that can be found in the LICENSE file or at
6
+ https://opensource.org/licenses/MIT.
7
+ */
8
+
9
+ import type { RouteHandler, RouteHandlerCallbackOptions } from "@serwist/core";
10
+ import { matchPrecache } from "@serwist/precaching";
11
+ import { setCatchHandler } from "@serwist/routing";
12
+
13
+ export interface OfflineFallbackOptions {
14
+ /**
15
+ * Precache name to match for page fallbacks. Defaults to offline.html.
16
+ */
17
+ pageFallback?: string;
18
+ /**
19
+ * Precache name to match for image fallbacks.
20
+ */
21
+ imageFallback?: string;
22
+ /**
23
+ * Precache name to match for font fallbacks.
24
+ */
25
+ fontFallback?: string;
26
+ }
27
+
28
+ // Give TypeScript the correct global.
29
+ declare let self: ServiceWorkerGlobalScope;
30
+
31
+ /**
32
+ * An implementation of the [comprehensive fallbacks recipe](https://developers.google.com/web/tools/workbox/guides/advanced-recipes#comprehensive_fallbacks).
33
+ * Be sure to include the fallbacks in your precache injection.
34
+
35
+ * @param options
36
+ */
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
+ self.addEventListener("install", (event) => {
43
+ const files = [pageFallback];
44
+ if (imageFallback) {
45
+ files.push(imageFallback);
46
+ }
47
+ if (fontFallback) {
48
+ files.push(fontFallback);
49
+ }
50
+
51
+ event.waitUntil(self.caches.open("serwist-offline-fallbacks").then((cache) => cache.addAll(files)));
52
+ });
53
+
54
+ const handler: RouteHandler = async (options: RouteHandlerCallbackOptions) => {
55
+ const dest = options.request.destination;
56
+ const cache = await self.caches.open("serwist-offline-fallbacks");
57
+
58
+ if (dest === "document") {
59
+ const match = (await matchPrecache(pageFallback)) || (await cache.match(pageFallback));
60
+ return match || Response.error();
61
+ }
62
+
63
+ if (dest === "image" && imageFallback !== false) {
64
+ const match = (await matchPrecache(imageFallback)) || (await cache.match(imageFallback));
65
+ return match || Response.error();
66
+ }
67
+
68
+ if (dest === "font" && fontFallback !== false) {
69
+ const match = (await matchPrecache(fontFallback)) || (await cache.match(fontFallback));
70
+ return match || Response.error();
71
+ }
72
+
73
+ return Response.error();
74
+ };
75
+
76
+ setCatchHandler(handler);
77
+ }
78
+
79
+ export { offlineFallback };
@@ -0,0 +1,74 @@
1
+ /*
2
+ Copyright 2020 Google LLC
3
+
4
+ Use of this source code is governed by an MIT-style
5
+ license that can be found in the LICENSE file or at
6
+ https://opensource.org/licenses/MIT.
7
+ */
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
+
14
+ import { warmStrategyCache } from "./warmStrategyCache.js";
15
+
16
+ export interface PageCacheOptions {
17
+ /**
18
+ * Name for cache. Defaults to pages.
19
+ */
20
+ cacheName?: string;
21
+ /**
22
+ * Serwist callback function to call to match to. Defaults to request.mode === 'navigate'.
23
+ */
24
+ matchCallback?: RouteMatchCallback;
25
+ /**
26
+ * Maximum amount of time, in seconds, to wait on the network before falling back to cache.
27
+ *
28
+ * @default 3
29
+ */
30
+ networkTimeoutSeconds?: number;
31
+ /**
32
+ * Additional plugins to use for this recipe.
33
+ */
34
+ plugins?: SerwistPlugin[];
35
+ /**
36
+ * Paths to call to use to warm this cache
37
+ */
38
+ warmCache?: string[];
39
+ }
40
+
41
+ /**
42
+ * An implementation of a page caching recipe with a network timeout.
43
+ *
44
+ * @param options
45
+ */
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 || [];
53
+ plugins.push(
54
+ new CacheableResponsePlugin({
55
+ statuses: [0, 200],
56
+ }),
57
+ );
58
+
59
+ const strategy = new NetworkFirst({
60
+ networkTimeoutSeconds,
61
+ cacheName,
62
+ plugins,
63
+ });
64
+
65
+ // Registers the route
66
+ registerRoute(matchCallback, strategy);
67
+
68
+ // Warms the cache
69
+ if (options.warmCache) {
70
+ warmStrategyCache({ urls: options.warmCache, strategy });
71
+ }
72
+ }
73
+
74
+ export { pageCache };
@@ -0,0 +1,70 @@
1
+ /*
2
+ Copyright 2020 Google LLC
3
+
4
+ Use of this source code is governed by an MIT-style
5
+ license that can be found in the LICENSE file or at
6
+ https://opensource.org/licenses/MIT.
7
+ */
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
+
14
+ import { warmStrategyCache } from "./warmStrategyCache.js";
15
+
16
+ export interface StaticResourceOptions {
17
+ /**
18
+ * Name for cache.
19
+ *
20
+ * @default "static-resources"
21
+ */
22
+ cacheName?: string;
23
+ /**
24
+ * Serwist callback function to call to match to.
25
+ *
26
+ * @default request.destination === 'style' || request.destination === 'script' || request.destination === 'worker'
27
+ */
28
+ matchCallback?: RouteMatchCallback;
29
+ /**
30
+ * Additional plugins to use for this recipe.
31
+ */
32
+ plugins?: SerwistPlugin[];
33
+ /**
34
+ * Paths to call to use to warm this cache.
35
+ */
36
+ warmCache?: string[];
37
+ }
38
+
39
+ /**
40
+ * An implementation of the [CSS and JavaScript files recipe](https://developers.google.com/web/tools/workbox/guides/common-recipes#cache_css_and_javascript_files).
41
+ *
42
+ * @param options
43
+ */
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 || [];
51
+ plugins.push(
52
+ new CacheableResponsePlugin({
53
+ statuses: [0, 200],
54
+ }),
55
+ );
56
+
57
+ const strategy = new StaleWhileRevalidate({
58
+ cacheName,
59
+ plugins,
60
+ });
61
+
62
+ registerRoute(matchCallback, strategy);
63
+
64
+ // Warms the cache
65
+ if (options.warmCache) {
66
+ warmStrategyCache({ urls: options.warmCache, strategy });
67
+ }
68
+ }
69
+
70
+ export { staticResourceCache };
@@ -0,0 +1,34 @@
1
+ import type { Strategy } from "@serwist/strategies";
2
+
3
+ export interface WarmStrategyCacheOptions {
4
+ /**
5
+ * Paths to warm the strategy's cache with.
6
+ */
7
+ urls: string[];
8
+ /**
9
+ * Strategy to use.
10
+ */
11
+ strategy: Strategy;
12
+ }
13
+
14
+ // Give TypeScript the correct global.
15
+ declare let self: ServiceWorkerGlobalScope;
16
+
17
+ /**
18
+ * @param options
19
+ */
20
+ function warmStrategyCache(options: WarmStrategyCacheOptions): void {
21
+ self.addEventListener("install", (event) => {
22
+ const done = options.urls.map(
23
+ (path) =>
24
+ options.strategy.handleAll({
25
+ event,
26
+ request: new Request(path),
27
+ })[1],
28
+ );
29
+
30
+ event.waitUntil(Promise.all(done));
31
+ });
32
+ }
33
+
34
+ export { warmStrategyCache };
package/dist/index.cjs DELETED
@@ -1,194 +0,0 @@
1
- 'use strict';
2
-
3
- var cacheableResponse = require('@serwist/cacheable-response');
4
- var expiration = require('@serwist/expiration');
5
- var routing = require('@serwist/routing');
6
- var strategies = require('@serwist/strategies');
7
- var precaching = require('@serwist/precaching');
8
-
9
- /**
10
- * An implementation of the [Google fonts](https://developers.google.com/web/tools/workbox/guides/common-recipes#google_fonts) caching recipe.
11
- *
12
- * @param options
13
- */ function googleFontsCache(options = {}) {
14
- const sheetCacheName = `${options.cachePrefix || "google-fonts"}-stylesheets`;
15
- const fontCacheName = `${options.cachePrefix || "google-fonts"}-webfonts`;
16
- const maxAgeSeconds = options.maxAgeSeconds || 60 * 60 * 24 * 365;
17
- const maxEntries = options.maxEntries || 30;
18
- // Cache the Google Fonts stylesheets with a stale-while-revalidate strategy.
19
- routing.registerRoute(({ url })=>url.origin === "https://fonts.googleapis.com", new strategies.StaleWhileRevalidate({
20
- cacheName: sheetCacheName
21
- }));
22
- // Cache the underlying font files with a cache-first strategy for 1 year.
23
- routing.registerRoute(({ url })=>url.origin === "https://fonts.gstatic.com", new strategies.CacheFirst({
24
- cacheName: fontCacheName,
25
- plugins: [
26
- new cacheableResponse.CacheableResponsePlugin({
27
- statuses: [
28
- 0,
29
- 200
30
- ]
31
- }),
32
- new expiration.ExpirationPlugin({
33
- maxAgeSeconds,
34
- maxEntries
35
- })
36
- ]
37
- }));
38
- }
39
-
40
- /**
41
- * @param options
42
- */ function warmStrategyCache(options) {
43
- self.addEventListener("install", (event)=>{
44
- const done = options.urls.map((path)=>options.strategy.handleAll({
45
- event,
46
- request: new Request(path)
47
- })[1]);
48
- event.waitUntil(Promise.all(done));
49
- });
50
- }
51
-
52
- /**
53
- * An implementation of the [image caching recipe](https://developers.google.com/web/tools/workbox/guides/common-recipes#caching_images).
54
- *
55
- * @param options
56
- */ function imageCache(options = {}) {
57
- const defaultMatchCallback = ({ request })=>request.destination === "image";
58
- const cacheName = options.cacheName || "images";
59
- const matchCallback = options.matchCallback || defaultMatchCallback;
60
- const maxAgeSeconds = options.maxAgeSeconds || 30 * 24 * 60 * 60;
61
- const maxEntries = options.maxEntries || 60;
62
- const plugins = options.plugins || [];
63
- plugins.push(new cacheableResponse.CacheableResponsePlugin({
64
- statuses: [
65
- 0,
66
- 200
67
- ]
68
- }));
69
- plugins.push(new expiration.ExpirationPlugin({
70
- maxEntries,
71
- maxAgeSeconds
72
- }));
73
- const strategy = new strategies.CacheFirst({
74
- cacheName,
75
- plugins
76
- });
77
- routing.registerRoute(matchCallback, strategy);
78
- // Warms the cache
79
- if (options.warmCache) {
80
- warmStrategyCache({
81
- urls: options.warmCache,
82
- strategy
83
- });
84
- }
85
- }
86
-
87
- /**
88
- * An implementation of the [comprehensive fallbacks recipe](https://developers.google.com/web/tools/workbox/guides/advanced-recipes#comprehensive_fallbacks).
89
- * Be sure to include the fallbacks in your precache injection.
90
-
91
- * @param options
92
- */ function offlineFallback(options = {}) {
93
- const pageFallback = options.pageFallback || "offline.html";
94
- const imageFallback = options.imageFallback || false;
95
- const fontFallback = options.fontFallback || false;
96
- self.addEventListener("install", (event)=>{
97
- const files = [
98
- pageFallback
99
- ];
100
- if (imageFallback) {
101
- files.push(imageFallback);
102
- }
103
- if (fontFallback) {
104
- files.push(fontFallback);
105
- }
106
- event.waitUntil(self.caches.open("serwist-offline-fallbacks").then((cache)=>cache.addAll(files)));
107
- });
108
- const handler = async (options)=>{
109
- const dest = options.request.destination;
110
- const cache = await self.caches.open("serwist-offline-fallbacks");
111
- if (dest === "document") {
112
- const match = await precaching.matchPrecache(pageFallback) || await cache.match(pageFallback);
113
- return match || Response.error();
114
- }
115
- if (dest === "image" && imageFallback !== false) {
116
- const match = await precaching.matchPrecache(imageFallback) || await cache.match(imageFallback);
117
- return match || Response.error();
118
- }
119
- if (dest === "font" && fontFallback !== false) {
120
- const match = await precaching.matchPrecache(fontFallback) || await cache.match(fontFallback);
121
- return match || Response.error();
122
- }
123
- return Response.error();
124
- };
125
- routing.setCatchHandler(handler);
126
- }
127
-
128
- /**
129
- * An implementation of a page caching recipe with a network timeout.
130
- *
131
- * @param options
132
- */ function pageCache(options = {}) {
133
- const defaultMatchCallback = ({ request })=>request.mode === "navigate";
134
- const cacheName = options.cacheName || "pages";
135
- const matchCallback = options.matchCallback || defaultMatchCallback;
136
- const networkTimeoutSeconds = options.networkTimeoutSeconds || 3;
137
- const plugins = options.plugins || [];
138
- plugins.push(new cacheableResponse.CacheableResponsePlugin({
139
- statuses: [
140
- 0,
141
- 200
142
- ]
143
- }));
144
- const strategy = new strategies.NetworkFirst({
145
- networkTimeoutSeconds,
146
- cacheName,
147
- plugins
148
- });
149
- // Registers the route
150
- routing.registerRoute(matchCallback, strategy);
151
- // Warms the cache
152
- if (options.warmCache) {
153
- warmStrategyCache({
154
- urls: options.warmCache,
155
- strategy
156
- });
157
- }
158
- }
159
-
160
- /**
161
- * An implementation of the [CSS and JavaScript files recipe](https://developers.google.com/web/tools/workbox/guides/common-recipes#cache_css_and_javascript_files).
162
- *
163
- * @param options
164
- */ function staticResourceCache(options = {}) {
165
- const defaultMatchCallback = ({ request })=>request.destination === "style" || request.destination === "script" || request.destination === "worker";
166
- const cacheName = options.cacheName || "static-resources";
167
- const matchCallback = options.matchCallback || defaultMatchCallback;
168
- const plugins = options.plugins || [];
169
- plugins.push(new cacheableResponse.CacheableResponsePlugin({
170
- statuses: [
171
- 0,
172
- 200
173
- ]
174
- }));
175
- const strategy = new strategies.StaleWhileRevalidate({
176
- cacheName,
177
- plugins
178
- });
179
- routing.registerRoute(matchCallback, strategy);
180
- // Warms the cache
181
- if (options.warmCache) {
182
- warmStrategyCache({
183
- urls: options.warmCache,
184
- strategy
185
- });
186
- }
187
- }
188
-
189
- exports.googleFontsCache = googleFontsCache;
190
- exports.imageCache = imageCache;
191
- exports.offlineFallback = offlineFallback;
192
- exports.pageCache = pageCache;
193
- exports.staticResourceCache = staticResourceCache;
194
- exports.warmStrategyCache = warmStrategyCache;