@@ -3,43 +3,45 @@ import { notFound } from "next/navigation"
3
3
4
4
import ProductTemplate from "@modules/products/templates"
5
5
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"
7
8
8
9
type Props = {
9
10
params : { countryCode : string ; handle : string }
10
11
}
11
12
12
13
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
+ )
20
18
21
- if ( ! countryCodes ) {
22
- return null
23
- }
19
+ if ( ! countryCodes ) {
20
+ return [ ]
21
+ }
24
22
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" ] } }
39
26
)
40
- . flat ( )
41
27
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
+ }
43
45
}
44
46
45
47
export async function generateMetadata ( { params } : Props ) : Promise < Metadata > {
0 commit comments