@serwist/recipes 9.5.7 → 9.5.8
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/index.d.mts +212 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +122 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +10 -11
- package/dist/googleFontsCache.d.ts +0 -26
- package/dist/googleFontsCache.d.ts.map +0 -1
- package/dist/imageCache.d.ts +0 -39
- package/dist/imageCache.d.ts.map +0 -1
- package/dist/index.d.ts +0 -15
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js +0 -132
- package/dist/offlineFallback.d.ts +0 -27
- package/dist/offlineFallback.d.ts.map +0 -1
- package/dist/pageCache.d.ts +0 -37
- package/dist/pageCache.d.ts.map +0 -1
- package/dist/staticResourceCache.d.ts +0 -35
- package/dist/staticResourceCache.d.ts.map +0 -1
- package/dist/warmStrategyCache.d.ts +0 -16
- package/dist/warmStrategyCache.d.ts.map +0 -1
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
import { RouteMatchCallback, Serwist, SerwistPlugin, Strategy } from "serwist";
|
|
2
|
+
|
|
3
|
+
//#region src/googleFontsCache.d.ts
|
|
4
|
+
interface GoogleFontCacheOptions {
|
|
5
|
+
/**
|
|
6
|
+
* A {@linkcode Serwist} instance.
|
|
7
|
+
*/
|
|
8
|
+
serwist: Serwist;
|
|
9
|
+
/**
|
|
10
|
+
* Cache prefix for caching stylesheets and webfonts. Defaults to google-fonts.
|
|
11
|
+
*/
|
|
12
|
+
cachePrefix?: string;
|
|
13
|
+
/**
|
|
14
|
+
* Maximum age, in seconds, that font entries will be cached for. Defaults to 1 year.
|
|
15
|
+
*/
|
|
16
|
+
maxAgeSeconds?: number;
|
|
17
|
+
/**
|
|
18
|
+
* Maximum number of fonts that will be cached. Defaults to 30.
|
|
19
|
+
*/
|
|
20
|
+
maxEntries?: number;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* An implementation of the [Google fonts](https://developers.google.com/web/tools/workbox/guides/common-recipes#google_fonts) caching recipe.
|
|
24
|
+
*
|
|
25
|
+
* @param options
|
|
26
|
+
*/
|
|
27
|
+
declare const googleFontsCache: ({
|
|
28
|
+
serwist,
|
|
29
|
+
cachePrefix,
|
|
30
|
+
maxAgeSeconds,
|
|
31
|
+
maxEntries
|
|
32
|
+
}: GoogleFontCacheOptions) => void;
|
|
33
|
+
//#endregion
|
|
34
|
+
//#region src/imageCache.d.ts
|
|
35
|
+
interface ImageCacheOptions {
|
|
36
|
+
/**
|
|
37
|
+
* A {@linkcode Serwist} instance.
|
|
38
|
+
*/
|
|
39
|
+
serwist: Serwist;
|
|
40
|
+
/**
|
|
41
|
+
* Name for cache. Defaults to images.
|
|
42
|
+
*/
|
|
43
|
+
cacheName?: string;
|
|
44
|
+
/**
|
|
45
|
+
* Serwist callback function to call to match to. Defaults to request.destination === 'image'.
|
|
46
|
+
*/
|
|
47
|
+
matchCallback?: RouteMatchCallback;
|
|
48
|
+
/**
|
|
49
|
+
* Maximum age, in seconds, that image entries will be cached for. Defaults to 30 days.
|
|
50
|
+
*/
|
|
51
|
+
maxAgeSeconds?: number;
|
|
52
|
+
/**
|
|
53
|
+
* Maximum number of images that will be cached. Defaults to 60.
|
|
54
|
+
*/
|
|
55
|
+
maxEntries?: number;
|
|
56
|
+
/**
|
|
57
|
+
* Additional plugins to use for this recipe.
|
|
58
|
+
*/
|
|
59
|
+
plugins?: SerwistPlugin[];
|
|
60
|
+
/**
|
|
61
|
+
* Paths to call to use to warm this cache.
|
|
62
|
+
*/
|
|
63
|
+
warmCache?: string[];
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* An implementation of the [image caching recipe](https://developers.google.com/web/tools/workbox/guides/common-recipes#caching_images).
|
|
67
|
+
*
|
|
68
|
+
* @param options
|
|
69
|
+
*/
|
|
70
|
+
declare const imageCache: ({
|
|
71
|
+
serwist,
|
|
72
|
+
cacheName,
|
|
73
|
+
matchCallback,
|
|
74
|
+
maxAgeSeconds,
|
|
75
|
+
maxEntries,
|
|
76
|
+
plugins,
|
|
77
|
+
warmCache
|
|
78
|
+
}: ImageCacheOptions) => void;
|
|
79
|
+
//#endregion
|
|
80
|
+
//#region src/offlineFallback.d.ts
|
|
81
|
+
interface OfflineFallbackOptions {
|
|
82
|
+
/**
|
|
83
|
+
* A {@linkcode Serwist} instance.
|
|
84
|
+
*/
|
|
85
|
+
serwist: Serwist;
|
|
86
|
+
/**
|
|
87
|
+
* Precache name to match for page fallbacks. Defaults to offline.html.
|
|
88
|
+
*/
|
|
89
|
+
pageFallback?: string;
|
|
90
|
+
/**
|
|
91
|
+
* Precache name to match for image fallbacks.
|
|
92
|
+
*/
|
|
93
|
+
imageFallback?: string;
|
|
94
|
+
/**
|
|
95
|
+
* Precache name to match for font fallbacks.
|
|
96
|
+
*/
|
|
97
|
+
fontFallback?: string;
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* An implementation of the [comprehensive fallbacks recipe](https://developers.google.com/web/tools/workbox/guides/advanced-recipes#comprehensive_fallbacks).
|
|
101
|
+
* Be sure to include the fallbacks in your precache injection.
|
|
102
|
+
|
|
103
|
+
* @param options
|
|
104
|
+
*/
|
|
105
|
+
declare const offlineFallback: ({
|
|
106
|
+
serwist,
|
|
107
|
+
pageFallback,
|
|
108
|
+
imageFallback,
|
|
109
|
+
fontFallback
|
|
110
|
+
}: OfflineFallbackOptions) => void;
|
|
111
|
+
//#endregion
|
|
112
|
+
//#region src/pageCache.d.ts
|
|
113
|
+
interface PageCacheOptions {
|
|
114
|
+
/**
|
|
115
|
+
* A {@linkcode Serwist} instance.
|
|
116
|
+
*/
|
|
117
|
+
serwist: Serwist;
|
|
118
|
+
/**
|
|
119
|
+
* Name for cache. Defaults to pages.
|
|
120
|
+
*/
|
|
121
|
+
cacheName?: string;
|
|
122
|
+
/**
|
|
123
|
+
* Serwist callback function to call to match to. Defaults to request.mode === 'navigate'.
|
|
124
|
+
*/
|
|
125
|
+
matchCallback?: RouteMatchCallback;
|
|
126
|
+
/**
|
|
127
|
+
* Maximum amount of time, in seconds, to wait on the network before falling back to cache.
|
|
128
|
+
*
|
|
129
|
+
* @default 3
|
|
130
|
+
*/
|
|
131
|
+
networkTimeoutSeconds?: number;
|
|
132
|
+
/**
|
|
133
|
+
* Additional plugins to use for this recipe.
|
|
134
|
+
*/
|
|
135
|
+
plugins?: SerwistPlugin[];
|
|
136
|
+
/**
|
|
137
|
+
* Paths to call to use to warm this cache
|
|
138
|
+
*/
|
|
139
|
+
warmCache?: string[];
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* An implementation of a page caching recipe with a network timeout.
|
|
143
|
+
*
|
|
144
|
+
* @param options
|
|
145
|
+
*/
|
|
146
|
+
declare const pageCache: ({
|
|
147
|
+
serwist,
|
|
148
|
+
cacheName,
|
|
149
|
+
matchCallback,
|
|
150
|
+
networkTimeoutSeconds,
|
|
151
|
+
plugins,
|
|
152
|
+
warmCache
|
|
153
|
+
}: PageCacheOptions) => void;
|
|
154
|
+
//#endregion
|
|
155
|
+
//#region src/staticResourceCache.d.ts
|
|
156
|
+
interface StaticResourceOptions {
|
|
157
|
+
/**
|
|
158
|
+
* A {@linkcode Serwist} instance.
|
|
159
|
+
*/
|
|
160
|
+
serwist: Serwist;
|
|
161
|
+
/**
|
|
162
|
+
* Name for cache.
|
|
163
|
+
*
|
|
164
|
+
* @default "static-resources"
|
|
165
|
+
*/
|
|
166
|
+
cacheName?: string;
|
|
167
|
+
/**
|
|
168
|
+
* Serwist callback function to call to match to.
|
|
169
|
+
*
|
|
170
|
+
* @default request.destination === 'style' || request.destination === 'script' || request.destination === 'worker'
|
|
171
|
+
*/
|
|
172
|
+
matchCallback?: RouteMatchCallback;
|
|
173
|
+
/**
|
|
174
|
+
* Additional plugins to use for this recipe.
|
|
175
|
+
*/
|
|
176
|
+
plugins?: SerwistPlugin[];
|
|
177
|
+
/**
|
|
178
|
+
* Paths to call to use to warm this cache.
|
|
179
|
+
*/
|
|
180
|
+
warmCache?: string[];
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* An implementation of the [CSS and JavaScript files recipe](https://developers.google.com/web/tools/workbox/guides/common-recipes#cache_css_and_javascript_files).
|
|
184
|
+
*
|
|
185
|
+
* @param options
|
|
186
|
+
*/
|
|
187
|
+
declare const staticResourceCache: ({
|
|
188
|
+
serwist,
|
|
189
|
+
cacheName,
|
|
190
|
+
matchCallback,
|
|
191
|
+
plugins,
|
|
192
|
+
warmCache
|
|
193
|
+
}: StaticResourceOptions) => void;
|
|
194
|
+
//#endregion
|
|
195
|
+
//#region src/warmStrategyCache.d.ts
|
|
196
|
+
interface WarmStrategyCacheOptions {
|
|
197
|
+
/**
|
|
198
|
+
* Paths to warm the strategy's cache with.
|
|
199
|
+
*/
|
|
200
|
+
urls: string[];
|
|
201
|
+
/**
|
|
202
|
+
* Strategy to use.
|
|
203
|
+
*/
|
|
204
|
+
strategy: Strategy;
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* @param options
|
|
208
|
+
*/
|
|
209
|
+
declare const warmStrategyCache: (options: WarmStrategyCacheOptions) => void;
|
|
210
|
+
//#endregion
|
|
211
|
+
export { type GoogleFontCacheOptions, type ImageCacheOptions, type OfflineFallbackOptions, type PageCacheOptions, type StaticResourceOptions, type WarmStrategyCacheOptions, googleFontsCache, imageCache, offlineFallback, pageCache, staticResourceCache, warmStrategyCache };
|
|
212
|
+
//# sourceMappingURL=index.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.mts","names":[],"sources":["../src/googleFontsCache.ts","../src/imageCache.ts","../src/offlineFallback.ts","../src/pageCache.ts","../src/staticResourceCache.ts","../src/warmStrategyCache.ts"],"mappings":";;;UASiB,sBAAA;;AAAjB;;EAIE,OAAA,EAAS,OAAA;EAAO;;;EAIhB,WAAA;EAIA;;;EAAA,aAAA;EAYW;;;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KHE,IAAA;ELOgB;;;EKHhB,QAAA,EAAU,QAAA;AAAA;;;;cASC,iBAAA,GAAqB,OAAA,EAAS,wBAAA"}
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import { CacheFirst, CacheableResponsePlugin, ExpirationPlugin, NetworkFirst, StaleWhileRevalidate } from "serwist";
|
|
2
|
+
//#region src/googleFontsCache.ts
|
|
3
|
+
/**
|
|
4
|
+
* An implementation of the [Google fonts](https://developers.google.com/web/tools/workbox/guides/common-recipes#google_fonts) caching recipe.
|
|
5
|
+
*
|
|
6
|
+
* @param options
|
|
7
|
+
*/
|
|
8
|
+
const googleFontsCache = ({ serwist, cachePrefix = "google-fonts", maxAgeSeconds = 3600 * 24 * 365, maxEntries = 30 }) => {
|
|
9
|
+
serwist.registerCapture(({ url }) => url.origin === "https://fonts.googleapis.com", new StaleWhileRevalidate({ cacheName: `${cachePrefix}-stylesheets` }));
|
|
10
|
+
serwist.registerCapture(({ url }) => url.origin === "https://fonts.gstatic.com", new CacheFirst({
|
|
11
|
+
cacheName: `${cachePrefix}-webfonts`,
|
|
12
|
+
plugins: [new CacheableResponsePlugin({ statuses: [0, 200] }), new ExpirationPlugin({
|
|
13
|
+
maxAgeSeconds,
|
|
14
|
+
maxEntries
|
|
15
|
+
})]
|
|
16
|
+
}));
|
|
17
|
+
};
|
|
18
|
+
//#endregion
|
|
19
|
+
//#region src/warmStrategyCache.ts
|
|
20
|
+
/**
|
|
21
|
+
* @param options
|
|
22
|
+
*/
|
|
23
|
+
const warmStrategyCache = (options) => {
|
|
24
|
+
self.addEventListener("install", (event) => {
|
|
25
|
+
const done = options.urls.map((path) => options.strategy.handleAll({
|
|
26
|
+
event,
|
|
27
|
+
request: new Request(path)
|
|
28
|
+
})[1]);
|
|
29
|
+
event.waitUntil(Promise.all(done));
|
|
30
|
+
});
|
|
31
|
+
};
|
|
32
|
+
//#endregion
|
|
33
|
+
//#region src/imageCache.ts
|
|
34
|
+
/**
|
|
35
|
+
* An implementation of the [image caching recipe](https://developers.google.com/web/tools/workbox/guides/common-recipes#caching_images).
|
|
36
|
+
*
|
|
37
|
+
* @param options
|
|
38
|
+
*/
|
|
39
|
+
const imageCache = ({ serwist, cacheName = "images", matchCallback = ({ request }) => request.destination === "image", maxAgeSeconds = 720 * 60 * 60, maxEntries = 60, plugins = [], warmCache }) => {
|
|
40
|
+
plugins.push(new CacheableResponsePlugin({ statuses: [0, 200] }));
|
|
41
|
+
plugins.push(new ExpirationPlugin({
|
|
42
|
+
maxEntries,
|
|
43
|
+
maxAgeSeconds
|
|
44
|
+
}));
|
|
45
|
+
const strategy = new CacheFirst({
|
|
46
|
+
cacheName,
|
|
47
|
+
plugins
|
|
48
|
+
});
|
|
49
|
+
serwist.registerCapture(matchCallback, strategy);
|
|
50
|
+
if (warmCache) warmStrategyCache({
|
|
51
|
+
urls: warmCache,
|
|
52
|
+
strategy
|
|
53
|
+
});
|
|
54
|
+
};
|
|
55
|
+
//#endregion
|
|
56
|
+
//#region src/offlineFallback.ts
|
|
57
|
+
/**
|
|
58
|
+
* An implementation of the [comprehensive fallbacks recipe](https://developers.google.com/web/tools/workbox/guides/advanced-recipes#comprehensive_fallbacks).
|
|
59
|
+
* Be sure to include the fallbacks in your precache injection.
|
|
60
|
+
|
|
61
|
+
* @param options
|
|
62
|
+
*/
|
|
63
|
+
const offlineFallback = ({ serwist, pageFallback = "offline.html", imageFallback, fontFallback }) => {
|
|
64
|
+
self.addEventListener("install", (event) => {
|
|
65
|
+
const files = [pageFallback];
|
|
66
|
+
if (imageFallback) files.push(imageFallback);
|
|
67
|
+
if (fontFallback) files.push(fontFallback);
|
|
68
|
+
event.waitUntil(self.caches.open("serwist-offline-fallbacks").then((cache) => cache.addAll(files)));
|
|
69
|
+
});
|
|
70
|
+
const handler = async (options) => {
|
|
71
|
+
const dest = options.request.destination;
|
|
72
|
+
const cache = await self.caches.open("serwist-offline-fallbacks");
|
|
73
|
+
if (dest === "document") return await serwist.matchPrecache(pageFallback) || await cache.match(pageFallback) || Response.error();
|
|
74
|
+
if (dest === "image" && imageFallback !== void 0) return await serwist.matchPrecache(imageFallback) || await cache.match(imageFallback) || Response.error();
|
|
75
|
+
if (dest === "font" && fontFallback !== void 0) return await serwist.matchPrecache(fontFallback) || await cache.match(fontFallback) || Response.error();
|
|
76
|
+
return Response.error();
|
|
77
|
+
};
|
|
78
|
+
serwist.setCatchHandler(handler);
|
|
79
|
+
};
|
|
80
|
+
//#endregion
|
|
81
|
+
//#region src/pageCache.ts
|
|
82
|
+
/**
|
|
83
|
+
* An implementation of a page caching recipe with a network timeout.
|
|
84
|
+
*
|
|
85
|
+
* @param options
|
|
86
|
+
*/
|
|
87
|
+
const pageCache = ({ serwist, cacheName = "pages", matchCallback = ({ request }) => request.mode === "navigate", networkTimeoutSeconds = 3, plugins = [], warmCache }) => {
|
|
88
|
+
plugins.push(new CacheableResponsePlugin({ statuses: [0, 200] }));
|
|
89
|
+
const strategy = new NetworkFirst({
|
|
90
|
+
networkTimeoutSeconds,
|
|
91
|
+
cacheName,
|
|
92
|
+
plugins
|
|
93
|
+
});
|
|
94
|
+
serwist.registerCapture(matchCallback, strategy);
|
|
95
|
+
if (warmCache) warmStrategyCache({
|
|
96
|
+
urls: warmCache,
|
|
97
|
+
strategy
|
|
98
|
+
});
|
|
99
|
+
};
|
|
100
|
+
//#endregion
|
|
101
|
+
//#region src/staticResourceCache.ts
|
|
102
|
+
/**
|
|
103
|
+
* An implementation of the [CSS and JavaScript files recipe](https://developers.google.com/web/tools/workbox/guides/common-recipes#cache_css_and_javascript_files).
|
|
104
|
+
*
|
|
105
|
+
* @param options
|
|
106
|
+
*/
|
|
107
|
+
const staticResourceCache = ({ serwist, cacheName = "static-resources", matchCallback = ({ request }) => request.destination === "style" || request.destination === "script" || request.destination === "worker", plugins = [], warmCache }) => {
|
|
108
|
+
plugins.push(new CacheableResponsePlugin({ statuses: [0, 200] }));
|
|
109
|
+
const strategy = new StaleWhileRevalidate({
|
|
110
|
+
cacheName,
|
|
111
|
+
plugins
|
|
112
|
+
});
|
|
113
|
+
serwist.registerCapture(matchCallback, strategy);
|
|
114
|
+
if (warmCache) warmStrategyCache({
|
|
115
|
+
urls: warmCache,
|
|
116
|
+
strategy
|
|
117
|
+
});
|
|
118
|
+
};
|
|
119
|
+
//#endregion
|
|
120
|
+
export { googleFontsCache, imageCache, offlineFallback, pageCache, staticResourceCache, warmStrategyCache };
|
|
121
|
+
|
|
122
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","names":[],"sources":["../src/googleFontsCache.ts","../src/warmStrategyCache.ts","../src/imageCache.ts","../src/offlineFallback.ts","../src/pageCache.ts","../src/staticResourceCache.ts"],"sourcesContent":["/*\n Copyright 2020 Google LLC\n\n Use of this source code is governed by an MIT-style\n license that can be found in the LICENSE file or at\n https://opensource.org/licenses/MIT.\n*/\nimport { CacheableResponsePlugin, CacheFirst, ExpirationPlugin, type Serwist, StaleWhileRevalidate } from \"serwist\";\n\nexport interface GoogleFontCacheOptions {\n /**\n * A {@linkcode Serwist} instance.\n */\n serwist: Serwist;\n /**\n * Cache prefix for caching stylesheets and webfonts. Defaults to google-fonts.\n */\n cachePrefix?: string;\n /**\n * Maximum age, in seconds, that font entries will be cached for. Defaults to 1 year.\n */\n maxAgeSeconds?: number;\n /**\n * Maximum number of fonts that will be cached. Defaults to 30.\n */\n maxEntries?: number;\n}\n\n/**\n * An implementation of the [Google fonts](https://developers.google.com/web/tools/workbox/guides/common-recipes#google_fonts) caching recipe.\n *\n * @param options\n */\nexport const googleFontsCache = ({\n serwist,\n cachePrefix = \"google-fonts\",\n maxAgeSeconds = 60 * 60 * 24 * 365,\n maxEntries = 30,\n}: GoogleFontCacheOptions): void => {\n // Cache the Google Fonts stylesheets with a stale-while-revalidate strategy.\n serwist.registerCapture(\n ({ url }) => url.origin === \"https://fonts.googleapis.com\",\n new StaleWhileRevalidate({\n cacheName: `${cachePrefix}-stylesheets`,\n }),\n );\n\n // Cache the underlying font files with a cache-first strategy for 1 year.\n serwist.registerCapture(\n ({ url }) => url.origin === \"https://fonts.gstatic.com\",\n new CacheFirst({\n cacheName: `${cachePrefix}-webfonts`,\n plugins: [\n new CacheableResponsePlugin({\n statuses: [0, 200],\n }),\n new ExpirationPlugin({\n maxAgeSeconds,\n maxEntries,\n }),\n ],\n }),\n );\n};\n","import type { Strategy } from \"serwist\";\n\nexport interface WarmStrategyCacheOptions {\n /**\n * Paths to warm the strategy's cache with.\n */\n urls: string[];\n /**\n * Strategy to use.\n */\n strategy: Strategy;\n}\n\n// Give TypeScript the correct global.\ndeclare let self: ServiceWorkerGlobalScope;\n\n/**\n * @param options\n */\nexport const warmStrategyCache = (options: WarmStrategyCacheOptions): void => {\n self.addEventListener(\"install\", (event) => {\n const done = options.urls.map(\n (path) =>\n options.strategy.handleAll({\n event,\n request: new Request(path),\n })[1],\n );\n\n event.waitUntil(Promise.all(done));\n });\n};\n","/*\n Copyright 2020 Google LLC\n\n Use of this source code is governed by an MIT-style\n license that can be found in the LICENSE file or at\n https://opensource.org/licenses/MIT.\n*/\n\nimport type { RouteMatchCallback, RouteMatchCallbackOptions, SerwistPlugin } from \"serwist\";\nimport { CacheableResponsePlugin, CacheFirst, ExpirationPlugin, type Serwist } from \"serwist\";\nimport { warmStrategyCache } from \"./warmStrategyCache.js\";\n\nexport interface ImageCacheOptions {\n /**\n * A {@linkcode Serwist} instance.\n */\n serwist: Serwist;\n /**\n * Name for cache. Defaults to images.\n */\n cacheName?: string;\n /**\n * Serwist callback function to call to match to. Defaults to request.destination === 'image'.\n */\n matchCallback?: RouteMatchCallback;\n /**\n * Maximum age, in seconds, that image entries will be cached for. Defaults to 30 days.\n */\n maxAgeSeconds?: number;\n /**\n * Maximum number of images that will be cached. Defaults to 60.\n */\n maxEntries?: number;\n /**\n * Additional plugins to use for this recipe.\n */\n plugins?: SerwistPlugin[];\n /**\n * Paths to call to use to warm this cache.\n */\n warmCache?: string[];\n}\n\n/**\n * An implementation of the [image caching recipe](https://developers.google.com/web/tools/workbox/guides/common-recipes#caching_images).\n *\n * @param options\n */\nexport const imageCache = ({\n serwist,\n cacheName = \"images\",\n matchCallback = ({ request }: RouteMatchCallbackOptions) => request.destination === \"image\",\n maxAgeSeconds = 30 * 24 * 60 * 60,\n maxEntries = 60,\n plugins = [],\n warmCache,\n}: ImageCacheOptions): void => {\n plugins.push(\n new CacheableResponsePlugin({\n statuses: [0, 200],\n }),\n );\n plugins.push(\n new ExpirationPlugin({\n maxEntries,\n maxAgeSeconds,\n }),\n );\n\n const strategy = new CacheFirst({\n cacheName,\n plugins,\n });\n\n serwist.registerCapture(matchCallback, strategy);\n\n // Warms the cache\n if (warmCache) {\n warmStrategyCache({ urls: warmCache, strategy });\n }\n};\n","/*\n Copyright 2020 Google LLC\n\n Use of this source code is governed by an MIT-style\n license that can be found in the LICENSE file or at\n https://opensource.org/licenses/MIT.\n*/\n\nimport type { RouteHandler, RouteHandlerCallbackOptions, Serwist } from \"serwist\";\n\nexport interface OfflineFallbackOptions {\n /**\n * A {@linkcode Serwist} instance.\n */\n serwist: Serwist;\n /**\n * Precache name to match for page fallbacks. Defaults to offline.html.\n */\n pageFallback?: string;\n /**\n * Precache name to match for image fallbacks.\n */\n imageFallback?: string;\n /**\n * Precache name to match for font fallbacks.\n */\n fontFallback?: string;\n}\n\n// Give TypeScript the correct global.\ndeclare let self: ServiceWorkerGlobalScope;\n\n/**\n * An implementation of the [comprehensive fallbacks recipe](https://developers.google.com/web/tools/workbox/guides/advanced-recipes#comprehensive_fallbacks). \n * Be sure to include the fallbacks in your precache injection.\n\n * @param options\n */\nexport const offlineFallback = ({ serwist, pageFallback = \"offline.html\", imageFallback, fontFallback }: OfflineFallbackOptions): void => {\n self.addEventListener(\"install\", (event) => {\n const files = [pageFallback];\n if (imageFallback) {\n files.push(imageFallback);\n }\n if (fontFallback) {\n files.push(fontFallback);\n }\n\n event.waitUntil(self.caches.open(\"serwist-offline-fallbacks\").then((cache) => cache.addAll(files)));\n });\n\n const handler: RouteHandler = async (options: RouteHandlerCallbackOptions) => {\n const dest = options.request.destination;\n const cache = await self.caches.open(\"serwist-offline-fallbacks\");\n\n if (dest === \"document\") {\n const match = (await serwist.matchPrecache(pageFallback)) || (await cache.match(pageFallback));\n return match || Response.error();\n }\n\n if (dest === \"image\" && imageFallback !== undefined) {\n const match = (await serwist.matchPrecache(imageFallback)) || (await cache.match(imageFallback));\n return match || Response.error();\n }\n\n if (dest === \"font\" && fontFallback !== undefined) {\n const match = (await serwist.matchPrecache(fontFallback)) || (await cache.match(fontFallback));\n return match || Response.error();\n }\n\n return Response.error();\n };\n\n serwist.setCatchHandler(handler);\n};\n","/*\n Copyright 2020 Google LLC\n\n Use of this source code is governed by an MIT-style\n license that can be found in the LICENSE file or at\n https://opensource.org/licenses/MIT.\n*/\n\nimport type { RouteMatchCallback, RouteMatchCallbackOptions, SerwistPlugin } from \"serwist\";\nimport { CacheableResponsePlugin, NetworkFirst, type Serwist } from \"serwist\";\nimport { warmStrategyCache } from \"./warmStrategyCache.js\";\n\nexport interface PageCacheOptions {\n /**\n * A {@linkcode Serwist} instance.\n */\n serwist: Serwist;\n /**\n * Name for cache. Defaults to pages.\n */\n cacheName?: string;\n /**\n * Serwist callback function to call to match to. Defaults to request.mode === 'navigate'.\n */\n matchCallback?: RouteMatchCallback;\n /**\n * Maximum amount of time, in seconds, to wait on the network before falling back to cache.\n *\n * @default 3\n */\n networkTimeoutSeconds?: number;\n /**\n * Additional plugins to use for this recipe.\n */\n plugins?: SerwistPlugin[];\n /**\n * Paths to call to use to warm this cache\n */\n warmCache?: string[];\n}\n\n/**\n * An implementation of a page caching recipe with a network timeout.\n *\n * @param options\n */\nexport const pageCache = ({\n serwist,\n cacheName = \"pages\",\n matchCallback = ({ request }: RouteMatchCallbackOptions) => request.mode === \"navigate\",\n networkTimeoutSeconds = 3,\n plugins = [],\n warmCache,\n}: PageCacheOptions): void => {\n plugins.push(\n new CacheableResponsePlugin({\n statuses: [0, 200],\n }),\n );\n\n const strategy = new NetworkFirst({\n networkTimeoutSeconds,\n cacheName,\n plugins,\n });\n\n // Registers the route\n serwist.registerCapture(matchCallback, strategy);\n\n // Warms the cache\n if (warmCache) {\n warmStrategyCache({ urls: warmCache, strategy });\n }\n};\n","/*\n Copyright 2020 Google LLC\n\n Use of this source code is governed by an MIT-style\n license that can be found in the LICENSE file or at\n https://opensource.org/licenses/MIT.\n*/\n\nimport type { RouteMatchCallback, RouteMatchCallbackOptions, SerwistPlugin } from \"serwist\";\nimport { CacheableResponsePlugin, type Serwist, StaleWhileRevalidate } from \"serwist\";\nimport { warmStrategyCache } from \"./warmStrategyCache.js\";\n\nexport interface StaticResourceOptions {\n /**\n * A {@linkcode Serwist} instance.\n */\n serwist: Serwist;\n /**\n * Name for cache.\n *\n * @default \"static-resources\"\n */\n cacheName?: string;\n /**\n * Serwist callback function to call to match to.\n *\n * @default request.destination === 'style' || request.destination === 'script' || request.destination === 'worker'\n */\n matchCallback?: RouteMatchCallback;\n /**\n * Additional plugins to use for this recipe.\n */\n plugins?: SerwistPlugin[];\n /**\n * Paths to call to use to warm this cache.\n */\n warmCache?: string[];\n}\n\n/**\n * An implementation of the [CSS and JavaScript files recipe](https://developers.google.com/web/tools/workbox/guides/common-recipes#cache_css_and_javascript_files).\n *\n * @param options\n */\nexport const staticResourceCache = ({\n serwist,\n cacheName = \"static-resources\",\n matchCallback = ({ request }: RouteMatchCallbackOptions) =>\n request.destination === \"style\" || request.destination === \"script\" || request.destination === \"worker\",\n plugins = [],\n warmCache,\n}: StaticResourceOptions): void => {\n plugins.push(\n new CacheableResponsePlugin({\n statuses: [0, 200],\n }),\n );\n\n const strategy = new StaleWhileRevalidate({\n cacheName,\n plugins,\n });\n\n serwist.registerCapture(matchCallback, strategy);\n\n // Warms the cache\n if (warmCache) {\n warmStrategyCache({ urls: warmCache, strategy });\n }\n};\n"],"mappings":";;;;;;;AAiCA,MAAa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ACjC,SAAQ,KACN,IAAI,wBAAwB,EAC1B,UAAU,CAAC,GAAG,IAAI,EACnB,CAAC,CACH;CAED,MAAM,WAAW,IAAI,qBAAqB;EACxC;EACA;EACD,CAAC;AAEF,SAAQ,gBAAgB,eAAe,SAAS;AAGhD,KAAI,UACF,mBAAkB;EAAE,MAAM;EAAW;EAAU,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@serwist/recipes",
|
|
3
|
-
"version": "9.5.
|
|
3
|
+
"version": "9.5.8",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "A service worker helper library to manage common request and caching patterns",
|
|
6
6
|
"files": [
|
|
@@ -23,22 +23,21 @@
|
|
|
23
23
|
"repository": "https://github.com/serwist/serwist",
|
|
24
24
|
"bugs": "https://github.com/serwist/serwist/issues",
|
|
25
25
|
"homepage": "https://serwist.pages.dev",
|
|
26
|
-
"main": "./dist/index.
|
|
27
|
-
"types": "./dist/index.d.
|
|
26
|
+
"main": "./dist/index.mjs",
|
|
27
|
+
"types": "./dist/index.d.mts",
|
|
28
28
|
"exports": {
|
|
29
29
|
".": {
|
|
30
|
-
"types": "./dist/index.d.
|
|
31
|
-
"default": "./dist/index.
|
|
30
|
+
"types": "./dist/index.d.mts",
|
|
31
|
+
"default": "./dist/index.mjs"
|
|
32
32
|
},
|
|
33
33
|
"./package.json": "./package.json"
|
|
34
34
|
},
|
|
35
35
|
"dependencies": {
|
|
36
|
-
"serwist": "9.5.
|
|
36
|
+
"serwist": "9.5.8"
|
|
37
37
|
},
|
|
38
38
|
"devDependencies": {
|
|
39
|
-
"
|
|
40
|
-
"typescript": "
|
|
41
|
-
"@serwist/configs": "9.5.7"
|
|
39
|
+
"tsdown": "0.21.10",
|
|
40
|
+
"typescript": "6.0.3"
|
|
42
41
|
},
|
|
43
42
|
"peerDependencies": {
|
|
44
43
|
"typescript": ">=5.0.0"
|
|
@@ -49,8 +48,8 @@
|
|
|
49
48
|
}
|
|
50
49
|
},
|
|
51
50
|
"scripts": {
|
|
52
|
-
"build": "rimraf dist && NODE_ENV=production
|
|
53
|
-
"dev": "
|
|
51
|
+
"build": "rimraf dist && NODE_ENV=production tsdown",
|
|
52
|
+
"dev": "tsdown --watch",
|
|
54
53
|
"lint": "biome lint ./src",
|
|
55
54
|
"typecheck": "tsc"
|
|
56
55
|
}
|
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
import { type Serwist } from "serwist";
|
|
2
|
-
export interface GoogleFontCacheOptions {
|
|
3
|
-
/**
|
|
4
|
-
* A {@linkcode Serwist} instance.
|
|
5
|
-
*/
|
|
6
|
-
serwist: Serwist;
|
|
7
|
-
/**
|
|
8
|
-
* Cache prefix for caching stylesheets and webfonts. Defaults to google-fonts.
|
|
9
|
-
*/
|
|
10
|
-
cachePrefix?: string;
|
|
11
|
-
/**
|
|
12
|
-
* Maximum age, in seconds, that font entries will be cached for. Defaults to 1 year.
|
|
13
|
-
*/
|
|
14
|
-
maxAgeSeconds?: number;
|
|
15
|
-
/**
|
|
16
|
-
* Maximum number of fonts that will be cached. Defaults to 30.
|
|
17
|
-
*/
|
|
18
|
-
maxEntries?: number;
|
|
19
|
-
}
|
|
20
|
-
/**
|
|
21
|
-
* An implementation of the [Google fonts](https://developers.google.com/web/tools/workbox/guides/common-recipes#google_fonts) caching recipe.
|
|
22
|
-
*
|
|
23
|
-
* @param options
|
|
24
|
-
*/
|
|
25
|
-
export declare const googleFontsCache: ({ serwist, cachePrefix, maxAgeSeconds, maxEntries, }: GoogleFontCacheOptions) => void;
|
|
26
|
-
//# sourceMappingURL=googleFontsCache.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"googleFontsCache.d.ts","sourceRoot":"","sources":["../src/googleFontsCache.ts"],"names":[],"mappings":"AAOA,OAAO,EAAyD,KAAK,OAAO,EAAwB,MAAM,SAAS,CAAC;AAEpH,MAAM,WAAW,sBAAsB;IACrC;;OAEG;IACH,OAAO,EAAE,OAAO,CAAC;IACjB;;OAEG;IACH,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB;;OAEG;IACH,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB;;OAEG;IACH,UAAU,CAAC,EAAE,MAAM,CAAC;CACrB;AAED;;;;GAIG;AACH,eAAO,MAAM,gBAAgB,GAAI,sDAK9B,sBAAsB,KAAG,IAyB3B,CAAC"}
|
package/dist/imageCache.d.ts
DELETED
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
import type { RouteMatchCallback, SerwistPlugin } from "serwist";
|
|
2
|
-
import { type Serwist } from "serwist";
|
|
3
|
-
export interface ImageCacheOptions {
|
|
4
|
-
/**
|
|
5
|
-
* A {@linkcode Serwist} instance.
|
|
6
|
-
*/
|
|
7
|
-
serwist: Serwist;
|
|
8
|
-
/**
|
|
9
|
-
* Name for cache. Defaults to images.
|
|
10
|
-
*/
|
|
11
|
-
cacheName?: string;
|
|
12
|
-
/**
|
|
13
|
-
* Serwist callback function to call to match to. Defaults to request.destination === 'image'.
|
|
14
|
-
*/
|
|
15
|
-
matchCallback?: RouteMatchCallback;
|
|
16
|
-
/**
|
|
17
|
-
* Maximum age, in seconds, that image entries will be cached for. Defaults to 30 days.
|
|
18
|
-
*/
|
|
19
|
-
maxAgeSeconds?: number;
|
|
20
|
-
/**
|
|
21
|
-
* Maximum number of images that will be cached. Defaults to 60.
|
|
22
|
-
*/
|
|
23
|
-
maxEntries?: number;
|
|
24
|
-
/**
|
|
25
|
-
* Additional plugins to use for this recipe.
|
|
26
|
-
*/
|
|
27
|
-
plugins?: SerwistPlugin[];
|
|
28
|
-
/**
|
|
29
|
-
* Paths to call to use to warm this cache.
|
|
30
|
-
*/
|
|
31
|
-
warmCache?: string[];
|
|
32
|
-
}
|
|
33
|
-
/**
|
|
34
|
-
* An implementation of the [image caching recipe](https://developers.google.com/web/tools/workbox/guides/common-recipes#caching_images).
|
|
35
|
-
*
|
|
36
|
-
* @param options
|
|
37
|
-
*/
|
|
38
|
-
export declare const imageCache: ({ serwist, cacheName, matchCallback, maxAgeSeconds, maxEntries, plugins, warmCache, }: ImageCacheOptions) => void;
|
|
39
|
-
//# sourceMappingURL=imageCache.d.ts.map
|
package/dist/imageCache.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"imageCache.d.ts","sourceRoot":"","sources":["../src/imageCache.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAAE,kBAAkB,EAA6B,aAAa,EAAE,MAAM,SAAS,CAAC;AAC5F,OAAO,EAAyD,KAAK,OAAO,EAAE,MAAM,SAAS,CAAC;AAG9F,MAAM,WAAW,iBAAiB;IAChC;;OAEG;IACH,OAAO,EAAE,OAAO,CAAC;IACjB;;OAEG;IACH,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB;;OAEG;IACH,aAAa,CAAC,EAAE,kBAAkB,CAAC;IACnC;;OAEG;IACH,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB;;OAEG;IACH,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB;;OAEG;IACH,OAAO,CAAC,EAAE,aAAa,EAAE,CAAC;IAC1B;;OAEG;IACH,SAAS,CAAC,EAAE,MAAM,EAAE,CAAC;CACtB;AAED;;;;GAIG;AACH,eAAO,MAAM,UAAU,GAAI,uFAQxB,iBAAiB,KAAG,IAwBtB,CAAC"}
|
package/dist/index.d.ts
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
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
|
package/dist/index.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
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
DELETED
|
@@ -1,132 +0,0 @@
|
|
|
1
|
-
import { StaleWhileRevalidate, CacheFirst, CacheableResponsePlugin, ExpirationPlugin, NetworkFirst } from 'serwist';
|
|
2
|
-
|
|
3
|
-
const googleFontsCache = ({ serwist, cachePrefix = "google-fonts", maxAgeSeconds = 60 * 60 * 24 * 365, maxEntries = 30 })=>{
|
|
4
|
-
serwist.registerCapture(({ url })=>url.origin === "https://fonts.googleapis.com", new StaleWhileRevalidate({
|
|
5
|
-
cacheName: `${cachePrefix}-stylesheets`
|
|
6
|
-
}));
|
|
7
|
-
serwist.registerCapture(({ url })=>url.origin === "https://fonts.gstatic.com", new CacheFirst({
|
|
8
|
-
cacheName: `${cachePrefix}-webfonts`,
|
|
9
|
-
plugins: [
|
|
10
|
-
new CacheableResponsePlugin({
|
|
11
|
-
statuses: [
|
|
12
|
-
0,
|
|
13
|
-
200
|
|
14
|
-
]
|
|
15
|
-
}),
|
|
16
|
-
new ExpirationPlugin({
|
|
17
|
-
maxAgeSeconds,
|
|
18
|
-
maxEntries
|
|
19
|
-
})
|
|
20
|
-
]
|
|
21
|
-
}));
|
|
22
|
-
};
|
|
23
|
-
|
|
24
|
-
const warmStrategyCache = (options)=>{
|
|
25
|
-
self.addEventListener("install", (event)=>{
|
|
26
|
-
const done = options.urls.map((path)=>options.strategy.handleAll({
|
|
27
|
-
event,
|
|
28
|
-
request: new Request(path)
|
|
29
|
-
})[1]);
|
|
30
|
-
event.waitUntil(Promise.all(done));
|
|
31
|
-
});
|
|
32
|
-
};
|
|
33
|
-
|
|
34
|
-
const imageCache = ({ serwist, cacheName = "images", matchCallback = ({ request })=>request.destination === "image", maxAgeSeconds = 30 * 24 * 60 * 60, maxEntries = 60, plugins = [], warmCache })=>{
|
|
35
|
-
plugins.push(new CacheableResponsePlugin({
|
|
36
|
-
statuses: [
|
|
37
|
-
0,
|
|
38
|
-
200
|
|
39
|
-
]
|
|
40
|
-
}));
|
|
41
|
-
plugins.push(new ExpirationPlugin({
|
|
42
|
-
maxEntries,
|
|
43
|
-
maxAgeSeconds
|
|
44
|
-
}));
|
|
45
|
-
const strategy = new CacheFirst({
|
|
46
|
-
cacheName,
|
|
47
|
-
plugins
|
|
48
|
-
});
|
|
49
|
-
serwist.registerCapture(matchCallback, strategy);
|
|
50
|
-
if (warmCache) {
|
|
51
|
-
warmStrategyCache({
|
|
52
|
-
urls: warmCache,
|
|
53
|
-
strategy
|
|
54
|
-
});
|
|
55
|
-
}
|
|
56
|
-
};
|
|
57
|
-
|
|
58
|
-
const offlineFallback = ({ serwist, pageFallback = "offline.html", imageFallback, fontFallback })=>{
|
|
59
|
-
self.addEventListener("install", (event)=>{
|
|
60
|
-
const files = [
|
|
61
|
-
pageFallback
|
|
62
|
-
];
|
|
63
|
-
if (imageFallback) {
|
|
64
|
-
files.push(imageFallback);
|
|
65
|
-
}
|
|
66
|
-
if (fontFallback) {
|
|
67
|
-
files.push(fontFallback);
|
|
68
|
-
}
|
|
69
|
-
event.waitUntil(self.caches.open("serwist-offline-fallbacks").then((cache)=>cache.addAll(files)));
|
|
70
|
-
});
|
|
71
|
-
const handler = async (options)=>{
|
|
72
|
-
const dest = options.request.destination;
|
|
73
|
-
const cache = await self.caches.open("serwist-offline-fallbacks");
|
|
74
|
-
if (dest === "document") {
|
|
75
|
-
const match = await serwist.matchPrecache(pageFallback) || await cache.match(pageFallback);
|
|
76
|
-
return match || Response.error();
|
|
77
|
-
}
|
|
78
|
-
if (dest === "image" && imageFallback !== undefined) {
|
|
79
|
-
const match = await serwist.matchPrecache(imageFallback) || await cache.match(imageFallback);
|
|
80
|
-
return match || Response.error();
|
|
81
|
-
}
|
|
82
|
-
if (dest === "font" && fontFallback !== undefined) {
|
|
83
|
-
const match = await serwist.matchPrecache(fontFallback) || await cache.match(fontFallback);
|
|
84
|
-
return match || Response.error();
|
|
85
|
-
}
|
|
86
|
-
return Response.error();
|
|
87
|
-
};
|
|
88
|
-
serwist.setCatchHandler(handler);
|
|
89
|
-
};
|
|
90
|
-
|
|
91
|
-
const pageCache = ({ serwist, cacheName = "pages", matchCallback = ({ request })=>request.mode === "navigate", networkTimeoutSeconds = 3, plugins = [], warmCache })=>{
|
|
92
|
-
plugins.push(new CacheableResponsePlugin({
|
|
93
|
-
statuses: [
|
|
94
|
-
0,
|
|
95
|
-
200
|
|
96
|
-
]
|
|
97
|
-
}));
|
|
98
|
-
const strategy = new NetworkFirst({
|
|
99
|
-
networkTimeoutSeconds,
|
|
100
|
-
cacheName,
|
|
101
|
-
plugins
|
|
102
|
-
});
|
|
103
|
-
serwist.registerCapture(matchCallback, strategy);
|
|
104
|
-
if (warmCache) {
|
|
105
|
-
warmStrategyCache({
|
|
106
|
-
urls: warmCache,
|
|
107
|
-
strategy
|
|
108
|
-
});
|
|
109
|
-
}
|
|
110
|
-
};
|
|
111
|
-
|
|
112
|
-
const staticResourceCache = ({ serwist, cacheName = "static-resources", matchCallback = ({ request })=>request.destination === "style" || request.destination === "script" || request.destination === "worker", plugins = [], warmCache })=>{
|
|
113
|
-
plugins.push(new CacheableResponsePlugin({
|
|
114
|
-
statuses: [
|
|
115
|
-
0,
|
|
116
|
-
200
|
|
117
|
-
]
|
|
118
|
-
}));
|
|
119
|
-
const strategy = new StaleWhileRevalidate({
|
|
120
|
-
cacheName,
|
|
121
|
-
plugins
|
|
122
|
-
});
|
|
123
|
-
serwist.registerCapture(matchCallback, strategy);
|
|
124
|
-
if (warmCache) {
|
|
125
|
-
warmStrategyCache({
|
|
126
|
-
urls: warmCache,
|
|
127
|
-
strategy
|
|
128
|
-
});
|
|
129
|
-
}
|
|
130
|
-
};
|
|
131
|
-
|
|
132
|
-
export { googleFontsCache, imageCache, offlineFallback, pageCache, staticResourceCache, warmStrategyCache };
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import type { Serwist } from "serwist";
|
|
2
|
-
export interface OfflineFallbackOptions {
|
|
3
|
-
/**
|
|
4
|
-
* A {@linkcode Serwist} instance.
|
|
5
|
-
*/
|
|
6
|
-
serwist: Serwist;
|
|
7
|
-
/**
|
|
8
|
-
* Precache name to match for page fallbacks. Defaults to offline.html.
|
|
9
|
-
*/
|
|
10
|
-
pageFallback?: string;
|
|
11
|
-
/**
|
|
12
|
-
* Precache name to match for image fallbacks.
|
|
13
|
-
*/
|
|
14
|
-
imageFallback?: string;
|
|
15
|
-
/**
|
|
16
|
-
* Precache name to match for font fallbacks.
|
|
17
|
-
*/
|
|
18
|
-
fontFallback?: string;
|
|
19
|
-
}
|
|
20
|
-
/**
|
|
21
|
-
* An implementation of the [comprehensive fallbacks recipe](https://developers.google.com/web/tools/workbox/guides/advanced-recipes#comprehensive_fallbacks).
|
|
22
|
-
* Be sure to include the fallbacks in your precache injection.
|
|
23
|
-
|
|
24
|
-
* @param options
|
|
25
|
-
*/
|
|
26
|
-
export declare const offlineFallback: ({ serwist, pageFallback, imageFallback, fontFallback }: OfflineFallbackOptions) => void;
|
|
27
|
-
//# sourceMappingURL=offlineFallback.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"offlineFallback.d.ts","sourceRoot":"","sources":["../src/offlineFallback.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAA6C,OAAO,EAAE,MAAM,SAAS,CAAC;AAElF,MAAM,WAAW,sBAAsB;IACrC;;OAEG;IACH,OAAO,EAAE,OAAO,CAAC;IACjB;;OAEG;IACH,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB;;OAEG;IACH,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB;;OAEG;IACH,YAAY,CAAC,EAAE,MAAM,CAAC;CACvB;AAKD;;;;;GAKG;AACH,eAAO,MAAM,eAAe,GAAI,wDAAyE,sBAAsB,KAAG,IAoCjI,CAAC"}
|
package/dist/pageCache.d.ts
DELETED
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
import type { RouteMatchCallback, SerwistPlugin } from "serwist";
|
|
2
|
-
import { type Serwist } from "serwist";
|
|
3
|
-
export interface PageCacheOptions {
|
|
4
|
-
/**
|
|
5
|
-
* A {@linkcode Serwist} instance.
|
|
6
|
-
*/
|
|
7
|
-
serwist: Serwist;
|
|
8
|
-
/**
|
|
9
|
-
* Name for cache. Defaults to pages.
|
|
10
|
-
*/
|
|
11
|
-
cacheName?: string;
|
|
12
|
-
/**
|
|
13
|
-
* Serwist callback function to call to match to. Defaults to request.mode === 'navigate'.
|
|
14
|
-
*/
|
|
15
|
-
matchCallback?: RouteMatchCallback;
|
|
16
|
-
/**
|
|
17
|
-
* Maximum amount of time, in seconds, to wait on the network before falling back to cache.
|
|
18
|
-
*
|
|
19
|
-
* @default 3
|
|
20
|
-
*/
|
|
21
|
-
networkTimeoutSeconds?: number;
|
|
22
|
-
/**
|
|
23
|
-
* Additional plugins to use for this recipe.
|
|
24
|
-
*/
|
|
25
|
-
plugins?: SerwistPlugin[];
|
|
26
|
-
/**
|
|
27
|
-
* Paths to call to use to warm this cache
|
|
28
|
-
*/
|
|
29
|
-
warmCache?: string[];
|
|
30
|
-
}
|
|
31
|
-
/**
|
|
32
|
-
* An implementation of a page caching recipe with a network timeout.
|
|
33
|
-
*
|
|
34
|
-
* @param options
|
|
35
|
-
*/
|
|
36
|
-
export declare const pageCache: ({ serwist, cacheName, matchCallback, networkTimeoutSeconds, plugins, warmCache, }: PageCacheOptions) => void;
|
|
37
|
-
//# sourceMappingURL=pageCache.d.ts.map
|
package/dist/pageCache.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"pageCache.d.ts","sourceRoot":"","sources":["../src/pageCache.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAAE,kBAAkB,EAA6B,aAAa,EAAE,MAAM,SAAS,CAAC;AAC5F,OAAO,EAAyC,KAAK,OAAO,EAAE,MAAM,SAAS,CAAC;AAG9E,MAAM,WAAW,gBAAgB;IAC/B;;OAEG;IACH,OAAO,EAAE,OAAO,CAAC;IACjB;;OAEG;IACH,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB;;OAEG;IACH,aAAa,CAAC,EAAE,kBAAkB,CAAC;IACnC;;;;OAIG;IACH,qBAAqB,CAAC,EAAE,MAAM,CAAC;IAC/B;;OAEG;IACH,OAAO,CAAC,EAAE,aAAa,EAAE,CAAC;IAC1B;;OAEG;IACH,SAAS,CAAC,EAAE,MAAM,EAAE,CAAC;CACtB;AAED;;;;GAIG;AACH,eAAO,MAAM,SAAS,GAAI,mFAOvB,gBAAgB,KAAG,IAoBrB,CAAC"}
|
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
import type { RouteMatchCallback, SerwistPlugin } from "serwist";
|
|
2
|
-
import { type Serwist } from "serwist";
|
|
3
|
-
export interface StaticResourceOptions {
|
|
4
|
-
/**
|
|
5
|
-
* A {@linkcode Serwist} instance.
|
|
6
|
-
*/
|
|
7
|
-
serwist: Serwist;
|
|
8
|
-
/**
|
|
9
|
-
* Name for cache.
|
|
10
|
-
*
|
|
11
|
-
* @default "static-resources"
|
|
12
|
-
*/
|
|
13
|
-
cacheName?: string;
|
|
14
|
-
/**
|
|
15
|
-
* Serwist callback function to call to match to.
|
|
16
|
-
*
|
|
17
|
-
* @default request.destination === 'style' || request.destination === 'script' || request.destination === 'worker'
|
|
18
|
-
*/
|
|
19
|
-
matchCallback?: RouteMatchCallback;
|
|
20
|
-
/**
|
|
21
|
-
* Additional plugins to use for this recipe.
|
|
22
|
-
*/
|
|
23
|
-
plugins?: SerwistPlugin[];
|
|
24
|
-
/**
|
|
25
|
-
* Paths to call to use to warm this cache.
|
|
26
|
-
*/
|
|
27
|
-
warmCache?: string[];
|
|
28
|
-
}
|
|
29
|
-
/**
|
|
30
|
-
* An implementation of the [CSS and JavaScript files recipe](https://developers.google.com/web/tools/workbox/guides/common-recipes#cache_css_and_javascript_files).
|
|
31
|
-
*
|
|
32
|
-
* @param options
|
|
33
|
-
*/
|
|
34
|
-
export declare const staticResourceCache: ({ serwist, cacheName, matchCallback, plugins, warmCache, }: StaticResourceOptions) => void;
|
|
35
|
-
//# sourceMappingURL=staticResourceCache.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"staticResourceCache.d.ts","sourceRoot":"","sources":["../src/staticResourceCache.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAAE,kBAAkB,EAA6B,aAAa,EAAE,MAAM,SAAS,CAAC;AAC5F,OAAO,EAA2B,KAAK,OAAO,EAAwB,MAAM,SAAS,CAAC;AAGtF,MAAM,WAAW,qBAAqB;IACpC;;OAEG;IACH,OAAO,EAAE,OAAO,CAAC;IACjB;;;;OAIG;IACH,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB;;;;OAIG;IACH,aAAa,CAAC,EAAE,kBAAkB,CAAC;IACnC;;OAEG;IACH,OAAO,CAAC,EAAE,aAAa,EAAE,CAAC;IAC1B;;OAEG;IACH,SAAS,CAAC,EAAE,MAAM,EAAE,CAAC;CACtB;AAED;;;;GAIG;AACH,eAAO,MAAM,mBAAmB,GAAI,4DAOjC,qBAAqB,KAAG,IAkB1B,CAAC"}
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import type { Strategy } from "serwist";
|
|
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
|
-
export declare const warmStrategyCache: (options: WarmStrategyCacheOptions) => void;
|
|
16
|
-
//# sourceMappingURL=warmStrategyCache.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"warmStrategyCache.d.ts","sourceRoot":"","sources":["../src/warmStrategyCache.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AAExC,MAAM,WAAW,wBAAwB;IACvC;;OAEG;IACH,IAAI,EAAE,MAAM,EAAE,CAAC;IACf;;OAEG;IACH,QAAQ,EAAE,QAAQ,CAAC;CACpB;AAKD;;GAEG;AACH,eAAO,MAAM,iBAAiB,GAAI,SAAS,wBAAwB,KAAG,IAYrE,CAAC"}
|