@mandujs/core 0.18.22 → 0.19.2
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.
- package/README.ko.md +0 -14
- package/package.json +4 -1
- package/src/brain/architecture/analyzer.ts +4 -4
- package/src/brain/doctor/analyzer.ts +18 -14
- package/src/bundler/build.test.ts +127 -0
- package/src/bundler/build.ts +291 -113
- package/src/bundler/css.ts +20 -5
- package/src/bundler/dev.ts +55 -2
- package/src/bundler/prerender.ts +195 -0
- package/src/change/snapshot.ts +4 -23
- package/src/change/types.ts +2 -3
- package/src/client/Form.tsx +105 -0
- package/src/client/__tests__/use-sse.test.ts +153 -0
- package/src/client/hooks.ts +105 -6
- package/src/client/index.ts +35 -6
- package/src/client/router.ts +670 -433
- package/src/client/rpc.ts +140 -0
- package/src/client/runtime.ts +24 -21
- package/src/client/use-fetch.ts +239 -0
- package/src/client/use-head.ts +197 -0
- package/src/client/use-sse.ts +378 -0
- package/src/components/Image.tsx +162 -0
- package/src/config/mandu.ts +5 -0
- package/src/config/validate.ts +34 -0
- package/src/content/index.ts +5 -1
- package/src/devtools/client/catchers/error-catcher.ts +17 -0
- package/src/devtools/client/catchers/network-proxy.ts +390 -367
- package/src/devtools/client/components/kitchen-root.tsx +479 -467
- package/src/devtools/client/components/panel/diff-viewer.tsx +219 -0
- package/src/devtools/client/components/panel/guard-panel.tsx +374 -244
- package/src/devtools/client/components/panel/index.ts +45 -32
- package/src/devtools/client/components/panel/panel-container.tsx +332 -312
- package/src/devtools/client/components/panel/preview-panel.tsx +188 -0
- package/src/devtools/client/state-manager.ts +535 -478
- package/src/devtools/design-tokens.ts +265 -264
- package/src/devtools/types.ts +345 -319
- package/src/filling/context.ts +65 -0
- package/src/filling/filling.ts +336 -14
- package/src/filling/index.ts +5 -1
- package/src/filling/session.ts +216 -0
- package/src/filling/ws.ts +78 -0
- package/src/generator/generate.ts +2 -2
- package/src/guard/auto-correct.ts +0 -29
- package/src/guard/check.ts +14 -31
- package/src/guard/presets/index.ts +296 -294
- package/src/guard/rules.ts +15 -19
- package/src/guard/validator.ts +834 -834
- package/src/index.ts +5 -1
- package/src/island/index.ts +373 -304
- package/src/kitchen/api/contract-api.ts +225 -0
- package/src/kitchen/api/diff-parser.ts +108 -0
- package/src/kitchen/api/file-api.ts +273 -0
- package/src/kitchen/api/guard-api.ts +83 -0
- package/src/kitchen/api/guard-decisions.ts +100 -0
- package/src/kitchen/api/routes-api.ts +50 -0
- package/src/kitchen/index.ts +21 -0
- package/src/kitchen/kitchen-handler.ts +256 -0
- package/src/kitchen/kitchen-ui.ts +1732 -0
- package/src/kitchen/stream/activity-sse.ts +145 -0
- package/src/kitchen/stream/file-tailer.ts +99 -0
- package/src/middleware/compress.ts +62 -0
- package/src/middleware/cors.ts +47 -0
- package/src/middleware/index.ts +10 -0
- package/src/middleware/jwt.ts +134 -0
- package/src/middleware/logger.ts +58 -0
- package/src/middleware/timeout.ts +55 -0
- package/src/paths.ts +0 -4
- package/src/plugins/hooks.ts +64 -0
- package/src/plugins/index.ts +3 -0
- package/src/plugins/types.ts +5 -0
- package/src/report/build.ts +0 -6
- package/src/resource/__tests__/backward-compat.test.ts +0 -1
- package/src/router/fs-patterns.ts +11 -1
- package/src/router/fs-routes.ts +78 -14
- package/src/router/fs-scanner.ts +2 -2
- package/src/router/fs-types.ts +2 -1
- package/src/runtime/adapter-bun.ts +62 -0
- package/src/runtime/adapter.ts +47 -0
- package/src/runtime/cache.ts +310 -0
- package/src/runtime/handler.ts +65 -0
- package/src/runtime/image-handler.ts +195 -0
- package/src/runtime/index.ts +12 -0
- package/src/runtime/middleware.ts +263 -0
- package/src/runtime/server.ts +686 -92
- package/src/runtime/ssr.ts +55 -29
- package/src/runtime/streaming-ssr.ts +106 -82
- package/src/spec/index.ts +0 -1
- package/src/spec/schema.ts +1 -0
- package/src/testing/index.ts +144 -0
- package/src/watcher/watcher.ts +27 -1
- package/src/spec/lock.ts +0 -56
package/src/client/router.ts
CHANGED
|
@@ -1,435 +1,672 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Mandu Client-side Router 🧭
|
|
3
|
-
* SPA 스타일 네비게이션을 위한 클라이언트 라우터
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
import type { ReactNode } from "react";
|
|
7
|
-
import {
|
|
8
|
-
getManduData,
|
|
9
|
-
getManduRoute,
|
|
10
|
-
getRouterListeners,
|
|
11
|
-
getRouterState as getWindowRouterState,
|
|
12
|
-
setRouterState as setWindowRouterState,
|
|
13
|
-
setServerData,
|
|
14
|
-
} from "./window-state";
|
|
15
|
-
import { LRUCache } from "../utils/lru-cache";
|
|
16
|
-
import { LIMITS } from "../constants";
|
|
17
|
-
|
|
18
|
-
// ========== Types ==========
|
|
19
|
-
|
|
20
|
-
export interface RouteInfo {
|
|
21
|
-
id: string;
|
|
22
|
-
pattern: string;
|
|
23
|
-
params: Record<string, string>;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
export interface NavigationState {
|
|
27
|
-
state: "idle" | "loading";
|
|
28
|
-
location?: string;
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
type RouterListener = (state: RouterState) => void;
|
|
45
|
-
|
|
46
|
-
function getGlobalRouterState(): RouterState {
|
|
47
|
-
if (typeof window === "undefined") {
|
|
48
|
-
return { currentRoute: null, loaderData: undefined, navigation: { state: "idle" } };
|
|
49
|
-
}
|
|
50
|
-
if (!getWindowRouterState()) {
|
|
51
|
-
// SSR에서 주입된 __MANDU_ROUTE__에서 초기화
|
|
52
|
-
const route = getManduRoute();
|
|
53
|
-
const data = getManduData();
|
|
54
|
-
|
|
55
|
-
setWindowRouterState({
|
|
56
|
-
currentRoute: route
|
|
57
|
-
? {
|
|
58
|
-
id: route.id,
|
|
59
|
-
pattern: route.pattern,
|
|
60
|
-
params: route.params || {},
|
|
61
|
-
}
|
|
62
|
-
: null,
|
|
63
|
-
loaderData: route && data?.[route.id]?.serverData,
|
|
64
|
-
navigation: { state: "idle" },
|
|
65
|
-
});
|
|
66
|
-
}
|
|
67
|
-
return getWindowRouterState()!;
|
|
1
|
+
/**
|
|
2
|
+
* Mandu Client-side Router 🧭
|
|
3
|
+
* SPA 스타일 네비게이션을 위한 클라이언트 라우터
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import type { ReactNode } from "react";
|
|
7
|
+
import {
|
|
8
|
+
getManduData,
|
|
9
|
+
getManduRoute,
|
|
10
|
+
getRouterListeners,
|
|
11
|
+
getRouterState as getWindowRouterState,
|
|
12
|
+
setRouterState as setWindowRouterState,
|
|
13
|
+
setServerData,
|
|
14
|
+
} from "./window-state";
|
|
15
|
+
import { LRUCache } from "../utils/lru-cache";
|
|
16
|
+
import { LIMITS } from "../constants";
|
|
17
|
+
|
|
18
|
+
// ========== Types ==========
|
|
19
|
+
|
|
20
|
+
export interface RouteInfo {
|
|
21
|
+
id: string;
|
|
22
|
+
pattern: string;
|
|
23
|
+
params: Record<string, string>;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export interface NavigationState {
|
|
27
|
+
state: "idle" | "loading" | "submitting";
|
|
28
|
+
location?: string;
|
|
29
|
+
formAction?: string;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface RouterState {
|
|
33
|
+
currentRoute: RouteInfo | null;
|
|
34
|
+
loaderData: unknown;
|
|
35
|
+
actionData: unknown;
|
|
36
|
+
navigation: NavigationState;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export interface ActionResult {
|
|
40
|
+
ok: boolean;
|
|
41
|
+
actionData?: unknown;
|
|
42
|
+
loaderData?: unknown;
|
|
68
43
|
}
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
const
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
)
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
44
|
+
|
|
45
|
+
export interface NavigateOptions {
|
|
46
|
+
/** history.replaceState 사용 여부 */
|
|
47
|
+
replace?: boolean;
|
|
48
|
+
/** 스크롤 위치 복원 여부 */
|
|
49
|
+
scroll?: boolean;
|
|
50
|
+
/** revalidation 스킵 (기존 loaderData 유지) */
|
|
51
|
+
skipRevalidation?: boolean;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* shouldRevalidate 콜백 타입
|
|
56
|
+
* false 반환 시 loader 재실행을 건너뜀
|
|
57
|
+
*/
|
|
58
|
+
export type ShouldRevalidateFunction = (args: {
|
|
59
|
+
currentUrl: URL;
|
|
60
|
+
nextUrl: URL;
|
|
61
|
+
formAction?: string;
|
|
62
|
+
defaultShouldRevalidate: boolean;
|
|
63
|
+
}) => boolean;
|
|
64
|
+
|
|
65
|
+
/** 글로벌 shouldRevalidate 핸들러 */
|
|
66
|
+
let globalShouldRevalidate: ShouldRevalidateFunction | null = null;
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* shouldRevalidate 핸들러 등록
|
|
70
|
+
*
|
|
71
|
+
* @example
|
|
72
|
+
* ```typescript
|
|
73
|
+
* setShouldRevalidate(({ currentUrl, nextUrl }) => {
|
|
74
|
+
* // 같은 탭 내 이동이면 loader 재실행 안 함
|
|
75
|
+
* return currentUrl.pathname !== nextUrl.pathname;
|
|
76
|
+
* });
|
|
77
|
+
* ```
|
|
78
|
+
*/
|
|
79
|
+
export function setShouldRevalidate(fn: ShouldRevalidateFunction | null): void {
|
|
80
|
+
globalShouldRevalidate = fn;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
type RouterListener = (state: RouterState) => void;
|
|
84
|
+
|
|
85
|
+
function getGlobalRouterState(): RouterState {
|
|
86
|
+
if (typeof window === "undefined") {
|
|
87
|
+
return { currentRoute: null, loaderData: undefined, actionData: undefined, navigation: { state: "idle" } };
|
|
88
|
+
}
|
|
89
|
+
if (!getWindowRouterState()) {
|
|
90
|
+
// SSR에서 주입된 __MANDU_ROUTE__에서 초기화
|
|
91
|
+
const route = getManduRoute();
|
|
92
|
+
const data = getManduData();
|
|
93
|
+
|
|
94
|
+
setWindowRouterState({
|
|
95
|
+
currentRoute: route
|
|
96
|
+
? {
|
|
97
|
+
id: route.id,
|
|
98
|
+
pattern: route.pattern,
|
|
99
|
+
params: route.params || {},
|
|
100
|
+
}
|
|
101
|
+
: null,
|
|
102
|
+
loaderData: route && data?.[route.id]?.serverData,
|
|
103
|
+
actionData: undefined,
|
|
104
|
+
navigation: { state: "idle" },
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
return getWindowRouterState()!;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function setGlobalRouterState(state: RouterState): void {
|
|
111
|
+
if (typeof window !== "undefined") {
|
|
112
|
+
setWindowRouterState(state);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
function getGlobalListeners(): Set<RouterListener> {
|
|
117
|
+
return getRouterListeners();
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// Getter for routerState (전역 상태 참조)
|
|
121
|
+
const getRouterStateInternal = () => getGlobalRouterState();
|
|
122
|
+
const setRouterStateInternal = (state: RouterState) => setGlobalRouterState(state);
|
|
123
|
+
const listeners = { get current() { return getGlobalListeners(); } };
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* 초기화: 서버에서 전달된 라우트 정보로 상태 설정
|
|
127
|
+
*/
|
|
128
|
+
function initializeFromServer(): void {
|
|
129
|
+
if (typeof window === "undefined") return;
|
|
130
|
+
|
|
131
|
+
const route = getManduRoute();
|
|
132
|
+
const data = getManduData();
|
|
133
|
+
|
|
134
|
+
if (route) {
|
|
135
|
+
// URL에서 실제 params 추출
|
|
136
|
+
const params = extractParamsFromPath(route.pattern, window.location.pathname);
|
|
137
|
+
|
|
138
|
+
setRouterStateInternal({
|
|
139
|
+
currentRoute: {
|
|
140
|
+
id: route.id,
|
|
141
|
+
pattern: route.pattern,
|
|
142
|
+
params,
|
|
143
|
+
},
|
|
144
|
+
loaderData: data?.[route.id]?.serverData,
|
|
145
|
+
actionData: undefined,
|
|
146
|
+
navigation: { state: "idle" },
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// ========== Pattern Matching ==========
|
|
152
|
+
|
|
153
|
+
interface CompiledPattern {
|
|
154
|
+
regex: RegExp;
|
|
155
|
+
paramNames: string[];
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
const patternCache = new LRUCache<string, CompiledPattern>(LIMITS.ROUTER_PATTERN_CACHE);
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* 패턴을 정규식으로 컴파일
|
|
162
|
+
*/
|
|
163
|
+
function compilePattern(pattern: string): CompiledPattern {
|
|
164
|
+
const cached = patternCache.get(pattern);
|
|
165
|
+
if (cached) return cached;
|
|
166
|
+
|
|
167
|
+
const paramNames: string[] = [];
|
|
168
|
+
const PARAM_PLACEHOLDER = "\x00PARAM\x00";
|
|
169
|
+
const paramMatches: string[] = [];
|
|
170
|
+
|
|
171
|
+
const withPlaceholders = pattern.replace(
|
|
172
|
+
/:([a-zA-Z_][a-zA-Z0-9_]*)/g,
|
|
173
|
+
(_, paramName) => {
|
|
174
|
+
paramMatches.push(paramName);
|
|
175
|
+
return PARAM_PLACEHOLDER;
|
|
176
|
+
}
|
|
177
|
+
);
|
|
178
|
+
|
|
179
|
+
const escaped = withPlaceholders.replace(/[.*+?^${}()|[\]\\\/]/g, "\\$&");
|
|
180
|
+
|
|
181
|
+
let paramIndex = 0;
|
|
182
|
+
const regexStr = escaped.replace(
|
|
183
|
+
new RegExp(PARAM_PLACEHOLDER.replace(/\x00/g, "\\x00"), "g"),
|
|
184
|
+
() => {
|
|
185
|
+
paramNames.push(paramMatches[paramIndex++]);
|
|
186
|
+
return "([^/]+)";
|
|
187
|
+
}
|
|
188
|
+
);
|
|
189
|
+
|
|
190
|
+
const compiled = {
|
|
191
|
+
regex: new RegExp(`^${regexStr}$`),
|
|
192
|
+
paramNames,
|
|
193
|
+
};
|
|
194
|
+
|
|
195
|
+
patternCache.set(pattern, compiled);
|
|
196
|
+
return compiled;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* 패턴에서 파라미터 추출
|
|
201
|
+
*/
|
|
202
|
+
function extractParamsFromPath(
|
|
203
|
+
pattern: string,
|
|
204
|
+
pathname: string
|
|
205
|
+
): Record<string, string> {
|
|
206
|
+
const compiled = compilePattern(pattern);
|
|
207
|
+
const match = pathname.match(compiled.regex);
|
|
208
|
+
|
|
209
|
+
if (!match) return {};
|
|
210
|
+
|
|
211
|
+
const params: Record<string, string> = {};
|
|
212
|
+
compiled.paramNames.forEach((name, index) => {
|
|
213
|
+
params[name] = match[index + 1];
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
return params;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
// ========== Navigation ==========
|
|
220
|
+
|
|
221
|
+
/** 현재 진행 중인 네비게이션의 AbortController (race condition 방지) */
|
|
222
|
+
let activeNavigationController: AbortController | null = null;
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* 페이지 네비게이션
|
|
226
|
+
*/
|
|
183
227
|
export async function navigate(
|
|
184
|
-
to: string,
|
|
185
|
-
options: NavigateOptions = {}
|
|
186
|
-
): Promise<void> {
|
|
187
|
-
const { replace = false, scroll = true } = options;
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
//
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
}
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
}
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
228
|
+
to: string,
|
|
229
|
+
options: NavigateOptions = {}
|
|
230
|
+
): Promise<void> {
|
|
231
|
+
const { replace = false, scroll = true, skipRevalidation = false } = options;
|
|
232
|
+
|
|
233
|
+
// 이전 네비게이션이 진행 중이면 취소
|
|
234
|
+
if (activeNavigationController) {
|
|
235
|
+
activeNavigationController.abort();
|
|
236
|
+
activeNavigationController = null;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
const controller = new AbortController();
|
|
240
|
+
activeNavigationController = controller;
|
|
241
|
+
|
|
242
|
+
try {
|
|
243
|
+
const url = new URL(to, window.location.origin);
|
|
244
|
+
|
|
245
|
+
// 외부 URL은 일반 네비게이션
|
|
246
|
+
if (url.origin !== window.location.origin) {
|
|
247
|
+
window.location.href = to;
|
|
248
|
+
return;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
// shouldRevalidate 체크 — false면 fetch 없이 URL만 변경
|
|
252
|
+
if (!skipRevalidation && globalShouldRevalidate) {
|
|
253
|
+
const currentUrl = new URL(window.location.href);
|
|
254
|
+
const shouldFetch = globalShouldRevalidate({
|
|
255
|
+
currentUrl,
|
|
256
|
+
nextUrl: url,
|
|
257
|
+
defaultShouldRevalidate: true,
|
|
258
|
+
});
|
|
259
|
+
if (!shouldFetch) {
|
|
260
|
+
const currentState = getRouterStateInternal();
|
|
261
|
+
const nextRoute = currentState.currentRoute
|
|
262
|
+
? {
|
|
263
|
+
...currentState.currentRoute,
|
|
264
|
+
params: extractParamsFromPath(currentState.currentRoute.pattern, url.pathname),
|
|
265
|
+
}
|
|
266
|
+
: null;
|
|
267
|
+
const historyState = nextRoute
|
|
268
|
+
? { routeId: nextRoute.id, params: nextRoute.params }
|
|
269
|
+
: null;
|
|
270
|
+
if (replace) {
|
|
271
|
+
history.replaceState(historyState, "", to);
|
|
272
|
+
} else {
|
|
273
|
+
history.pushState(historyState, "", to);
|
|
274
|
+
}
|
|
275
|
+
setRouterStateInternal({
|
|
276
|
+
...currentState,
|
|
277
|
+
currentRoute: nextRoute,
|
|
278
|
+
actionData: undefined,
|
|
279
|
+
navigation: { state: "idle" },
|
|
280
|
+
});
|
|
281
|
+
notifyListeners();
|
|
282
|
+
if (scroll) window.scrollTo(0, 0);
|
|
283
|
+
return;
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
// 로딩 상태 시작
|
|
288
|
+
setRouterStateInternal({
|
|
289
|
+
...getRouterStateInternal(),
|
|
290
|
+
navigation: { state: "loading", location: to },
|
|
291
|
+
});
|
|
292
|
+
notifyListeners();
|
|
293
|
+
|
|
294
|
+
// 데이터 fetch (signal 연결로 취소 가능)
|
|
295
|
+
const dataUrl = `${url.pathname}${url.search ? url.search + "&" : "?"}_data=1`;
|
|
296
|
+
const response = await fetch(dataUrl, { signal: controller.signal });
|
|
297
|
+
|
|
298
|
+
// 이 요청이 이미 abort된 경우 (새 네비게이션이 시작됨) 무시
|
|
299
|
+
if (controller.signal.aborted) return;
|
|
300
|
+
|
|
301
|
+
if (!response.ok) {
|
|
302
|
+
// 에러 시 full navigation fallback
|
|
303
|
+
window.location.href = to;
|
|
304
|
+
return;
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
const data = await response.json();
|
|
308
|
+
|
|
309
|
+
// json 파싱 사이에 새 네비게이션이 시작됐을 수 있음
|
|
310
|
+
if (controller.signal.aborted) return;
|
|
311
|
+
|
|
312
|
+
// 상태 + History + 스크롤을 한 번에 적용하는 함수
|
|
313
|
+
const applyUpdate = () => {
|
|
314
|
+
const historyState = { routeId: data.routeId, params: data.params };
|
|
315
|
+
if (replace) {
|
|
316
|
+
history.replaceState(historyState, "", to);
|
|
317
|
+
} else {
|
|
318
|
+
history.pushState(historyState, "", to);
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
setRouterStateInternal({
|
|
322
|
+
currentRoute: {
|
|
323
|
+
id: data.routeId,
|
|
324
|
+
pattern: data.pattern,
|
|
325
|
+
params: data.params,
|
|
326
|
+
},
|
|
327
|
+
loaderData: data.loaderData,
|
|
328
|
+
actionData: undefined,
|
|
329
|
+
navigation: { state: "idle" },
|
|
330
|
+
});
|
|
331
|
+
setServerData(data.routeId, data.loaderData);
|
|
332
|
+
notifyListeners();
|
|
333
|
+
if (scroll) window.scrollTo(0, 0);
|
|
334
|
+
};
|
|
335
|
+
|
|
336
|
+
// View Transitions API — 브라우저 지원 시 URL + DOM 전환을 동기화
|
|
337
|
+
if (!replace && "startViewTransition" in document) {
|
|
338
|
+
(document as any).startViewTransition(applyUpdate);
|
|
339
|
+
} else {
|
|
340
|
+
applyUpdate();
|
|
341
|
+
}
|
|
342
|
+
} catch (error) {
|
|
343
|
+
// abort된 네비게이션은 조용히 무시 (새 네비게이션이 대체함)
|
|
344
|
+
if (controller.signal.aborted) return;
|
|
345
|
+
|
|
346
|
+
console.error("[Mandu Router] Navigation failed:", error);
|
|
347
|
+
// 에러 시 full navigation fallback
|
|
348
|
+
window.location.href = to;
|
|
349
|
+
} finally {
|
|
350
|
+
// 이 controller가 아직 active면 정리
|
|
351
|
+
if (activeNavigationController === controller) {
|
|
352
|
+
activeNavigationController = null;
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
/**
|
|
358
|
+
* 뒤로가기/앞으로가기 처리
|
|
359
|
+
*/
|
|
360
|
+
function handlePopState(event: PopStateEvent): void {
|
|
361
|
+
const state = event.state;
|
|
362
|
+
|
|
363
|
+
if (state?.routeId) {
|
|
364
|
+
// Mandu로 방문한 페이지 - 데이터 다시 fetch
|
|
365
|
+
navigate(window.location.pathname + window.location.search, {
|
|
366
|
+
replace: true,
|
|
367
|
+
scroll: false,
|
|
368
|
+
});
|
|
369
|
+
} else {
|
|
370
|
+
// SPA 네비게이션 이력이 아닌 페이지 — 현재 라우터 상태 유지
|
|
371
|
+
// (getManduRoute()는 SSR 초기값이라 SPA 네비게이션 후에는 stale)
|
|
372
|
+
const current = getGlobalRouterState();
|
|
373
|
+
setGlobalRouterState({
|
|
374
|
+
...current,
|
|
375
|
+
actionData: undefined,
|
|
376
|
+
navigation: { state: "idle" },
|
|
377
|
+
});
|
|
378
|
+
notifyListeners();
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
// ========== State Management ==========
|
|
383
|
+
|
|
384
|
+
/**
|
|
385
|
+
* 리스너에게 상태 변경 알림
|
|
386
|
+
*/
|
|
387
|
+
function notifyListeners(): void {
|
|
388
|
+
const state = getRouterStateInternal();
|
|
389
|
+
for (const listener of listeners.current) {
|
|
390
|
+
try {
|
|
391
|
+
listener(state);
|
|
392
|
+
} catch (error) {
|
|
393
|
+
console.error("[Mandu Router] Listener error:", error);
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
/**
|
|
399
|
+
* 상태 변경 구독
|
|
400
|
+
*/
|
|
401
|
+
export function subscribe(listener: RouterListener): () => void {
|
|
402
|
+
listeners.current.add(listener);
|
|
403
|
+
return () => listeners.current.delete(listener);
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
/**
|
|
407
|
+
* 현재 라우터 상태 가져오기
|
|
408
|
+
*/
|
|
409
|
+
export function getRouterState(): RouterState {
|
|
410
|
+
return getRouterStateInternal();
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
/**
|
|
414
|
+
* 현재 라우트 정보 가져오기
|
|
415
|
+
*/
|
|
416
|
+
export function getCurrentRoute(): RouteInfo | null {
|
|
417
|
+
return getRouterStateInternal().currentRoute;
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
/**
|
|
421
|
+
* 현재 loader 데이터 가져오기
|
|
422
|
+
*/
|
|
423
|
+
export function getLoaderData<T = unknown>(): T | undefined {
|
|
424
|
+
return getRouterStateInternal().loaderData as T | undefined;
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
/**
|
|
428
|
+
* 네비게이션 상태 가져오기
|
|
429
|
+
*/
|
|
430
|
+
export function getNavigationState(): NavigationState {
|
|
431
|
+
return getRouterStateInternal().navigation;
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
// ========== Link Click Handler ==========
|
|
435
|
+
|
|
436
|
+
/**
|
|
437
|
+
* 링크 클릭 이벤트 핸들러 (이벤트 위임용)
|
|
438
|
+
*/
|
|
439
|
+
function handleLinkClick(event: MouseEvent): void {
|
|
440
|
+
// 기본 동작 조건 체크
|
|
441
|
+
if (
|
|
442
|
+
event.defaultPrevented ||
|
|
443
|
+
event.button !== 0 ||
|
|
444
|
+
event.metaKey ||
|
|
445
|
+
event.altKey ||
|
|
446
|
+
event.ctrlKey ||
|
|
447
|
+
event.shiftKey
|
|
448
|
+
) {
|
|
449
|
+
return;
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
// 가장 가까운 앵커 태그 찾기
|
|
453
|
+
const anchor = (event.target as HTMLElement).closest("a");
|
|
454
|
+
if (!anchor) return;
|
|
455
|
+
|
|
456
|
+
// data-mandu-link 속성이 있는 링크만 처리
|
|
457
|
+
if (!anchor.hasAttribute("data-mandu-link")) return;
|
|
458
|
+
|
|
459
|
+
const href = anchor.getAttribute("href");
|
|
460
|
+
if (!href) return;
|
|
461
|
+
|
|
462
|
+
// 외부 링크 체크
|
|
463
|
+
try {
|
|
464
|
+
const url = new URL(href, window.location.origin);
|
|
465
|
+
if (url.origin !== window.location.origin) return;
|
|
466
|
+
} catch {
|
|
467
|
+
return;
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
// 기본 동작 방지 및 Client-side 네비게이션
|
|
471
|
+
event.preventDefault();
|
|
472
|
+
navigate(href);
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
// ========== Prefetch ==========
|
|
476
|
+
|
|
477
|
+
const prefetchedUrls = new LRUCache<string, true>(LIMITS.ROUTER_PREFETCH_CACHE);
|
|
478
|
+
|
|
479
|
+
/**
|
|
480
|
+
* 페이지 데이터 미리 로드
|
|
481
|
+
*/
|
|
482
|
+
export async function prefetch(url: string): Promise<void> {
|
|
483
|
+
if (prefetchedUrls.has(url)) return;
|
|
484
|
+
|
|
485
|
+
try {
|
|
486
|
+
const parsed = new URL(url, window.location.origin);
|
|
487
|
+
const dataUrl = `${parsed.pathname}${parsed.search ? parsed.search + "&" : "?"}_data=1`;
|
|
488
|
+
await fetch(dataUrl, { priority: "low" } as RequestInit);
|
|
489
|
+
prefetchedUrls.set(url, true);
|
|
490
|
+
} catch {
|
|
491
|
+
// Prefetch 실패는 무시
|
|
492
|
+
}
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
// ========== Action Submission ==========
|
|
496
|
+
|
|
497
|
+
/**
|
|
498
|
+
* 서버 action 제출 (mutation)
|
|
499
|
+
* 응답에 _revalidated가 있으면 loaderData를 자동 갱신
|
|
500
|
+
*/
|
|
501
|
+
/** 진행 중인 action의 AbortController */
|
|
502
|
+
let activeActionController: AbortController | null = null;
|
|
503
|
+
|
|
504
|
+
export async function submitAction(
|
|
505
|
+
url: string,
|
|
506
|
+
data: FormData | Record<string, unknown>,
|
|
507
|
+
actionName: string,
|
|
508
|
+
method: string = "POST"
|
|
509
|
+
): Promise<ActionResult> {
|
|
510
|
+
// 진행 중인 navigate 취소 (action이 우선)
|
|
511
|
+
if (activeNavigationController) {
|
|
512
|
+
activeNavigationController.abort();
|
|
513
|
+
activeNavigationController = null;
|
|
514
|
+
}
|
|
515
|
+
// 진행 중인 이전 action 취소
|
|
516
|
+
if (activeActionController) {
|
|
517
|
+
activeActionController.abort();
|
|
518
|
+
}
|
|
519
|
+
const controller = new AbortController();
|
|
520
|
+
activeActionController = controller;
|
|
521
|
+
|
|
522
|
+
// submitting 상태 시작
|
|
523
|
+
setRouterStateInternal({
|
|
524
|
+
...getRouterStateInternal(),
|
|
525
|
+
navigation: { state: "submitting", formAction: url },
|
|
526
|
+
});
|
|
527
|
+
notifyListeners();
|
|
528
|
+
|
|
529
|
+
try {
|
|
530
|
+
const isFormData = data instanceof FormData;
|
|
531
|
+
const body = isFormData ? data : JSON.stringify({ _action: actionName, ...data });
|
|
532
|
+
const headers: Record<string, string> = {
|
|
533
|
+
"X-Requested-With": "ManduAction",
|
|
534
|
+
"Accept": "application/json",
|
|
535
|
+
};
|
|
536
|
+
if (!isFormData) {
|
|
537
|
+
headers["Content-Type"] = "application/json";
|
|
538
|
+
} else if (!data.has("_action")) {
|
|
539
|
+
data.set("_action", actionName);
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
const response = await fetch(url, {
|
|
543
|
+
method: method.toUpperCase(),
|
|
544
|
+
body,
|
|
545
|
+
headers,
|
|
546
|
+
signal: controller.signal,
|
|
547
|
+
});
|
|
548
|
+
|
|
549
|
+
if (controller.signal.aborted) return { ok: false };
|
|
550
|
+
|
|
551
|
+
// JSON 파싱 (실패 시 빈 객체 — 타입 안전)
|
|
552
|
+
let result: Record<string, unknown> = {};
|
|
553
|
+
const contentType = response.headers.get("content-type") ?? "";
|
|
554
|
+
if (contentType.includes("application/json")) {
|
|
555
|
+
try {
|
|
556
|
+
const parsed = await response.json();
|
|
557
|
+
if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) {
|
|
558
|
+
result = parsed as Record<string, unknown>;
|
|
559
|
+
}
|
|
560
|
+
} catch {
|
|
561
|
+
// malformed JSON — result stays {}
|
|
562
|
+
}
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
if (controller.signal.aborted) return { ok: false };
|
|
566
|
+
|
|
567
|
+
// Revalidation: 서버가 loader를 재실행해서 fresh data를 보내줬으면 갱신
|
|
568
|
+
const nextActionData = result._revalidated ? result.actionData : result;
|
|
569
|
+
|
|
570
|
+
if (result._revalidated && result.loaderData !== undefined) {
|
|
571
|
+
setRouterStateInternal({
|
|
572
|
+
...getRouterStateInternal(),
|
|
573
|
+
loaderData: result.loaderData,
|
|
574
|
+
actionData: nextActionData,
|
|
575
|
+
navigation: { state: "idle" },
|
|
576
|
+
});
|
|
577
|
+
|
|
578
|
+
const currentRoute = getRouterStateInternal().currentRoute;
|
|
579
|
+
if (currentRoute) {
|
|
580
|
+
setServerData(currentRoute.id, result.loaderData);
|
|
581
|
+
}
|
|
582
|
+
} else {
|
|
583
|
+
setRouterStateInternal({
|
|
584
|
+
...getRouterStateInternal(),
|
|
585
|
+
actionData: nextActionData,
|
|
586
|
+
navigation: { state: "idle" },
|
|
587
|
+
});
|
|
588
|
+
}
|
|
589
|
+
|
|
590
|
+
notifyListeners();
|
|
591
|
+
return {
|
|
592
|
+
ok: response.ok,
|
|
593
|
+
actionData: nextActionData,
|
|
594
|
+
loaderData: result._revalidated ? result.loaderData as unknown : undefined,
|
|
595
|
+
};
|
|
596
|
+
} catch (error) {
|
|
597
|
+
if (controller.signal.aborted) return { ok: false };
|
|
598
|
+
|
|
599
|
+
setRouterStateInternal({
|
|
600
|
+
...getRouterStateInternal(),
|
|
601
|
+
navigation: { state: "idle" },
|
|
602
|
+
});
|
|
603
|
+
notifyListeners();
|
|
604
|
+
return { ok: false };
|
|
605
|
+
} finally {
|
|
606
|
+
if (activeActionController === controller) {
|
|
607
|
+
activeActionController = null;
|
|
608
|
+
}
|
|
609
|
+
}
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
/**
|
|
613
|
+
* 현재 action 데이터 가져오기
|
|
614
|
+
*/
|
|
615
|
+
export function getActionData<T = unknown>(): T | undefined {
|
|
616
|
+
return getRouterStateInternal().actionData as T | undefined;
|
|
617
|
+
}
|
|
618
|
+
|
|
619
|
+
// ========== Initialization ==========
|
|
620
|
+
|
|
621
|
+
let initialized = false;
|
|
622
|
+
|
|
623
|
+
/**
|
|
624
|
+
* 라우터 초기화
|
|
625
|
+
*/
|
|
626
|
+
export function initializeRouter(): void {
|
|
627
|
+
if (typeof window === "undefined" || initialized) return;
|
|
628
|
+
|
|
629
|
+
initialized = true;
|
|
630
|
+
|
|
631
|
+
// 서버 데이터로 초기화
|
|
632
|
+
initializeFromServer();
|
|
633
|
+
|
|
634
|
+
// popstate 이벤트 리스너
|
|
635
|
+
window.addEventListener("popstate", handlePopState);
|
|
636
|
+
|
|
637
|
+
// 링크 클릭 이벤트 위임
|
|
638
|
+
document.addEventListener("click", handleLinkClick);
|
|
639
|
+
|
|
640
|
+
console.log("[Mandu Router] Initialized");
|
|
641
|
+
}
|
|
642
|
+
|
|
643
|
+
/**
|
|
644
|
+
* 라우터 정리
|
|
645
|
+
*/
|
|
646
|
+
export function cleanupRouter(): void {
|
|
647
|
+
if (typeof window === "undefined" || !initialized) return;
|
|
648
|
+
|
|
649
|
+
// 진행 중인 네비게이션/액션 취소
|
|
650
|
+
if (activeNavigationController) {
|
|
651
|
+
activeNavigationController.abort();
|
|
652
|
+
activeNavigationController = null;
|
|
653
|
+
}
|
|
654
|
+
if (activeActionController) {
|
|
655
|
+
activeActionController.abort();
|
|
656
|
+
activeActionController = null;
|
|
657
|
+
}
|
|
658
|
+
|
|
659
|
+
window.removeEventListener("popstate", handlePopState);
|
|
660
|
+
document.removeEventListener("click", handleLinkClick);
|
|
661
|
+
listeners.current.clear();
|
|
662
|
+
initialized = false;
|
|
663
|
+
}
|
|
664
|
+
|
|
665
|
+
// 자동 초기화 (DOM 준비 시)
|
|
666
|
+
if (typeof window !== "undefined") {
|
|
667
|
+
if (document.readyState === "loading") {
|
|
668
|
+
document.addEventListener("DOMContentLoaded", initializeRouter);
|
|
669
|
+
} else {
|
|
670
|
+
initializeRouter();
|
|
671
|
+
}
|
|
672
|
+
}
|