Skip to content

Commit d87a6d2

Browse files
committed
feat: 하이드레이션 조건 보강
1 parent 282c133 commit d87a6d2

File tree

5 files changed

+26
-10
lines changed

5 files changed

+26
-10
lines changed

packages/react/src/api/mockApi.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ function filterProducts(products: Product[], query: Record<string, string>) {
4242
return filtered;
4343
}
4444

45-
const delay = (ms = 50) => new Promise((r) => setTimeout(r, ms));
45+
const delay = (ms = 200) => new Promise((r) => setTimeout(r, ms));
4646

4747
export async function getProducts(params: StringRecord = {}) {
4848
const { limit = 20, search = "", category1 = "", category2 = "", sort = "price_asc" } = params;

packages/react/src/main.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ function main() {
2828
}
2929

3030
// 애플리케이션 시작
31-
if (import.meta.env.DEV || (typeof window !== "undefined" && !window.__INITIAL_DATA__)) {
31+
if (import.meta.env.DEV) {
32+
enableMocking().then(main);
33+
} else if (typeof window !== "undefined" && !window.__INITIAL_DATA__) {
3234
enableMocking().then(main);
3335
} else {
3436
main();

packages/react/src/pages/HomePage.tsx

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { useEffect } from "react";
2-
import { loadNextProducts, loadProductsAndCategories, ProductList, SearchBar } from "../entities";
2+
import { loadNextProducts, loadProductsAndCategories, ProductList, SearchBar, useProductStore } from "../entities";
33
import { PageWrapper } from "./PageWrapper";
44

55
const headerLeft = (
@@ -27,12 +27,22 @@ const unregisterScrollHandler = () => {
2727
};
2828

2929
export const HomePage = () => {
30+
const state = useProductStore();
3031
useEffect(() => {
3132
registerScrollHandler();
32-
if (!(typeof window !== "undefined" && window.__INITIAL_DATA__)) {
33+
const hasSSRData =
34+
state.status === "done" &&
35+
(state.products.length > 0 ||
36+
!!state.currentProduct ||
37+
(state.categories && Object.keys(state.categories).length > 0));
38+
39+
if (!hasSSRData) {
3340
loadProductsAndCategories();
3441
}
35-
return unregisterScrollHandler;
42+
43+
return () => {
44+
unregisterScrollHandler();
45+
};
3646
}, []);
3747

3848
return (

packages/react/static-site-generate.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,13 @@ async function generateStaticSite() {
2828
? `<script>window.__INITIAL_DATA__=${JSON.stringify(rendered.initialData).replace(/</g, "\\u003c")}</script>`
2929
: "";
3030

31-
const html = template
32-
.replace("<!--app-head-->", appHead)
33-
.replace("<!--app-html-->", appHtml)
34-
.replace("</head>", `${initialDataScript}</head>`);
31+
let html = template.replace("<!--app-head-->", appHead).replace("<!--app-html-->", appHtml);
32+
if (initialDataScript) {
33+
html = html.replace(/<script\s+type="module"/, `${initialDataScript}\n<script type="module"`);
34+
if (!/<script\s+type="module"/.test(template)) {
35+
html = html.replace("</head>", `${initialDataScript}</head>`);
36+
}
37+
}
3538

3639
saveHtmlFile(page.filePath, html);
3740
}),

packages/react/vite.config.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import react from "@vitejs/plugin-react";
22
import { createViteConfig } from "../../createViteConfig";
33

4-
const base: string = process.env.NODE_ENV === "production" ? "/front_6th_chapter4-1/react/" : "";
4+
const isProdLike = process.env.NODE_ENV === "production" || process.env.CI === "true" || !!process.env.GITHUB_ACTIONS;
5+
const base: string = isProdLike ? "/front_6th_chapter4-1/react/" : "";
56

67
export default createViteConfig({
78
base,

0 commit comments

Comments
 (0)