Skip to content

Commit

Permalink
17.7 SSR + SWR
Browse files Browse the repository at this point in the history
  • Loading branch information
serranoarevalo committed Feb 17, 2022
1 parent 6443d3e commit ea024d1
Showing 1 changed file with 34 additions and 14 deletions.
48 changes: 34 additions & 14 deletions pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Item from "@components/item";
import Layout from "@components/layout";
import useUser from "@libs/client/useUser";
import Head from "next/head";
import useSWR from "swr";
import useSWR, { SWRConfig } from "swr";
import { Product } from "@prisma/client";
import client from "@libs/server/client";

Expand All @@ -19,25 +19,27 @@ interface ProductsResponse {
products: ProductWithCount[];
}

const Home: NextPage<{ products: ProductWithCount[] }> = ({ products }) => {
const Home: NextPage = () => {
const { user, isLoading } = useUser();
// const { data } = useSWR<ProductsResponse>("/api/products");
const { data } = useSWR<ProductsResponse>("/api/products");
return (
<Layout title="홈" hasTabBar>
<Head>
<title>Home</title>
</Head>
<div className="flex flex-col space-y-5 divide-y">
{products?.map((product) => (
<Item
id={product.id}
key={product.id}
title={product.name}
price={product.price}
hearts={product._count?.favs}
image={product.image}
/>
))}
{data
? data?.products?.map((product) => (
<Item
id={product.id}
key={product.id}
title={product.name}
price={product.price}
hearts={product._count?.favs || 0}
image={product.image}
/>
))
: "Loading..."}
<FloatingButton href="/products/upload">
<svg
className="h-6 w-6"
Expand All @@ -60,7 +62,25 @@ const Home: NextPage<{ products: ProductWithCount[] }> = ({ products }) => {
);
};

const Page: NextPage<{ products: ProductWithCount[] }> = ({ products }) => {
return (
<SWRConfig
value={{
fallback: {
"/api/products": {
ok: true,
products,
},
},
}}
>
<Home />
</SWRConfig>
);
};

export async function getServerSideProps() {
console.log("SSR");
const products = await client.product.findMany({});
return {
props: {
Expand All @@ -69,4 +89,4 @@ export async function getServerSideProps() {
};
}

export default Home;
export default Page;

0 comments on commit ea024d1

Please sign in to comment.