@wix/site-service-above-the-fold 1.0.5 → 1.0.7

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/README.md CHANGED
@@ -5,7 +5,7 @@ Service for checking whether a component id/path is marked as above the fold.
5
5
  ## Install
6
6
 
7
7
  ```sh
8
- yarn add @wix/site-service-above-the-fold
8
+ pnpm add @wix/site-service-above-the-fold
9
9
  ```
10
10
 
11
11
  ## Usage
@@ -1,10 +1,76 @@
1
1
  import type { ServiceDefinition } from '@wix/services-definitions';
2
+ /** ID of a component on the page, such as `comp-lxk2h8f1`. */
2
3
  export type CompId = string;
4
+ /** Path of a component on the page. */
3
5
  export type CompPath = string;
6
+ /** Components the host marks as above the fold or not, keyed by component ID or component path. */
4
7
  export type IAboveTheFoldServiceConfig = {
5
8
  [key: CompId | CompPath]: boolean;
6
9
  };
10
+ /**
11
+ * Checks whether a component on the current page is above the fold, meaning it's visible when the page
12
+ * first loads, before a site visitor scrolls.
13
+ */
7
14
  export interface IAboveTheFoldService {
15
+ /**
16
+ * Checks whether a component is above the fold on the current page.
17
+ *
18
+ * Use it to decide what to load or render first, such as loading an image right away when it's
19
+ * above the fold and lazily when it isn't.
20
+ *
21
+ * Returns `undefined` for a component that isn't marked, and never throws. Treat anything other than
22
+ * `true` as not above the fold.
23
+ *
24
+ * The result describes the page's initial layout, not where the site visitor has scrolled to. This
25
+ * value doesn't change for the lifetime of the page. The method does the same lookup while the page
26
+ * renders on the server and in the browser.
27
+ *
28
+ * The result is based on the page's layout, not on the site visitor's screen size, so use it to
29
+ * prioritize loading, not to tell whether a component is visible.
30
+ *
31
+ * @param key Component ID. Case-sensitive, and must match the component's ID exactly.
32
+ * @returns Whether the component is above the fold:
33
+ * - `true`: The host marks the component as above the fold.
34
+ * - `false`: The host marks the component as not above the fold.
35
+ * - `undefined`: The host doesn't mark the component.
36
+ *
37
+ * @exampleValue key 'comp-lxk2h8f1'
38
+ * @exampleValue true
39
+ *
40
+ * @example Load an image right away when it's above the fold
41
+ * ```tsx
42
+ * import { useService } from '@wix/services-manager-react'
43
+ * import { AboveTheFoldDefinition } from '@wix/site-service-above-the-fold/definition'
44
+ *
45
+ * function ProductImage({ id, src, alt }: { id: string; src: string; alt: string }) {
46
+ * const aboveTheFoldService = useService(AboveTheFoldDefinition)
47
+ * const isAboveTheFold = aboveTheFoldService.isInFirstFold(id) === true
48
+ *
49
+ * return (
50
+ * <img
51
+ * id={id}
52
+ * src={src}
53
+ * alt={alt}
54
+ * loading={isAboveTheFold ? 'eager' : 'lazy'}
55
+ * fetchPriority={isAboveTheFold ? 'high' : 'auto'}
56
+ * />
57
+ * )
58
+ * }
59
+ * ```
60
+ *
61
+ * @example Preload the images of components above the fold
62
+ * ```ts
63
+ * import { AboveTheFoldDefinition } from '@wix/site-service-above-the-fold/definition'
64
+ *
65
+ * const aboveTheFoldService = servicesManager.getService(AboveTheFoldDefinition)
66
+ *
67
+ * function imagesToPreload(images: { compId: string; src: string }[]) {
68
+ * return images
69
+ * .filter(({ compId }) => aboveTheFoldService.isInFirstFold(compId) === true)
70
+ * .map(({ src }) => src)
71
+ * }
72
+ * ```
73
+ */
8
74
  isInFirstFold: (key: CompId | CompPath) => boolean | undefined;
9
75
  }
10
76
  export type IAboveTheFoldDefinition = ServiceDefinition<IAboveTheFoldService, IAboveTheFoldServiceConfig>;
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/definition/index.ts"],"names":[],"mappings":";;;AAAA,oEAAyD;AAgB5C,QAAA,sBAAsB,GAAG,IAAA,oCAAa,EAClD,kCAAkC,CAClC,CAAA"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/definition/index.ts"],"names":[],"mappings":";;;AAAA,oEAAyD;AAkF5C,QAAA,sBAAsB,GAAG,IAAA,oCAAa,EAClD,kCAAkC,CAClC,CAAA"}
@@ -1,10 +1,76 @@
1
1
  import type { ServiceDefinition } from '@wix/services-definitions';
2
+ /** ID of a component on the page, such as `comp-lxk2h8f1`. */
2
3
  export type CompId = string;
4
+ /** Path of a component on the page. */
3
5
  export type CompPath = string;
6
+ /** Components the host marks as above the fold or not, keyed by component ID or component path. */
4
7
  export type IAboveTheFoldServiceConfig = {
5
8
  [key: CompId | CompPath]: boolean;
6
9
  };
10
+ /**
11
+ * Checks whether a component on the current page is above the fold, meaning it's visible when the page
12
+ * first loads, before a site visitor scrolls.
13
+ */
7
14
  export interface IAboveTheFoldService {
15
+ /**
16
+ * Checks whether a component is above the fold on the current page.
17
+ *
18
+ * Use it to decide what to load or render first, such as loading an image right away when it's
19
+ * above the fold and lazily when it isn't.
20
+ *
21
+ * Returns `undefined` for a component that isn't marked, and never throws. Treat anything other than
22
+ * `true` as not above the fold.
23
+ *
24
+ * The result describes the page's initial layout, not where the site visitor has scrolled to. This
25
+ * value doesn't change for the lifetime of the page. The method does the same lookup while the page
26
+ * renders on the server and in the browser.
27
+ *
28
+ * The result is based on the page's layout, not on the site visitor's screen size, so use it to
29
+ * prioritize loading, not to tell whether a component is visible.
30
+ *
31
+ * @param key Component ID. Case-sensitive, and must match the component's ID exactly.
32
+ * @returns Whether the component is above the fold:
33
+ * - `true`: The host marks the component as above the fold.
34
+ * - `false`: The host marks the component as not above the fold.
35
+ * - `undefined`: The host doesn't mark the component.
36
+ *
37
+ * @exampleValue key 'comp-lxk2h8f1'
38
+ * @exampleValue true
39
+ *
40
+ * @example Load an image right away when it's above the fold
41
+ * ```tsx
42
+ * import { useService } from '@wix/services-manager-react'
43
+ * import { AboveTheFoldDefinition } from '@wix/site-service-above-the-fold/definition'
44
+ *
45
+ * function ProductImage({ id, src, alt }: { id: string; src: string; alt: string }) {
46
+ * const aboveTheFoldService = useService(AboveTheFoldDefinition)
47
+ * const isAboveTheFold = aboveTheFoldService.isInFirstFold(id) === true
48
+ *
49
+ * return (
50
+ * <img
51
+ * id={id}
52
+ * src={src}
53
+ * alt={alt}
54
+ * loading={isAboveTheFold ? 'eager' : 'lazy'}
55
+ * fetchPriority={isAboveTheFold ? 'high' : 'auto'}
56
+ * />
57
+ * )
58
+ * }
59
+ * ```
60
+ *
61
+ * @example Preload the images of components above the fold
62
+ * ```ts
63
+ * import { AboveTheFoldDefinition } from '@wix/site-service-above-the-fold/definition'
64
+ *
65
+ * const aboveTheFoldService = servicesManager.getService(AboveTheFoldDefinition)
66
+ *
67
+ * function imagesToPreload(images: { compId: string; src: string }[]) {
68
+ * return images
69
+ * .filter(({ compId }) => aboveTheFoldService.isInFirstFold(compId) === true)
70
+ * .map(({ src }) => src)
71
+ * }
72
+ * ```
73
+ */
8
74
  isInFirstFold: (key: CompId | CompPath) => boolean | undefined;
9
75
  }
10
76
  export type IAboveTheFoldDefinition = ServiceDefinition<IAboveTheFoldService, IAboveTheFoldServiceConfig>;
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/definition/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,2BAA2B,CAAA;AAgBzD,MAAM,CAAC,MAAM,sBAAsB,GAAG,aAAa,CAClD,kCAAkC,CAClC,CAAA"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/definition/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,2BAA2B,CAAA;AAkFzD,MAAM,CAAC,MAAM,sBAAsB,GAAG,aAAa,CAClD,kCAAkC,CAClC,CAAA"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wix/site-service-above-the-fold",
3
- "version": "1.0.5",
3
+ "version": "1.0.7",
4
4
  "license": "UNLICENSED",
5
5
  "files": [
6
6
  "dist",
@@ -48,8 +48,8 @@
48
48
  },
49
49
  "scripts": {
50
50
  "build": "tsc -b . && tsc --project tsconfig.cjs.json",
51
- "lint": "yarn run -T lint:package $npm_package_name",
52
- "test": "vitest --run && yarn test-type-check",
51
+ "lint": "pnpm --workspace-root run lint:package --package $npm_package_name",
52
+ "test": "vitest --run && pnpm run test-type-check",
53
53
  "test-type-check": "echo 'validating typescript in test files' && tsc --project tsconfig.test.json && echo 'Finished validating typescript in test files successfully'"
54
54
  },
55
55
  "dependencies": {
@@ -77,5 +77,5 @@
77
77
  ]
78
78
  }
79
79
  },
80
- "falconPackageHash": "6626a796649d8ada9edf7de9eaaed0f271334cbedda0ccdef7d84de6"
80
+ "falconPackageHash": "ee484956d5ae8e2abf96bf98aacf631d1c238d9fafbc622abefd5d42"
81
81
  }
@@ -1,14 +1,80 @@
1
1
  import { defineService } from '@wix/services-definitions'
2
2
  import type { ServiceDefinition } from '@wix/services-definitions'
3
3
 
4
+ /** ID of a component on the page, such as `comp-lxk2h8f1`. */
4
5
  export type CompId = string
6
+ /** Path of a component on the page. */
5
7
  export type CompPath = string
6
8
 
9
+ /** Components the host marks as above the fold or not, keyed by component ID or component path. */
7
10
  export type IAboveTheFoldServiceConfig = {
8
11
  [key: CompId | CompPath]: boolean
9
12
  }
10
13
 
14
+ /**
15
+ * Checks whether a component on the current page is above the fold, meaning it's visible when the page
16
+ * first loads, before a site visitor scrolls.
17
+ */
11
18
  export interface IAboveTheFoldService {
19
+ /**
20
+ * Checks whether a component is above the fold on the current page.
21
+ *
22
+ * Use it to decide what to load or render first, such as loading an image right away when it's
23
+ * above the fold and lazily when it isn't.
24
+ *
25
+ * Returns `undefined` for a component that isn't marked, and never throws. Treat anything other than
26
+ * `true` as not above the fold.
27
+ *
28
+ * The result describes the page's initial layout, not where the site visitor has scrolled to. This
29
+ * value doesn't change for the lifetime of the page. The method does the same lookup while the page
30
+ * renders on the server and in the browser.
31
+ *
32
+ * The result is based on the page's layout, not on the site visitor's screen size, so use it to
33
+ * prioritize loading, not to tell whether a component is visible.
34
+ *
35
+ * @param key Component ID. Case-sensitive, and must match the component's ID exactly.
36
+ * @returns Whether the component is above the fold:
37
+ * - `true`: The host marks the component as above the fold.
38
+ * - `false`: The host marks the component as not above the fold.
39
+ * - `undefined`: The host doesn't mark the component.
40
+ *
41
+ * @exampleValue key 'comp-lxk2h8f1'
42
+ * @exampleValue true
43
+ *
44
+ * @example Load an image right away when it's above the fold
45
+ * ```tsx
46
+ * import { useService } from '@wix/services-manager-react'
47
+ * import { AboveTheFoldDefinition } from '@wix/site-service-above-the-fold/definition'
48
+ *
49
+ * function ProductImage({ id, src, alt }: { id: string; src: string; alt: string }) {
50
+ * const aboveTheFoldService = useService(AboveTheFoldDefinition)
51
+ * const isAboveTheFold = aboveTheFoldService.isInFirstFold(id) === true
52
+ *
53
+ * return (
54
+ * <img
55
+ * id={id}
56
+ * src={src}
57
+ * alt={alt}
58
+ * loading={isAboveTheFold ? 'eager' : 'lazy'}
59
+ * fetchPriority={isAboveTheFold ? 'high' : 'auto'}
60
+ * />
61
+ * )
62
+ * }
63
+ * ```
64
+ *
65
+ * @example Preload the images of components above the fold
66
+ * ```ts
67
+ * import { AboveTheFoldDefinition } from '@wix/site-service-above-the-fold/definition'
68
+ *
69
+ * const aboveTheFoldService = servicesManager.getService(AboveTheFoldDefinition)
70
+ *
71
+ * function imagesToPreload(images: { compId: string; src: string }[]) {
72
+ * return images
73
+ * .filter(({ compId }) => aboveTheFoldService.isInFirstFold(compId) === true)
74
+ * .map(({ src }) => src)
75
+ * }
76
+ * ```
77
+ */
12
78
  isInFirstFold: (key: CompId | CompPath) => boolean | undefined
13
79
  }
14
80