diff --git a/src/app/[countryCode]/(main)/products/[handle]/page.tsx b/src/app/[countryCode]/(main)/products/[handle]/page.tsx index d73803239..b48cda365 100644 --- a/src/app/[countryCode]/(main)/products/[handle]/page.tsx +++ b/src/app/[countryCode]/(main)/products/[handle]/page.tsx @@ -3,43 +3,45 @@ import { notFound } from "next/navigation" import ProductTemplate from "@modules/products/templates" import { getRegion, listRegions } from "@lib/data/regions" -import { getProductByHandle, getProductsList } from "@lib/data/products" +import { getProductByHandle } from "@lib/data/products" +import { sdk } from "@lib/config" type Props = { params: { countryCode: string; handle: string } } export async function generateStaticParams() { - const countryCodes = await listRegions().then( - (regions) => - regions - ?.map((r) => r.countries?.map((c) => c.iso_2)) - .flat() - .filter(Boolean) as string[] - ) + try { + const countryCodes = await listRegions().then((regions) => + regions?.map((r) => r.countries?.map((c) => c.iso_2)).flat() + ) - if (!countryCodes) { - return null - } + if (!countryCodes) { + return [] + } - const products = await Promise.all( - countryCodes.map((countryCode) => { - return getProductsList({ countryCode }) - }) - ).then((responses) => - responses.map(({ response }) => response.products).flat() - ) - - const staticParams = countryCodes - ?.map((countryCode) => - products.map((product) => ({ - countryCode, - handle: product.handle, - })) + const { products } = await sdk.store.product.list( + { fields: "handle" }, + { next: { tags: ["products"] } } ) - .flat() - return staticParams + return countryCodes + .map((countryCode) => + products.map((product) => ({ + countryCode, + handle: product.handle, + })) + ) + .flat() + .filter((param) => param.handle) + } catch (error) { + console.error( + `Failed to generate static paths for product pages: ${ + error instanceof Error ? error.message : "Unknown error" + }.` + ) + return [] + } } export async function generateMetadata({ params }: Props): Promise {