mirror of
https://github.com/vercel/commerce.git
synced 2025-07-23 04:36:49 +00:00
🙋 test getStaticProps with revalidate
This commit is contained in:
@@ -1,19 +1,29 @@
|
|||||||
|
import commerce from '@lib/api/commerce';
|
||||||
|
import { GetStaticPropsContext } from 'next';
|
||||||
import { Layout } from 'src/components/common';
|
import { Layout } from 'src/components/common';
|
||||||
import { FeaturedProductsCarousel, HomeBanner, HomeCategories, HomeCollection, HomeCTA, HomeFeature, HomeRecipe, HomeSubscribe, HomeVideo } from 'src/components/modules/home';
|
import { FeaturedProductsCarousel, HomeBanner, HomeCategories, HomeCollection, HomeCTA, HomeFeature, HomeRecipe, HomeSubscribe, HomeVideo } from 'src/components/modules/home';
|
||||||
import HomeSpice from 'src/components/modules/home/HomeSpice/HomeSpice';
|
import HomeSpice from 'src/components/modules/home/HomeSpice/HomeSpice';
|
||||||
|
|
||||||
export default function Home() {
|
interface Props {
|
||||||
|
products: any
|
||||||
|
}
|
||||||
|
export default function Home({ products }: Props) {
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
|
<p>
|
||||||
|
TOTAL: {products?.length}
|
||||||
|
</p>
|
||||||
|
|
||||||
|
{JSON.stringify(products[0])}
|
||||||
<HomeBanner />
|
<HomeBanner />
|
||||||
<HomeFeature />
|
<HomeFeature />
|
||||||
<HomeCategories />
|
<HomeCategories />
|
||||||
<HomeCollection />
|
<HomeCollection />
|
||||||
<HomeVideo />
|
<HomeVideo />
|
||||||
<HomeSpice/>
|
<HomeSpice />
|
||||||
<FeaturedProductsCarousel/>
|
<FeaturedProductsCarousel />
|
||||||
<HomeCTA />
|
<HomeCTA />
|
||||||
<HomeRecipe />
|
<HomeRecipe />
|
||||||
<HomeSubscribe />
|
<HomeSubscribe />
|
||||||
|
|
||||||
{/* // todo: uncomment
|
{/* // todo: uncomment
|
||||||
@@ -22,4 +32,37 @@ export default function Home() {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
export async function getStaticProps({
|
||||||
|
preview,
|
||||||
|
locale,
|
||||||
|
locales,
|
||||||
|
}: GetStaticPropsContext) {
|
||||||
|
const config = { locale, locales }
|
||||||
|
const productsPromise = commerce.getAllProducts({
|
||||||
|
// const productsPromise = commerce.getAllFacets({
|
||||||
|
variables: {
|
||||||
|
first: 70,
|
||||||
|
// filter: {
|
||||||
|
// name: {
|
||||||
|
// contains: 'ca'
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
},
|
||||||
|
config,
|
||||||
|
preview,
|
||||||
|
// Saleor provider only
|
||||||
|
...({ featured: true } as any),
|
||||||
|
})
|
||||||
|
|
||||||
|
const { products } = await productsPromise
|
||||||
|
|
||||||
|
|
||||||
|
return {
|
||||||
|
props: { products },
|
||||||
|
revalidate: 20,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
Home.Layout = Layout
|
Home.Layout = Layout
|
||||||
|
@@ -1,42 +1,51 @@
|
|||||||
import { QueryFacetsArgs } from '@framework/schema'
|
import commerce from '@lib/api/commerce';
|
||||||
import { useMemo, useState } from 'react'
|
import { GetStaticPropsContext } from 'next';
|
||||||
import { Layout } from 'src/components/common'
|
import { Layout } from 'src/components/common';
|
||||||
import { useFacets } from 'src/components/hooks/facets'
|
|
||||||
|
|
||||||
export default function Test() {
|
|
||||||
const [keyword, setKeyword] = useState('c')
|
|
||||||
|
|
||||||
const optionsFilter = useMemo(() => {
|
|
||||||
console.log("change options")
|
|
||||||
return {
|
|
||||||
options: {
|
|
||||||
filter: {
|
|
||||||
name: {
|
|
||||||
contains: keyword
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} as QueryFacetsArgs
|
|
||||||
}, [keyword])
|
|
||||||
|
|
||||||
const { items, totalItems } = useFacets(optionsFilter)
|
|
||||||
|
|
||||||
const changeQuery = () => {
|
|
||||||
setKeyword('ca')
|
|
||||||
}
|
|
||||||
|
|
||||||
|
interface Props {
|
||||||
|
products: any
|
||||||
|
}
|
||||||
|
export default function Home({ products }: Props) {
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<div>Lorem ipsum dolor sit amet consectetur adipisicing elit. Inventore, praesentium.</div>
|
<p>
|
||||||
<div>
|
TOTAL: {products?.length}
|
||||||
total: {totalItems}
|
</p>
|
||||||
</div>
|
{JSON.stringify(products[0])}
|
||||||
<div>
|
|
||||||
ITEMS: {JSON.stringify(items)}
|
|
||||||
</div>
|
|
||||||
<button onClick={changeQuery}>change</button>
|
|
||||||
</>
|
</>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
Test.Layout = Layout
|
|
||||||
|
export async function getServerSideProps({
|
||||||
|
preview,
|
||||||
|
locale,
|
||||||
|
locales,
|
||||||
|
}: GetStaticPropsContext) {
|
||||||
|
const config = { locale, locales }
|
||||||
|
const productsPromise = commerce.getAllProducts({
|
||||||
|
// const productsPromise = commerce.getAllFacets({
|
||||||
|
variables: {
|
||||||
|
first: 70,
|
||||||
|
// filter: {
|
||||||
|
// name: {
|
||||||
|
// contains: 'ca'
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
},
|
||||||
|
config,
|
||||||
|
preview,
|
||||||
|
// Saleor provider only
|
||||||
|
...({ featured: true } as any),
|
||||||
|
})
|
||||||
|
|
||||||
|
const { products } = await productsPromise
|
||||||
|
|
||||||
|
|
||||||
|
return {
|
||||||
|
props: { products },
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Home.Layout = Layout
|
||||||
|
Reference in New Issue
Block a user