@serwist/recipes 9.5.12 → 9.5.14

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 CHANGED
@@ -23,7 +23,7 @@ interface GoogleFontCacheOptions {
23
23
  *
24
24
  * @param options
25
25
  */
26
- declare const googleFontsCache: ({ serwist, cachePrefix, maxAgeSeconds, maxEntries }: GoogleFontCacheOptions) => void;
26
+ export declare const googleFontsCache: ({ serwist, cachePrefix, maxAgeSeconds, maxEntries }: GoogleFontCacheOptions) => void;
27
27
  //#endregion
28
28
  //#region src/imageCache.d.ts
29
29
  interface ImageCacheOptions {
@@ -61,7 +61,7 @@ interface ImageCacheOptions {
61
61
  *
62
62
  * @param options
63
63
  */
64
- declare const imageCache: ({ serwist, cacheName, matchCallback, maxAgeSeconds, maxEntries, plugins, warmCache }: ImageCacheOptions) => void;
64
+ export declare const imageCache: ({ serwist, cacheName, matchCallback, maxAgeSeconds, maxEntries, plugins, warmCache }: ImageCacheOptions) => void;
65
65
  //#endregion
66
66
  //#region src/offlineFallback.d.ts
67
67
  interface OfflineFallbackOptions {
@@ -87,7 +87,7 @@ interface OfflineFallbackOptions {
87
87
  * Be sure to include the fallbacks in your precache injection.
88
88
  * @param options
89
89
  */
90
- declare const offlineFallback: ({ serwist, pageFallback, imageFallback, fontFallback }: OfflineFallbackOptions) => void;
90
+ export declare const offlineFallback: ({ serwist, pageFallback, imageFallback, fontFallback }: OfflineFallbackOptions) => void;
91
91
  //#endregion
92
92
  //#region src/pageCache.d.ts
93
93
  interface PageCacheOptions {
@@ -123,7 +123,7 @@ interface PageCacheOptions {
123
123
  *
124
124
  * @param options
125
125
  */
126
- declare const pageCache: ({ serwist, cacheName, matchCallback, networkTimeoutSeconds, plugins, warmCache }: PageCacheOptions) => void;
126
+ export declare const pageCache: ({ serwist, cacheName, matchCallback, networkTimeoutSeconds, plugins, warmCache }: PageCacheOptions) => void;
127
127
  //#endregion
128
128
  //#region src/staticResourceCache.d.ts
129
129
  interface StaticResourceOptions {
@@ -157,7 +157,7 @@ interface StaticResourceOptions {
157
157
  *
158
158
  * @param options
159
159
  */
160
- declare const staticResourceCache: ({ serwist, cacheName, matchCallback, plugins, warmCache }: StaticResourceOptions) => void;
160
+ export declare const staticResourceCache: ({ serwist, cacheName, matchCallback, plugins, warmCache }: StaticResourceOptions) => void;
161
161
  //#endregion
162
162
  //#region src/warmStrategyCache.d.ts
163
163
  interface WarmStrategyCacheOptions {
@@ -173,7 +173,7 @@ interface WarmStrategyCacheOptions {
173
173
  /**
174
174
  * @param options
175
175
  */
176
- declare const warmStrategyCache: (options: WarmStrategyCacheOptions) => void;
176
+ export declare const warmStrategyCache: (options: WarmStrategyCacheOptions) => void;
177
177
  //#endregion
178
- export { type GoogleFontCacheOptions, type ImageCacheOptions, type OfflineFallbackOptions, type PageCacheOptions, type StaticResourceOptions, type WarmStrategyCacheOptions, googleFontsCache, imageCache, offlineFallback, pageCache, staticResourceCache, warmStrategyCache };
178
+ export type { GoogleFontCacheOptions, ImageCacheOptions, OfflineFallbackOptions, PageCacheOptions, StaticResourceOptions, WarmStrategyCacheOptions };
179
179
  //# sourceMappingURL=index.d.mts.map
@@ -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":";;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"}
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;;;;;;;qBAQW,qBAAgB,SAAA,aAAA,eAAA,cAK1B;;;UC1Bc;;;;EAIf,SAAS;;;;EAIT;;;;EAIA,gBAAgB;;;;EAIhB;;;;EAIA;;;;EAIA,UAAU;;;;EAIV;;;;;;;qBAQW,eAAU,SAAA,WAAA,eAAA,eAAA,YAAA,SAAA,aAQpB;;;UC9Cc;;;;EAIf,SAAS;;;;EAIT;;;;EAIA;;;;EAIA;;;;;;;qBAYW,oBAAe,SAAA,cAAA,eAAA,gBAA6E;;;UC1BxF;;;;EAIf,SAAS;;;;EAIT;;;;EAIA,gBAAgB;;;;;;EAMhB;;;;EAIA,UAAU;;;;EAIV;;;;;;;qBAQW,cAAS,SAAA,WAAA,eAAA,uBAAA,SAAA,aAOnB;;;UCzCc;;;;EAIf,SAAS;;;;;;EAMT;;;;;;EAMA,gBAAgB;;;;EAIhB,UAAU;;;;EAIV;;;;;;;qBAQW,wBAAmB,SAAA,WAAA,eAAA,SAAA,aAO7B;;;UCjDc;;;;EAIf;;;;EAIA,UAAU;;;;;qBASC,oBAAiB,SAAa"}
package/dist/index.mjs CHANGED
@@ -5,7 +5,7 @@ import { CacheFirst, CacheableResponsePlugin, ExpirationPlugin, NetworkFirst, St
5
5
  *
6
6
  * @param options
7
7
  */
8
- const googleFontsCache = ({ serwist, cachePrefix = "google-fonts", maxAgeSeconds = 3600 * 24 * 365, maxEntries = 30 }) => {
8
+ const googleFontsCache = ({ serwist, cachePrefix = "google-fonts", maxAgeSeconds = 31536e3, maxEntries = 30 }) => {
9
9
  serwist.registerCapture(({ url }) => url.origin === "https://fonts.googleapis.com", new StaleWhileRevalidate({ cacheName: `${cachePrefix}-stylesheets` }));
10
10
  serwist.registerCapture(({ url }) => url.origin === "https://fonts.gstatic.com", new CacheFirst({
11
11
  cacheName: `${cachePrefix}-webfonts`,
@@ -36,7 +36,7 @@ const warmStrategyCache = (options) => {
36
36
  *
37
37
  * @param options
38
38
  */
39
- const imageCache = ({ serwist, cacheName = "images", matchCallback = ({ request }) => request.destination === "image", maxAgeSeconds = 720 * 60 * 60, maxEntries = 60, plugins = [], warmCache }) => {
39
+ const imageCache = ({ serwist, cacheName = "images", matchCallback = ({ request }) => request.destination === "image", maxAgeSeconds = 2592e3, maxEntries = 60, plugins = [], warmCache }) => {
40
40
  plugins.push(new CacheableResponsePlugin({ statuses: [0, 200] }));
41
41
  plugins.push(new ExpirationPlugin({
42
42
  maxEntries,
@@ -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;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,KAAK,iBAAiB,YAAY,UAAU;EAC1C,MAAM,OAAO,QAAQ,KAAK,KACvB,SACC,QAAQ,SAAS,UAAU;GACzB;GACA,SAAS,IAAI,QAAQ,IAAI;EAC3B,CAAC,CAAC,CAAC,EACP;EAEA,MAAM,UAAU,QAAQ,IAAI,IAAI,CAAC;CACnC,CAAC;AACH;;;;;;;;ACiBA,MAAa,cAAc,EACzB,SACA,YAAY,UACZ,iBAAiB,EAAE,cAAyC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ACjC;;;;;;;;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"}
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,SAChB,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,KAAK,iBAAiB,YAAY,UAAU;EAC1C,MAAM,OAAO,QAAQ,KAAK,KACvB,SACC,QAAQ,SAAS,UAAU;GACzB;GACA,SAAS,IAAI,QAAQ,IAAI;EAC3B,CAAC,CAAC,CAAC,EACP;EAEA,MAAM,UAAU,QAAQ,IAAI,IAAI,CAAC;CACnC,CAAC;AACH;;;;;;;;ACiBA,MAAa,cAAc,EACzB,SACA,YAAY,UACZ,iBAAiB,EAAE,cAAyC,QAAQ,gBAAgB,SACpF,gBAAgB,QAChB,aAAa,IACb,UAAU,CAAC,GACX,gBAC6B;CAC7B,QAAQ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AAwB,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.12",
3
+ "version": "9.5.14",
4
4
  "type": "module",
5
5
  "description": "A service worker helper library to manage common request and caching patterns",
6
6
  "files": [
@@ -36,10 +36,10 @@
36
36
  "./package.json": "./package.json"
37
37
  },
38
38
  "dependencies": {
39
- "serwist": "9.5.12"
39
+ "serwist": "9.5.14"
40
40
  },
41
41
  "devDependencies": {
42
- "tsdown": "0.22.12",
42
+ "tsdown": "0.23.0",
43
43
  "typescript": "7.0.2"
44
44
  },
45
45
  "peerDependencies": {