Skip to content

Commit a279b95

Browse files
author
YeongseoYoon
committed
feat: HomePage에 withServerSideProps를 적용하여 SSR을 통한 상품 및 카테고리 로딩 기능 추가
1 parent d04f378 commit a279b95

File tree

1 file changed

+68
-40
lines changed

1 file changed

+68
-40
lines changed
Lines changed: 68 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { useEffect } from "react";
22
import { ProductList, SearchBar, useProductStoreContext } from "../entities";
33
import { PageWrapper } from "./PageWrapper";
4+
import { withServerSideProps } from "../utils";
5+
import { getProducts, getCategories } from "../api/productApi";
46

57
const headerLeft = (
68
<h1 className="text-xl font-bold text-gray-900">
@@ -10,43 +12,69 @@ const headerLeft = (
1012
</h1>
1113
);
1214

13-
export const HomePage = () => {
14-
const {
15-
action: { loadProductsAndCategories, loadNextProducts },
16-
} = useProductStoreContext();
17-
18-
// 무한 스크롤 이벤트 등록
19-
let scrollHandlerRegistered = false;
20-
21-
const registerScrollHandler = () => {
22-
if (scrollHandlerRegistered) return;
23-
24-
window.addEventListener("scroll", loadNextProducts);
25-
scrollHandlerRegistered = true;
26-
};
27-
28-
const unregisterScrollHandler = () => {
29-
if (!scrollHandlerRegistered) return;
30-
window.removeEventListener("scroll", loadNextProducts);
31-
scrollHandlerRegistered = false;
32-
};
33-
34-
useEffect(() => {
35-
registerScrollHandler();
36-
loadProductsAndCategories();
37-
38-
return unregisterScrollHandler;
39-
}, []);
40-
41-
return (
42-
<PageWrapper headerLeft={headerLeft}>
43-
{/* 검색 및 필터 */}
44-
<SearchBar />
45-
46-
{/* 상품 목록 */}
47-
<div className="mb-6">
48-
<ProductList />
49-
</div>
50-
</PageWrapper>
51-
);
52-
};
15+
export const HomePage = withServerSideProps(
16+
{
17+
ssr: async ({ query }) => {
18+
const [
19+
{
20+
products,
21+
pagination: { total },
22+
},
23+
categories,
24+
] = await Promise.all([getProducts(query), getCategories()]);
25+
26+
return {
27+
products,
28+
categories,
29+
totalCount: total,
30+
loading: false,
31+
status: "done",
32+
};
33+
},
34+
metadata: async () => {
35+
return {
36+
title: "쇼핑몰 - 홈",
37+
};
38+
},
39+
},
40+
() => {
41+
const {
42+
action: { loadProductsAndCategories, loadNextProducts },
43+
} = useProductStoreContext();
44+
45+
// 무한 스크롤 이벤트 등록
46+
let scrollHandlerRegistered = false;
47+
48+
const registerScrollHandler = () => {
49+
if (scrollHandlerRegistered) return;
50+
51+
window.addEventListener("scroll", loadNextProducts);
52+
scrollHandlerRegistered = true;
53+
};
54+
55+
const unregisterScrollHandler = () => {
56+
if (!scrollHandlerRegistered) return;
57+
window.removeEventListener("scroll", loadNextProducts);
58+
scrollHandlerRegistered = false;
59+
};
60+
61+
useEffect(() => {
62+
registerScrollHandler();
63+
loadProductsAndCategories();
64+
65+
return unregisterScrollHandler;
66+
}, []);
67+
68+
return (
69+
<PageWrapper headerLeft={headerLeft}>
70+
{/* 검색 및 필터 */}
71+
<SearchBar />
72+
73+
{/* 상품 목록 */}
74+
<div className="mb-6">
75+
<ProductList />
76+
</div>
77+
</PageWrapper>
78+
);
79+
},
80+
);

0 commit comments

Comments
 (0)