@@ -11,8 +11,6 @@ import { App } from "./App";
1111
1212export const render = async ( url : string , query : Record < string , string > ) => {
1313 console . log ( "SSR render 시작:" , { url, query } ) ;
14- console . log ( url ) ;
15- console . log ( query ) ;
1614
1715 const serverRouter = new Router < FunctionComponent > ( BASE_URL ) ;
1816
@@ -44,19 +42,32 @@ export const render = async (url: string, query: Record<string, string>) => {
4442 const productId = splitUrl [ 1 ] ;
4543 console . log ( "상품 상세 페이지 데이터 로딩:" , productId ) ;
4644
47- const product = await getProduct ( productId ) ;
48- initialData . currentProduct = product ;
49-
50- if ( product . category2 ) {
51- const relatedData = await getProducts ( {
52- category2 : product . category2 ,
53- limit : "20" ,
54- } ) ;
55- initialData . relatedProducts = relatedData . products . filter ( ( p ) => p . productId !== productId ) ;
45+ try {
46+ const product = await getProduct ( productId ) ;
47+ console . log ( "상품 로딩 성공:" , product . title ) ;
48+
49+ initialData . currentProduct = product ;
50+
51+ // 관련 상품 로딩
52+ if ( product . category2 ) {
53+ try {
54+ const relatedData = await getProducts ( {
55+ category2 : product . category2 ,
56+ limit : "20" ,
57+ } ) ;
58+ initialData . relatedProducts = relatedData . products . filter ( ( p ) => p . productId !== productId ) ;
59+ console . log ( "관련 상품 로딩 성공:" , initialData . relatedProducts . length , "개" ) ;
60+ } catch ( relatedError ) {
61+ console . error ( "관련 상품 로딩 실패:" , relatedError ) ;
62+ initialData . relatedProducts = [ ] ;
63+ }
64+ }
65+
66+ const categoriesData = await getCategories ( ) ;
67+ initialData . categories = categoriesData ;
68+ } catch ( productError ) {
69+ console . error ( "상품 로딩 실패:" , productError ) ;
5670 }
57-
58- const categoriesData = await getCategories ( ) ;
59- initialData . categories = categoriesData ;
6071 } else {
6172 // 홈페이지
6273 console . log ( "홈페이지 데이터 로딩" ) ;
@@ -81,6 +92,8 @@ export const render = async (url: string, query: Record<string, string>) => {
8192 productsCount : initialData . products . length ,
8293 categoriesCount : Object . keys ( initialData . categories ) . length ,
8394 totalCount : initialData . totalCount ,
95+ currentProduct : initialData . currentProduct ,
96+ relatedCount : initialData . relatedProducts . length ,
8497 } ) ;
8598 } catch ( error ) {
8699 console . error ( "데이터 로딩 오류:" , error ) ;
@@ -90,36 +103,14 @@ export const render = async (url: string, query: Record<string, string>) => {
90103 let html = "" ;
91104 try {
92105 console . log ( "React 컴포넌트 렌더링 시작" ) ;
93- console . log ( "App 컴포넌트 타입:" , typeof App ) ;
94- console . log ( "ProductProvider 타입:" , typeof ProductProvider ) ;
95106
96- // 단계별 렌더링 테스트
107+ // 스토어 초기화
97108 productStore . dispatch ( {
98109 type : PRODUCT_ACTIONS . SET_INITIAL_DATA ,
99110 payload : initialData ,
100111 } ) ;
101112
102- // 1단계: App만 렌더링
103- try {
104- const appOnly = renderToString ( < App /> ) ;
105- console . log ( "App 단독 렌더링 성공, 길이:" , appOnly . length ) ;
106- } catch ( appError ) {
107- console . error ( "App 렌더링 오류:" , appError ) ;
108- }
109-
110- // 2단계: ProductProvider만 렌더링
111- try {
112- const providerOnly = renderToString (
113- < ProductProvider initialData = { { ...initialData } } >
114- < div > Provider Test</ div >
115- </ ProductProvider > ,
116- ) ;
117- console . log ( "ProductProvider 단독 렌더링 성공, 길이:" , providerOnly . length ) ;
118- } catch ( providerError ) {
119- console . error ( "ProductProvider 렌더링 오류:" , providerError ) ;
120- }
121-
122- // 3단계: 전체 렌더링
113+ // 전체 렌더링
123114 html = renderToString (
124115 < ProductProvider initialData = { initialData } >
125116 < App url = { url } />
@@ -128,24 +119,17 @@ export const render = async (url: string, query: Record<string, string>) => {
128119
129120 console . log ( "전체 렌더링 완료, HTML 길이:" , html . length ) ;
130121
131- if ( html . length > 0 ) {
132- //console.log("HTML 시작 부분:", html.substring(0, 200));
133- //console.log("HTML 끝 부분:", html.substring(html.length - 100));
134- } else {
122+ if ( html . length === 0 ) {
135123 console . error ( "⚠️ 렌더링된 HTML이 비어있습니다!" ) ;
136-
137- // 폴백: 간단한 HTML 구조 생성
138- html = `` ;
139- console . log ( "폴백 HTML 생성 완료, 길이:" , html . length ) ;
124+ // 폴백 HTML
125+ html = `<div id="root"><!-- SSR 실패, 클라이언트에서 렌더링됩니다 --></div>` ;
140126 }
141127 } catch ( renderError ) {
142- console . error ( "React 렌더링 심각한 오류:" , renderError ) ;
143-
144- // 최종 폴백 HTML
145- html = `` ;
128+ console . error ( "React 렌더링 오류:" , renderError ) ;
129+ html = `<div id="root"><!-- 렌더링 오류: ${ renderError } --></div>` ;
146130 }
147131
148- const head = " <title>쇼핑몰 - 홈</title>" ;
132+ const head = ` <title>${ initialData . currentProduct ? ` ${ initialData . currentProduct ?. title } - 쇼핑몰` : "쇼핑몰 - 홈" } </title>` ;
149133
150134 console . log ( "SSR render 완료" ) ;
151135
@@ -156,7 +140,6 @@ export const render = async (url: string, query: Record<string, string>) => {
156140 } ;
157141} ;
158142
159- // 추가 디버깅 함수들
160143export const debugSSR = ( ) => {
161144 console . log ( "=== SSR 디버깅 정보 ===" ) ;
162145 console . log ( "renderToString 함수:" , typeof renderToString ) ;
0 commit comments