Skip to content

Commit

Permalink
Merge pull request #393 from Mohmdev/fix/product-static-params
Browse files Browse the repository at this point in the history
Fix: Optimize product page static generation
  • Loading branch information
VariableVic authored Nov 12, 2024
2 parents 34dfe7c + 1ba0d8d commit 0058c52
Showing 1 changed file with 29 additions and 27 deletions.
56 changes: 29 additions & 27 deletions src/app/[countryCode]/(main)/products/[handle]/page.tsx
Original file line number Diff line number Diff line change
@@ -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<Metadata> {

0 comments on commit 0058c52

Please sign in to comment.