Skip to content

Commit 5a81f31

Browse files
committed
feat: 서버에서 window return
1 parent 2db0b46 commit 5a81f31

File tree

5 files changed

+56
-19
lines changed

5 files changed

+56
-19
lines changed

packages/react/server.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,7 @@ const setupMiddlewares = async () => {
1919
return viteServer;
2020
}
2121
const compression = (await import("compression")).default;
22-
const sirv = (await import("sirv")).default;
23-
2422
app.use(compression());
25-
app.use(base, sirv("./dist/react", { extensions: [] }));
26-
2723
return null;
2824
};
2925

@@ -45,8 +41,11 @@ const get = {
4541
},
4642
};
4743

48-
app.get("*all", async (request, response) => {
44+
app.use(async (request, response, next) => {
4945
try {
46+
const accept = request.headers.accept || "";
47+
if (!String(accept).includes("text/html")) return next();
48+
5049
const url = request.originalUrl.replace(base, "");
5150

5251
const template = await get.template(viteServer, url);
@@ -68,6 +67,11 @@ app.get("*all", async (request, response) => {
6867
}
6968
});
7069

70+
if (prod) {
71+
const sirv = (await import("sirv")).default;
72+
app.use(base, sirv("./dist/react", { extensions: [] }));
73+
}
74+
7175
// Start http server
7276
app.listen(port, () => {
7377
console.log(`React Server started at http://localhost:${port}`);
Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
1-
import { useRouterParams } from "../../../../router";
21
import { useEffect } from "react";
32
import { loadProductDetailForPage } from "../../productUseCase";
3+
import { BASE_URL } from "../../../../constants";
44

55
export const useLoadProductDetail = () => {
6-
const productId = useRouterParams((params) => params.id);
76
useEffect(() => {
8-
loadProductDetailForPage(productId);
9-
}, [productId]);
7+
if (typeof window === "undefined") return;
8+
const rawPath = window.location.pathname;
9+
const base = BASE_URL.replace(/\/$/, "");
10+
const normalizedPath = base && rawPath.startsWith(base) ? rawPath.slice(base.length) || "/" : rawPath;
11+
const match = normalizedPath.match(/^\/product\/(\d+)(?:\/?|\?|$)/);
12+
const productId = match ? match[1] : undefined;
13+
if (productId) {
14+
loadProductDetailForPage(productId);
15+
}
16+
}, []);
1017
};

packages/react/src/entities/products/productUseCase.ts

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,24 @@
11
import { getCategories, getProduct, getProducts } from "../../api/productApi";
2-
import { router } from "../../router";
32
import type { StringRecord } from "../../types";
43
import { initialProductState, PRODUCT_ACTIONS, productStore } from "./productStore";
54
import { isNearBottom } from "../../utils";
65

76
const createErrorMessage = (error: unknown, defaultMessage = "알 수 없는 오류 발생") =>
87
error instanceof Error ? error.message : defaultMessage;
98

9+
const getRouter = async () => {
10+
if (typeof window === "undefined") {
11+
return {
12+
query: {},
13+
route: { path: "/" },
14+
};
15+
}
16+
const mod = await import("../../router");
17+
return mod.router;
18+
};
19+
1020
export const loadProductsAndCategories = async () => {
21+
const router = await getRouter();
1122
router.query = { current: undefined }; // 항상 첫 페이지로 초기화
1223
productStore.dispatch({
1324
type: PRODUCT_ACTIONS.SETUP,
@@ -54,6 +65,7 @@ export const loadProducts = async (resetList = true) => {
5465
payload: { loading: true, status: "pending", error: null },
5566
});
5667

68+
const router = await getRouter();
5769
const {
5870
products,
5971
pagination: { total },
@@ -83,22 +95,27 @@ export const loadMoreProducts = async () => {
8395
return;
8496
}
8597

98+
const router = await getRouter();
8699
router.query = { current: Number(router.query.current ?? 1) + 1 };
87100
await loadProducts(false);
88101
};
89-
export const searchProducts = (search: string) => {
102+
export const searchProducts = async (search: string) => {
103+
const router = await getRouter();
90104
router.query = { search, current: 1 };
91105
};
92106

93-
export const setCategory = (categoryData: StringRecord) => {
107+
export const setCategory = async (categoryData: StringRecord) => {
108+
const router = await getRouter();
94109
router.query = { ...categoryData, current: 1 };
95110
};
96111

97-
export const setSort = (sort: string) => {
112+
export const setSort = async (sort: string) => {
113+
const router = await getRouter();
98114
router.query = { sort, current: 1 };
99115
};
100116

101-
export const setLimit = (limit: number) => {
117+
export const setLimit = async (limit: number) => {
118+
const router = await getRouter();
102119
router.query = { limit, current: 1 };
103120
};
104121

@@ -174,6 +191,7 @@ export const loadRelatedProducts = async (category2: string, excludeProductId: s
174191

175192
export const loadNextProducts = async () => {
176193
// 현재 라우트가 홈이 아니면 무한 스크롤 비활성화
194+
const router = await getRouter();
177195
if (router.route?.path !== "/") {
178196
return;
179197
}

packages/react/src/main.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ const enableMocking = () =>
1414
);
1515

1616
function main() {
17-
router.start();
17+
if (typeof window !== "undefined") {
18+
router.start();
19+
}
1820

1921
const rootElement = document.getElementById("root")!;
2022
createRoot(rootElement).render(<App />);

packages/react/src/utils/log.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,18 @@ declare global {
66
}
77
}
88

9-
window.__spyCalls = [];
10-
window.__spyCallsClear = () => {
9+
const isClient = typeof window !== "undefined";
10+
11+
if (isClient) {
1112
window.__spyCalls = [];
12-
};
13+
window.__spyCallsClear = () => {
14+
window.__spyCalls = [];
15+
};
16+
}
1317

1418
export const log: typeof console.log = (...args) => {
15-
window.__spyCalls.push(args);
19+
if (isClient) {
20+
window.__spyCalls.push(args);
21+
}
1622
return console.log(...args);
1723
};

0 commit comments

Comments
 (0)