Skip to content

Commit 0058c52

Browse files
authored
Merge pull request #393 from Mohmdev/fix/product-static-params
Fix: Optimize product page static generation
2 parents 34dfe7c + 1ba0d8d commit 0058c52

File tree

1 file changed

+29
-27
lines changed
  • src/app/[countryCode]/(main)/products/[handle]

1 file changed

+29
-27
lines changed

src/app/[countryCode]/(main)/products/[handle]/page.tsx

+29-27
Original file line numberDiff line numberDiff line change
@@ -3,43 +3,45 @@ import { notFound } from "next/navigation"
33

44
import ProductTemplate from "@modules/products/templates"
55
import { getRegion, listRegions } from "@lib/data/regions"
6-
import { getProductByHandle, getProductsList } from "@lib/data/products"
6+
import { getProductByHandle } from "@lib/data/products"
7+
import { sdk } from "@lib/config"
78

89
type Props = {
910
params: { countryCode: string; handle: string }
1011
}
1112

1213
export async function generateStaticParams() {
13-
const countryCodes = await listRegions().then(
14-
(regions) =>
15-
regions
16-
?.map((r) => r.countries?.map((c) => c.iso_2))
17-
.flat()
18-
.filter(Boolean) as string[]
19-
)
14+
try {
15+
const countryCodes = await listRegions().then((regions) =>
16+
regions?.map((r) => r.countries?.map((c) => c.iso_2)).flat()
17+
)
2018

21-
if (!countryCodes) {
22-
return null
23-
}
19+
if (!countryCodes) {
20+
return []
21+
}
2422

25-
const products = await Promise.all(
26-
countryCodes.map((countryCode) => {
27-
return getProductsList({ countryCode })
28-
})
29-
).then((responses) =>
30-
responses.map(({ response }) => response.products).flat()
31-
)
32-
33-
const staticParams = countryCodes
34-
?.map((countryCode) =>
35-
products.map((product) => ({
36-
countryCode,
37-
handle: product.handle,
38-
}))
23+
const { products } = await sdk.store.product.list(
24+
{ fields: "handle" },
25+
{ next: { tags: ["products"] } }
3926
)
40-
.flat()
4127

42-
return staticParams
28+
return countryCodes
29+
.map((countryCode) =>
30+
products.map((product) => ({
31+
countryCode,
32+
handle: product.handle,
33+
}))
34+
)
35+
.flat()
36+
.filter((param) => param.handle)
37+
} catch (error) {
38+
console.error(
39+
`Failed to generate static paths for product pages: ${
40+
error instanceof Error ? error.message : "Unknown error"
41+
}.`
42+
)
43+
return []
44+
}
4345
}
4446

4547
export async function generateMetadata({ params }: Props): Promise<Metadata> {

0 commit comments

Comments
 (0)