@@ -3,43 +3,45 @@ import { notFound } from "next/navigation"
33
44import ProductTemplate from "@modules/products/templates"
55import { 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
89type Props = {
910 params : { countryCode : string ; handle : string }
1011}
1112
1213export 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
4547export async function generateMetadata ( { params } : Props ) : Promise < Metadata > {
0 commit comments