@serwist/recipes 8.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.
- package/.turbo/turbo-build.log +10 -0
- package/.turbo/turbo-lint.log +4 -0
- package/.turbo/turbo-typecheck.log +4 -0
- package/CHANGELOG.md +38 -0
- package/LICENSE +21 -0
- package/README.md +1 -0
- package/dist/googleFontsCache.d.ts +22 -0
- package/dist/googleFontsCache.d.ts.map +1 -0
- package/dist/imageCache.d.ts +35 -0
- package/dist/imageCache.d.ts.map +1 -0
- package/dist/index.d.ts +15 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +187 -0
- package/dist/index.old.cjs +194 -0
- package/dist/offlineFallback.d.ts +23 -0
- package/dist/offlineFallback.d.ts.map +1 -0
- package/dist/pageCache.d.ts +33 -0
- package/dist/pageCache.d.ts.map +1 -0
- package/dist/staticResourceCache.d.ts +31 -0
- package/dist/staticResourceCache.d.ts.map +1 -0
- package/dist/warmStrategyCache.d.ts +17 -0
- package/dist/warmStrategyCache.d.ts.map +1 -0
- package/package.json +47 -0
- package/rollup.config.js +27 -0
- package/src/googleFontsCache.ts +66 -0
- package/src/imageCache.ts +82 -0
- package/src/index.ts +23 -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/tsconfig.json +8 -0
@@ -0,0 +1,10 @@
|
|
1
|
+
|
2
|
+
> @serwist/recipes@8.0.0 build /home/runner/work/serwist/serwist/packages/recipes
|
3
|
+
> rimraf dist && cross-env NODE_ENV=production rollup --config rollup.config.js
|
4
|
+
|
5
|
+
(node:2995) ExperimentalWarning: Import assertions are not a stable feature of the JavaScript language. Avoid relying on their current behavior and syntax as those might change in a future version of Node.js.
|
6
|
+
(Use `node --trace-warnings ...` to show where the warning was created)
|
7
|
+
(node:2995) ExperimentalWarning: Importing JSON modules is an experimental feature and might change at any time
|
8
|
+
[36m
|
9
|
+
[1msrc/index.ts[22m → [1mdist/index.old.cjs, dist/index.js[22m...[39m
|
10
|
+
[32mcreated [1mdist/index.old.cjs, dist/index.js[22m in [1m1.8s[22m[39m
|
package/CHANGELOG.md
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
# @serwist/recipes
|
2
|
+
|
3
|
+
## 8.0.0
|
4
|
+
|
5
|
+
### Major Changes
|
6
|
+
|
7
|
+
- [`e0313f0`](https://github.com/serwist/serwist/commit/e0313f02f661a07ccbe9edc64e44e1af6136c73e) Thanks [@DuCanhGH](https://github.com/DuCanhGH)! - chore: initial release
|
8
|
+
|
9
|
+
- Reimagined `@serwist/next`.
|
10
|
+
- Removed various options.
|
11
|
+
- Removed `aggressiveFrontEndNavCaching` to reassess its usefulness.
|
12
|
+
- Removed `browserslist`. Use `swcEnvTargets` instead (TODO: add this option).
|
13
|
+
- Custom workers have been removed. You should use `swSrc`.
|
14
|
+
- Removed `extendDefaultRuntimeCaching`. Use the spread syntax instead (use `import { defaultCache } from "@serwist/next/browser"` to import the default runtimeCaching array).
|
15
|
+
- Temporarily removed `fallbacks` to investigate module-friendly alternatives.
|
16
|
+
- Removed `swcMinify`.
|
17
|
+
- Removed `watchWorkersInDev`.
|
18
|
+
- Removed `cacheStartUrl`, `dynamicStartUrl`, and `dynamicStartUrl`. These shall be re-added only when their use cases are made more clear to me.
|
19
|
+
- Merged `workboxOptions` with the plugin's options.
|
20
|
+
- Removed `swc-loader`, `terser-minify`, `webpack-builders`,... (we now leverage `ChildCompilationPlugin` to compile workers - this change will be backported to `@ducanh2912/next-pwa@10`)
|
21
|
+
- Removed the ability to use GenerateSW. `@serwist/sw.installSerwist` is provided as a replacement.
|
22
|
+
- `swSrc` is now a required property.
|
23
|
+
- Moved minimum support Next.js version from `11.0.0` to `14.0.0`.
|
24
|
+
- Removed GenerateSW (replaced by `@serwist/sw.installSerwist`).
|
25
|
+
- See `examples/next-basic/app/sw.ts` to see how `installSerwist` should be used.
|
26
|
+
- Repurposed `@serwist/sw`.
|
27
|
+
- The old package might be reintroduced if there's demand.
|
28
|
+
- **Note:** This is just the initial release, and there is still a lot of ground to cover (a lot of legacy code to be removed, a lot of features to be reintroduced,...). Here's to a bright future for the project :\_)
|
29
|
+
|
30
|
+
### Patch Changes
|
31
|
+
|
32
|
+
- Updated dependencies [[`e0313f0`](https://github.com/serwist/serwist/commit/e0313f02f661a07ccbe9edc64e44e1af6136c73e)]:
|
33
|
+
- @serwist/cacheable-response@8.0.0
|
34
|
+
- @serwist/expiration@8.0.0
|
35
|
+
- @serwist/precaching@8.0.0
|
36
|
+
- @serwist/strategies@8.0.0
|
37
|
+
- @serwist/routing@8.0.0
|
38
|
+
- @serwist/core@8.0.0
|
package/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2018 Google LLC, 2019 ShadowWalker w@weiw.io https://weiw.io, 2023 Serwist
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
This module's documentation can be found at https://developers.google.com/web/tools/workbox/modules/workbox-recipes
|
@@ -0,0 +1,22 @@
|
|
1
|
+
export interface GoogleFontCacheOptions {
|
2
|
+
/**
|
3
|
+
* Cache prefix for caching stylesheets and webfonts. Defaults to google-fonts.
|
4
|
+
*/
|
5
|
+
cachePrefix?: string;
|
6
|
+
/**
|
7
|
+
* Maximum age, in seconds, that font entries will be cached for. Defaults to 1 year.
|
8
|
+
*/
|
9
|
+
maxAgeSeconds?: number;
|
10
|
+
/**
|
11
|
+
* Maximum number of fonts that will be cached. Defaults to 30.
|
12
|
+
*/
|
13
|
+
maxEntries?: number;
|
14
|
+
}
|
15
|
+
/**
|
16
|
+
* An implementation of the [Google fonts](https://developers.google.com/web/tools/workbox/guides/common-recipes#google_fonts) caching recipe.
|
17
|
+
*
|
18
|
+
* @param options
|
19
|
+
*/
|
20
|
+
declare function googleFontsCache(options?: GoogleFontCacheOptions): void;
|
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"}
|
@@ -0,0 +1,35 @@
|
|
1
|
+
import type { RouteMatchCallback, SerwistPlugin } from "@serwist/core";
|
2
|
+
export interface ImageCacheOptions {
|
3
|
+
/**
|
4
|
+
* Name for cache. Defaults to images.
|
5
|
+
*/
|
6
|
+
cacheName?: string;
|
7
|
+
/**
|
8
|
+
* Serwist callback function to call to match to. Defaults to request.destination === 'image'.
|
9
|
+
*/
|
10
|
+
matchCallback?: RouteMatchCallback;
|
11
|
+
/**
|
12
|
+
* Maximum age, in seconds, that image entries will be cached for. Defaults to 30 days.
|
13
|
+
*/
|
14
|
+
maxAgeSeconds?: number;
|
15
|
+
/**
|
16
|
+
* Maximum number of images that will be cached. Defaults to 60.
|
17
|
+
*/
|
18
|
+
maxEntries?: number;
|
19
|
+
/**
|
20
|
+
* Additional plugins to use for this recipe.
|
21
|
+
*/
|
22
|
+
plugins?: SerwistPlugin[];
|
23
|
+
/**
|
24
|
+
* Paths to call to use to warm this cache.
|
25
|
+
*/
|
26
|
+
warmCache?: string[];
|
27
|
+
}
|
28
|
+
/**
|
29
|
+
* An implementation of the [image caching recipe](https://developers.google.com/web/tools/workbox/guides/common-recipes#caching_images).
|
30
|
+
*
|
31
|
+
* @param options
|
32
|
+
*/
|
33
|
+
declare function imageCache(options?: ImageCacheOptions): void;
|
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
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
import type { GoogleFontCacheOptions } from "./googleFontsCache.js";
|
2
|
+
import { googleFontsCache } from "./googleFontsCache.js";
|
3
|
+
import type { ImageCacheOptions } from "./imageCache.js";
|
4
|
+
import { imageCache } from "./imageCache.js";
|
5
|
+
import type { OfflineFallbackOptions } from "./offlineFallback.js";
|
6
|
+
import { offlineFallback } from "./offlineFallback.js";
|
7
|
+
import type { PageCacheOptions } from "./pageCache.js";
|
8
|
+
import { pageCache } from "./pageCache.js";
|
9
|
+
import type { StaticResourceOptions } from "./staticResourceCache.js";
|
10
|
+
import { staticResourceCache } from "./staticResourceCache.js";
|
11
|
+
import type { WarmStrategyCacheOptions } from "./warmStrategyCache.js";
|
12
|
+
import { warmStrategyCache } from "./warmStrategyCache.js";
|
13
|
+
export { googleFontsCache, imageCache, offlineFallback, pageCache, staticResourceCache, warmStrategyCache };
|
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
ADDED
@@ -0,0 +1,187 @@
|
|
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';
|
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 = {}) {
|
12
|
+
const sheetCacheName = `${options.cachePrefix || "google-fonts"}-stylesheets`;
|
13
|
+
const fontCacheName = `${options.cachePrefix || "google-fonts"}-webfonts`;
|
14
|
+
const maxAgeSeconds = options.maxAgeSeconds || 60 * 60 * 24 * 365;
|
15
|
+
const maxEntries = options.maxEntries || 30;
|
16
|
+
// Cache the Google Fonts stylesheets with a stale-while-revalidate strategy.
|
17
|
+
registerRoute(({ url })=>url.origin === "https://fonts.googleapis.com", new StaleWhileRevalidate({
|
18
|
+
cacheName: sheetCacheName
|
19
|
+
}));
|
20
|
+
// Cache the underlying font files with a cache-first strategy for 1 year.
|
21
|
+
registerRoute(({ url })=>url.origin === "https://fonts.gstatic.com", new CacheFirst({
|
22
|
+
cacheName: fontCacheName,
|
23
|
+
plugins: [
|
24
|
+
new CacheableResponsePlugin({
|
25
|
+
statuses: [
|
26
|
+
0,
|
27
|
+
200
|
28
|
+
]
|
29
|
+
}),
|
30
|
+
new ExpirationPlugin({
|
31
|
+
maxAgeSeconds,
|
32
|
+
maxEntries
|
33
|
+
})
|
34
|
+
]
|
35
|
+
}));
|
36
|
+
}
|
37
|
+
|
38
|
+
/**
|
39
|
+
* @param options
|
40
|
+
*/ function warmStrategyCache(options) {
|
41
|
+
self.addEventListener("install", (event)=>{
|
42
|
+
const done = options.urls.map((path)=>options.strategy.handleAll({
|
43
|
+
event,
|
44
|
+
request: new Request(path)
|
45
|
+
})[1]);
|
46
|
+
event.waitUntil(Promise.all(done));
|
47
|
+
});
|
48
|
+
}
|
49
|
+
|
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 = {}) {
|
55
|
+
const defaultMatchCallback = ({ request })=>request.destination === "image";
|
56
|
+
const cacheName = options.cacheName || "images";
|
57
|
+
const matchCallback = options.matchCallback || defaultMatchCallback;
|
58
|
+
const maxAgeSeconds = options.maxAgeSeconds || 30 * 24 * 60 * 60;
|
59
|
+
const maxEntries = options.maxEntries || 60;
|
60
|
+
const plugins = options.plugins || [];
|
61
|
+
plugins.push(new CacheableResponsePlugin({
|
62
|
+
statuses: [
|
63
|
+
0,
|
64
|
+
200
|
65
|
+
]
|
66
|
+
}));
|
67
|
+
plugins.push(new ExpirationPlugin({
|
68
|
+
maxEntries,
|
69
|
+
maxAgeSeconds
|
70
|
+
}));
|
71
|
+
const strategy = new CacheFirst({
|
72
|
+
cacheName,
|
73
|
+
plugins
|
74
|
+
});
|
75
|
+
registerRoute(matchCallback, strategy);
|
76
|
+
// Warms the cache
|
77
|
+
if (options.warmCache) {
|
78
|
+
warmStrategyCache({
|
79
|
+
urls: options.warmCache,
|
80
|
+
strategy
|
81
|
+
});
|
82
|
+
}
|
83
|
+
}
|
84
|
+
|
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 = {}) {
|
91
|
+
const pageFallback = options.pageFallback || "offline.html";
|
92
|
+
const imageFallback = options.imageFallback || false;
|
93
|
+
const fontFallback = options.fontFallback || false;
|
94
|
+
self.addEventListener("install", (event)=>{
|
95
|
+
const files = [
|
96
|
+
pageFallback
|
97
|
+
];
|
98
|
+
if (imageFallback) {
|
99
|
+
files.push(imageFallback);
|
100
|
+
}
|
101
|
+
if (fontFallback) {
|
102
|
+
files.push(fontFallback);
|
103
|
+
}
|
104
|
+
event.waitUntil(self.caches.open("serwist-offline-fallbacks").then((cache)=>cache.addAll(files)));
|
105
|
+
});
|
106
|
+
const handler = async (options)=>{
|
107
|
+
const dest = options.request.destination;
|
108
|
+
const cache = await self.caches.open("serwist-offline-fallbacks");
|
109
|
+
if (dest === "document") {
|
110
|
+
const match = await matchPrecache(pageFallback) || await cache.match(pageFallback);
|
111
|
+
return match || Response.error();
|
112
|
+
}
|
113
|
+
if (dest === "image" && imageFallback !== false) {
|
114
|
+
const match = await matchPrecache(imageFallback) || await cache.match(imageFallback);
|
115
|
+
return match || Response.error();
|
116
|
+
}
|
117
|
+
if (dest === "font" && fontFallback !== false) {
|
118
|
+
const match = await matchPrecache(fontFallback) || await cache.match(fontFallback);
|
119
|
+
return match || Response.error();
|
120
|
+
}
|
121
|
+
return Response.error();
|
122
|
+
};
|
123
|
+
setCatchHandler(handler);
|
124
|
+
}
|
125
|
+
|
126
|
+
/**
|
127
|
+
* An implementation of a page caching recipe with a network timeout.
|
128
|
+
*
|
129
|
+
* @param options
|
130
|
+
*/ function pageCache(options = {}) {
|
131
|
+
const defaultMatchCallback = ({ request })=>request.mode === "navigate";
|
132
|
+
const cacheName = options.cacheName || "pages";
|
133
|
+
const matchCallback = options.matchCallback || defaultMatchCallback;
|
134
|
+
const networkTimeoutSeconds = options.networkTimeoutSeconds || 3;
|
135
|
+
const plugins = options.plugins || [];
|
136
|
+
plugins.push(new CacheableResponsePlugin({
|
137
|
+
statuses: [
|
138
|
+
0,
|
139
|
+
200
|
140
|
+
]
|
141
|
+
}));
|
142
|
+
const strategy = new NetworkFirst({
|
143
|
+
networkTimeoutSeconds,
|
144
|
+
cacheName,
|
145
|
+
plugins
|
146
|
+
});
|
147
|
+
// Registers the route
|
148
|
+
registerRoute(matchCallback, strategy);
|
149
|
+
// Warms the cache
|
150
|
+
if (options.warmCache) {
|
151
|
+
warmStrategyCache({
|
152
|
+
urls: options.warmCache,
|
153
|
+
strategy
|
154
|
+
});
|
155
|
+
}
|
156
|
+
}
|
157
|
+
|
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 = {}) {
|
163
|
+
const defaultMatchCallback = ({ request })=>request.destination === "style" || request.destination === "script" || request.destination === "worker";
|
164
|
+
const cacheName = options.cacheName || "static-resources";
|
165
|
+
const matchCallback = options.matchCallback || defaultMatchCallback;
|
166
|
+
const plugins = options.plugins || [];
|
167
|
+
plugins.push(new CacheableResponsePlugin({
|
168
|
+
statuses: [
|
169
|
+
0,
|
170
|
+
200
|
171
|
+
]
|
172
|
+
}));
|
173
|
+
const strategy = new StaleWhileRevalidate({
|
174
|
+
cacheName,
|
175
|
+
plugins
|
176
|
+
});
|
177
|
+
registerRoute(matchCallback, strategy);
|
178
|
+
// Warms the cache
|
179
|
+
if (options.warmCache) {
|
180
|
+
warmStrategyCache({
|
181
|
+
urls: options.warmCache,
|
182
|
+
strategy
|
183
|
+
});
|
184
|
+
}
|
185
|
+
}
|
186
|
+
|
187
|
+
export { googleFontsCache, imageCache, offlineFallback, pageCache, staticResourceCache, warmStrategyCache };
|
@@ -0,0 +1,194 @@
|
|
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;
|
@@ -0,0 +1,23 @@
|
|
1
|
+
export interface OfflineFallbackOptions {
|
2
|
+
/**
|
3
|
+
* Precache name to match for page fallbacks. Defaults to offline.html.
|
4
|
+
*/
|
5
|
+
pageFallback?: string;
|
6
|
+
/**
|
7
|
+
* Precache name to match for image fallbacks.
|
8
|
+
*/
|
9
|
+
imageFallback?: string;
|
10
|
+
/**
|
11
|
+
* Precache name to match for font fallbacks.
|
12
|
+
*/
|
13
|
+
fontFallback?: string;
|
14
|
+
}
|
15
|
+
/**
|
16
|
+
* An implementation of the [comprehensive fallbacks recipe](https://developers.google.com/web/tools/workbox/guides/advanced-recipes#comprehensive_fallbacks).
|
17
|
+
* Be sure to include the fallbacks in your precache injection.
|
18
|
+
|
19
|
+
* @param options
|
20
|
+
*/
|
21
|
+
declare function offlineFallback(options?: OfflineFallbackOptions): void;
|
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"}
|
@@ -0,0 +1,33 @@
|
|
1
|
+
import type { RouteMatchCallback, SerwistPlugin } from "@serwist/core";
|
2
|
+
export interface PageCacheOptions {
|
3
|
+
/**
|
4
|
+
* Name for cache. Defaults to pages.
|
5
|
+
*/
|
6
|
+
cacheName?: string;
|
7
|
+
/**
|
8
|
+
* Serwist callback function to call to match to. Defaults to request.mode === 'navigate'.
|
9
|
+
*/
|
10
|
+
matchCallback?: RouteMatchCallback;
|
11
|
+
/**
|
12
|
+
* Maximum amount of time, in seconds, to wait on the network before falling back to cache.
|
13
|
+
*
|
14
|
+
* @default 3
|
15
|
+
*/
|
16
|
+
networkTimeoutSeconds?: number;
|
17
|
+
/**
|
18
|
+
* Additional plugins to use for this recipe.
|
19
|
+
*/
|
20
|
+
plugins?: SerwistPlugin[];
|
21
|
+
/**
|
22
|
+
* Paths to call to use to warm this cache
|
23
|
+
*/
|
24
|
+
warmCache?: string[];
|
25
|
+
}
|
26
|
+
/**
|
27
|
+
* An implementation of a page caching recipe with a network timeout.
|
28
|
+
*
|
29
|
+
* @param options
|
30
|
+
*/
|
31
|
+
declare function pageCache(options?: PageCacheOptions): void;
|
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"}
|
@@ -0,0 +1,31 @@
|
|
1
|
+
import type { RouteMatchCallback, SerwistPlugin } from "@serwist/core";
|
2
|
+
export interface StaticResourceOptions {
|
3
|
+
/**
|
4
|
+
* Name for cache.
|
5
|
+
*
|
6
|
+
* @default "static-resources"
|
7
|
+
*/
|
8
|
+
cacheName?: string;
|
9
|
+
/**
|
10
|
+
* Serwist callback function to call to match to.
|
11
|
+
*
|
12
|
+
* @default request.destination === 'style' || request.destination === 'script' || request.destination === 'worker'
|
13
|
+
*/
|
14
|
+
matchCallback?: RouteMatchCallback;
|
15
|
+
/**
|
16
|
+
* Additional plugins to use for this recipe.
|
17
|
+
*/
|
18
|
+
plugins?: SerwistPlugin[];
|
19
|
+
/**
|
20
|
+
* Paths to call to use to warm this cache.
|
21
|
+
*/
|
22
|
+
warmCache?: string[];
|
23
|
+
}
|
24
|
+
/**
|
25
|
+
* An implementation of the [CSS and JavaScript files recipe](https://developers.google.com/web/tools/workbox/guides/common-recipes#cache_css_and_javascript_files).
|
26
|
+
*
|
27
|
+
* @param options
|
28
|
+
*/
|
29
|
+
declare function staticResourceCache(options?: StaticResourceOptions): void;
|
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"}
|
@@ -0,0 +1,17 @@
|
|
1
|
+
import type { Strategy } from "@serwist/strategies";
|
2
|
+
export interface WarmStrategyCacheOptions {
|
3
|
+
/**
|
4
|
+
* Paths to warm the strategy's cache with.
|
5
|
+
*/
|
6
|
+
urls: string[];
|
7
|
+
/**
|
8
|
+
* Strategy to use.
|
9
|
+
*/
|
10
|
+
strategy: Strategy;
|
11
|
+
}
|
12
|
+
/**
|
13
|
+
* @param options
|
14
|
+
*/
|
15
|
+
declare function warmStrategyCache(options: WarmStrategyCacheOptions): void;
|
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
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
{
|
2
|
+
"name": "@serwist/recipes",
|
3
|
+
"version": "8.0.0",
|
4
|
+
"type": "module",
|
5
|
+
"description": "A service worker helper library to manage common request and caching patterns",
|
6
|
+
"keywords": [
|
7
|
+
"serwist",
|
8
|
+
"serwistjs",
|
9
|
+
"service worker",
|
10
|
+
"sw",
|
11
|
+
"router",
|
12
|
+
"routing"
|
13
|
+
],
|
14
|
+
"author": "Google's Web DevRel Team, Serwist's Team",
|
15
|
+
"license": "MIT",
|
16
|
+
"repository": "serwist/serwist",
|
17
|
+
"bugs": "https://github.com/serwist/serwist/issues",
|
18
|
+
"homepage": "https://ducanh-next-pwa.vercel.app",
|
19
|
+
"module": "./dist/index.js",
|
20
|
+
"main": "./dist/index.old.cjs",
|
21
|
+
"types": "./dist/index.d.ts",
|
22
|
+
"exports": {
|
23
|
+
".": {
|
24
|
+
"import": "./dist/index.js",
|
25
|
+
"require": "./dist/index.old.cjs",
|
26
|
+
"types": "./dist/index.d.ts"
|
27
|
+
},
|
28
|
+
"./package.json": "./package.json"
|
29
|
+
},
|
30
|
+
"dependencies": {
|
31
|
+
"@serwist/cacheable-response": "8.0.0",
|
32
|
+
"@serwist/core": "8.0.0",
|
33
|
+
"@serwist/expiration": "8.0.0",
|
34
|
+
"@serwist/precaching": "8.0.0",
|
35
|
+
"@serwist/routing": "8.0.0",
|
36
|
+
"@serwist/strategies": "8.0.0"
|
37
|
+
},
|
38
|
+
"devDependencies": {
|
39
|
+
"rollup": "3.28.1",
|
40
|
+
"@serwist/constants": "8.0.0"
|
41
|
+
},
|
42
|
+
"scripts": {
|
43
|
+
"build": "rimraf dist && cross-env NODE_ENV=production rollup --config rollup.config.js",
|
44
|
+
"lint": "eslint src --ext ts,tsx,js,jsx,cjs,mjs",
|
45
|
+
"typecheck": "tsc"
|
46
|
+
}
|
47
|
+
}
|
package/rollup.config.js
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
// @ts-check
|
2
|
+
import { getRollupOptions } from "@serwist/constants/rollup";
|
3
|
+
|
4
|
+
import packageJson from "./package.json" assert { type: "json" };
|
5
|
+
|
6
|
+
const isDev = process.env.NODE_ENV === "development";
|
7
|
+
|
8
|
+
export default getRollupOptions({
|
9
|
+
packageJson,
|
10
|
+
jsFiles: [
|
11
|
+
{
|
12
|
+
input: "src/index.ts",
|
13
|
+
output: [
|
14
|
+
{
|
15
|
+
file: "dist/index.old.cjs",
|
16
|
+
format: "cjs",
|
17
|
+
exports: "named",
|
18
|
+
},
|
19
|
+
{
|
20
|
+
file: "dist/index.js",
|
21
|
+
format: "esm",
|
22
|
+
},
|
23
|
+
],
|
24
|
+
},
|
25
|
+
],
|
26
|
+
shouldEmitDeclaration: !isDev,
|
27
|
+
});
|
@@ -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 };
|
package/src/index.ts
ADDED
@@ -0,0 +1,23 @@
|
|
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 { GoogleFontCacheOptions } from "./googleFontsCache.js";
|
10
|
+
import { googleFontsCache } from "./googleFontsCache.js";
|
11
|
+
import type { ImageCacheOptions } from "./imageCache.js";
|
12
|
+
import { imageCache } from "./imageCache.js";
|
13
|
+
import type { OfflineFallbackOptions } from "./offlineFallback.js";
|
14
|
+
import { offlineFallback } from "./offlineFallback.js";
|
15
|
+
import type { PageCacheOptions } from "./pageCache.js";
|
16
|
+
import { pageCache } from "./pageCache.js";
|
17
|
+
import type { StaticResourceOptions } from "./staticResourceCache.js";
|
18
|
+
import { staticResourceCache } from "./staticResourceCache.js";
|
19
|
+
import type { WarmStrategyCacheOptions } from "./warmStrategyCache.js";
|
20
|
+
import { warmStrategyCache } from "./warmStrategyCache.js";
|
21
|
+
|
22
|
+
export { googleFontsCache, imageCache, offlineFallback, pageCache, staticResourceCache, warmStrategyCache };
|
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 };
|