@serwist/recipes 8.0.0 → 8.0.1

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.
@@ -19,4 +19,3 @@ export interface GoogleFontCacheOptions {
19
19
  */
20
20
  declare function googleFontsCache(options?: GoogleFontCacheOptions): void;
21
21
  export { googleFontsCache };
22
- //# sourceMappingURL=googleFontsCache.d.ts.map
@@ -32,4 +32,3 @@ export interface ImageCacheOptions {
32
32
  */
33
33
  declare function imageCache(options?: ImageCacheOptions): void;
34
34
  export { imageCache };
35
- //# sourceMappingURL=imageCache.d.ts.map
package/dist/index.d.ts CHANGED
@@ -12,4 +12,3 @@ import type { WarmStrategyCacheOptions } from "./warmStrategyCache.js";
12
12
  import { warmStrategyCache } from "./warmStrategyCache.js";
13
13
  export { googleFontsCache, imageCache, offlineFallback, pageCache, staticResourceCache, warmStrategyCache };
14
14
  export type { GoogleFontCacheOptions, ImageCacheOptions, OfflineFallbackOptions, PageCacheOptions, StaticResourceOptions, WarmStrategyCacheOptions };
15
- //# sourceMappingURL=index.d.ts.map
@@ -20,4 +20,3 @@ export interface OfflineFallbackOptions {
20
20
  */
21
21
  declare function offlineFallback(options?: OfflineFallbackOptions): void;
22
22
  export { offlineFallback };
23
- //# sourceMappingURL=offlineFallback.d.ts.map
@@ -30,4 +30,3 @@ export interface PageCacheOptions {
30
30
  */
31
31
  declare function pageCache(options?: PageCacheOptions): void;
32
32
  export { pageCache };
33
- //# sourceMappingURL=pageCache.d.ts.map
@@ -28,4 +28,3 @@ export interface StaticResourceOptions {
28
28
  */
29
29
  declare function staticResourceCache(options?: StaticResourceOptions): void;
30
30
  export { staticResourceCache };
31
- //# sourceMappingURL=staticResourceCache.d.ts.map
@@ -14,4 +14,3 @@ export interface WarmStrategyCacheOptions {
14
14
  */
15
15
  declare function warmStrategyCache(options: WarmStrategyCacheOptions): void;
16
16
  export { warmStrategyCache };
17
- //# sourceMappingURL=warmStrategyCache.d.ts.map
package/package.json CHANGED
@@ -1,8 +1,12 @@
1
1
  {
2
2
  "name": "@serwist/recipes",
3
- "version": "8.0.0",
3
+ "version": "8.0.1",
4
4
  "type": "module",
5
5
  "description": "A service worker helper library to manage common request and caching patterns",
6
+ "files": [
7
+ "dist",
8
+ "!dist/**/dts"
9
+ ],
6
10
  "keywords": [
7
11
  "serwist",
8
12
  "serwistjs",
@@ -28,16 +32,16 @@
28
32
  "./package.json": "./package.json"
29
33
  },
30
34
  "dependencies": {
31
- "@serwist/cacheable-response": "8.0.0",
32
- "@serwist/core": "8.0.0",
33
- "@serwist/expiration": "8.0.0",
34
- "@serwist/precaching": "8.0.0",
35
- "@serwist/routing": "8.0.0",
36
- "@serwist/strategies": "8.0.0"
35
+ "@serwist/cacheable-response": "8.0.1",
36
+ "@serwist/core": "8.0.1",
37
+ "@serwist/expiration": "8.0.1",
38
+ "@serwist/precaching": "8.0.1",
39
+ "@serwist/routing": "8.0.1",
40
+ "@serwist/strategies": "8.0.1"
37
41
  },
38
42
  "devDependencies": {
39
43
  "rollup": "3.28.1",
40
- "@serwist/constants": "8.0.0"
44
+ "@serwist/constants": "8.0.1"
41
45
  },
42
46
  "scripts": {
43
47
  "build": "rimraf dist && cross-env NODE_ENV=production rollup --config rollup.config.js",
@@ -1,10 +0,0 @@
1
-
2
- > @serwist/recipes@8.0.0 build /home/runner/work/serwist/serwist/packages/recipes
3
- > rimraf dist && cross-env NODE_ENV=production rollup --config rollup.config.js
4
-
5
- (node:2995) ExperimentalWarning: Import assertions are not a stable feature of the JavaScript language. Avoid relying on their current behavior and syntax as those might change in a future version of Node.js.
6
- (Use `node --trace-warnings ...` to show where the warning was created)
7
- (node:2995) ExperimentalWarning: Importing JSON modules is an experimental feature and might change at any time
8
- 
9
- src/index.ts → dist/index.old.cjs, dist/index.js...
10
- created dist/index.old.cjs, dist/index.js in 1.8s
@@ -1,4 +0,0 @@
1
-
2
- > @serwist/recipes@8.0.0 lint /home/runner/work/serwist/serwist/packages/recipes
3
- > eslint src --ext ts,tsx,js,jsx,cjs,mjs
4
-
@@ -1,4 +0,0 @@
1
-
2
- > @serwist/recipes@8.0.0 typecheck /home/runner/work/serwist/serwist/packages/recipes
3
- > tsc
4
-
package/CHANGELOG.md DELETED
@@ -1,38 +0,0 @@
1
- # @serwist/recipes
2
-
3
- ## 8.0.0
4
-
5
- ### Major Changes
6
-
7
- - [`e0313f0`](https://github.com/serwist/serwist/commit/e0313f02f661a07ccbe9edc64e44e1af6136c73e) Thanks [@DuCanhGH](https://github.com/DuCanhGH)! - chore: initial release
8
-
9
- - Reimagined `@serwist/next`.
10
- - Removed various options.
11
- - Removed `aggressiveFrontEndNavCaching` to reassess its usefulness.
12
- - Removed `browserslist`. Use `swcEnvTargets` instead (TODO: add this option).
13
- - Custom workers have been removed. You should use `swSrc`.
14
- - Removed `extendDefaultRuntimeCaching`. Use the spread syntax instead (use `import { defaultCache } from "@serwist/next/browser"` to import the default runtimeCaching array).
15
- - Temporarily removed `fallbacks` to investigate module-friendly alternatives.
16
- - Removed `swcMinify`.
17
- - Removed `watchWorkersInDev`.
18
- - Removed `cacheStartUrl`, `dynamicStartUrl`, and `dynamicStartUrl`. These shall be re-added only when their use cases are made more clear to me.
19
- - Merged `workboxOptions` with the plugin's options.
20
- - Removed `swc-loader`, `terser-minify`, `webpack-builders`,... (we now leverage `ChildCompilationPlugin` to compile workers - this change will be backported to `@ducanh2912/next-pwa@10`)
21
- - Removed the ability to use GenerateSW. `@serwist/sw.installSerwist` is provided as a replacement.
22
- - `swSrc` is now a required property.
23
- - Moved minimum support Next.js version from `11.0.0` to `14.0.0`.
24
- - Removed GenerateSW (replaced by `@serwist/sw.installSerwist`).
25
- - See `examples/next-basic/app/sw.ts` to see how `installSerwist` should be used.
26
- - Repurposed `@serwist/sw`.
27
- - The old package might be reintroduced if there's demand.
28
- - **Note:** This is just the initial release, and there is still a lot of ground to cover (a lot of legacy code to be removed, a lot of features to be reintroduced,...). Here's to a bright future for the project :\_)
29
-
30
- ### Patch Changes
31
-
32
- - Updated dependencies [[`e0313f0`](https://github.com/serwist/serwist/commit/e0313f02f661a07ccbe9edc64e44e1af6136c73e)]:
33
- - @serwist/cacheable-response@8.0.0
34
- - @serwist/expiration@8.0.0
35
- - @serwist/precaching@8.0.0
36
- - @serwist/strategies@8.0.0
37
- - @serwist/routing@8.0.0
38
- - @serwist/core@8.0.0
@@ -1 +0,0 @@
1
- {"version":3,"file":"googleFontsCache.d.ts","sourceRoot":"","sources":["../src/googleFontsCache.ts"],"names":[],"mappings":"AAaA,MAAM,WAAW,sBAAsB;IACrC;;OAEG;IACH,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB;;OAEG;IACH,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB;;OAEG;IACH,UAAU,CAAC,EAAE,MAAM,CAAC;CACrB;AAED;;;;GAIG;AACH,iBAAS,gBAAgB,CAAC,OAAO,GAAE,sBAA2B,GAAG,IAAI,CA8BpE;AAED,OAAO,EAAE,gBAAgB,EAAE,CAAC"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"imageCache.d.ts","sourceRoot":"","sources":["../src/imageCache.ts"],"names":[],"mappings":"AASA,OAAO,KAAK,EAAE,kBAAkB,EAA6B,aAAa,EAAE,MAAM,eAAe,CAAC;AAOlG,MAAM,WAAW,iBAAiB;IAChC;;OAEG;IACH,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB;;OAEG;IACH,aAAa,CAAC,EAAE,kBAAkB,CAAC;IACnC;;OAEG;IACH,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB;;OAEG;IACH,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB;;OAEG;IACH,OAAO,CAAC,EAAE,aAAa,EAAE,CAAC;IAC1B;;OAEG;IACH,SAAS,CAAC,EAAE,MAAM,EAAE,CAAC;CACtB;AAED;;;;GAIG;AACH,iBAAS,UAAU,CAAC,OAAO,GAAE,iBAAsB,GAAG,IAAI,CA+BzD;AAED,OAAO,EAAE,UAAU,EAAE,CAAC"}
@@ -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"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"offlineFallback.d.ts","sourceRoot":"","sources":["../src/offlineFallback.ts"],"names":[],"mappings":"AAYA,MAAM,WAAW,sBAAsB;IACrC;;OAEG;IACH,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB;;OAEG;IACH,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB;;OAEG;IACH,YAAY,CAAC,EAAE,MAAM,CAAC;CACvB;AAKD;;;;;GAKG;AACH,iBAAS,eAAe,CAAC,OAAO,GAAE,sBAA2B,GAAG,IAAI,CAwCnE;AAED,OAAO,EAAE,eAAe,EAAE,CAAC"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"pageCache.d.ts","sourceRoot":"","sources":["../src/pageCache.ts"],"names":[],"mappings":"AASA,OAAO,KAAK,EAAE,kBAAkB,EAA6B,aAAa,EAAE,MAAM,eAAe,CAAC;AAMlG,MAAM,WAAW,gBAAgB;IAC/B;;OAEG;IACH,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB;;OAEG;IACH,aAAa,CAAC,EAAE,kBAAkB,CAAC;IACnC;;;;OAIG;IACH,qBAAqB,CAAC,EAAE,MAAM,CAAC;IAC/B;;OAEG;IACH,OAAO,CAAC,EAAE,aAAa,EAAE,CAAC;IAC1B;;OAEG;IACH,SAAS,CAAC,EAAE,MAAM,EAAE,CAAC;CACtB;AAED;;;;GAIG;AACH,iBAAS,SAAS,CAAC,OAAO,GAAE,gBAAqB,GAAG,IAAI,CA0BvD;AAED,OAAO,EAAE,SAAS,EAAE,CAAC"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"staticResourceCache.d.ts","sourceRoot":"","sources":["../src/staticResourceCache.ts"],"names":[],"mappings":"AASA,OAAO,KAAK,EAAE,kBAAkB,EAA6B,aAAa,EAAE,MAAM,eAAe,CAAC;AAMlG,MAAM,WAAW,qBAAqB;IACpC;;;;OAIG;IACH,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB;;;;OAIG;IACH,aAAa,CAAC,EAAE,kBAAkB,CAAC;IACnC;;OAEG;IACH,OAAO,CAAC,EAAE,aAAa,EAAE,CAAC;IAC1B;;OAEG;IACH,SAAS,CAAC,EAAE,MAAM,EAAE,CAAC;CACtB;AAED;;;;GAIG;AACH,iBAAS,mBAAmB,CAAC,OAAO,GAAE,qBAA0B,GAAG,IAAI,CAwBtE;AAED,OAAO,EAAE,mBAAmB,EAAE,CAAC"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"warmStrategyCache.d.ts","sourceRoot":"","sources":["../src/warmStrategyCache.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,qBAAqB,CAAC;AAEpD,MAAM,WAAW,wBAAwB;IACvC;;OAEG;IACH,IAAI,EAAE,MAAM,EAAE,CAAC;IACf;;OAEG;IACH,QAAQ,EAAE,QAAQ,CAAC;CACpB;AAKD;;GAEG;AACH,iBAAS,iBAAiB,CAAC,OAAO,EAAE,wBAAwB,GAAG,IAAI,CAYlE;AAED,OAAO,EAAE,iBAAiB,EAAE,CAAC"}
package/rollup.config.js DELETED
@@ -1,27 +0,0 @@
1
- // @ts-check
2
- import { getRollupOptions } from "@serwist/constants/rollup";
3
-
4
- import packageJson from "./package.json" assert { type: "json" };
5
-
6
- const isDev = process.env.NODE_ENV === "development";
7
-
8
- export default getRollupOptions({
9
- packageJson,
10
- jsFiles: [
11
- {
12
- input: "src/index.ts",
13
- output: [
14
- {
15
- file: "dist/index.old.cjs",
16
- format: "cjs",
17
- exports: "named",
18
- },
19
- {
20
- file: "dist/index.js",
21
- format: "esm",
22
- },
23
- ],
24
- },
25
- ],
26
- shouldEmitDeclaration: !isDev,
27
- });
@@ -1,66 +0,0 @@
1
- /*
2
- Copyright 2020 Google LLC
3
-
4
- Use of this source code is governed by an MIT-style
5
- license that can be found in the LICENSE file or at
6
- https://opensource.org/licenses/MIT.
7
- */
8
-
9
- import { CacheableResponsePlugin } from "@serwist/cacheable-response";
10
- import { ExpirationPlugin } from "@serwist/expiration";
11
- import { registerRoute } from "@serwist/routing";
12
- import { CacheFirst, StaleWhileRevalidate } from "@serwist/strategies";
13
-
14
- export interface GoogleFontCacheOptions {
15
- /**
16
- * Cache prefix for caching stylesheets and webfonts. Defaults to google-fonts.
17
- */
18
- cachePrefix?: string;
19
- /**
20
- * Maximum age, in seconds, that font entries will be cached for. Defaults to 1 year.
21
- */
22
- maxAgeSeconds?: number;
23
- /**
24
- * Maximum number of fonts that will be cached. Defaults to 30.
25
- */
26
- maxEntries?: number;
27
- }
28
-
29
- /**
30
- * An implementation of the [Google fonts](https://developers.google.com/web/tools/workbox/guides/common-recipes#google_fonts) caching recipe.
31
- *
32
- * @param options
33
- */
34
- function googleFontsCache(options: GoogleFontCacheOptions = {}): void {
35
- const sheetCacheName = `${options.cachePrefix || "google-fonts"}-stylesheets`;
36
- const fontCacheName = `${options.cachePrefix || "google-fonts"}-webfonts`;
37
- const maxAgeSeconds = options.maxAgeSeconds || 60 * 60 * 24 * 365;
38
- const maxEntries = options.maxEntries || 30;
39
-
40
- // Cache the Google Fonts stylesheets with a stale-while-revalidate strategy.
41
- registerRoute(
42
- ({ url }) => url.origin === "https://fonts.googleapis.com",
43
- new StaleWhileRevalidate({
44
- cacheName: sheetCacheName,
45
- })
46
- );
47
-
48
- // Cache the underlying font files with a cache-first strategy for 1 year.
49
- registerRoute(
50
- ({ url }) => url.origin === "https://fonts.gstatic.com",
51
- new CacheFirst({
52
- cacheName: fontCacheName,
53
- plugins: [
54
- new CacheableResponsePlugin({
55
- statuses: [0, 200],
56
- }),
57
- new ExpirationPlugin({
58
- maxAgeSeconds,
59
- maxEntries,
60
- }),
61
- ],
62
- })
63
- );
64
- }
65
-
66
- export { googleFontsCache };
package/src/imageCache.ts DELETED
@@ -1,82 +0,0 @@
1
- /*
2
- Copyright 2020 Google LLC
3
-
4
- Use of this source code is governed by an MIT-style
5
- license that can be found in the LICENSE file or at
6
- https://opensource.org/licenses/MIT.
7
- */
8
-
9
- import { CacheableResponsePlugin } from "@serwist/cacheable-response";
10
- import type { RouteMatchCallback, RouteMatchCallbackOptions, SerwistPlugin } from "@serwist/core";
11
- import { ExpirationPlugin } from "@serwist/expiration";
12
- import { registerRoute } from "@serwist/routing";
13
- import { CacheFirst } from "@serwist/strategies";
14
-
15
- import { warmStrategyCache } from "./warmStrategyCache.js";
16
-
17
- export interface ImageCacheOptions {
18
- /**
19
- * Name for cache. Defaults to images.
20
- */
21
- cacheName?: string;
22
- /**
23
- * Serwist callback function to call to match to. Defaults to request.destination === 'image'.
24
- */
25
- matchCallback?: RouteMatchCallback;
26
- /**
27
- * Maximum age, in seconds, that image entries will be cached for. Defaults to 30 days.
28
- */
29
- maxAgeSeconds?: number;
30
- /**
31
- * Maximum number of images that will be cached. Defaults to 60.
32
- */
33
- maxEntries?: number;
34
- /**
35
- * Additional plugins to use for this recipe.
36
- */
37
- plugins?: SerwistPlugin[];
38
- /**
39
- * Paths to call to use to warm this cache.
40
- */
41
- warmCache?: string[];
42
- }
43
-
44
- /**
45
- * An implementation of the [image caching recipe](https://developers.google.com/web/tools/workbox/guides/common-recipes#caching_images).
46
- *
47
- * @param options
48
- */
49
- function imageCache(options: ImageCacheOptions = {}): void {
50
- const defaultMatchCallback = ({ request }: RouteMatchCallbackOptions) => request.destination === "image";
51
-
52
- const cacheName = options.cacheName || "images";
53
- const matchCallback = options.matchCallback || defaultMatchCallback;
54
- const maxAgeSeconds = options.maxAgeSeconds || 30 * 24 * 60 * 60;
55
- const maxEntries = options.maxEntries || 60;
56
- const plugins = options.plugins || [];
57
- plugins.push(
58
- new CacheableResponsePlugin({
59
- statuses: [0, 200],
60
- })
61
- );
62
- plugins.push(
63
- new ExpirationPlugin({
64
- maxEntries,
65
- maxAgeSeconds,
66
- })
67
- );
68
-
69
- const strategy = new CacheFirst({
70
- cacheName,
71
- plugins,
72
- });
73
-
74
- registerRoute(matchCallback, strategy);
75
-
76
- // Warms the cache
77
- if (options.warmCache) {
78
- warmStrategyCache({ urls: options.warmCache, strategy });
79
- }
80
- }
81
-
82
- export { imageCache };
package/src/index.ts DELETED
@@ -1,23 +0,0 @@
1
- /*
2
- Copyright 2020 Google LLC
3
-
4
- Use of this source code is governed by an MIT-style
5
- license that can be found in the LICENSE file or at
6
- https://opensource.org/licenses/MIT.
7
- */
8
-
9
- import type { GoogleFontCacheOptions } from "./googleFontsCache.js";
10
- import { googleFontsCache } from "./googleFontsCache.js";
11
- import type { ImageCacheOptions } from "./imageCache.js";
12
- import { imageCache } from "./imageCache.js";
13
- import type { OfflineFallbackOptions } from "./offlineFallback.js";
14
- import { offlineFallback } from "./offlineFallback.js";
15
- import type { PageCacheOptions } from "./pageCache.js";
16
- import { pageCache } from "./pageCache.js";
17
- import type { StaticResourceOptions } from "./staticResourceCache.js";
18
- import { staticResourceCache } from "./staticResourceCache.js";
19
- import type { WarmStrategyCacheOptions } from "./warmStrategyCache.js";
20
- import { warmStrategyCache } from "./warmStrategyCache.js";
21
-
22
- export { googleFontsCache, imageCache, offlineFallback, pageCache, staticResourceCache, warmStrategyCache };
23
- export type { GoogleFontCacheOptions, ImageCacheOptions, OfflineFallbackOptions, PageCacheOptions, StaticResourceOptions, WarmStrategyCacheOptions };
@@ -1,79 +0,0 @@
1
- /*
2
- Copyright 2020 Google LLC
3
-
4
- Use of this source code is governed by an MIT-style
5
- license that can be found in the LICENSE file or at
6
- https://opensource.org/licenses/MIT.
7
- */
8
-
9
- import type { RouteHandler, RouteHandlerCallbackOptions } from "@serwist/core";
10
- import { matchPrecache } from "@serwist/precaching";
11
- import { setCatchHandler } from "@serwist/routing";
12
-
13
- export interface OfflineFallbackOptions {
14
- /**
15
- * Precache name to match for page fallbacks. Defaults to offline.html.
16
- */
17
- pageFallback?: string;
18
- /**
19
- * Precache name to match for image fallbacks.
20
- */
21
- imageFallback?: string;
22
- /**
23
- * Precache name to match for font fallbacks.
24
- */
25
- fontFallback?: string;
26
- }
27
-
28
- // Give TypeScript the correct global.
29
- declare let self: ServiceWorkerGlobalScope;
30
-
31
- /**
32
- * An implementation of the [comprehensive fallbacks recipe](https://developers.google.com/web/tools/workbox/guides/advanced-recipes#comprehensive_fallbacks).
33
- * Be sure to include the fallbacks in your precache injection.
34
-
35
- * @param options
36
- */
37
- function offlineFallback(options: OfflineFallbackOptions = {}): void {
38
- const pageFallback = options.pageFallback || "offline.html";
39
- const imageFallback = options.imageFallback || false;
40
- const fontFallback = options.fontFallback || false;
41
-
42
- self.addEventListener("install", (event) => {
43
- const files = [pageFallback];
44
- if (imageFallback) {
45
- files.push(imageFallback);
46
- }
47
- if (fontFallback) {
48
- files.push(fontFallback);
49
- }
50
-
51
- event.waitUntil(self.caches.open("serwist-offline-fallbacks").then((cache) => cache.addAll(files)));
52
- });
53
-
54
- const handler: RouteHandler = async (options: RouteHandlerCallbackOptions) => {
55
- const dest = options.request.destination;
56
- const cache = await self.caches.open("serwist-offline-fallbacks");
57
-
58
- if (dest === "document") {
59
- const match = (await matchPrecache(pageFallback)) || (await cache.match(pageFallback));
60
- return match || Response.error();
61
- }
62
-
63
- if (dest === "image" && imageFallback !== false) {
64
- const match = (await matchPrecache(imageFallback)) || (await cache.match(imageFallback));
65
- return match || Response.error();
66
- }
67
-
68
- if (dest === "font" && fontFallback !== false) {
69
- const match = (await matchPrecache(fontFallback)) || (await cache.match(fontFallback));
70
- return match || Response.error();
71
- }
72
-
73
- return Response.error();
74
- };
75
-
76
- setCatchHandler(handler);
77
- }
78
-
79
- export { offlineFallback };
package/src/pageCache.ts DELETED
@@ -1,74 +0,0 @@
1
- /*
2
- Copyright 2020 Google LLC
3
-
4
- Use of this source code is governed by an MIT-style
5
- license that can be found in the LICENSE file or at
6
- https://opensource.org/licenses/MIT.
7
- */
8
-
9
- import { CacheableResponsePlugin } from "@serwist/cacheable-response";
10
- import type { RouteMatchCallback, RouteMatchCallbackOptions, SerwistPlugin } from "@serwist/core";
11
- import { registerRoute } from "@serwist/routing";
12
- import { NetworkFirst } from "@serwist/strategies";
13
-
14
- import { warmStrategyCache } from "./warmStrategyCache.js";
15
-
16
- export interface PageCacheOptions {
17
- /**
18
- * Name for cache. Defaults to pages.
19
- */
20
- cacheName?: string;
21
- /**
22
- * Serwist callback function to call to match to. Defaults to request.mode === 'navigate'.
23
- */
24
- matchCallback?: RouteMatchCallback;
25
- /**
26
- * Maximum amount of time, in seconds, to wait on the network before falling back to cache.
27
- *
28
- * @default 3
29
- */
30
- networkTimeoutSeconds?: number;
31
- /**
32
- * Additional plugins to use for this recipe.
33
- */
34
- plugins?: SerwistPlugin[];
35
- /**
36
- * Paths to call to use to warm this cache
37
- */
38
- warmCache?: string[];
39
- }
40
-
41
- /**
42
- * An implementation of a page caching recipe with a network timeout.
43
- *
44
- * @param options
45
- */
46
- function pageCache(options: PageCacheOptions = {}): void {
47
- const defaultMatchCallback = ({ request }: RouteMatchCallbackOptions) => request.mode === "navigate";
48
-
49
- const cacheName = options.cacheName || "pages";
50
- const matchCallback = options.matchCallback || defaultMatchCallback;
51
- const networkTimeoutSeconds = options.networkTimeoutSeconds || 3;
52
- const plugins = options.plugins || [];
53
- plugins.push(
54
- new CacheableResponsePlugin({
55
- statuses: [0, 200],
56
- })
57
- );
58
-
59
- const strategy = new NetworkFirst({
60
- networkTimeoutSeconds,
61
- cacheName,
62
- plugins,
63
- });
64
-
65
- // Registers the route
66
- registerRoute(matchCallback, strategy);
67
-
68
- // Warms the cache
69
- if (options.warmCache) {
70
- warmStrategyCache({ urls: options.warmCache, strategy });
71
- }
72
- }
73
-
74
- export { pageCache };
@@ -1,70 +0,0 @@
1
- /*
2
- Copyright 2020 Google LLC
3
-
4
- Use of this source code is governed by an MIT-style
5
- license that can be found in the LICENSE file or at
6
- https://opensource.org/licenses/MIT.
7
- */
8
-
9
- import { CacheableResponsePlugin } from "@serwist/cacheable-response";
10
- import type { RouteMatchCallback, RouteMatchCallbackOptions, SerwistPlugin } from "@serwist/core";
11
- import { registerRoute } from "@serwist/routing";
12
- import { StaleWhileRevalidate } from "@serwist/strategies";
13
-
14
- import { warmStrategyCache } from "./warmStrategyCache.js";
15
-
16
- export interface StaticResourceOptions {
17
- /**
18
- * Name for cache.
19
- *
20
- * @default "static-resources"
21
- */
22
- cacheName?: string;
23
- /**
24
- * Serwist callback function to call to match to.
25
- *
26
- * @default request.destination === 'style' || request.destination === 'script' || request.destination === 'worker'
27
- */
28
- matchCallback?: RouteMatchCallback;
29
- /**
30
- * Additional plugins to use for this recipe.
31
- */
32
- plugins?: SerwistPlugin[];
33
- /**
34
- * Paths to call to use to warm this cache.
35
- */
36
- warmCache?: string[];
37
- }
38
-
39
- /**
40
- * An implementation of the [CSS and JavaScript files recipe](https://developers.google.com/web/tools/workbox/guides/common-recipes#cache_css_and_javascript_files).
41
- *
42
- * @param options
43
- */
44
- function staticResourceCache(options: StaticResourceOptions = {}): void {
45
- const defaultMatchCallback = ({ request }: RouteMatchCallbackOptions) =>
46
- request.destination === "style" || request.destination === "script" || request.destination === "worker";
47
-
48
- const cacheName = options.cacheName || "static-resources";
49
- const matchCallback = options.matchCallback || defaultMatchCallback;
50
- const plugins = options.plugins || [];
51
- plugins.push(
52
- new CacheableResponsePlugin({
53
- statuses: [0, 200],
54
- })
55
- );
56
-
57
- const strategy = new StaleWhileRevalidate({
58
- cacheName,
59
- plugins,
60
- });
61
-
62
- registerRoute(matchCallback, strategy);
63
-
64
- // Warms the cache
65
- if (options.warmCache) {
66
- warmStrategyCache({ urls: options.warmCache, strategy });
67
- }
68
- }
69
-
70
- export { staticResourceCache };
@@ -1,34 +0,0 @@
1
- import type { Strategy } from "@serwist/strategies";
2
-
3
- export interface WarmStrategyCacheOptions {
4
- /**
5
- * Paths to warm the strategy's cache with.
6
- */
7
- urls: string[];
8
- /**
9
- * Strategy to use.
10
- */
11
- strategy: Strategy;
12
- }
13
-
14
- // Give TypeScript the correct global.
15
- declare let self: ServiceWorkerGlobalScope;
16
-
17
- /**
18
- * @param options
19
- */
20
- function warmStrategyCache(options: WarmStrategyCacheOptions): void {
21
- self.addEventListener("install", (event) => {
22
- const done = options.urls.map(
23
- (path) =>
24
- options.strategy.handleAll({
25
- event,
26
- request: new Request(path),
27
- })[1]
28
- );
29
-
30
- event.waitUntil(Promise.all(done));
31
- });
32
- }
33
-
34
- export { warmStrategyCache };
package/tsconfig.json DELETED
@@ -1,8 +0,0 @@
1
- {
2
- "extends": "../../tsconfig.json",
3
- "compilerOptions": {
4
- "module": "NodeNext"
5
- },
6
- "include": ["**/*.ts", "rollup.config.js"],
7
- "exclude": ["dist", "node_modules"]
8
- }