@serwist/recipes 9.5.10 → 9.5.12
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 +5 -38
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs.map +1 -1
- package/package.json +8 -5
package/dist/index.d.mts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { RouteMatchCallback, Serwist, SerwistPlugin, Strategy } from "serwist";
|
|
2
|
-
|
|
3
2
|
//#region src/googleFontsCache.d.ts
|
|
4
3
|
interface GoogleFontCacheOptions {
|
|
5
4
|
/**
|
|
@@ -24,12 +23,7 @@ interface GoogleFontCacheOptions {
|
|
|
24
23
|
*
|
|
25
24
|
* @param options
|
|
26
25
|
*/
|
|
27
|
-
declare const googleFontsCache: ({
|
|
28
|
-
serwist,
|
|
29
|
-
cachePrefix,
|
|
30
|
-
maxAgeSeconds,
|
|
31
|
-
maxEntries
|
|
32
|
-
}: GoogleFontCacheOptions) => void;
|
|
26
|
+
declare const googleFontsCache: ({ serwist, cachePrefix, maxAgeSeconds, maxEntries }: GoogleFontCacheOptions) => void;
|
|
33
27
|
//#endregion
|
|
34
28
|
//#region src/imageCache.d.ts
|
|
35
29
|
interface ImageCacheOptions {
|
|
@@ -67,15 +61,7 @@ interface ImageCacheOptions {
|
|
|
67
61
|
*
|
|
68
62
|
* @param options
|
|
69
63
|
*/
|
|
70
|
-
declare const imageCache: ({
|
|
71
|
-
serwist,
|
|
72
|
-
cacheName,
|
|
73
|
-
matchCallback,
|
|
74
|
-
maxAgeSeconds,
|
|
75
|
-
maxEntries,
|
|
76
|
-
plugins,
|
|
77
|
-
warmCache
|
|
78
|
-
}: ImageCacheOptions) => void;
|
|
64
|
+
declare const imageCache: ({ serwist, cacheName, matchCallback, maxAgeSeconds, maxEntries, plugins, warmCache }: ImageCacheOptions) => void;
|
|
79
65
|
//#endregion
|
|
80
66
|
//#region src/offlineFallback.d.ts
|
|
81
67
|
interface OfflineFallbackOptions {
|
|
@@ -99,15 +85,9 @@ interface OfflineFallbackOptions {
|
|
|
99
85
|
/**
|
|
100
86
|
* An implementation of the [comprehensive fallbacks recipe](https://developers.google.com/web/tools/workbox/guides/advanced-recipes#comprehensive_fallbacks).
|
|
101
87
|
* Be sure to include the fallbacks in your precache injection.
|
|
102
|
-
|
|
103
88
|
* @param options
|
|
104
89
|
*/
|
|
105
|
-
declare const offlineFallback: ({
|
|
106
|
-
serwist,
|
|
107
|
-
pageFallback,
|
|
108
|
-
imageFallback,
|
|
109
|
-
fontFallback
|
|
110
|
-
}: OfflineFallbackOptions) => void;
|
|
90
|
+
declare const offlineFallback: ({ serwist, pageFallback, imageFallback, fontFallback }: OfflineFallbackOptions) => void;
|
|
111
91
|
//#endregion
|
|
112
92
|
//#region src/pageCache.d.ts
|
|
113
93
|
interface PageCacheOptions {
|
|
@@ -143,14 +123,7 @@ interface PageCacheOptions {
|
|
|
143
123
|
*
|
|
144
124
|
* @param options
|
|
145
125
|
*/
|
|
146
|
-
declare const pageCache: ({
|
|
147
|
-
serwist,
|
|
148
|
-
cacheName,
|
|
149
|
-
matchCallback,
|
|
150
|
-
networkTimeoutSeconds,
|
|
151
|
-
plugins,
|
|
152
|
-
warmCache
|
|
153
|
-
}: PageCacheOptions) => void;
|
|
126
|
+
declare const pageCache: ({ serwist, cacheName, matchCallback, networkTimeoutSeconds, plugins, warmCache }: PageCacheOptions) => void;
|
|
154
127
|
//#endregion
|
|
155
128
|
//#region src/staticResourceCache.d.ts
|
|
156
129
|
interface StaticResourceOptions {
|
|
@@ -184,13 +157,7 @@ interface StaticResourceOptions {
|
|
|
184
157
|
*
|
|
185
158
|
* @param options
|
|
186
159
|
*/
|
|
187
|
-
declare const staticResourceCache: ({
|
|
188
|
-
serwist,
|
|
189
|
-
cacheName,
|
|
190
|
-
matchCallback,
|
|
191
|
-
plugins,
|
|
192
|
-
warmCache
|
|
193
|
-
}: StaticResourceOptions) => void;
|
|
160
|
+
declare const staticResourceCache: ({ serwist, cacheName, matchCallback, plugins, warmCache }: StaticResourceOptions) => void;
|
|
194
161
|
//#endregion
|
|
195
162
|
//#region src/warmStrategyCache.d.ts
|
|
196
163
|
interface WarmStrategyCacheOptions {
|
package/dist/index.d.mts.map
CHANGED
|
@@ -1 +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":"
|
|
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;;;;EAIf,SAAS;;;;EAIT;;;;EAIA;;;;EAIA;;;;;;;cAQW,qBAAgB,SAAA,aAAA,eAAA,cAK1B;;;UC1Bc;;;;EAIf,SAAS;;;;EAIT;;;;EAIA,gBAAgB;;;;EAIhB;;;;EAIA;;;;EAIA,UAAU;;;;EAIV;;;;;;;cAQW,eAAU,SAAA,WAAA,eAAA,eAAA,YAAA,SAAA,aAQpB;;;UC9Cc;;;;EAIf,SAAS;;;;EAIT;;;;EAIA;;;;EAIA;;;;;;;cAYW,oBAAe,SAAA,cAAA,eAAA,gBAA6E;;;UC1BxF;;;;EAIf,SAAS;;;;EAIT;;;;EAIA,gBAAgB;;;;;;EAMhB;;;;EAIA,UAAU;;;;EAIV;;;;;;;cAQW,cAAS,SAAA,WAAA,eAAA,uBAAA,SAAA,aAOnB;;;UCzCc;;;;EAIf,SAAS;;;;;;EAMT;;;;;;EAMA,gBAAgB;;;;EAIhB,UAAU;;;;EAIV;;;;;;;cAQW,wBAAmB,SAAA,WAAA,eAAA,SAAA,aAO7B;;;UCjDc;;;;EAIf;;;;EAIA,UAAU;;;;;cASC,oBAAiB,SAAa"}
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +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,oBAAoB,EAC/B,SACA,cAAc,gBACd,gBAAgB,OAAU,KAAK,KAC/B,aAAa,SACqB;AAElC,SAAQ,iBACL,EAAE,UAAU,IAAI,WAAW,gCAC5B,IAAI,qBAAqB,EACvB,WAAW,GAAG,YAAY,eAC3B,CAAC,CACH;AAGD,SAAQ,iBACL,EAAE,UAAU,IAAI,WAAW,6BAC5B,IAAI,WAAW;EACb,WAAW,GAAG,YAAY;EAC1B,SAAS,CACP,IAAI,wBAAwB,EAC1B,UAAU,CAAC,GAAG,IAAI,EAC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c,aAAa,IAAM,MAAM,MAAM,MAAM,aAAa,IAC7E,SAAS,OAAO;AAGlC,MAAI,SAAS,WAAW,kBAAkB,KAAA,EAExC,QADe,MAAM,QAAQ,cAAc,cAAc,IAAM,MAAM,MAAM,MAAM,cAAc,IAC/E,SAAS,OAAO;AAGlC,MAAI,SAAS,UAAU,iBAAiB,KAAA,EAEtC,QADe,MAAM,QAAQ,cAAc,aAAa,IAAM,MAAM,MAAM,MAAM,aAAa,IAC7E,SAAS,OAAO;AAGlC,SAAO,SAAS,OAAO;;AAGzB,SAAQ,gBAAgB,QAAQ;;;;;;;;;AC3BlC,MAAa,aAAa,EACxB,SACA,YAAY,SACZ,iBAAiB,EAAE,cAAyC,QAAQ,SAAS,YAC7E,wBAAwB,GACxB,UAAU,EAAE,EACZ,gBAC4B;AAC5B,SAAQ,KACN,IAAI,wBAAwB,EAC1B,UAAU,CAAC,GAAG,IAAI,EACnB,CAAC,CACH;CAED,MAAM,WAAW,IAAI,aAAa;EAChC;EACA;EACA;EACD,CAAC;AAGF,SAAQ,gBAAgB,eAAe,SAAS;AAGhD,KAAI,UACF,mBAAkB;EAAE,MAAM;EAAW;EAAU,CAAC;;;;;;;;;AC3BpD,MAAa,uBAAuB,EAClC,SACA,YAAY,oBACZ,iBAAiB,EAAE,cACjB,QAAQ,gBAAgB,WAAW,QAAQ,gBAAgB,YAAY,QAAQ,gBAAgB,UACjG,UAAU,EAAE,EACZ,gBACiC;AACjC,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"}
|
|
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,oBAAoB,EAC/B,SACA,cAAc,gBACd,gBAAgB,OAAU,KAAK,KAC/B,aAAa,SACqB;CAElC,QAAQ,iBACL,EAAE,UAAU,IAAI,WAAW,gCAC5B,IAAI,qBAAqB,EACvB,WAAW,GAAG,YAAY,cAC5B,CAAC,CACH;CAGA,QAAQ,iBACL,EAAE,UAAU,IAAI,WAAW,6BAC5B,IAAI,WAAW;EACb,WAAW,GAAG,YAAY;EAC1B,SAAS,CACP,IAAI,wBAAwB,EAC1B,UAAU,CAAC,GAAG,GAAG,EACnB,CAAC,GACD,IAAI,iBAAiB;GACnB;GACA;EACF,CAAC,CACH;CACF,CAAC,CACH;AACF;;;;;;AC5CA,MAAa,qBAAqB,YAA4C;CAC5E,KA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AAkB,KAAA,GAExC,OADe,MAAM,QAAQ,cAAc,aAAa,KAAO,MAAM,MAAM,MAAM,aAAa,KAC9E,SAAS,MAAM;EAGjC,IAAI,SAAS,UAAU,iBAAiB,KAAA,GAEtC,OADe,MAAM,QAAQ,cAAc,YAAY,KAAO,MAAM,MAAM,MAAM,YAAY,KAC5E,SAAS,MAAM;EAGjC,OAAO,SAAS,MAAM;CACxB;CAEA,QAAQ,gBAAgB,OAAO;AACjC;;;;;;;;AC5BA,MAAa,aAAa,EACxB,SACA,YAAY,SACZ,iBAAiB,EAAE,cAAyC,QAAQ,SAAS,YAC7E,wBAAwB,GACxB,UAAU,CAAC,GACX,gBAC4B;CAC5B,QAAQ,KACN,IAAI,wBAAwB,EAC1B,UAAU,CAAC,GAAG,GAAG,EACnB,CAAC,CACH;CAEA,MAAM,WAAW,IAAI,aAAa;EAChC;EACA;EACA;CACF,CAAC;CAGD,QAAQ,gBAAgB,eAAe,QAAQ;CAG/C,IAAI,WACF,kBAAkB;EAAE,MAAM;EAAW;CAAS,CAAC;AAEnD;;;;;;;;AC7BA,MAAa,uBAAuB,EAClC,SACA,YAAY,oBACZ,iBAAiB,EAAE,cACjB,QAAQ,gBAAgB,WAAW,QAAQ,gBAAgB,YAAY,QAAQ,gBAAgB,UACjG,UAAU,CAAC,GACX,gBACiC;CACjC,QAAQ,KACN,IAAI,wBAAwB,EAC1B,UAAU,CAAC,GAAG,GAAG,EACnB,CAAC,CACH;CAEA,MAAM,WAAW,IAAI,qBAAqB;EACxC;EACA;CACF,CAAC;CAED,QAAQ,gBAAgB,eAAe,QAAQ;CAG/C,IAAI,WACF,kBAAkB;EAAE,MAAM;EAAW;CAAS,CAAC;AAEnD"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@serwist/recipes",
|
|
3
|
-
"version": "9.5.
|
|
3
|
+
"version": "9.5.12",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "A service worker helper library to manage common request and caching patterns",
|
|
6
6
|
"files": [
|
|
@@ -20,7 +20,10 @@
|
|
|
20
20
|
"Serwist <ducanh2912.rusty@gmail.com> (https://serwist.pages.dev/)"
|
|
21
21
|
],
|
|
22
22
|
"license": "MIT",
|
|
23
|
-
"repository":
|
|
23
|
+
"repository": {
|
|
24
|
+
"type": "git",
|
|
25
|
+
"url": "https://github.com/serwist/serwist"
|
|
26
|
+
},
|
|
24
27
|
"bugs": "https://github.com/serwist/serwist/issues",
|
|
25
28
|
"homepage": "https://serwist.pages.dev",
|
|
26
29
|
"main": "./dist/index.mjs",
|
|
@@ -33,11 +36,11 @@
|
|
|
33
36
|
"./package.json": "./package.json"
|
|
34
37
|
},
|
|
35
38
|
"dependencies": {
|
|
36
|
-
"serwist": "9.5.
|
|
39
|
+
"serwist": "9.5.12"
|
|
37
40
|
},
|
|
38
41
|
"devDependencies": {
|
|
39
|
-
"tsdown": "0.
|
|
40
|
-
"typescript": "
|
|
42
|
+
"tsdown": "0.22.12",
|
|
43
|
+
"typescript": "7.0.2"
|
|
41
44
|
},
|
|
42
45
|
"peerDependencies": {
|
|
43
46
|
"typescript": ">=5.0.0"
|