From 667ef16d54b161283a14f52139f2c144d7d035d2 Mon Sep 17 00:00:00 2001 From: Marcos Candeia Date: Wed, 17 May 2023 12:47:36 -0300 Subject: [PATCH] Fix shopify loader (#73) Signed-off-by: Marcos Candeia --- commerce/shopify/transform.ts | 10 ++++++---- functions/shopifyProductDetailsPage.ts | 2 +- functions/shopifyProductList.ts | 2 +- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/commerce/shopify/transform.ts b/commerce/shopify/transform.ts index f4aafcfa..6670d501 100644 --- a/commerce/shopify/transform.ts +++ b/commerce/shopify/transform.ts @@ -39,6 +39,7 @@ const nonEmptyArray = (array: T[] | null | undefined) => export const toProductPage = ( product: ProductShopify, + url: URL, maybeSkuId?: number, ): ProductDetailsPage => { const skuId = maybeSkuId @@ -53,7 +54,7 @@ export const toProductPage = ( return { "@type": "ProductDetailsPage", breadcrumbList: toBreadcrumbList(product, sku), - product: toProduct(product, sku), + product: toProduct(product, sku, url), }; }; @@ -76,6 +77,7 @@ export const toBreadcrumbList = ( export const toProduct = ( product: ProductShopify, sku: SkuShopify, + url: URL, level = 0, // prevent inifinte loop while self referencing the product ): Product => { const { @@ -100,7 +102,7 @@ export const toProduct = ( const additionalProperty = selectedOptions.map(toPropertyValue); const allImages = nonEmptyArray([image, ...images.nodes]) ?? [DEFAULT_IMAGE]; const hasVariant = level < 1 && - variants.nodes.map((variant) => toProduct(product, variant, 1)); + variants.nodes.map((variant) => toProduct(product, variant, url, 1)); const priceSpec: UnitPriceSpecification[] = [{ "@type": "UnitPriceSpecification", priceType: "https://schema.org/SalePrice", @@ -118,7 +120,7 @@ export const toProduct = ( return { "@type": "Product", productID, - url: getPath(product, sku), + url: `${url.host}${getPath(product, sku)}`, name: sku.title, description, sku: productID, @@ -130,7 +132,7 @@ export const toProduct = ( "@type": "ProductGroup", productGroupID, hasVariant: hasVariant || [], - url: getPath(product), + url: `${url.host}${getPath(product)}`, name: product.title, additionalProperty: [], }, diff --git a/functions/shopifyProductDetailsPage.ts b/functions/shopifyProductDetailsPage.ts index 8efc6221..a0ddbb5a 100644 --- a/functions/shopifyProductDetailsPage.ts +++ b/functions/shopifyProductDetailsPage.ts @@ -36,7 +36,7 @@ const productPageLoader: LoaderFunction< }; } - const product = toProductPage(data.product, maybeSkuId); + const product = toProductPage(data.product, new URL(_req.url), maybeSkuId); return { data: product }; }; diff --git a/functions/shopifyProductList.ts b/functions/shopifyProductList.ts index a8d55cad..62725b14 100644 --- a/functions/shopifyProductList.ts +++ b/functions/shopifyProductList.ts @@ -41,7 +41,7 @@ const searchLoader: LoaderFunction< // If a property is missing from the final `products` array you can add // it in here const products = data?.products.nodes.map((p) => - toProduct(p, p.variants.nodes[0]) + toProduct(p, p.variants.nodes[0], new URL(_req.url)) ); return {