@scayle/storefront-product-detail 1.2.0 → 1.4.0

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/CHANGELOG.md CHANGED
@@ -1,5 +1,64 @@
1
1
  # @scayle/storefront-product-detail
2
2
 
3
+ ## 1.4.0
4
+
5
+ ### Minor Changes
6
+
7
+ - **\[PDP\]** Added `generateProductHreflangLinks` utils to generate hreflang links for a product for all shops.
8
+ - **\[PDP\]** Added RPC method `getAllShopProductsForId` and the composable `useAllShopProductsForId` for product detail page to fetch product for all shops. This is used to generate hreflang links for the product for all shops.
9
+
10
+ ## 1.3.0
11
+
12
+ ### Minor Changes
13
+
14
+ - **\[SEO\]** Moved the product image from the `ProductGroup` JSONLD to the `Product` JSONLD to better align with [Google's recommendation](https://developers.google.com/search/docs/appearance/structured-data/merchant-listing).
15
+
16
+ Instead of passing all product images to `useProductSeoData`, the primary product image is expected to be included in the passed `variants`.
17
+
18
+ ```ts
19
+ useProductSeoData({
20
+ name: 'Name',
21
+ brand: 'Brand',
22
+ productDescription: 'Description',
23
+ variants: variants.value.map((variant) => {
24
+ return generateProductSchema({
25
+ productName: 'Name',
26
+ variant,
27
+ url: `${$config.public.baseUrl}${route.fullPath}`,
28
+ size,
29
+ })
30
+ }),
31
+ images: images.value,
32
+ productId: product.value?.id || 0,
33
+ color: formatColors(colors.value),
34
+ variesBy: variants.value.length > 1
35
+ ? ['https://schema.org/size']
36
+ : undefined,
37
+ })
38
+
39
+ // Will become
40
+
41
+ useProductSeoData({
42
+ name: 'Name',
43
+ brand: 'Brand',
44
+ productDescription: 'Description',
45
+ variants: variants.value.map((variant) => {
46
+ return generateProductSchema({
47
+ productName: 'Name',
48
+ variant,
49
+ url: `${$config.public.baseUrl}${route.fullPath}`,
50
+ size,
51
+ images: images.value[0],
52
+ })
53
+ }),
54
+ productId: product.value?.id || 0,
55
+ color: formatColors(colors.value),
56
+ variesBy: variants.value.length > 1
57
+ ? ['https://schema.org/size']
58
+ : undefined,
59
+ })
60
+ ```
61
+
3
62
  ## 1.2.0
4
63
 
5
64
  ### Minor Changes
package/dist/module.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "@scayle/storefront-product-detail",
3
3
  "configKey": "product-detail",
4
- "version": "1.2.0",
4
+ "version": "1.4.0",
5
5
  "compatibility": {
6
6
  "bridge": false,
7
7
  "nuxt": ">=3.13"
8
8
  },
9
9
  "builder": {
10
10
  "@nuxt/module-builder": "1.0.1",
11
- "unbuild": "2.0.0"
11
+ "unbuild": "3.5.0"
12
12
  }
13
13
  }
package/dist/module.mjs CHANGED
@@ -1,7 +1,7 @@
1
- import { defineNuxtModule, createResolver, addImportsDir } from '@nuxt/kit';
1
+ import { defineNuxtModule, createResolver, resolvePath, addImportsDir } from '@nuxt/kit';
2
2
 
3
3
  const PACKAGE_NAME = "@scayle/storefront-product-detail";
4
- const PACKAGE_VERSION = "1.2.0";
4
+ const PACKAGE_VERSION = "1.4.0";
5
5
  const module = defineNuxtModule({
6
6
  meta: {
7
7
  name: PACKAGE_NAME,
@@ -17,6 +17,12 @@ const module = defineNuxtModule({
17
17
  const { resolve } = createResolver(import.meta.url);
18
18
  nuxt.options.alias["#storefront-product-detail"] = resolve("./runtime");
19
19
  nuxt.options.build.transpile.push("#storefront-product-detail/runtime");
20
+ nuxt.hook("storefront:custom-rpc:extend", async (customRpcs) => {
21
+ customRpcs.push({
22
+ source: await resolvePath("./runtime/rpc/methods/products"),
23
+ names: ["getAllShopProductsForId"]
24
+ });
25
+ });
20
26
  if (options.autoImports) {
21
27
  addImportsDir(resolve("./runtime/composables"));
22
28
  addImportsDir(resolve("./runtime/utils"));
@@ -0,0 +1,4 @@
1
+ import type { KeysOf, NormalizedRpcResponse, UseRpcReturn } from '@scayle/storefront-nuxt/composables';
2
+ import type { MaybeRefOrGetter } from 'vue';
3
+ import type { RpcMethodParameters } from '@scayle/storefront-nuxt';
4
+ export declare function useAllShopProductsById<DataT = NormalizedRpcResponse<'getAllShopProductsForId'>, PickKeys extends KeysOf<DataT> = KeysOf<DataT>, DefaultT = null>(params: MaybeRefOrGetter<RpcMethodParameters<'getAllShopProductsForId'>>, key?: string): UseRpcReturn<'getAllShopProductsForId', DataT, PickKeys, DefaultT>;
@@ -0,0 +1,8 @@
1
+ import { useRpc } from "@scayle/storefront-nuxt/composables";
2
+ export function useAllShopProductsById(params, key = "useAllShopProductsById") {
3
+ return useRpc(
4
+ "getAllShopProductsForId",
5
+ key,
6
+ params
7
+ );
8
+ }
@@ -16,8 +16,6 @@ interface ProductInfo {
16
16
  color: string;
17
17
  /** Array of product variants. */
18
18
  variants: WithContext<Product>[];
19
- /** Array of image URLs for the product. */
20
- images: string[];
21
19
  variesBy?: ProductGroup['variesBy'];
22
20
  }
23
21
  export interface UseProductSeoDataReturn {
@@ -19,7 +19,6 @@ export function useProductSeoData(breadcrumbs, urlParams, productInfo) {
19
19
  productId: info.productId.toString(),
20
20
  name: info.name,
21
21
  description: info.productDescription,
22
- images: info.images,
23
22
  brandName: info.brand,
24
23
  color: info.color,
25
24
  variants: info.variants,
@@ -0,0 +1,16 @@
1
+ import type { FetchProductParams, Product, RpcHandler, ShopConfig } from '@scayle/storefront-nuxt';
2
+ type ShopProduct = Pick<ShopConfig, 'locale' | 'path'> & {
3
+ product: Product;
4
+ };
5
+ /**
6
+ * Fetches all shop products for a given product ID.
7
+ *
8
+ * This function retrieves all products from all shops in the runtime configuration.
9
+ * It uses the SAPI client to fetch the product details for each shop.
10
+ *
11
+ * @param options - The parameters for fetching the product.
12
+ *
13
+ * @returns An array of objects containing the products for each shop.
14
+ */
15
+ export declare const getAllShopProductsForId: RpcHandler<Pick<FetchProductParams, 'id' | 'with'>, ShopProduct[]>;
16
+ export {};
@@ -0,0 +1,32 @@
1
+ export const getAllShopProductsForId = async function getAllShopProductsForId2(options, context) {
2
+ const { runtimeConfiguration, sapiClient, log, cached } = context;
3
+ const fetchAllProducts = async () => {
4
+ const products = [];
5
+ const shops = Object.values(
6
+ runtimeConfiguration.storefront.shops
7
+ );
8
+ await Promise.all(
9
+ shops.map(async (shop) => {
10
+ try {
11
+ const product = await sapiClient.clone({ shopId: shop.shopId }).products.getById(options.id, {
12
+ with: options.with
13
+ });
14
+ products.push({
15
+ locale: shop.locale,
16
+ path: Array.isArray(shop.path) ? shop.path[0] : shop.path,
17
+ product
18
+ });
19
+ } catch (error) {
20
+ log.error(error instanceof Error ? error : String(error));
21
+ }
22
+ })
23
+ );
24
+ return products;
25
+ };
26
+ const result = await cached(fetchAllProducts, {
27
+ cacheKeyPrefix: `getAllShopProductsForId-${options.id}`,
28
+ ttl: 12 * 60 * 60
29
+ // 12 hours
30
+ })();
31
+ return result;
32
+ };
@@ -1,26 +1,65 @@
1
- import type { Variant } from '@scayle/storefront-nuxt';
1
+ import type { ShopConfig, Variant } from '@scayle/storefront-nuxt';
2
2
  import type { Product, WithContext, ProductGroup } from 'schema-dts';
3
+ import type { MaybeRefOrGetter } from 'vue';
4
+ type ShopProductHref = Pick<ShopConfig, 'locale' | 'path'> & {
5
+ productHref: string;
6
+ };
3
7
  /**
4
8
  * Generates an object following the schema.org `Product` type with context.
5
9
  *
10
+ * @param params - Options for generating the schema product.
11
+ * @param params.productName - Name of the product.
12
+ * @param params.variant - Variant of the product.
13
+ * @param params.url - URL of the product.
14
+ * @param params.image - URL of the product image.
15
+ * @param params.size - Size of the product.
16
+ *
6
17
  * @see https://schema.org/Product Used to specify `@type` of the returned schema.
7
18
  * @see https://schema.org/Brand Used to define schema of `brand`.
8
19
  *
9
20
  * @returns An object following the schema.org `Product` type with context.
10
21
  */
11
- export declare const generateProductSchema: ({ productName, variant, url, size, }: {
22
+ export declare const generateProductSchema: ({ productName, variant, url, image, size, }: {
12
23
  productName: string;
13
24
  variant: Variant;
14
25
  url: string;
26
+ image: string;
15
27
  size?: string;
16
28
  }) => WithContext<Product>;
17
- export declare const generateProductGroupSchema: ({ productId, name, description, images, brandName, color, variants, variesBy, }: {
29
+ /**
30
+ * Generates an object following the schema.org `ProductGroup` type with context.
31
+ *
32
+ * @param params - Options for generating the schema product group.
33
+ * @param params.productId - ID of the product group.
34
+ * @param params.name - Name of the product group.
35
+ * @param params.description - Description of the product group.
36
+ * @param params.brandName - Name of the brand.
37
+ * @param params.color - Color of the product group.
38
+ * @param params.variants - Variants of the product group.
39
+ * @param params.variesBy - Varies by of the product group.
40
+ *
41
+ * @returns An object following the schema.org `ProductGroup` type with context.
42
+ */
43
+ export declare const generateProductGroupSchema: ({ productId, name, description, brandName, color, variants, variesBy, }: {
18
44
  productId: string;
19
45
  name: string;
20
46
  description: string;
21
- images: string[];
22
47
  brandName: string;
23
48
  color: string;
24
49
  variants: Product[];
25
50
  variesBy?: ProductGroup["variesBy"];
26
51
  }) => WithContext<ProductGroup>;
52
+ /**
53
+ * Generates hreflang links for a product.
54
+ *
55
+ * @param _productsForAllShops - A list of product links for all shops.
56
+ * @param defaultLocale - The default locale of the store.
57
+ *
58
+ * @returns An array of hreflang links.
59
+ */
60
+ export declare const generateProductHreflangLinks: (_productsForAllShops: MaybeRefOrGetter<ShopProductHref[]>, defaultLocale: string) => {
61
+ rel: string;
62
+ hreflang: string;
63
+ href: string;
64
+ }[];
65
+ export {};
@@ -1,4 +1,5 @@
1
1
  import { isInStock } from "@scayle/storefront-nuxt";
2
+ import { toValue } from "vue";
2
3
  const generateSchemaProductOffer = ({
3
4
  variant,
4
5
  baseUrl
@@ -20,6 +21,7 @@ export const generateProductSchema = ({
20
21
  productName,
21
22
  variant,
22
23
  url,
24
+ image,
23
25
  size
24
26
  }) => {
25
27
  return {
@@ -27,14 +29,14 @@ export const generateProductSchema = ({
27
29
  "@type": "Product",
28
30
  name: `${productName} (${size})`,
29
31
  size,
30
- offers: generateSchemaProductOffer({ variant, baseUrl: url })
32
+ offers: generateSchemaProductOffer({ variant, baseUrl: url }),
33
+ image
31
34
  };
32
35
  };
33
36
  export const generateProductGroupSchema = ({
34
37
  productId,
35
38
  name,
36
39
  description,
37
- images,
38
40
  brandName,
39
41
  color,
40
42
  variants,
@@ -46,7 +48,6 @@ export const generateProductGroupSchema = ({
46
48
  productGroupID: productId,
47
49
  name: `${name}, ${color}`,
48
50
  description,
49
- image: images,
50
51
  brand: {
51
52
  "@type": "Brand",
52
53
  name: brandName
@@ -56,3 +57,26 @@ export const generateProductGroupSchema = ({
56
57
  ...variesBy ? { variesBy } : {}
57
58
  };
58
59
  };
60
+ export const generateProductHreflangLinks = (_productsForAllShops, defaultLocale) => {
61
+ const productsForAllShops = toValue(_productsForAllShops);
62
+ if (!productsForAllShops) {
63
+ return [];
64
+ }
65
+ return productsForAllShops.flatMap(({ productHref, path, locale }) => {
66
+ const links = [
67
+ {
68
+ rel: "alternate",
69
+ hreflang: locale,
70
+ href: productHref
71
+ }
72
+ ];
73
+ if (path === defaultLocale) {
74
+ links.push({
75
+ rel: "alternate",
76
+ hreflang: "x-default",
77
+ href: productHref
78
+ });
79
+ }
80
+ return links;
81
+ });
82
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@scayle/storefront-product-detail",
3
- "version": "1.2.0",
3
+ "version": "1.4.0",
4
4
  "description": "Collection of essential composables and utilities to work with product detail",
5
5
  "author": "SCAYLE Commerce Engine",
6
6
  "license": "MIT",
@@ -31,15 +31,15 @@
31
31
  "build": "nuxt-module-build build",
32
32
  "dev": "nuxi dev playground",
33
33
  "dev:build": "nuxi build playground",
34
- "prep": "nuxt-module-build build --stub && nuxt-module-build prepare && nuxi prepare playground",
34
+ "prep": "nuxt-module-build prepare && nuxi prepare playground",
35
35
  "lint": "eslint .",
36
36
  "lint:ci": "eslint . --format gitlab",
37
37
  "lint:fix": "eslint . --fix",
38
38
  "format": "dprint check",
39
39
  "format:fix": "dprint fmt",
40
- "test": "vitest --run --passWithNoTests",
41
- "test:watch": "vitest --passWithNoTests",
42
- "test:ci": "vitest --run --passWithNoTests --coverage --reporter=default --reporter=junit --outputFile=./coverage/junit.xml",
40
+ "test": "vitest --run",
41
+ "test:watch": "vitest",
42
+ "test:ci": "vitest --run --coverage --reporter=default --reporter=junit --outputFile=./coverage/junit.xml",
43
43
  "typecheck": "vue-tsc --noEmit && cd playground && vue-tsc --noEmit",
44
44
  "package:lint": "publint",
45
45
  "verify-packaging": "attw --pack . --profile esm-only"
@@ -54,28 +54,28 @@
54
54
  "vue": "^3.5.13"
55
55
  },
56
56
  "devDependencies": {
57
- "@arethetypeswrong/cli": "0.17.4",
58
- "@nuxt/kit": "3.15.4",
57
+ "@arethetypeswrong/cli": "0.18.1",
58
+ "@nuxt/kit": "3.16.2",
59
59
  "@nuxt/module-builder": "1.0.1",
60
- "@nuxt/schema": "3.15.4",
61
- "@nuxt/test-utils": "3.17.2",
62
- "@scayle/eslint-config-storefront": "4.5.0",
63
- "@scayle/storefront-nuxt": "8.24.0",
64
- "@types/node": "22.14.1",
60
+ "@nuxt/schema": "3.16.2",
61
+ "@nuxt/test-utils": "3.18.0",
62
+ "@scayle/eslint-config-storefront": "4.5.2",
63
+ "@scayle/storefront-nuxt": "8.27.0",
64
+ "@types/node": "22.15.21",
65
65
  "@vue/test-utils": "2.4.6",
66
- "@vueuse/core": "13.1.0",
67
- "dprint": "0.49.1",
68
- "eslint-formatter-gitlab": "5.1.0",
69
- "eslint": "9.24.0",
70
- "happy-dom": "17.4.4",
71
- "nuxt": "3.15.4",
66
+ "@vueuse/core": "13.2.0",
67
+ "dprint": "0.50.0",
68
+ "eslint-formatter-gitlab": "6.0.0",
69
+ "eslint": "9.27.0",
70
+ "happy-dom": "17.4.7",
71
+ "nuxt": "3.16.2",
72
72
  "publint": "0.2.12",
73
73
  "schema-dts": "1.1.5",
74
74
  "typescript": "5.8.3",
75
- "unbuild": "2.0.0",
76
- "vitest": "3.1.1",
77
- "vue-router": "4.5.0",
78
- "vue-tsc": "2.2.8",
79
- "vue": "3.5.13"
75
+ "unbuild": "3.5.0",
76
+ "vitest": "3.1.4",
77
+ "vue-router": "4.5.1",
78
+ "vue-tsc": "2.2.10",
79
+ "vue": "3.5.14"
80
80
  }
81
81
  }