4
0
forked from crowetic/commerce

Removing heavy comp from the client

This commit is contained in:
Belen Curcio 2020-11-06 11:43:11 -03:00
parent c419c3f361
commit b9cbf4f97f
3 changed files with 76 additions and 65 deletions

View File

@ -42,7 +42,7 @@ const Navbar: FC<Props> = ({ className }) => {
</div> </div>
<div className="flex pb-4 lg:px-6 lg:hidden"> <div className="flex pb-4 lg:px-6 lg:hidden">
<Searchbar /> <Searchbar id="mobileSearch" />
</div> </div>
</div> </div>
) )

View File

@ -1,4 +1,4 @@
import { FC, useEffect } from 'react' import { FC, useEffect, useMemo } from 'react'
import cn from 'classnames' import cn from 'classnames'
import s from './Searchbar.module.css' import s from './Searchbar.module.css'
import { useRouter } from 'next/router' import { useRouter } from 'next/router'
@ -15,7 +15,8 @@ const Searchbar: FC<Props> = ({ className, id = 'search' }) => {
router.prefetch('/search') router.prefetch('/search')
}, []) }, [])
return ( return useMemo(
() => (
<div <div
className={cn( className={cn(
'relative text-sm bg-accents-1 text-base w-full transition-colors duration-150', 'relative text-sm bg-accents-1 text-base w-full transition-colors duration-150',
@ -57,6 +58,8 @@ const Searchbar: FC<Props> = ({ className, id = 'search' }) => {
</svg> </svg>
</div> </div>
</div> </div>
),
[]
) )
} }

View File

@ -17,47 +17,33 @@ export async function getStaticProps({
}: GetStaticPropsContext) { }: GetStaticPropsContext) {
const config = getConfig({ locale }) const config = getConfig({ locale })
// Get Featured Products
const { products: featuredProducts } = await getAllProducts({ const { products: featuredProducts } = await getAllProducts({
variables: { field: 'featuredProducts', first: 6 }, variables: { field: 'featuredProducts', first: 6 },
config, config,
preview, preview,
}) })
// Get Best Selling Products
const { products: bestSellingProducts } = await getAllProducts({ const { products: bestSellingProducts } = await getAllProducts({
variables: { field: 'bestSellingProducts', first: 6 }, variables: { field: 'bestSellingProducts', first: 6 },
config, config,
preview, preview,
}) })
// Get Best Newest Products
const { products: newestProducts } = await getAllProducts({ const { products: newestProducts } = await getAllProducts({
variables: { field: 'newestProducts', first: 12 }, variables: { field: 'newestProducts', first: 12 },
config, config,
preview, preview,
}) })
const { categories, brands } = await getSiteInfo({ config, preview }) const { categories, brands } = await getSiteInfo({ config, preview })
const { pages } = await getAllPages({ config, preview }) const { pages } = await getAllPages({ config, preview })
return { // These are the products that are going to be displayed in the landing.
props: { // We prefer to do the computation at buildtime/servertime
featuredProducts, const { featured, bestSelling } = (() => {
bestSellingProducts,
newestProducts,
categories,
brands,
pages,
},
revalidate: 10,
}
}
const nonNullable = (v: any) => v
export default function Home({
featuredProducts,
bestSellingProducts,
newestProducts,
categories,
brands,
}: InferGetStaticPropsType<typeof getStaticProps>) {
const { featured, bestSelling } = useMemo(() => {
// Create a copy of products that we can mutate // Create a copy of products that we can mutate
const products = [...newestProducts] const products = [...newestProducts]
// If the lists of featured and best selling products don't have enough // If the lists of featured and best selling products don't have enough
@ -73,8 +59,30 @@ export default function Home({
(i) => bestSellingProducts[i] ?? products.shift() (i) => bestSellingProducts[i] ?? products.shift()
).filter(nonNullable), ).filter(nonNullable),
} }
}, [newestProducts, featuredProducts, bestSellingProducts]) })()
return {
props: {
featured,
bestSelling,
newestProducts,
categories,
brands,
pages,
},
revalidate: 10,
}
}
const nonNullable = (v: any) => v
export default function Home({
featured,
bestSelling,
brands,
categories,
newestProducts,
}: InferGetStaticPropsType<typeof getStaticProps>) {
return ( return (
<div> <div>
<Grid> <Grid>