@serwist/recipes 8.4.4 → 9.0.0-preview.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.
- package/dist/googleFontsCache.d.ts +1 -0
- package/dist/googleFontsCache.d.ts.map +1 -0
- package/dist/imageCache.d.ts +1 -0
- package/dist/imageCache.d.ts.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/offlineFallback.d.ts +1 -0
- package/dist/offlineFallback.d.ts.map +1 -0
- package/dist/pageCache.d.ts +1 -0
- package/dist/pageCache.d.ts.map +1 -0
- package/dist/staticResourceCache.d.ts +1 -0
- package/dist/staticResourceCache.d.ts.map +1 -0
- package/dist/warmStrategyCache.d.ts +1 -0
- package/dist/warmStrategyCache.d.ts.map +1 -0
- package/package.json +23 -21
- package/src/googleFontsCache.ts +66 -0
- package/src/imageCache.ts +82 -0
- package/{dist/index.d.cts → src/index.ts} +9 -0
- package/src/offlineFallback.ts +79 -0
- package/src/pageCache.ts +74 -0
- package/src/staticResourceCache.ts +70 -0
- package/src/warmStrategyCache.ts +34 -0
- package/dist/index.cjs +0 -194
@@ -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"}
|
package/dist/imageCache.d.ts
CHANGED
@@ -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"}
|
@@ -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"}
|
package/dist/pageCache.d.ts
CHANGED
@@ -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"}
|
@@ -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"}
|
@@ -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": "
|
3
|
+
"version": "9.0.0-preview.0",
|
4
4
|
"type": "module",
|
5
5
|
"description": "A service worker helper library to manage common request and caching patterns",
|
6
6
|
"files": [
|
7
|
-
"
|
8
|
-
"
|
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
|
-
"
|
24
|
-
"main": "./dist/index.cjs",
|
23
|
+
"main": "./dist/index.js",
|
25
24
|
"types": "./dist/index.d.ts",
|
26
25
|
"exports": {
|
27
26
|
".": {
|
28
|
-
"
|
29
|
-
|
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": "
|
41
|
-
"@serwist/core": "
|
42
|
-
"@serwist/expiration": "
|
43
|
-
"@serwist/precaching": "
|
44
|
-
"@serwist/routing": "
|
45
|
-
"@serwist/strategies": "
|
33
|
+
"@serwist/cacheable-response": "9.0.0-preview.0",
|
34
|
+
"@serwist/core": "9.0.0-preview.0",
|
35
|
+
"@serwist/expiration": "9.0.0-preview.0",
|
36
|
+
"@serwist/precaching": "9.0.0-preview.0",
|
37
|
+
"@serwist/routing": "9.0.0-preview.0",
|
38
|
+
"@serwist/strategies": "9.0.0-preview.0"
|
46
39
|
},
|
47
40
|
"devDependencies": {
|
48
|
-
"rollup": "4.9.
|
49
|
-
"
|
41
|
+
"rollup": "4.9.6",
|
42
|
+
"typescript": "5.4.0-dev.20240203",
|
43
|
+
"@serwist/constants": "9.0.0-preview.0"
|
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 };
|
package/src/pageCache.ts
ADDED
@@ -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;
|