1+ import { ServerRouter } from "@hanghae-plus/lib" ;
12import { renderToString } from "react-dom/server" ;
23
34import { getCategories , getProduct , getProducts } from "./api" ;
45import { App } from "./App" ;
6+ import { BASE_URL } from "./constants" ;
57import { initialProductState , PRODUCT_ACTIONS , productStore , type Product } from "./entities" ;
68import { HomePage , NotFoundPage , ProductDetailPage } from "./pages" ;
79import { router } from "./router" ;
810
911export class SSRService {
1012 constructor ( ) {
11- router . addRoute ( "/" , HomePage ) ;
12- router . addRoute ( "/product/:id/" , ProductDetailPage ) ;
13- router . addRoute ( ".*" , NotFoundPage ) ;
13+ if ( router instanceof ServerRouter && ( router . routes ?. size === 0 || ! router . routes ) ) {
14+ router . addRoute ( "/" , HomePage ) ;
15+ router . addRoute ( "/product/:id/" , ProductDetailPage ) ;
16+ router . addRoute ( ".*" , NotFoundPage ) ;
17+ }
1418 }
1519
1620 async render ( url : string , query : Record < string , string > ) {
17- router . push ( url ) ;
21+ productStore . dispatch ( {
22+ type : PRODUCT_ACTIONS . SETUP ,
23+ payload : initialProductState ,
24+ } ) ;
25+
26+ const fullUrl = BASE_URL && BASE_URL !== "/" ? `${ BASE_URL } ${ url } ` : url ;
27+ router . push ( fullUrl ) ;
1828 router . query = query ;
1929
2030 const route = router . route ;
2131
22- if ( ! route || route . path === ".*" ) {
23- return this . #renderNotFoundPage( ) ;
24- }
25-
26- if ( route . path === "/" ) {
32+ if ( route ?. path === "/" ) {
2733 return this . #renderHomePage( query ) ;
2834 }
2935
30- if ( route . path === "/product/:id/" ) {
36+ if ( route ? .path === "/product/:id/" ) {
3137 const productId = route . params ?. id ;
3238 if ( productId ) {
3339 return this . #renderProductDetailPage( productId ) ;
@@ -44,7 +50,7 @@ export class SSRService {
4450 productStore . dispatch ( {
4551 type : PRODUCT_ACTIONS . SETUP ,
4652 payload : {
47- products : products . products ,
53+ products,
4854 categories,
4955 totalCount : products . pagination . total ,
5056 loading : false ,
@@ -53,13 +59,19 @@ export class SSRService {
5359 } ) ;
5460
5561 const state = productStore . getState ( ) ;
62+
5663 return {
5764 head : /* HTML */ `<title>쇼핑몰 - 홈</title>` ,
5865 html : renderToString ( < App /> ) ,
5966 data : {
6067 products : state . products ,
61- categories : state . categories ,
6268 totalCount : state . totalCount ,
69+ currentProduct : state . currentProduct ,
70+ relatedProducts : state . relatedProducts ,
71+ loading : state . loading ,
72+ error : state . error ,
73+ status : state . status ,
74+ categories : state . categories ,
6375 } ,
6476 } ;
6577 } catch ( error ) {
@@ -89,7 +101,12 @@ export class SSRService {
89101 limit : "20" ,
90102 page : "1" ,
91103 } ) ;
92- relatedProducts = relatedResponse . products . filter ( ( p ) => p . productId !== productId ) ;
104+ relatedProducts = relatedResponse . products
105+ . filter ( ( p ) => p . productId !== productId )
106+ . map ( ( p ) => ( {
107+ ...p ,
108+ image : p . image ?. replace ( / \. ( \d + \. ) ? j p g $ / , ".jpg" ) ,
109+ } ) ) ;
93110 } catch ( error ) {
94111 console . error ( "관련 상품 로드 실패:" , error ) ;
95112 }
@@ -110,10 +127,20 @@ export class SSRService {
110127 } ) ;
111128
112129 const state = productStore . getState ( ) ;
130+
113131 return {
114132 head : /* HTML */ `<title>${ product . title } - 쇼핑몰</title>` ,
115133 html : renderToString ( < App /> ) ,
116- data : state ,
134+ data : {
135+ products : state . products ,
136+ totalCount : state . totalCount ,
137+ currentProduct : state . currentProduct ,
138+ relatedProducts : state . relatedProducts ,
139+ loading : state . loading ,
140+ error : state . error ,
141+ status : state . status ,
142+ categories : state . categories ,
143+ } ,
117144 } ;
118145 } catch ( error ) {
119146 console . error ( "상품 상세 페이지 렌더링 실패:" , error ) ;
0 commit comments