@mandujs/core 0.5.7 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -22,6 +22,10 @@ export interface SSROptions {
22
22
  isDev?: boolean;
23
23
  /** HMR 포트 (개발 모드에서 사용) */
24
24
  hmrPort?: number;
25
+ /** Client-side Routing 활성화 여부 */
26
+ enableClientRouter?: boolean;
27
+ /** 라우트 패턴 (Client-side Routing용) */
28
+ routePattern?: string;
25
29
  }
26
30
 
27
31
  /**
@@ -105,6 +109,8 @@ export function renderToHTML(element: ReactElement, options: SSROptions = {}): s
105
109
  bodyEndTags = "",
106
110
  isDev = false,
107
111
  hmrPort,
112
+ enableClientRouter = false,
113
+ routePattern,
108
114
  } = options;
109
115
 
110
116
  let content = renderToString(element);
@@ -129,12 +135,24 @@ export function renderToHTML(element: ReactElement, options: SSROptions = {}): s
129
135
  dataScript = serializeServerData(wrappedData);
130
136
  }
131
137
 
138
+ // Client-side Routing: 라우트 정보 주입
139
+ let routeScript = "";
140
+ if (enableClientRouter && routeId) {
141
+ routeScript = generateRouteScript(routeId, routePattern || "", serverData);
142
+ }
143
+
132
144
  // Hydration 스크립트
133
145
  let hydrationScripts = "";
134
146
  if (needsHydration && bundleManifest) {
135
147
  hydrationScripts = generateHydrationScripts(routeId, bundleManifest);
136
148
  }
137
149
 
150
+ // Client-side Router 스크립트
151
+ let routerScript = "";
152
+ if (enableClientRouter && bundleManifest) {
153
+ routerScript = generateClientRouterScript(bundleManifest);
154
+ }
155
+
138
156
  // HMR 스크립트 (개발 모드)
139
157
  let hmrScript = "";
140
158
  if (isDev && hmrPort) {
@@ -152,13 +170,60 @@ export function renderToHTML(element: ReactElement, options: SSROptions = {}): s
152
170
  <body>
153
171
  <div id="root">${content}</div>
154
172
  ${dataScript}
173
+ ${routeScript}
155
174
  ${hydrationScripts}
175
+ ${routerScript}
156
176
  ${hmrScript}
157
177
  ${bodyEndTags}
158
178
  </body>
159
179
  </html>`;
160
180
  }
161
181
 
182
+ /**
183
+ * Client-side Routing: 현재 라우트 정보 스크립트 생성
184
+ */
185
+ function generateRouteScript(
186
+ routeId: string,
187
+ pattern: string,
188
+ serverData?: Record<string, unknown>
189
+ ): string {
190
+ const routeInfo = {
191
+ id: routeId,
192
+ pattern,
193
+ params: extractParamsFromUrl(pattern),
194
+ };
195
+
196
+ const json = JSON.stringify(routeInfo)
197
+ .replace(/</g, "\\u003c")
198
+ .replace(/>/g, "\\u003e");
199
+
200
+ return `<script>window.__MANDU_ROUTE__ = ${json};</script>`;
201
+ }
202
+
203
+ /**
204
+ * URL 패턴에서 파라미터 추출 (클라이언트에서 사용)
205
+ */
206
+ function extractParamsFromUrl(pattern: string): Record<string, string> {
207
+ // 서버에서는 실제 params를 전달받으므로 빈 객체 반환
208
+ // 실제 params는 serverData나 별도 전달
209
+ return {};
210
+ }
211
+
212
+ /**
213
+ * Client-side Router 스크립트 로드
214
+ */
215
+ function generateClientRouterScript(manifest: BundleManifest): string {
216
+ // Import map 먼저 (이미 hydration에서 추가되었을 수 있음)
217
+ const scripts: string[] = [];
218
+
219
+ // 라우터 번들이 있으면 로드
220
+ if (manifest.shared?.router) {
221
+ scripts.push(`<script type="module" src="${manifest.shared.router}"></script>`);
222
+ }
223
+
224
+ return scripts.join("\n");
225
+ }
226
+
162
227
  /**
163
228
  * HMR 스크립트 생성
164
229
  */