mirror of
https://github.com/vercel/commerce.git
synced 2025-07-23 04:36:49 +00:00
✨ feat: getStaticProps in product detail
:%s
This commit is contained in:
@@ -1,12 +1,14 @@
|
|||||||
|
|
||||||
import { Product } from '@framework/schema'
|
|
||||||
import commerce from '@lib/api/commerce'
|
import commerce from '@lib/api/commerce'
|
||||||
import { GetStaticPathsContext, GetStaticPropsContext, InferGetStaticPropsType } from 'next'
|
import { GetStaticPathsContext, GetStaticPropsContext, InferGetStaticPropsType } from 'next'
|
||||||
import { Layout, RecipeDetail, RecommendedRecipes, RelevantBlogPosts } from 'src/components/common'
|
import { Layout, RecipeDetail, RecommendedRecipes, RelevantBlogPosts } from 'src/components/common'
|
||||||
import { ProductInfoDetail, ReleventProducts, ViewedProducts } from 'src/components/modules/product-detail'
|
import { ProductInfoDetail, ReleventProducts, ViewedProducts } from 'src/components/modules/product-detail'
|
||||||
|
import { REVALIDATE_TIME } from 'src/utils/constanst.utils'
|
||||||
import { BLOGS_DATA_TEST, INGREDIENT_DATA_TEST, RECIPE_DATA_TEST } from 'src/utils/demo-data'
|
import { BLOGS_DATA_TEST, INGREDIENT_DATA_TEST, RECIPE_DATA_TEST } from 'src/utils/demo-data'
|
||||||
|
import { getAllPromies } from 'src/utils/funtion.utils'
|
||||||
|
import { PromiseWithKey } from 'src/utils/types.utils'
|
||||||
|
|
||||||
export default function Slug({ product } : InferGetStaticPropsType<typeof getStaticProps>) {
|
export default function Slug({ product }: InferGetStaticPropsType<typeof getStaticProps>) {
|
||||||
console.log("product: ", product)
|
console.log("product: ", product)
|
||||||
|
|
||||||
return <>
|
return <>
|
||||||
@@ -26,26 +28,31 @@ export async function getStaticProps({
|
|||||||
preview,
|
preview,
|
||||||
}: GetStaticPropsContext<{ slug: string }>) {
|
}: GetStaticPropsContext<{ slug: string }>) {
|
||||||
const config = { locale, locales }
|
const config = { locale, locales }
|
||||||
|
let promisesWithKey = [] as PromiseWithKey[]
|
||||||
|
let props = {} as any
|
||||||
|
|
||||||
const productPromise = commerce.getProduct({
|
const productPromise = commerce.getProduct({
|
||||||
variables: { slug: params!.slug },
|
variables: { slug: params!.slug },
|
||||||
config,
|
config,
|
||||||
preview,
|
preview,
|
||||||
})
|
})
|
||||||
|
promisesWithKey.push({ key: 'product', promise: productPromise, keyResult: 'product' })
|
||||||
|
|
||||||
console.log('slug: ', params!.slug)
|
try {
|
||||||
|
const promises = getAllPromies(promisesWithKey)
|
||||||
|
const rs = await Promise.all(promises)
|
||||||
|
|
||||||
|
promisesWithKey.map((item, index) => {
|
||||||
|
props[item.key] = item.keyResult ? rs[index][item.keyResult] : rs[index]
|
||||||
|
return null
|
||||||
|
})
|
||||||
|
|
||||||
const { product } = await productPromise
|
return {
|
||||||
|
props,
|
||||||
|
revalidate: REVALIDATE_TIME,
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
|
||||||
if (!product) {
|
|
||||||
throw new Error(`Product with slug '${params!.slug}' not found`)
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
|
||||||
props: {
|
|
||||||
product,
|
|
||||||
},
|
|
||||||
revalidate: 60,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -56,12 +63,12 @@ export async function getStaticPaths({ locales }: GetStaticPathsContext) {
|
|||||||
return {
|
return {
|
||||||
paths: locales
|
paths: locales
|
||||||
? locales.reduce<string[]>((arr, locale) => {
|
? locales.reduce<string[]>((arr, locale) => {
|
||||||
// Add a product path for every locale
|
// Add a product path for every locale
|
||||||
products.forEach((product: any) => {
|
products.forEach((product: any) => {
|
||||||
arr.push(`/${locale}/product${product.path}`)
|
arr.push(`/${locale}/product${product.path}`)
|
||||||
})
|
})
|
||||||
return arr
|
return arr
|
||||||
}, [])
|
}, [])
|
||||||
: products.map((product: any) => `/product${product.path}`),
|
: products.map((product: any) => `/product${product.path}`),
|
||||||
fallback: 'blocking',
|
fallback: 'blocking',
|
||||||
}
|
}
|
||||||
|
@@ -5,7 +5,7 @@ import { GetStaticPropsContext } from 'next';
|
|||||||
import { Layout } from 'src/components/common';
|
import { Layout } from 'src/components/common';
|
||||||
import { ViewedProducts } from 'src/components/modules/product-detail';
|
import { ViewedProducts } from 'src/components/modules/product-detail';
|
||||||
import ProductListFilter from 'src/components/modules/product-list/ProductListFilter/ProductListFilter';
|
import ProductListFilter from 'src/components/modules/product-list/ProductListFilter/ProductListFilter';
|
||||||
import { CODE_FACET_BRAND, CODE_FACET_FEATURED, DEFAULT_PAGE_SIZE } from 'src/utils/constanst.utils';
|
import { CODE_FACET_BRAND, CODE_FACET_FEATURED, DEFAULT_PAGE_SIZE, REVALIDATE_TIME } from 'src/utils/constanst.utils';
|
||||||
import { getAllPromies } from 'src/utils/funtion.utils';
|
import { getAllPromies } from 'src/utils/funtion.utils';
|
||||||
import { PromiseWithKey, SortOrder } from 'src/utils/types.utils';
|
import { PromiseWithKey, SortOrder } from 'src/utils/types.utils';
|
||||||
import ProductListBanner from '../src/components/modules/product-list/ProductListBanner/ProductListBanner';
|
import ProductListBanner from '../src/components/modules/product-list/ProductListBanner/ProductListBanner';
|
||||||
@@ -87,7 +87,7 @@ export async function getStaticProps({
|
|||||||
|
|
||||||
return {
|
return {
|
||||||
props,
|
props,
|
||||||
revalidate: 60,
|
revalidate: REVALIDATE_TIME,
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
|
||||||
|
@@ -1,5 +1,6 @@
|
|||||||
import DefaultImg from '../../public/assets/images/default_img.jpg'
|
import DefaultImg from '../../public/assets/images/default_img.jpg'
|
||||||
|
|
||||||
|
export const REVALIDATE_TIME = 60
|
||||||
export const BLUR_DATA_IMG = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mN8fBIAApUBruKYvzsAAAAASUVORK5CYII='
|
export const BLUR_DATA_IMG = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mN8fBIAApUBruKYvzsAAAAASUVORK5CYII='
|
||||||
export const DEFAULT_IMG = DefaultImg
|
export const DEFAULT_IMG = DefaultImg
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user