what-router 0.5.5 → 0.6.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.md CHANGED
@@ -181,7 +181,7 @@ enableScrollRestoration(); // call once at app entry
181
181
  ## Links
182
182
 
183
183
  - [Documentation](https://whatfw.com)
184
- - [GitHub](https://github.com/CelsianJs/whatfw)
184
+ - [GitHub](https://github.com/CelsianJs/what-framework)
185
185
 
186
186
  ## License
187
187
 
package/dist/index.js ADDED
@@ -0,0 +1,505 @@
1
+ // packages/router/src/index.js
2
+ import { signal, effect, computed, batch, h, ErrorBoundary } from "what-core";
3
+ function isSafeUrl(url) {
4
+ if (typeof url !== "string") return false;
5
+ const trimmed = url.trim();
6
+ const normalized = trimmed.replace(/[\s\x00-\x1f]/g, "").toLowerCase();
7
+ if (normalized.startsWith("javascript:")) return false;
8
+ if (normalized.startsWith("data:")) return false;
9
+ if (normalized.startsWith("vbscript:")) return false;
10
+ return true;
11
+ }
12
+ var _url = signal(typeof location !== "undefined" ? location.pathname + location.search + location.hash : "/");
13
+ var _params = signal({});
14
+ var _query = signal({});
15
+ var _isNavigating = signal(false);
16
+ var _navigationError = signal(null);
17
+ var route = {
18
+ get url() {
19
+ return _url();
20
+ },
21
+ get path() {
22
+ return _url().split("?")[0].split("#")[0];
23
+ },
24
+ get params() {
25
+ return _params();
26
+ },
27
+ get query() {
28
+ return _query();
29
+ },
30
+ get hash() {
31
+ const h2 = _url().split("#")[1];
32
+ return h2 ? "#" + h2 : "";
33
+ },
34
+ get isNavigating() {
35
+ return _isNavigating();
36
+ },
37
+ get error() {
38
+ return _navigationError();
39
+ }
40
+ };
41
+ async function navigate(to, opts = {}) {
42
+ const { replace = false, state = null, transition = true, _fromPopstate = false } = opts;
43
+ if (!isSafeUrl(to)) {
44
+ if (typeof console !== "undefined") {
45
+ console.warn(`[what-router] Blocked navigation to unsafe URL: ${to}`);
46
+ }
47
+ return;
48
+ }
49
+ if (typeof window !== "undefined" && to.startsWith("#")) {
50
+ const currentUrl = _url();
51
+ const basePath = currentUrl.split("#")[0];
52
+ const newUrl = basePath + to;
53
+ history.replaceState(state, "", newUrl);
54
+ _url.set(newUrl);
55
+ const el = document.querySelector(to);
56
+ if (el) el.scrollIntoView({ behavior: "smooth" });
57
+ return;
58
+ }
59
+ if (to === _url()) return;
60
+ if (_isNavigating.peek()) return;
61
+ _isNavigating.set(true);
62
+ _navigationError.set(null);
63
+ const doNavigation = () => {
64
+ if (!_fromPopstate) {
65
+ if (typeof window !== "undefined") {
66
+ scrollPositions.set(_url(), { x: scrollX, y: scrollY });
67
+ }
68
+ if (replace) {
69
+ history.replaceState(state, "", to);
70
+ } else {
71
+ history.pushState(state, "", to);
72
+ }
73
+ }
74
+ _url.set(to);
75
+ _isNavigating.set(false);
76
+ };
77
+ if (transition && typeof document !== "undefined" && document.startViewTransition) {
78
+ try {
79
+ await document.startViewTransition(doNavigation).finished;
80
+ } catch (e) {
81
+ }
82
+ } else {
83
+ doNavigation();
84
+ }
85
+ }
86
+ if (typeof window !== "undefined") {
87
+ window.addEventListener("popstate", () => {
88
+ scrollPositions.set(_url(), { x: scrollX, y: scrollY });
89
+ const newUrl = location.pathname + location.search + location.hash;
90
+ navigate(newUrl, { replace: true, _fromPopstate: true, transition: false }).then(() => {
91
+ const saved = scrollPositions.get(newUrl);
92
+ if (saved) {
93
+ requestAnimationFrame(() => window.scrollTo(saved.x, saved.y));
94
+ }
95
+ });
96
+ });
97
+ }
98
+ function compilePath(path) {
99
+ const normalized = path.replace(/\([\w-]+\)\//g, "").replace(/\[\.\.\.(\w+)\]/g, (_, name) => `*:${name}`).replace(/\[(\w+)\]/g, ":$1");
100
+ const paramNames = [];
101
+ let catchAll = null;
102
+ const regexStr = normalized.split("/").map((segment) => {
103
+ if (segment.startsWith("*:")) {
104
+ catchAll = segment.slice(2);
105
+ paramNames.push(catchAll);
106
+ return "(.+)";
107
+ }
108
+ if (segment === "*") {
109
+ catchAll = "rest";
110
+ paramNames.push("rest");
111
+ return "(.+)";
112
+ }
113
+ if (segment.startsWith(":")) {
114
+ paramNames.push(segment.slice(1));
115
+ return "([^/]+)";
116
+ }
117
+ return segment.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
118
+ }).join("/");
119
+ const regex = new RegExp(`^${regexStr}$`);
120
+ return { regex, paramNames, catchAll };
121
+ }
122
+ function matchRoute(path, routes) {
123
+ const routable = routes.filter((r) => r.path);
124
+ const sorted = routable.sort((a, b) => {
125
+ const aSpecific = (a.path.match(/:/g) || []).length + (a.path.includes("*") ? 100 : 0);
126
+ const bSpecific = (b.path.match(/:/g) || []).length + (b.path.includes("*") ? 100 : 0);
127
+ return aSpecific - bSpecific;
128
+ });
129
+ for (const route2 of sorted) {
130
+ const { regex, paramNames } = compilePath(route2.path);
131
+ const match = path.match(regex);
132
+ if (match) {
133
+ const params = {};
134
+ paramNames.forEach((name, i) => {
135
+ params[name] = decodeURIComponent(match[i + 1]);
136
+ });
137
+ return { route: route2, params };
138
+ }
139
+ }
140
+ return null;
141
+ }
142
+ function parseQuery(search) {
143
+ const params = {};
144
+ if (!search) return params;
145
+ const qs = search.startsWith("?") ? search.slice(1) : search;
146
+ for (const pair of qs.split("&")) {
147
+ const [key, val] = pair.split("=");
148
+ if (!key) continue;
149
+ const decodedKey = decodeURIComponent(key);
150
+ const decodedVal = val ? decodeURIComponent(val) : "";
151
+ if (decodedKey in params) {
152
+ if (Array.isArray(params[decodedKey])) {
153
+ params[decodedKey].push(decodedVal);
154
+ } else {
155
+ params[decodedKey] = [params[decodedKey], decodedVal];
156
+ }
157
+ } else {
158
+ params[decodedKey] = decodedVal;
159
+ }
160
+ }
161
+ return params;
162
+ }
163
+ function buildLayoutChain(route2, routes) {
164
+ const layouts = [];
165
+ if (!route2.path) return layouts;
166
+ const segments = route2.path.split("/").filter(Boolean);
167
+ let currentPath = "";
168
+ for (const segment of segments) {
169
+ currentPath += "/" + segment;
170
+ const layoutRoute = routes.find(
171
+ (r) => r.layout && r.path === currentPath + "/_layout"
172
+ );
173
+ if (layoutRoute) {
174
+ layouts.push(layoutRoute.layout);
175
+ }
176
+ }
177
+ if (route2.layout) {
178
+ layouts.push(route2.layout);
179
+ }
180
+ return layouts;
181
+ }
182
+ var _redirectHistory = [];
183
+ var MAX_REDIRECTS = 10;
184
+ function Router({ routes, fallback, globalLayout }) {
185
+ return () => {
186
+ const currentUrl = _url();
187
+ const path = currentUrl.split("?")[0].split("#")[0];
188
+ const search = currentUrl.split("?")[1]?.split("#")[0] || "";
189
+ const isNavigating = _isNavigating();
190
+ const matched = matchRoute(path, routes);
191
+ if (matched) {
192
+ batch(() => {
193
+ _params.set(matched.params);
194
+ _query.set(parseQuery(search));
195
+ });
196
+ const { route: r, params } = matched;
197
+ const queryObj = parseQuery(search);
198
+ if (r.middleware && r.middleware.length > 0) {
199
+ for (const mw of r.middleware) {
200
+ const result = mw({ path, params, query: queryObj, route: r });
201
+ if (result === false) {
202
+ if (fallback) return h(fallback, {});
203
+ return h("div", { class: "what-403" }, h("h1", null, "403"), h("p", null, "Access denied"));
204
+ }
205
+ if (typeof result === "string") {
206
+ _redirectHistory.push(result);
207
+ if (_redirectHistory.length > MAX_REDIRECTS) {
208
+ const cycle = _redirectHistory.slice(-5).join(" \u2192 ");
209
+ _redirectHistory.length = 0;
210
+ console.error(`[what-router] Redirect loop detected: ${cycle}`);
211
+ _isNavigating.set(false);
212
+ return h(
213
+ "div",
214
+ { class: "what-redirect-loop" },
215
+ h("h1", null, "Redirect Loop"),
216
+ h("p", null, "Too many redirects. Check your middleware configuration.")
217
+ );
218
+ }
219
+ const seen = /* @__PURE__ */ new Set();
220
+ let hasCycle = false;
221
+ for (const url of _redirectHistory) {
222
+ if (seen.has(url)) {
223
+ hasCycle = true;
224
+ break;
225
+ }
226
+ seen.add(url);
227
+ }
228
+ if (hasCycle) {
229
+ const cycle = _redirectHistory.join(" \u2192 ");
230
+ _redirectHistory.length = 0;
231
+ console.error(`[what-router] Redirect cycle detected: ${cycle}`);
232
+ _isNavigating.set(false);
233
+ return h(
234
+ "div",
235
+ { class: "what-redirect-loop" },
236
+ h("h1", null, "Redirect Loop"),
237
+ h("p", null, "Circular redirect detected. Check your middleware configuration.")
238
+ );
239
+ }
240
+ navigate(result, { replace: true });
241
+ return null;
242
+ }
243
+ }
244
+ }
245
+ _redirectHistory.length = 0;
246
+ let element;
247
+ if (r.loading && isNavigating) {
248
+ element = h(r.loading, {});
249
+ } else {
250
+ element = h(r.component, {
251
+ params,
252
+ query: queryObj,
253
+ route: r
254
+ });
255
+ }
256
+ if (r.error) {
257
+ element = h(ErrorBoundary, { fallback: r.error }, element);
258
+ }
259
+ const layouts = buildLayoutChain(r, routes);
260
+ for (const Layout of layouts.reverse()) {
261
+ element = h(Layout, { params, query: queryObj }, element);
262
+ }
263
+ if (globalLayout) {
264
+ element = h(globalLayout, {}, element);
265
+ }
266
+ return element;
267
+ }
268
+ if (fallback) return h(fallback, {});
269
+ return h(
270
+ "div",
271
+ { class: "what-404" },
272
+ h("h1", null, "404"),
273
+ h("p", null, "Page not found")
274
+ );
275
+ };
276
+ }
277
+ function Link({
278
+ href,
279
+ class: cls,
280
+ className,
281
+ children,
282
+ replace: rep,
283
+ prefetch: shouldPrefetch = true,
284
+ activeClass = "active",
285
+ exactActiveClass = "exact-active",
286
+ transition = true,
287
+ ...rest
288
+ }) {
289
+ const safeHref = isSafeUrl(href) ? href : "about:blank";
290
+ if (!isSafeUrl(href) && typeof console !== "undefined") {
291
+ console.warn(`[what-router] Link blocked unsafe href: ${href}`);
292
+ }
293
+ const hrefPath = safeHref.split("?")[0].split("#")[0];
294
+ const reactiveClass = () => {
295
+ const currentPath = route.path;
296
+ const isActive = hrefPath === "/" ? currentPath === "/" : currentPath === hrefPath || currentPath.startsWith(hrefPath + "/");
297
+ const isExactActive = currentPath === hrefPath;
298
+ return [
299
+ cls || className,
300
+ isActive && activeClass,
301
+ isExactActive && exactActiveClass
302
+ ].filter(Boolean).join(" ") || void 0;
303
+ };
304
+ return h("a", {
305
+ href: safeHref,
306
+ class: reactiveClass,
307
+ onclick: (e) => {
308
+ if (e.ctrlKey || e.metaKey || e.shiftKey || e.altKey || e.button !== 0) return;
309
+ e.preventDefault();
310
+ navigate(safeHref, { replace: rep, transition });
311
+ },
312
+ onmouseenter: shouldPrefetch ? () => prefetch(safeHref) : void 0,
313
+ ...rest
314
+ }, ...Array.isArray(children) ? children : [children]);
315
+ }
316
+ function NavLink(props) {
317
+ return Link(props);
318
+ }
319
+ function defineRoutes(config) {
320
+ return Object.entries(config).map(([path, value]) => {
321
+ if (typeof value === "function") {
322
+ return { path, component: value };
323
+ }
324
+ return { path, ...value };
325
+ });
326
+ }
327
+ function nestedRoutes(basePath, children, options = {}) {
328
+ const { layout, loading, error } = options;
329
+ return children.map((child) => ({
330
+ ...child,
331
+ path: basePath + child.path,
332
+ layout: child.layout || layout,
333
+ loading: child.loading || loading,
334
+ error: child.error || error
335
+ }));
336
+ }
337
+ function routeGroup(name, routes, options = {}) {
338
+ const { layout, middleware } = options;
339
+ return routes.map((route2) => ({
340
+ ...route2,
341
+ _group: name,
342
+ layout: route2.layout || layout,
343
+ middleware: [...route2.middleware || [], ...middleware || []]
344
+ }));
345
+ }
346
+ function Redirect({ to }) {
347
+ navigate(to, { replace: true });
348
+ return null;
349
+ }
350
+ function guard(check, fallback) {
351
+ return (Component) => {
352
+ return function GuardedRoute(props) {
353
+ const result = check(props);
354
+ if (result instanceof Promise) {
355
+ return h("div", { class: "what-guard-loading" }, "Loading...");
356
+ }
357
+ if (result) {
358
+ return h(Component, props);
359
+ }
360
+ if (typeof fallback === "string") {
361
+ navigate(fallback, { replace: true });
362
+ return null;
363
+ }
364
+ return h(fallback, props);
365
+ };
366
+ };
367
+ }
368
+ function asyncGuard(check, options = {}) {
369
+ const { fallback = "/login", loading = null } = options;
370
+ return (Component) => {
371
+ return function AsyncGuardedRoute(props) {
372
+ const status = signal("pending");
373
+ const checkResult = signal(null);
374
+ let cancelled = false;
375
+ effect(() => {
376
+ cancelled = false;
377
+ Promise.resolve(check(props)).then((result) => {
378
+ if (cancelled) return;
379
+ checkResult.set(result);
380
+ status.set(result ? "allowed" : "denied");
381
+ }).catch(() => {
382
+ if (!cancelled) status.set("denied");
383
+ });
384
+ return () => {
385
+ cancelled = true;
386
+ };
387
+ });
388
+ return () => {
389
+ const currentStatus = status();
390
+ if (currentStatus === "pending") {
391
+ return loading ? h(loading, {}) : null;
392
+ }
393
+ if (currentStatus === "allowed") {
394
+ return h(Component, props);
395
+ }
396
+ if (typeof fallback === "string") {
397
+ navigate(fallback, { replace: true });
398
+ return null;
399
+ }
400
+ return h(fallback, props);
401
+ };
402
+ };
403
+ };
404
+ }
405
+ var prefetchedUrls = /* @__PURE__ */ new Set();
406
+ function prefetch(href) {
407
+ if (typeof document === "undefined") return;
408
+ if (prefetchedUrls.has(href)) return;
409
+ prefetchedUrls.add(href);
410
+ const link = document.createElement("link");
411
+ link.rel = "prefetch";
412
+ link.href = href;
413
+ document.head.appendChild(link);
414
+ }
415
+ var scrollPositions = /* @__PURE__ */ new Map();
416
+ function enableScrollRestoration() {
417
+ if (typeof window === "undefined") return;
418
+ window.addEventListener("beforeunload", () => {
419
+ scrollPositions.set(location.pathname, window.scrollY);
420
+ });
421
+ effect(() => {
422
+ const path = route.path;
423
+ const savedPosition = scrollPositions.get(path);
424
+ requestAnimationFrame(() => {
425
+ if (savedPosition !== void 0) {
426
+ window.scrollTo(0, savedPosition);
427
+ } else if (route.hash) {
428
+ const el = document.querySelector(route.hash);
429
+ el?.scrollIntoView();
430
+ } else {
431
+ window.scrollTo(0, 0);
432
+ }
433
+ });
434
+ });
435
+ }
436
+ function viewTransitionName(name) {
437
+ return { style: { viewTransitionName: name } };
438
+ }
439
+ function setViewTransition(type) {
440
+ if (typeof document === "undefined") return;
441
+ document.documentElement.dataset.transition = type;
442
+ }
443
+ function useRoute() {
444
+ return {
445
+ path: computed(() => route.path),
446
+ params: computed(() => route.params),
447
+ query: computed(() => route.query),
448
+ hash: computed(() => route.hash),
449
+ isNavigating: computed(() => route.isNavigating),
450
+ navigate,
451
+ prefetch
452
+ };
453
+ }
454
+ function Outlet({ children }) {
455
+ return children || null;
456
+ }
457
+ function FileRouter({
458
+ routes,
459
+ layout: globalLayout,
460
+ fallback,
461
+ error: globalError
462
+ }) {
463
+ const routerRoutes = routes.map((r) => ({
464
+ path: r.path,
465
+ component: r.component,
466
+ layout: r.layout || void 0,
467
+ // Attach page mode as metadata for build system
468
+ _mode: r.mode || "client"
469
+ }));
470
+ return Router({
471
+ routes: routerRoutes,
472
+ globalLayout,
473
+ fallback: fallback || Default404
474
+ });
475
+ }
476
+ function Default404() {
477
+ return h(
478
+ "div",
479
+ { style: "text-align:center;padding:60px 20px" },
480
+ h("h1", { style: "font-size:48px;margin-bottom:8px" }, "404"),
481
+ h("p", { style: "color:#64748b" }, "Page not found")
482
+ );
483
+ }
484
+ export {
485
+ FileRouter,
486
+ Link,
487
+ NavLink,
488
+ Outlet,
489
+ Redirect,
490
+ Router,
491
+ asyncGuard,
492
+ defineRoutes,
493
+ enableScrollRestoration,
494
+ guard,
495
+ isSafeUrl,
496
+ navigate,
497
+ nestedRoutes,
498
+ prefetch,
499
+ route,
500
+ routeGroup,
501
+ setViewTransition,
502
+ useRoute,
503
+ viewTransitionName
504
+ };
505
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../src/index.js"],
4
+ "sourcesContent": ["// What Framework - Router\n// Production-grade file-based routing with nested layouts, loading states,\n// route groups, view transitions, and middleware.\n\nimport { signal, effect, computed, batch, h, ErrorBoundary } from 'what-core';\n\n// --- URL Sanitization ---\n// Rejects javascript:, data:, vbscript: protocols (case-insensitive, trimmed).\n\nexport function isSafeUrl(url) {\n if (typeof url !== 'string') return false;\n const trimmed = url.trim();\n // Check for dangerous protocols (case-insensitive, ignoring whitespace/control chars)\n const normalized = trimmed.replace(/[\\s\\x00-\\x1f]/g, '').toLowerCase();\n if (normalized.startsWith('javascript:')) return false;\n if (normalized.startsWith('data:')) return false;\n if (normalized.startsWith('vbscript:')) return false;\n return true;\n}\n\n// --- Route State (global singleton) ---\n\nconst _url = signal(typeof location !== 'undefined' ? location.pathname + location.search + location.hash : '/');\nconst _params = signal({});\nconst _query = signal({});\nconst _isNavigating = signal(false);\nconst _navigationError = signal(null);\n\nexport const route = {\n get url() { return _url(); },\n get path() { return _url().split('?')[0].split('#')[0]; },\n get params() { return _params(); },\n get query() { return _query(); },\n get hash() {\n const h = _url().split('#')[1];\n return h ? '#' + h : '';\n },\n get isNavigating() { return _isNavigating(); },\n get error() { return _navigationError(); },\n};\n\n// --- Navigation with View Transitions ---\n\nexport async function navigate(to, opts = {}) {\n const { replace = false, state = null, transition = true, _fromPopstate = false } = opts;\n\n // Reject unsafe URLs\n if (!isSafeUrl(to)) {\n if (typeof console !== 'undefined') {\n console.warn(`[what-router] Blocked navigation to unsafe URL: ${to}`);\n }\n return;\n }\n\n // Handle same-page hash links \u2014 use replaceState and scroll directly\n if (typeof window !== 'undefined' && to.startsWith('#')) {\n const currentUrl = _url();\n const basePath = currentUrl.split('#')[0];\n const newUrl = basePath + to;\n history.replaceState(state, '', newUrl);\n _url.set(newUrl);\n const el = document.querySelector(to);\n if (el) el.scrollIntoView({ behavior: 'smooth' });\n return;\n }\n\n // Don't navigate if already on the same URL\n if (to === _url()) return;\n\n // Prevent concurrent navigations \u2014 wait for current to finish\n if (_isNavigating.peek()) return;\n\n _isNavigating.set(true);\n _navigationError.set(null);\n\n const doNavigation = () => {\n // Skip history manipulation on popstate (browser already updated the URL)\n if (!_fromPopstate) {\n // Save scroll position for current URL before navigating away\n if (typeof window !== 'undefined') {\n scrollPositions.set(_url(), { x: scrollX, y: scrollY });\n }\n if (replace) {\n history.replaceState(state, '', to);\n } else {\n history.pushState(state, '', to);\n }\n }\n _url.set(to);\n _isNavigating.set(false);\n };\n\n // Use View Transitions API if available and enabled\n if (transition && typeof document !== 'undefined' && document.startViewTransition) {\n try {\n await document.startViewTransition(doNavigation).finished;\n } catch (e) {\n // Transition failed, navigation still happened\n }\n } else {\n doNavigation();\n }\n}\n\n// Back/forward support \u2014 route through navigate() so middleware runs\nif (typeof window !== 'undefined') {\n window.addEventListener('popstate', () => {\n // Save scroll position for the URL we're leaving\n scrollPositions.set(_url(), { x: scrollX, y: scrollY });\n\n const newUrl = location.pathname + location.search + location.hash;\n // Use _fromPopstate flag so navigate() skips pushState (browser already updated URL)\n navigate(newUrl, { replace: true, _fromPopstate: true, transition: false }).then(() => {\n // Restore saved scroll position for the URL we're arriving at\n const saved = scrollPositions.get(newUrl);\n if (saved) {\n requestAnimationFrame(() => window.scrollTo(saved.x, saved.y));\n }\n });\n });\n}\n\n// --- Route Matching ---\n\nfunction compilePath(path) {\n // /users/:id -> regex + param names\n // /posts/* -> catch-all\n // /[slug] -> dynamic (file-based syntax)\n // (group) -> route group (ignored in URL)\n\n // Remove route groups from path (they don't affect URL matching)\n const normalized = path\n .replace(/\\([\\w-]+\\)\\//g, '') // Remove (group)/ prefixes\n .replace(/\\[\\.\\.\\.(\\w+)\\]/g, (_, name) => `*:${name}`) // Preserve catch-all name\n .replace(/\\[(\\w+)\\]/g, ':$1'); // File-based [param] to :param\n\n const paramNames = [];\n let catchAll = null;\n\n const regexStr = normalized\n .split('/')\n .map(segment => {\n if (segment.startsWith('*:')) {\n catchAll = segment.slice(2);\n paramNames.push(catchAll);\n return '(.+)';\n }\n if (segment === '*') {\n catchAll = 'rest';\n paramNames.push('rest');\n return '(.+)';\n }\n if (segment.startsWith(':')) {\n paramNames.push(segment.slice(1));\n return '([^/]+)';\n }\n return segment.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\$&');\n })\n .join('/');\n\n const regex = new RegExp(`^${regexStr}$`);\n return { regex, paramNames, catchAll };\n}\n\nfunction matchRoute(path, routes) {\n // Filter out routes without a path (layout-only routes, etc.)\n const routable = routes.filter(r => r.path);\n\n // Sort routes by specificity (more specific first)\n const sorted = routable.sort((a, b) => {\n const aSpecific = (a.path.match(/:/g) || []).length + (a.path.includes('*') ? 100 : 0);\n const bSpecific = (b.path.match(/:/g) || []).length + (b.path.includes('*') ? 100 : 0);\n return aSpecific - bSpecific;\n });\n\n for (const route of sorted) {\n const { regex, paramNames } = compilePath(route.path);\n const match = path.match(regex);\n if (match) {\n const params = {};\n paramNames.forEach((name, i) => {\n params[name] = decodeURIComponent(match[i + 1]);\n });\n return { route, params };\n }\n }\n return null;\n}\n\nfunction parseQuery(search) {\n const params = {};\n if (!search) return params;\n const qs = search.startsWith('?') ? search.slice(1) : search;\n for (const pair of qs.split('&')) {\n const [key, val] = pair.split('=');\n if (!key) continue;\n const decodedKey = decodeURIComponent(key);\n const decodedVal = val ? decodeURIComponent(val) : '';\n if (decodedKey in params) {\n // Collect repeated keys into arrays\n if (Array.isArray(params[decodedKey])) {\n params[decodedKey].push(decodedVal);\n } else {\n params[decodedKey] = [params[decodedKey], decodedVal];\n }\n } else {\n params[decodedKey] = decodedVal;\n }\n }\n return params;\n}\n\n// --- Nested Layouts ---\n\n// Build the layout chain for a route\nfunction buildLayoutChain(route, routes) {\n const layouts = [];\n if (!route.path) return layouts;\n\n // Check for nested layouts based on path segments\n const segments = route.path.split('/').filter(Boolean);\n let currentPath = '';\n\n for (const segment of segments) {\n currentPath += '/' + segment;\n\n // Find layout for this path level\n const layoutRoute = routes.find(r =>\n r.layout && r.path === currentPath + '/_layout'\n );\n if (layoutRoute) {\n layouts.push(layoutRoute.layout);\n }\n }\n\n // Add route's own layout if specified\n if (route.layout) {\n layouts.push(route.layout);\n }\n\n return layouts;\n}\n\n// --- Middleware redirect loop detection ---\nconst _redirectHistory = [];\nconst MAX_REDIRECTS = 10;\n\n// --- Router Component ---\n\nexport function Router({ routes, fallback, globalLayout }) {\n // Return a reactive function child. The Router component runs ONCE,\n // but the returned function re-evaluates whenever _url changes,\n // and the fine-grained runtime updates the DOM accordingly.\n return () => {\n const currentUrl = _url();\n const path = currentUrl.split('?')[0].split('#')[0];\n const search = currentUrl.split('?')[1]?.split('#')[0] || '';\n const isNavigating = _isNavigating();\n\n const matched = matchRoute(path, routes);\n\n if (matched) {\n batch(() => {\n _params.set(matched.params);\n _query.set(parseQuery(search));\n });\n\n const { route: r, params } = matched;\n const queryObj = parseQuery(search);\n\n // Run middleware (sync only \u2014 async middleware should use asyncGuard)\n if (r.middleware && r.middleware.length > 0) {\n for (const mw of r.middleware) {\n const result = mw({ path, params, query: queryObj, route: r });\n if (result === false) {\n // Middleware rejected \u2014 show fallback\n if (fallback) return h(fallback, {});\n return h('div', { class: 'what-403' }, h('h1', null, '403'), h('p', null, 'Access denied'));\n }\n if (typeof result === 'string') {\n // Redirect loop detection\n _redirectHistory.push(result);\n if (_redirectHistory.length > MAX_REDIRECTS) {\n const cycle = _redirectHistory.slice(-5).join(' \u2192 ');\n _redirectHistory.length = 0;\n console.error(`[what-router] Redirect loop detected: ${cycle}`);\n _isNavigating.set(false);\n return h('div', { class: 'what-redirect-loop' },\n h('h1', null, 'Redirect Loop'),\n h('p', null, 'Too many redirects. Check your middleware configuration.')\n );\n }\n // Check for direct cycle (A \u2192 B \u2192 A)\n const seen = new Set();\n let hasCycle = false;\n for (const url of _redirectHistory) {\n if (seen.has(url)) { hasCycle = true; break; }\n seen.add(url);\n }\n if (hasCycle) {\n const cycle = _redirectHistory.join(' \u2192 ');\n _redirectHistory.length = 0;\n console.error(`[what-router] Redirect cycle detected: ${cycle}`);\n _isNavigating.set(false);\n return h('div', { class: 'what-redirect-loop' },\n h('h1', null, 'Redirect Loop'),\n h('p', null, 'Circular redirect detected. Check your middleware configuration.')\n );\n }\n // Middleware returned a redirect path\n navigate(result, { replace: true });\n return null;\n }\n }\n }\n // Successful render \u2014 clear redirect history\n _redirectHistory.length = 0;\n\n // Build element with loading state support\n let element;\n\n if (r.loading && isNavigating) {\n element = h(r.loading, {});\n } else {\n element = h(r.component, {\n params,\n query: queryObj,\n route: r,\n });\n }\n\n // Wrap with per-route error boundary if specified\n if (r.error) {\n element = h(ErrorBoundary, { fallback: r.error }, element);\n }\n\n // Wrap with nested layouts (innermost to outermost)\n const layouts = buildLayoutChain(r, routes);\n for (const Layout of layouts.reverse()) {\n element = h(Layout, { params, query: queryObj }, element);\n }\n\n // Global layout wrapper\n if (globalLayout) {\n element = h(globalLayout, {}, element);\n }\n\n return element;\n }\n\n // 404\n if (fallback) return h(fallback, {});\n return h('div', { class: 'what-404' },\n h('h1', null, '404'),\n h('p', null, 'Page not found')\n );\n };\n}\n\n// --- Link Component ---\n\nexport function Link({\n href,\n class: cls,\n className,\n children,\n replace: rep,\n prefetch: shouldPrefetch = true,\n activeClass = 'active',\n exactActiveClass = 'exact-active',\n transition = true,\n ...rest\n}) {\n // Sanitize href \u2014 reject dangerous protocols\n const safeHref = isSafeUrl(href) ? href : 'about:blank';\n if (!isSafeUrl(href) && typeof console !== 'undefined') {\n console.warn(`[what-router] Link blocked unsafe href: ${href}`);\n }\n\n // Strip query string and hash from href for path comparison\n const hrefPath = safeHref.split('?')[0].split('#')[0];\n\n // Use a reactive function for class so active states update on navigation.\n // In the run-once model, reading route.path directly would snapshot it.\n const reactiveClass = () => {\n const currentPath = route.path;\n const isActive = hrefPath === '/'\n ? currentPath === '/'\n : currentPath === hrefPath || currentPath.startsWith(hrefPath + '/');\n const isExactActive = currentPath === hrefPath;\n\n return [\n cls || className,\n isActive && activeClass,\n isExactActive && exactActiveClass,\n ].filter(Boolean).join(' ') || undefined;\n };\n\n return h('a', {\n href: safeHref,\n class: reactiveClass,\n onclick: (e) => {\n // Only intercept left-clicks without modifiers\n if (e.ctrlKey || e.metaKey || e.shiftKey || e.altKey || e.button !== 0) return;\n e.preventDefault();\n navigate(safeHref, { replace: rep, transition });\n },\n onmouseenter: shouldPrefetch ? () => prefetch(safeHref) : undefined,\n ...rest,\n }, ...(Array.isArray(children) ? children : [children]));\n}\n\n// --- NavLink with active states ---\n\nexport function NavLink(props) {\n return Link(props);\n}\n\n// --- Define Routes Helper ---\n// Creates route config from a flat object for convenience.\n\nexport function defineRoutes(config) {\n return Object.entries(config).map(([path, value]) => {\n if (typeof value === 'function') {\n return { path, component: value };\n }\n // Object form with layout, middleware, loading, error, etc.\n return { path, ...value };\n });\n}\n\n// --- Nested Route Helper ---\n\nexport function nestedRoutes(basePath, children, options = {}) {\n const { layout, loading, error } = options;\n\n return children.map(child => ({\n ...child,\n path: basePath + child.path,\n layout: child.layout || layout,\n loading: child.loading || loading,\n error: child.error || error,\n }));\n}\n\n// --- Route Groups ---\n// Group routes without affecting URL structure\n\nexport function routeGroup(name, routes, options = {}) {\n const { layout, middleware } = options;\n\n return routes.map(route => ({\n ...route,\n _group: name,\n layout: route.layout || layout,\n middleware: [...(route.middleware || []), ...(middleware || [])],\n }));\n}\n\n// --- Redirect ---\n\nexport function Redirect({ to }) {\n navigate(to, { replace: true });\n return null;\n}\n\n// --- Route Guards / Middleware ---\n\nexport function guard(check, fallback) {\n return (Component) => {\n return function GuardedRoute(props) {\n const result = check(props);\n\n // Support async guards\n if (result instanceof Promise) {\n // Return loading while checking\n return h('div', { class: 'what-guard-loading' }, 'Loading...');\n }\n\n if (result) {\n return h(Component, props);\n }\n\n if (typeof fallback === 'string') {\n navigate(fallback, { replace: true });\n return null;\n }\n return h(fallback, props);\n };\n };\n}\n\n// Async guard with suspense\nexport function asyncGuard(check, options = {}) {\n const { fallback = '/login', loading = null } = options;\n\n return (Component) => {\n return function AsyncGuardedRoute(props) {\n const status = signal('pending');\n const checkResult = signal(null);\n let cancelled = false;\n\n effect(() => {\n cancelled = false;\n Promise.resolve(check(props))\n .then(result => {\n if (cancelled) return;\n checkResult.set(result);\n status.set(result ? 'allowed' : 'denied');\n })\n .catch(() => {\n if (!cancelled) status.set('denied');\n });\n return () => { cancelled = true; };\n });\n\n // Return a reactive function child so status changes update the DOM.\n // Components run once, so reading status() outside a reactive wrapper\n // would snapshot the value and never update.\n return () => {\n const currentStatus = status();\n\n if (currentStatus === 'pending') {\n return loading ? h(loading, {}) : null;\n }\n\n if (currentStatus === 'allowed') {\n return h(Component, props);\n }\n\n if (typeof fallback === 'string') {\n navigate(fallback, { replace: true });\n return null;\n }\n return h(fallback, props);\n };\n };\n };\n}\n\n// --- Prefetch ---\n// Hint the browser to prefetch a route's assets.\n\nconst prefetchedUrls = new Set();\n\nexport function prefetch(href) {\n if (typeof document === 'undefined') return;\n if (prefetchedUrls.has(href)) return;\n prefetchedUrls.add(href);\n\n const link = document.createElement('link');\n link.rel = 'prefetch';\n link.href = href;\n document.head.appendChild(link);\n}\n\n// --- Scroll Restoration ---\n\nconst scrollPositions = new Map();\n\nexport function enableScrollRestoration() {\n if (typeof window === 'undefined') return;\n\n // Save scroll position before navigation\n window.addEventListener('beforeunload', () => {\n scrollPositions.set(location.pathname, window.scrollY);\n });\n\n // Restore scroll position after navigation\n effect(() => {\n const path = route.path;\n const savedPosition = scrollPositions.get(path);\n\n requestAnimationFrame(() => {\n if (savedPosition !== undefined) {\n window.scrollTo(0, savedPosition);\n } else if (route.hash) {\n const el = document.querySelector(route.hash);\n el?.scrollIntoView();\n } else {\n window.scrollTo(0, 0);\n }\n });\n });\n}\n\n// --- View Transition Helpers ---\n\nexport function viewTransitionName(name) {\n return { style: { viewTransitionName: name } };\n}\n\n// Configure view transition types\nexport function setViewTransition(type) {\n if (typeof document === 'undefined') return;\n document.documentElement.dataset.transition = type;\n}\n\n// --- useRoute Hook ---\n\nexport function useRoute() {\n return {\n path: computed(() => route.path),\n params: computed(() => route.params),\n query: computed(() => route.query),\n hash: computed(() => route.hash),\n isNavigating: computed(() => route.isNavigating),\n navigate,\n prefetch,\n };\n}\n\n// --- Outlet Component ---\n// For nested route rendering\n\nexport function Outlet({ children }) {\n // Children passed from parent layout\n return children || null;\n}\n\n// --- File-Based Router ---\n// Consumes routes generated by what-compiler's file router (virtual:what-routes).\n// Usage:\n// import { routes } from 'virtual:what-routes';\n// mount(<FileRouter routes={routes} />, '#app');\n\nexport function FileRouter({\n routes,\n layout: globalLayout,\n fallback,\n error: globalError,\n}) {\n // Convert file-router route format to Router's expected format\n const routerRoutes = routes.map(r => ({\n path: r.path,\n component: r.component,\n layout: r.layout || undefined,\n // Attach page mode as metadata for build system\n _mode: r.mode || 'client',\n }));\n\n // Router already returns a reactive function child \u2014 just delegate\n return Router({\n routes: routerRoutes,\n globalLayout,\n fallback: fallback || Default404,\n });\n}\n\nfunction Default404() {\n return h('div', { style: 'text-align:center;padding:60px 20px' },\n h('h1', { style: 'font-size:48px;margin-bottom:8px' }, '404'),\n h('p', { style: 'color:#64748b' }, 'Page not found'),\n );\n}\n"],
5
+ "mappings": ";AAIA,SAAS,QAAQ,QAAQ,UAAU,OAAO,GAAG,qBAAqB;AAK3D,SAAS,UAAU,KAAK;AAC7B,MAAI,OAAO,QAAQ,SAAU,QAAO;AACpC,QAAM,UAAU,IAAI,KAAK;AAEzB,QAAM,aAAa,QAAQ,QAAQ,kBAAkB,EAAE,EAAE,YAAY;AACrE,MAAI,WAAW,WAAW,aAAa,EAAG,QAAO;AACjD,MAAI,WAAW,WAAW,OAAO,EAAG,QAAO;AAC3C,MAAI,WAAW,WAAW,WAAW,EAAG,QAAO;AAC/C,SAAO;AACT;AAIA,IAAM,OAAO,OAAO,OAAO,aAAa,cAAc,SAAS,WAAW,SAAS,SAAS,SAAS,OAAO,GAAG;AAC/G,IAAM,UAAU,OAAO,CAAC,CAAC;AACzB,IAAM,SAAS,OAAO,CAAC,CAAC;AACxB,IAAM,gBAAgB,OAAO,KAAK;AAClC,IAAM,mBAAmB,OAAO,IAAI;AAE7B,IAAM,QAAQ;AAAA,EACnB,IAAI,MAAM;AAAE,WAAO,KAAK;AAAA,EAAG;AAAA,EAC3B,IAAI,OAAO;AAAE,WAAO,KAAK,EAAE,MAAM,GAAG,EAAE,CAAC,EAAE,MAAM,GAAG,EAAE,CAAC;AAAA,EAAG;AAAA,EACxD,IAAI,SAAS;AAAE,WAAO,QAAQ;AAAA,EAAG;AAAA,EACjC,IAAI,QAA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IAAE;AAAA,IAAO,EAAE,OAAO,sCAAsC;AAAA,IAC7D,EAAE,MAAM,EAAE,OAAO,mCAAmC,GAAG,KAAK;AAAA,IAC5D,EAAE,KAAK,EAAE,OAAO,gBAAgB,GAAG,gBAAgB;AAAA,EACrD;AACF;",
6
+ "names": ["h", "route"]
7
+ }
@@ -0,0 +1,2 @@
1
+ import{signal as w,effect as _,computed as b,batch as $,h as a,ErrorBoundary as W}from"what-core";function k(e){if(typeof e!="string")return!1;let n=e.trim().replace(/[\s\x00-\x1f]/g,"").toLowerCase();return!(n.startsWith("javascript:")||n.startsWith("data:")||n.startsWith("vbscript:"))}var h=w(typeof location<"u"?location.pathname+location.search+location.hash:"/"),L=w({}),N=w({}),g=w(!1),U=w(null),m={get url(){return h()},get path(){return h().split("?")[0].split("#")[0]},get params(){return L()},get query(){return N()},get hash(){let e=h().split("#")[1];return e?"#"+e:""},get isNavigating(){return g()},get error(){return U()}};async function x(e,t={}){let{replace:n=!1,state:s=null,transition:o=!0,_fromPopstate:i=!1}=t;if(!k(e)){typeof console<"u"&&console.warn(`[what-router] Blocked navigation to unsafe URL: ${e}`);return}if(typeof window<"u"&&e.startsWith("#")){let f=h().split("#")[0]+e;history.replaceState(s,"",f),h.set(f);let l=document.querySelector(e);l&&l.scrollIntoView({behavior:"smooth"});return}if(e===h()||g.peek())return;g.set(!0),U.set(null);let r=()=>{i||(typeof window<"u"&&P.set(h(),{x:scrollX,y:scrollY}),n?history.replaceState(s,"",e):history.pushState(s,"",e)),h.set(e),g.set(!1)};if(o&&typeof document<"u"&&document.startViewTransition)try{await document.startViewTransition(r).finished}catch{}else r()}typeof window<"u"&&window.addEventListener("popstate",()=>{P.set(h(),{x:scrollX,y:scrollY});let e=location.pathname+location.search+location.hash;x(e,{replace:!0,_fromPopstate:!0,transition:!1}).then(()=>{let t=P.get(e);t&&requestAnimationFrame(()=>window.scrollTo(t.x,t.y))})});function j(e){let t=e.replace(/\([\w-]+\)\//g,"").replace(/\[\.\.\.(\w+)\]/g,(r,c)=>`*:${c}`).replace(/\[(\w+)\]/g,":$1"),n=[],s=null,o=t.split("/").map(r=>r.startsWith("*:")?(s=r.slice(2),n.push(s),"(.+)"):r==="*"?(s="rest",n.push("rest"),"(.+)"):r.startsWith(":")?(n.push(r.slice(1)),"([^/]+)"):r.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")).join("/");return{regex:new RegExp(`^${o}$`),paramNames:n,catchAll:s}}function I(e,t){let s=t.filter(o=>o.path).sort((o,i)=>{let r=(o.path.match(/:/g)||[]).length+(o.path.includes("*")?100:0),c=(i.path.match(/:/g)||[]).length+(i.path.includes("*")?100:0);return r-c});for(let o of s){let{regex:i,paramNames:r}=j(o.path),c=e.match(i);if(c){let u={};return r.forEach((f,l)=>{u[f]=decodeURIComponent(c[l+1])}),{route:o,params:u}}}return null}function C(e){let t={};if(!e)return t;let n=e.startsWith("?")?e.slice(1):e;for(let s of n.split("&")){let[o,i]=s.split("=");if(!o)continue;let r=decodeURIComponent(o),c=i?decodeURIComponent(i):"";r in t?Array.isArray(t[r])?t[r].push(c):t[r]=[t[r],c]:t[r]=c}return t}function V(e,t){let n=[];if(!e.path)return n;let s=e.path.split("/").filter(Boolean),o="";for(let i of s){o+="/"+i;let r=t.find(c=>c.layout&&c.path===o+"/_layout");r&&n.push(r.layout)}return e.layout&&n.push(e.layout),n}var y=[],K=10;function B({routes:e,fallback:t,globalLayout:n}){return()=>{let s=h(),o=s.split("?")[0].split("#")[0],i=s.split("?")[1]?.split("#")[0]||"",r=g(),c=I(o,e);if(c){$(()=>{L.set(c.params),N.set(C(i))});let{route:u,params:f}=c,l=C(i);if(u.middleware&&u.middleware.length>0)for(let d of u.middleware){let v=d({path:o,params:f,query:l,route:u});if(v===!1)return t?a(t,{}):a("div",{class:"what-403"},a("h1",null,"403"),a("p",null,"Access denied"));if(typeof v=="string"){if(y.push(v),y.length>K){let R=y.slice(-5).join(" \u2192 ");return y.length=0,console.error(`[what-router] Redirect loop detected: ${R}`),g.set(!1),a("div",{class:"what-redirect-loop"},a("h1",null,"Redirect Loop"),a("p",null,"Too many redirects. Check your middleware configuration."))}let S=new Set,A=!1;for(let R of y){if(S.has(R)){A=!0;break}S.add(R)}if(A){let R=y.join(" \u2192 ");return y.length=0,console.error(`[what-router] Redirect cycle detected: ${R}`),g.set(!1),a("div",{class:"what-redirect-loop"},a("h1",null,"Redirect Loop"),a("p",null,"Circular redirect detected. Check your middleware configuration."))}return x(v,{replace:!0}),null}}y.length=0;let p;u.loading&&r?p=a(u.loading,{}):p=a(u.component,{params:f,query:l,route:u}),u.error&&(p=a(W,{fallback:u.error},p));let q=V(u,e);for(let d of q.reverse())p=a(d,{params:f,query:l},p);return n&&(p=a(n,{},p)),p}return t?a(t,{}):a("div",{class:"what-404"},a("h1",null,"404"),a("p",null,"Page not found"))}}function G({href:e,class:t,className:n,children:s,replace:o,prefetch:i=!0,activeClass:r="active",exactActiveClass:c="exact-active",transition:u=!0,...f}){let l=k(e)?e:"about:blank";!k(e)&&typeof console<"u"&&console.warn(`[what-router] Link blocked unsafe href: ${e}`);let p=l.split("?")[0].split("#")[0];return a("a",{href:l,class:()=>{let d=m.path,v=p==="/"?d==="/":d===p||d.startsWith(p+"/");return[t||n,v&&r,d===p&&c].filter(Boolean).join(" ")||void 0},onclick:d=>{d.ctrlKey||d.metaKey||d.shiftKey||d.altKey||d.button!==0||(d.preventDefault(),x(l,{replace:o,transition:u}))},onmouseenter:i?()=>T(l):void 0,...f},...Array.isArray(s)?s:[s])}function F(e){return G(e)}function O(e){return Object.entries(e).map(([t,n])=>typeof n=="function"?{path:t,component:n}:{path:t,...n})}function X(e,t,n={}){let{layout:s,loading:o,error:i}=n;return t.map(r=>({...r,path:e+r.path,layout:r.layout||s,loading:r.loading||o,error:r.error||i}))}function Y(e,t,n={}){let{layout:s,middleware:o}=n;return t.map(i=>({...i,_group:e,layout:i.layout||s,middleware:[...i.middleware||[],...o||[]]}))}function H({to:e}){return x(e,{replace:!0}),null}function M(e,t){return n=>function(o){let i=e(o);return i instanceof Promise?a("div",{class:"what-guard-loading"},"Loading..."):i?a(n,o):typeof t=="string"?(x(t,{replace:!0}),null):a(t,o)}}function Q(e,t={}){let{fallback:n="/login",loading:s=null}=t;return o=>function(r){let c=w("pending"),u=w(null),f=!1;return _(()=>(f=!1,Promise.resolve(e(r)).then(l=>{f||(u.set(l),c.set(l?"allowed":"denied"))}).catch(()=>{f||c.set("denied")}),()=>{f=!0})),()=>{let l=c();return l==="pending"?s?a(s,{}):null:l==="allowed"?a(o,r):typeof n=="string"?(x(n,{replace:!0}),null):a(n,r)}}}var E=new Set;function T(e){if(typeof document>"u"||E.has(e))return;E.add(e);let t=document.createElement("link");t.rel="prefetch",t.href=e,document.head.appendChild(t)}var P=new Map;function J(){typeof window>"u"||(window.addEventListener("beforeunload",()=>{P.set(location.pathname,window.scrollY)}),_(()=>{let e=m.path,t=P.get(e);requestAnimationFrame(()=>{t!==void 0?window.scrollTo(0,t):m.hash?document.querySelector(m.hash)?.scrollIntoView():window.scrollTo(0,0)})}))}function Z(e){return{style:{viewTransitionName:e}}}function ee(e){typeof document>"u"||(document.documentElement.dataset.transition=e)}function te(){return{path:b(()=>m.path),params:b(()=>m.params),query:b(()=>m.query),hash:b(()=>m.hash),isNavigating:b(()=>m.isNavigating),navigate:x,prefetch:T}}function ne({children:e}){return e||null}function re({routes:e,layout:t,fallback:n,error:s}){let o=e.map(i=>({path:i.path,component:i.component,layout:i.layout||void 0,_mode:i.mode||"client"}));return B({routes:o,globalLayout:t,fallback:n||z})}function z(){return a("div",{style:"text-align:center;padding:60px 20px"},a("h1",{style:"font-size:48px;margin-bottom:8px"},"404"),a("p",{style:"color:#64748b"},"Page not found"))}export{re as FileRouter,G as Link,F as NavLink,ne as Outlet,H as Redirect,B as Router,Q as asyncGuard,O as defineRoutes,J as enableScrollRestoration,M as guard,k as isSafeUrl,x as navigate,X as nestedRoutes,T as prefetch,m as route,Y as routeGroup,ee as setViewTransition,te as useRoute,Z as viewTransitionName};
2
+ //# sourceMappingURL=index.min.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../src/index.js"],
4
+ "sourcesContent": ["// What Framework - Router\n// Production-grade file-based routing with nested layouts, loading states,\n// route groups, view transitions, and middleware.\n\nimport { signal, effect, computed, batch, h, ErrorBoundary } from 'what-core';\n\n// --- URL Sanitization ---\n// Rejects javascript:, data:, vbscript: protocols (case-insensitive, trimmed).\n\nexport function isSafeUrl(url) {\n if (typeof url !== 'string') return false;\n const trimmed = url.trim();\n // Check for dangerous protocols (case-insensitive, ignoring whitespace/control chars)\n const normalized = trimmed.replace(/[\\s\\x00-\\x1f]/g, '').toLowerCase();\n if (normalized.startsWith('javascript:')) return false;\n if (normalized.startsWith('data:')) return false;\n if (normalized.startsWith('vbscript:')) return false;\n return true;\n}\n\n// --- Route State (global singleton) ---\n\nconst _url = signal(typeof location !== 'undefined' ? location.pathname + location.search + location.hash : '/');\nconst _params = signal({});\nconst _query = signal({});\nconst _isNavigating = signal(false);\nconst _navigationError = signal(null);\n\nexport const route = {\n get url() { return _url(); },\n get path() { return _url().split('?')[0].split('#')[0]; },\n get params() { return _params(); },\n get query() { return _query(); },\n get hash() {\n const h = _url().split('#')[1];\n return h ? '#' + h : '';\n },\n get isNavigating() { return _isNavigating(); },\n get error() { return _navigationError(); },\n};\n\n// --- Navigation with View Transitions ---\n\nexport async function navigate(to, opts = {}) {\n const { replace = false, state = null, transition = true, _fromPopstate = false } = opts;\n\n // Reject unsafe URLs\n if (!isSafeUrl(to)) {\n if (typeof console !== 'undefined') {\n console.warn(`[what-router] Blocked navigation to unsafe URL: ${to}`);\n }\n return;\n }\n\n // Handle same-page hash links \u2014 use replaceState and scroll directly\n if (typeof window !== 'undefined' && to.startsWith('#')) {\n const currentUrl = _url();\n const basePath = currentUrl.split('#')[0];\n const newUrl = basePath + to;\n history.replaceState(state, '', newUrl);\n _url.set(newUrl);\n const el = document.querySelector(to);\n if (el) el.scrollIntoView({ behavior: 'smooth' });\n return;\n }\n\n // Don't navigate if already on the same URL\n if (to === _url()) return;\n\n // Prevent concurrent navigations \u2014 wait for current to finish\n if (_isNavigating.peek()) return;\n\n _isNavigating.set(true);\n _navigationError.set(null);\n\n const doNavigation = () => {\n // Skip history manipulation on popstate (browser already updated the URL)\n if (!_fromPopstate) {\n // Save scroll position for current URL before navigating away\n if (typeof window !== 'undefined') {\n scrollPositions.set(_url(), { x: scrollX, y: scrollY });\n }\n if (replace) {\n history.replaceState(state, '', to);\n } else {\n history.pushState(state, '', to);\n }\n }\n _url.set(to);\n _isNavigating.set(false);\n };\n\n // Use View Transitions API if available and enabled\n if (transition && typeof document !== 'undefined' && document.startViewTransition) {\n try {\n await document.startViewTransition(doNavigation).finished;\n } catch (e) {\n // Transition failed, navigation still happened\n }\n } else {\n doNavigation();\n }\n}\n\n// Back/forward support \u2014 route through navigate() so middleware runs\nif (typeof window !== 'undefined') {\n window.addEventListener('popstate', () => {\n // Save scroll position for the URL we're leaving\n scrollPositions.set(_url(), { x: scrollX, y: scrollY });\n\n const newUrl = location.pathname + location.search + location.hash;\n // Use _fromPopstate flag so navigate() skips pushState (browser already updated URL)\n navigate(newUrl, { replace: true, _fromPopstate: true, transition: false }).then(() => {\n // Restore saved scroll position for the URL we're arriving at\n const saved = scrollPositions.get(newUrl);\n if (saved) {\n requestAnimationFrame(() => window.scrollTo(saved.x, saved.y));\n }\n });\n });\n}\n\n// --- Route Matching ---\n\nfunction compilePath(path) {\n // /users/:id -> regex + param names\n // /posts/* -> catch-all\n // /[slug] -> dynamic (file-based syntax)\n // (group) -> route group (ignored in URL)\n\n // Remove route groups from path (they don't affect URL matching)\n const normalized = path\n .replace(/\\([\\w-]+\\)\\//g, '') // Remove (group)/ prefixes\n .replace(/\\[\\.\\.\\.(\\w+)\\]/g, (_, name) => `*:${name}`) // Preserve catch-all name\n .replace(/\\[(\\w+)\\]/g, ':$1'); // File-based [param] to :param\n\n const paramNames = [];\n let catchAll = null;\n\n const regexStr = normalized\n .split('/')\n .map(segment => {\n if (segment.startsWith('*:')) {\n catchAll = segment.slice(2);\n paramNames.push(catchAll);\n return '(.+)';\n }\n if (segment === '*') {\n catchAll = 'rest';\n paramNames.push('rest');\n return '(.+)';\n }\n if (segment.startsWith(':')) {\n paramNames.push(segment.slice(1));\n return '([^/]+)';\n }\n return segment.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\$&');\n })\n .join('/');\n\n const regex = new RegExp(`^${regexStr}$`);\n return { regex, paramNames, catchAll };\n}\n\nfunction matchRoute(path, routes) {\n // Filter out routes without a path (layout-only routes, etc.)\n const routable = routes.filter(r => r.path);\n\n // Sort routes by specificity (more specific first)\n const sorted = routable.sort((a, b) => {\n const aSpecific = (a.path.match(/:/g) || []).length + (a.path.includes('*') ? 100 : 0);\n const bSpecific = (b.path.match(/:/g) || []).length + (b.path.includes('*') ? 100 : 0);\n return aSpecific - bSpecific;\n });\n\n for (const route of sorted) {\n const { regex, paramNames } = compilePath(route.path);\n const match = path.match(regex);\n if (match) {\n const params = {};\n paramNames.forEach((name, i) => {\n params[name] = decodeURIComponent(match[i + 1]);\n });\n return { route, params };\n }\n }\n return null;\n}\n\nfunction parseQuery(search) {\n const params = {};\n if (!search) return params;\n const qs = search.startsWith('?') ? search.slice(1) : search;\n for (const pair of qs.split('&')) {\n const [key, val] = pair.split('=');\n if (!key) continue;\n const decodedKey = decodeURIComponent(key);\n const decodedVal = val ? decodeURIComponent(val) : '';\n if (decodedKey in params) {\n // Collect repeated keys into arrays\n if (Array.isArray(params[decodedKey])) {\n params[decodedKey].push(decodedVal);\n } else {\n params[decodedKey] = [params[decodedKey], decodedVal];\n }\n } else {\n params[decodedKey] = decodedVal;\n }\n }\n return params;\n}\n\n// --- Nested Layouts ---\n\n// Build the layout chain for a route\nfunction buildLayoutChain(route, routes) {\n const layouts = [];\n if (!route.path) return layouts;\n\n // Check for nested layouts based on path segments\n const segments = route.path.split('/').filter(Boolean);\n let currentPath = '';\n\n for (const segment of segments) {\n currentPath += '/' + segment;\n\n // Find layout for this path level\n const layoutRoute = routes.find(r =>\n r.layout && r.path === currentPath + '/_layout'\n );\n if (layoutRoute) {\n layouts.push(layoutRoute.layout);\n }\n }\n\n // Add route's own layout if specified\n if (route.layout) {\n layouts.push(route.layout);\n }\n\n return layouts;\n}\n\n// --- Middleware redirect loop detection ---\nconst _redirectHistory = [];\nconst MAX_REDIRECTS = 10;\n\n// --- Router Component ---\n\nexport function Router({ routes, fallback, globalLayout }) {\n // Return a reactive function child. The Router component runs ONCE,\n // but the returned function re-evaluates whenever _url changes,\n // and the fine-grained runtime updates the DOM accordingly.\n return () => {\n const currentUrl = _url();\n const path = currentUrl.split('?')[0].split('#')[0];\n const search = currentUrl.split('?')[1]?.split('#')[0] || '';\n const isNavigating = _isNavigating();\n\n const matched = matchRoute(path, routes);\n\n if (matched) {\n batch(() => {\n _params.set(matched.params);\n _query.set(parseQuery(search));\n });\n\n const { route: r, params } = matched;\n const queryObj = parseQuery(search);\n\n // Run middleware (sync only \u2014 async middleware should use asyncGuard)\n if (r.middleware && r.middleware.length > 0) {\n for (const mw of r.middleware) {\n const result = mw({ path, params, query: queryObj, route: r });\n if (result === false) {\n // Middleware rejected \u2014 show fallback\n if (fallback) return h(fallback, {});\n return h('div', { class: 'what-403' }, h('h1', null, '403'), h('p', null, 'Access denied'));\n }\n if (typeof result === 'string') {\n // Redirect loop detection\n _redirectHistory.push(result);\n if (_redirectHistory.length > MAX_REDIRECTS) {\n const cycle = _redirectHistory.slice(-5).join(' \u2192 ');\n _redirectHistory.length = 0;\n console.error(`[what-router] Redirect loop detected: ${cycle}`);\n _isNavigating.set(false);\n return h('div', { class: 'what-redirect-loop' },\n h('h1', null, 'Redirect Loop'),\n h('p', null, 'Too many redirects. Check your middleware configuration.')\n );\n }\n // Check for direct cycle (A \u2192 B \u2192 A)\n const seen = new Set();\n let hasCycle = false;\n for (const url of _redirectHistory) {\n if (seen.has(url)) { hasCycle = true; break; }\n seen.add(url);\n }\n if (hasCycle) {\n const cycle = _redirectHistory.join(' \u2192 ');\n _redirectHistory.length = 0;\n console.error(`[what-router] Redirect cycle detected: ${cycle}`);\n _isNavigating.set(false);\n return h('div', { class: 'what-redirect-loop' },\n h('h1', null, 'Redirect Loop'),\n h('p', null, 'Circular redirect detected. Check your middleware configuration.')\n );\n }\n // Middleware returned a redirect path\n navigate(result, { replace: true });\n return null;\n }\n }\n }\n // Successful render \u2014 clear redirect history\n _redirectHistory.length = 0;\n\n // Build element with loading state support\n let element;\n\n if (r.loading && isNavigating) {\n element = h(r.loading, {});\n } else {\n element = h(r.component, {\n params,\n query: queryObj,\n route: r,\n });\n }\n\n // Wrap with per-route error boundary if specified\n if (r.error) {\n element = h(ErrorBoundary, { fallback: r.error }, element);\n }\n\n // Wrap with nested layouts (innermost to outermost)\n const layouts = buildLayoutChain(r, routes);\n for (const Layout of layouts.reverse()) {\n element = h(Layout, { params, query: queryObj }, element);\n }\n\n // Global layout wrapper\n if (globalLayout) {\n element = h(globalLayout, {}, element);\n }\n\n return element;\n }\n\n // 404\n if (fallback) return h(fallback, {});\n return h('div', { class: 'what-404' },\n h('h1', null, '404'),\n h('p', null, 'Page not found')\n );\n };\n}\n\n// --- Link Component ---\n\nexport function Link({\n href,\n class: cls,\n className,\n children,\n replace: rep,\n prefetch: shouldPrefetch = true,\n activeClass = 'active',\n exactActiveClass = 'exact-active',\n transition = true,\n ...rest\n}) {\n // Sanitize href \u2014 reject dangerous protocols\n const safeHref = isSafeUrl(href) ? href : 'about:blank';\n if (!isSafeUrl(href) && typeof console !== 'undefined') {\n console.warn(`[what-router] Link blocked unsafe href: ${href}`);\n }\n\n // Strip query string and hash from href for path comparison\n const hrefPath = safeHref.split('?')[0].split('#')[0];\n\n // Use a reactive function for class so active states update on navigation.\n // In the run-once model, reading route.path directly would snapshot it.\n const reactiveClass = () => {\n const currentPath = route.path;\n const isActive = hrefPath === '/'\n ? currentPath === '/'\n : currentPath === hrefPath || currentPath.startsWith(hrefPath + '/');\n const isExactActive = currentPath === hrefPath;\n\n return [\n cls || className,\n isActive && activeClass,\n isExactActive && exactActiveClass,\n ].filter(Boolean).join(' ') || undefined;\n };\n\n return h('a', {\n href: safeHref,\n class: reactiveClass,\n onclick: (e) => {\n // Only intercept left-clicks without modifiers\n if (e.ctrlKey || e.metaKey || e.shiftKey || e.altKey || e.button !== 0) return;\n e.preventDefault();\n navigate(safeHref, { replace: rep, transition });\n },\n onmouseenter: shouldPrefetch ? () => prefetch(safeHref) : undefined,\n ...rest,\n }, ...(Array.isArray(children) ? children : [children]));\n}\n\n// --- NavLink with active states ---\n\nexport function NavLink(props) {\n return Link(props);\n}\n\n// --- Define Routes Helper ---\n// Creates route config from a flat object for convenience.\n\nexport function defineRoutes(config) {\n return Object.entries(config).map(([path, value]) => {\n if (typeof value === 'function') {\n return { path, component: value };\n }\n // Object form with layout, middleware, loading, error, etc.\n return { path, ...value };\n });\n}\n\n// --- Nested Route Helper ---\n\nexport function nestedRoutes(basePath, children, options = {}) {\n const { layout, loading, error } = options;\n\n return children.map(child => ({\n ...child,\n path: basePath + child.path,\n layout: child.layout || layout,\n loading: child.loading || loading,\n error: child.error || error,\n }));\n}\n\n// --- Route Groups ---\n// Group routes without affecting URL structure\n\nexport function routeGroup(name, routes, options = {}) {\n const { layout, middleware } = options;\n\n return routes.map(route => ({\n ...route,\n _group: name,\n layout: route.layout || layout,\n middleware: [...(route.middleware || []), ...(middleware || [])],\n }));\n}\n\n// --- Redirect ---\n\nexport function Redirect({ to }) {\n navigate(to, { replace: true });\n return null;\n}\n\n// --- Route Guards / Middleware ---\n\nexport function guard(check, fallback) {\n return (Component) => {\n return function GuardedRoute(props) {\n const result = check(props);\n\n // Support async guards\n if (result instanceof Promise) {\n // Return loading while checking\n return h('div', { class: 'what-guard-loading' }, 'Loading...');\n }\n\n if (result) {\n return h(Component, props);\n }\n\n if (typeof fallback === 'string') {\n navigate(fallback, { replace: true });\n return null;\n }\n return h(fallback, props);\n };\n };\n}\n\n// Async guard with suspense\nexport function asyncGuard(check, options = {}) {\n const { fallback = '/login', loading = null } = options;\n\n return (Component) => {\n return function AsyncGuardedRoute(props) {\n const status = signal('pending');\n const checkResult = signal(null);\n let cancelled = false;\n\n effect(() => {\n cancelled = false;\n Promise.resolve(check(props))\n .then(result => {\n if (cancelled) return;\n checkResult.set(result);\n status.set(result ? 'allowed' : 'denied');\n })\n .catch(() => {\n if (!cancelled) status.set('denied');\n });\n return () => { cancelled = true; };\n });\n\n // Return a reactive function child so status changes update the DOM.\n // Components run once, so reading status() outside a reactive wrapper\n // would snapshot the value and never update.\n return () => {\n const currentStatus = status();\n\n if (currentStatus === 'pending') {\n return loading ? h(loading, {}) : null;\n }\n\n if (currentStatus === 'allowed') {\n return h(Component, props);\n }\n\n if (typeof fallback === 'string') {\n navigate(fallback, { replace: true });\n return null;\n }\n return h(fallback, props);\n };\n };\n };\n}\n\n// --- Prefetch ---\n// Hint the browser to prefetch a route's assets.\n\nconst prefetchedUrls = new Set();\n\nexport function prefetch(href) {\n if (typeof document === 'undefined') return;\n if (prefetchedUrls.has(href)) return;\n prefetchedUrls.add(href);\n\n const link = document.createElement('link');\n link.rel = 'prefetch';\n link.href = href;\n document.head.appendChild(link);\n}\n\n// --- Scroll Restoration ---\n\nconst scrollPositions = new Map();\n\nexport function enableScrollRestoration() {\n if (typeof window === 'undefined') return;\n\n // Save scroll position before navigation\n window.addEventListener('beforeunload', () => {\n scrollPositions.set(location.pathname, window.scrollY);\n });\n\n // Restore scroll position after navigation\n effect(() => {\n const path = route.path;\n const savedPosition = scrollPositions.get(path);\n\n requestAnimationFrame(() => {\n if (savedPosition !== undefined) {\n window.scrollTo(0, savedPosition);\n } else if (route.hash) {\n const el = document.querySelector(route.hash);\n el?.scrollIntoView();\n } else {\n window.scrollTo(0, 0);\n }\n });\n });\n}\n\n// --- View Transition Helpers ---\n\nexport function viewTransitionName(name) {\n return { style: { viewTransitionName: name } };\n}\n\n// Configure view transition types\nexport function setViewTransition(type) {\n if (typeof document === 'undefined') return;\n document.documentElement.dataset.transition = type;\n}\n\n// --- useRoute Hook ---\n\nexport function useRoute() {\n return {\n path: computed(() => route.path),\n params: computed(() => route.params),\n query: computed(() => route.query),\n hash: computed(() => route.hash),\n isNavigating: computed(() => route.isNavigating),\n navigate,\n prefetch,\n };\n}\n\n// --- Outlet Component ---\n// For nested route rendering\n\nexport function Outlet({ children }) {\n // Children passed from parent layout\n return children || null;\n}\n\n// --- File-Based Router ---\n// Consumes routes generated by what-compiler's file router (virtual:what-routes).\n// Usage:\n// import { routes } from 'virtual:what-routes';\n// mount(<FileRouter routes={routes} />, '#app');\n\nexport function FileRouter({\n routes,\n layout: globalLayout,\n fallback,\n error: globalError,\n}) {\n // Convert file-router route format to Router's expected format\n const routerRoutes = routes.map(r => ({\n path: r.path,\n component: r.component,\n layout: r.layout || undefined,\n // Attach page mode as metadata for build system\n _mode: r.mode || 'client',\n }));\n\n // Router already returns a reactive function child \u2014 just delegate\n return Router({\n routes: routerRoutes,\n globalLayout,\n fallback: fallback || Default404,\n });\n}\n\nfunction Default404() {\n return h('div', { style: 'text-align:center;padding:60px 20px' },\n h('h1', { style: 'font-size:48px;margin-bottom:8px' }, '404'),\n h('p', { style: 'color:#64748b' }, 'Page not found'),\n );\n}\n"],
5
+ "mappings": "AAIA,OAAS,UAAAA,EAAQ,UAAAC,EAAQ,YAAAC,EAAU,SAAAC,EAAO,KAAAC,EAAG,iBAAAC,MAAqB,YAK3D,SAASC,EAAUC,EAAK,CAC7B,GAAI,OAAOA,GAAQ,SAAU,MAAO,GAGpC,IAAMC,EAFUD,EAAI,KAAK,EAEE,QAAQ,iBAAkB,EAAE,EAAE,YAAY,EAGrE,MAFI,EAAAC,EAAW,WAAW,aAAa,GACnCA,EAAW,WAAW,OAAO,GAC7BA,EAAW,WAAW,WAAW,EAEvC,CAIA,IAAMC,EAAOT,EAAO,OAAO,SAAa,IAAc,SAAS,SAAW,SAAS,OAAS,SAAS,KAAO,GAA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cZ,EAAS,IAAMY,EAAM,YAAY,EAC/C,SAAAC,EACA,SAAA2E,CACF,CACF,CAKO,SAASgC,GAAO,CAAE,SAAA1C,CAAS,EAAG,CAEnC,OAAOA,GAAY,IACrB,CAQO,SAAS2C,GAAW,CACzB,OAAAvF,EACA,OAAQ4B,EACR,SAAAD,EACA,MAAO6D,CACT,EAAG,CAED,IAAMC,EAAezF,EAAO,IAAIE,IAAM,CACpC,KAAMA,EAAE,KACR,UAAWA,EAAE,UACb,OAAQA,EAAE,QAAU,OAEpB,MAAOA,EAAE,MAAQ,QACnB,EAAE,EAGF,OAAOwB,EAAO,CACZ,OAAQ+D,EACR,aAAA7D,EACA,SAAUD,GAAY+D,CACxB,CAAC,CACH,CAEA,SAASA,GAAa,CACpB,OAAO1H,EAAE,MAAO,CAAE,MAAO,qCAAsC,EAC7DA,EAAE,KAAM,CAAE,MAAO,kCAAmC,EAAG,KAAK,EAC5DA,EAAE,IAAK,CAAE,MAAO,eAAgB,EAAG,gBAAgB,CACrD,CACF",
6
+ "names": ["signal", "effect", "computed", "batch", "h", "ErrorBoundary", "isSafeUrl", "url", "normalized", "_url", "_params", "_query", "_isNavigating", "_navigationError", "route", "navigate", "to", "opts", "replace", "state", "transition", "_fromPopstate", "newUrl", "el", "doNavigation", "scrollPositions", "saved", "compilePath", "path", "_", "name", "paramNames", "catchAll", "regexStr", "segment", "matchRoute", "routes", "sorted", "r", "a", "b", "aSpecific", "bSpecific", "regex", "match", "params", "i", "parseQuery", "search", "qs", "pair", "key", "val", "decodedKey", "decodedVal", "buildLayoutChain", "layouts", "segments", "currentPath", "layoutRoute", "_redirectHistory", "MAX_REDIRECTS", "Router", "fallback", "globalLayout", "currentUrl", "isNavigating", "matched", "queryObj", "mw", "result", "cycle", "seen", "hasCycle", "element", "Layout", "Link", "href", "cls", "className", "children", "rep", "shouldPrefetch", "activeClass", "exactActiveClass", "rest", "safeHref", "hrefPath", "isActive", "e", "prefetch", "NavLink", "props", "defineRoutes", "config", "value", "nestedRoutes", "basePath", "options", "layout", "loading", "error", "child", "routeGroup", "middleware", "Redirect", "guard", "check", "Component", "asyncGuard", "status", "checkResult", "cancelled", "currentStatus", "prefetchedUrls", "link", "enableScrollRestoration", "savedPosition", "viewTransitionName", "setViewTransition", "type", "useRoute", "Outlet", "FileRouter", "globalError", "routerRoutes", "Default404"]
7
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "what-router",
3
- "version": "0.5.5",
3
+ "version": "0.6.2",
4
4
  "description": "What Framework - File-based & programmatic router with View Transitions",
5
5
  "type": "module",
6
6
  "main": "src/index.js",
@@ -8,11 +8,13 @@
8
8
  "exports": {
9
9
  ".": {
10
10
  "types": "./index.d.ts",
11
+ "production": "./dist/index.min.js",
11
12
  "import": "./src/index.js"
12
13
  }
13
14
  },
14
15
  "files": [
15
16
  "src",
17
+ "dist",
16
18
  "index.d.ts"
17
19
  ],
18
20
  "sideEffects": false,
@@ -26,14 +28,14 @@
26
28
  "author": "ZVN DEV (https://zvndev.com)",
27
29
  "license": "MIT",
28
30
  "peerDependencies": {
29
- "what-core": "^0.5.3"
31
+ "what-core": "^0.6.2"
30
32
  },
31
33
  "repository": {
32
34
  "type": "git",
33
- "url": "https://github.com/CelsianJs/whatfw"
35
+ "url": "https://github.com/CelsianJs/what-framework"
34
36
  },
35
37
  "bugs": {
36
- "url": "https://github.com/CelsianJs/whatfw/issues"
38
+ "url": "https://github.com/CelsianJs/what-framework/issues"
37
39
  },
38
40
  "homepage": "https://whatfw.com"
39
41
  }
package/src/index.js CHANGED
@@ -4,6 +4,20 @@
4
4
 
5
5
  import { signal, effect, computed, batch, h, ErrorBoundary } from 'what-core';
6
6
 
7
+ // --- URL Sanitization ---
8
+ // Rejects javascript:, data:, vbscript: protocols (case-insensitive, trimmed).
9
+
10
+ export function isSafeUrl(url) {
11
+ if (typeof url !== 'string') return false;
12
+ const trimmed = url.trim();
13
+ // Check for dangerous protocols (case-insensitive, ignoring whitespace/control chars)
14
+ const normalized = trimmed.replace(/[\s\x00-\x1f]/g, '').toLowerCase();
15
+ if (normalized.startsWith('javascript:')) return false;
16
+ if (normalized.startsWith('data:')) return false;
17
+ if (normalized.startsWith('vbscript:')) return false;
18
+ return true;
19
+ }
20
+
7
21
  // --- Route State (global singleton) ---
8
22
 
9
23
  const _url = signal(typeof location !== 'undefined' ? location.pathname + location.search + location.hash : '/');
@@ -30,6 +44,26 @@ export const route = {
30
44
  export async function navigate(to, opts = {}) {
31
45
  const { replace = false, state = null, transition = true, _fromPopstate = false } = opts;
32
46
 
47
+ // Reject unsafe URLs
48
+ if (!isSafeUrl(to)) {
49
+ if (typeof console !== 'undefined') {
50
+ console.warn(`[what-router] Blocked navigation to unsafe URL: ${to}`);
51
+ }
52
+ return;
53
+ }
54
+
55
+ // Handle same-page hash links — use replaceState and scroll directly
56
+ if (typeof window !== 'undefined' && to.startsWith('#')) {
57
+ const currentUrl = _url();
58
+ const basePath = currentUrl.split('#')[0];
59
+ const newUrl = basePath + to;
60
+ history.replaceState(state, '', newUrl);
61
+ _url.set(newUrl);
62
+ const el = document.querySelector(to);
63
+ if (el) el.scrollIntoView({ behavior: 'smooth' });
64
+ return;
65
+ }
66
+
33
67
  // Don't navigate if already on the same URL
34
68
  if (to === _url()) return;
35
69
 
@@ -42,6 +76,10 @@ export async function navigate(to, opts = {}) {
42
76
  const doNavigation = () => {
43
77
  // Skip history manipulation on popstate (browser already updated the URL)
44
78
  if (!_fromPopstate) {
79
+ // Save scroll position for current URL before navigating away
80
+ if (typeof window !== 'undefined') {
81
+ scrollPositions.set(_url(), { x: scrollX, y: scrollY });
82
+ }
45
83
  if (replace) {
46
84
  history.replaceState(state, '', to);
47
85
  } else {
@@ -67,9 +105,18 @@ export async function navigate(to, opts = {}) {
67
105
  // Back/forward support — route through navigate() so middleware runs
68
106
  if (typeof window !== 'undefined') {
69
107
  window.addEventListener('popstate', () => {
108
+ // Save scroll position for the URL we're leaving
109
+ scrollPositions.set(_url(), { x: scrollX, y: scrollY });
110
+
70
111
  const newUrl = location.pathname + location.search + location.hash;
71
112
  // Use _fromPopstate flag so navigate() skips pushState (browser already updated URL)
72
- navigate(newUrl, { replace: true, _fromPopstate: true, transition: false });
113
+ navigate(newUrl, { replace: true, _fromPopstate: true, transition: false }).then(() => {
114
+ // Restore saved scroll position for the URL we're arriving at
115
+ const saved = scrollPositions.get(newUrl);
116
+ if (saved) {
117
+ requestAnimationFrame(() => window.scrollTo(saved.x, saved.y));
118
+ }
119
+ });
73
120
  });
74
121
  }
75
122
 
@@ -201,108 +248,113 @@ const MAX_REDIRECTS = 10;
201
248
  // --- Router Component ---
202
249
 
203
250
  export function Router({ routes, fallback, globalLayout }) {
204
- const currentUrl = _url();
205
- const path = currentUrl.split('?')[0].split('#')[0];
206
- const search = currentUrl.split('?')[1]?.split('#')[0] || '';
207
- const isNavigating = _isNavigating();
208
-
209
- const matched = matchRoute(path, routes);
210
-
211
- if (matched) {
212
- batch(() => {
213
- _params.set(matched.params);
214
- _query.set(parseQuery(search));
215
- });
251
+ // Return a reactive function child. The Router component runs ONCE,
252
+ // but the returned function re-evaluates whenever _url changes,
253
+ // and the fine-grained runtime updates the DOM accordingly.
254
+ return () => {
255
+ const currentUrl = _url();
256
+ const path = currentUrl.split('?')[0].split('#')[0];
257
+ const search = currentUrl.split('?')[1]?.split('#')[0] || '';
258
+ const isNavigating = _isNavigating();
259
+
260
+ const matched = matchRoute(path, routes);
261
+
262
+ if (matched) {
263
+ batch(() => {
264
+ _params.set(matched.params);
265
+ _query.set(parseQuery(search));
266
+ });
216
267
 
217
- const { route: r, params } = matched;
218
- const queryObj = parseQuery(search);
219
-
220
- // Run middleware (sync only — async middleware should use asyncGuard)
221
- if (r.middleware && r.middleware.length > 0) {
222
- for (const mw of r.middleware) {
223
- const result = mw({ path, params, query: queryObj, route: r });
224
- if (result === false) {
225
- // Middleware rejected — show fallback
226
- if (fallback) return h(fallback, {});
227
- return h('div', { class: 'what-403' }, h('h1', null, '403'), h('p', null, 'Access denied'));
228
- }
229
- if (typeof result === 'string') {
230
- // Redirect loop detection
231
- _redirectHistory.push(result);
232
- if (_redirectHistory.length > MAX_REDIRECTS) {
233
- const cycle = _redirectHistory.slice(-5).join(' → ');
234
- _redirectHistory.length = 0;
235
- console.error(`[what-router] Redirect loop detected: ${cycle}`);
236
- _isNavigating.set(false);
237
- return h('div', { class: 'what-redirect-loop' },
238
- h('h1', null, 'Redirect Loop'),
239
- h('p', null, 'Too many redirects. Check your middleware configuration.')
240
- );
268
+ const { route: r, params } = matched;
269
+ const queryObj = parseQuery(search);
270
+
271
+ // Run middleware (sync only — async middleware should use asyncGuard)
272
+ if (r.middleware && r.middleware.length > 0) {
273
+ for (const mw of r.middleware) {
274
+ const result = mw({ path, params, query: queryObj, route: r });
275
+ if (result === false) {
276
+ // Middleware rejected — show fallback
277
+ if (fallback) return h(fallback, {});
278
+ return h('div', { class: 'what-403' }, h('h1', null, '403'), h('p', null, 'Access denied'));
241
279
  }
242
- // Check for direct cycle (A B A)
243
- const seen = new Set();
244
- let hasCycle = false;
245
- for (const url of _redirectHistory) {
246
- if (seen.has(url)) { hasCycle = true; break; }
247
- seen.add(url);
280
+ if (typeof result === 'string') {
281
+ // Redirect loop detection
282
+ _redirectHistory.push(result);
283
+ if (_redirectHistory.length > MAX_REDIRECTS) {
284
+ const cycle = _redirectHistory.slice(-5).join(' ');
285
+ _redirectHistory.length = 0;
286
+ console.error(`[what-router] Redirect loop detected: ${cycle}`);
287
+ _isNavigating.set(false);
288
+ return h('div', { class: 'what-redirect-loop' },
289
+ h('h1', null, 'Redirect Loop'),
290
+ h('p', null, 'Too many redirects. Check your middleware configuration.')
291
+ );
292
+ }
293
+ // Check for direct cycle (A → B → A)
294
+ const seen = new Set();
295
+ let hasCycle = false;
296
+ for (const url of _redirectHistory) {
297
+ if (seen.has(url)) { hasCycle = true; break; }
298
+ seen.add(url);
299
+ }
300
+ if (hasCycle) {
301
+ const cycle = _redirectHistory.join(' → ');
302
+ _redirectHistory.length = 0;
303
+ console.error(`[what-router] Redirect cycle detected: ${cycle}`);
304
+ _isNavigating.set(false);
305
+ return h('div', { class: 'what-redirect-loop' },
306
+ h('h1', null, 'Redirect Loop'),
307
+ h('p', null, 'Circular redirect detected. Check your middleware configuration.')
308
+ );
309
+ }
310
+ // Middleware returned a redirect path
311
+ navigate(result, { replace: true });
312
+ return null;
248
313
  }
249
- if (hasCycle) {
250
- const cycle = _redirectHistory.join(' → ');
251
- _redirectHistory.length = 0;
252
- console.error(`[what-router] Redirect cycle detected: ${cycle}`);
253
- _isNavigating.set(false);
254
- return h('div', { class: 'what-redirect-loop' },
255
- h('h1', null, 'Redirect Loop'),
256
- h('p', null, 'Circular redirect detected. Check your middleware configuration.')
257
- );
258
- }
259
- // Middleware returned a redirect path
260
- navigate(result, { replace: true });
261
- return null;
262
314
  }
263
315
  }
264
- }
265
- // Successful render — clear redirect history
266
- _redirectHistory.length = 0;
316
+ // Successful render — clear redirect history
317
+ _redirectHistory.length = 0;
267
318
 
268
- // Build element with loading state support
269
- let element;
319
+ // Build element with loading state support
320
+ let element;
270
321
 
271
- if (r.loading && isNavigating) {
272
- element = h(r.loading, {});
273
- } else {
274
- element = h(r.component, {
275
- params,
276
- query: queryObj,
277
- route: r,
278
- });
279
- }
322
+ if (r.loading && isNavigating) {
323
+ element = h(r.loading, {});
324
+ } else {
325
+ element = h(r.component, {
326
+ params,
327
+ query: queryObj,
328
+ route: r,
329
+ });
330
+ }
280
331
 
281
- // Wrap with per-route error boundary if specified
282
- if (r.error) {
283
- element = h(ErrorBoundary, { fallback: r.error }, element);
284
- }
332
+ // Wrap with per-route error boundary if specified
333
+ if (r.error) {
334
+ element = h(ErrorBoundary, { fallback: r.error }, element);
335
+ }
285
336
 
286
- // Wrap with nested layouts (innermost to outermost)
287
- const layouts = buildLayoutChain(r, routes);
288
- for (const Layout of layouts.reverse()) {
289
- element = h(Layout, { params, query: queryObj }, element);
290
- }
337
+ // Wrap with nested layouts (innermost to outermost)
338
+ const layouts = buildLayoutChain(r, routes);
339
+ for (const Layout of layouts.reverse()) {
340
+ element = h(Layout, { params, query: queryObj }, element);
341
+ }
291
342
 
292
- // Global layout wrapper
293
- if (globalLayout) {
294
- element = h(globalLayout, {}, element);
295
- }
343
+ // Global layout wrapper
344
+ if (globalLayout) {
345
+ element = h(globalLayout, {}, element);
346
+ }
296
347
 
297
- return element;
298
- }
348
+ return element;
349
+ }
299
350
 
300
- // 404
301
- if (fallback) return h(fallback, {});
302
- return h('div', { class: 'what-404' },
303
- h('h1', null, '404'),
304
- h('p', null, 'Page not found')
305
- );
351
+ // 404
352
+ if (fallback) return h(fallback, {});
353
+ return h('div', { class: 'what-404' },
354
+ h('h1', null, '404'),
355
+ h('p', null, 'Page not found')
356
+ );
357
+ };
306
358
  }
307
359
 
308
360
  // --- Link Component ---
@@ -319,31 +371,41 @@ export function Link({
319
371
  transition = true,
320
372
  ...rest
321
373
  }) {
322
- const currentPath = route.path;
374
+ // Sanitize href — reject dangerous protocols
375
+ const safeHref = isSafeUrl(href) ? href : 'about:blank';
376
+ if (!isSafeUrl(href) && typeof console !== 'undefined') {
377
+ console.warn(`[what-router] Link blocked unsafe href: ${href}`);
378
+ }
379
+
323
380
  // Strip query string and hash from href for path comparison
324
- const hrefPath = href.split('?')[0].split('#')[0];
325
- // Segment-boundary matching: '/blog' matches '/blog/123' but not '/blog-archive'
326
- const isActive = hrefPath === '/'
327
- ? currentPath === '/'
328
- : currentPath === hrefPath || currentPath.startsWith(hrefPath + '/');
329
- const isExactActive = currentPath === hrefPath;
330
-
331
- const classes = [
332
- cls || className,
333
- isActive && activeClass,
334
- isExactActive && exactActiveClass,
335
- ].filter(Boolean).join(' ') || undefined;
381
+ const hrefPath = safeHref.split('?')[0].split('#')[0];
382
+
383
+ // Use a reactive function for class so active states update on navigation.
384
+ // In the run-once model, reading route.path directly would snapshot it.
385
+ const reactiveClass = () => {
386
+ const currentPath = route.path;
387
+ const isActive = hrefPath === '/'
388
+ ? currentPath === '/'
389
+ : currentPath === hrefPath || currentPath.startsWith(hrefPath + '/');
390
+ const isExactActive = currentPath === hrefPath;
391
+
392
+ return [
393
+ cls || className,
394
+ isActive && activeClass,
395
+ isExactActive && exactActiveClass,
396
+ ].filter(Boolean).join(' ') || undefined;
397
+ };
336
398
 
337
399
  return h('a', {
338
- href,
339
- class: classes,
400
+ href: safeHref,
401
+ class: reactiveClass,
340
402
  onclick: (e) => {
341
403
  // Only intercept left-clicks without modifiers
342
404
  if (e.ctrlKey || e.metaKey || e.shiftKey || e.altKey || e.button !== 0) return;
343
405
  e.preventDefault();
344
- navigate(href, { replace: rep, transition });
406
+ navigate(safeHref, { replace: rep, transition });
345
407
  },
346
- onmouseenter: shouldPrefetch ? () => prefetch(href) : undefined,
408
+ onmouseenter: shouldPrefetch ? () => prefetch(safeHref) : undefined,
347
409
  ...rest,
348
410
  }, ...(Array.isArray(children) ? children : [children]));
349
411
  }
@@ -452,21 +514,26 @@ export function asyncGuard(check, options = {}) {
452
514
  return () => { cancelled = true; };
453
515
  });
454
516
 
455
- const currentStatus = status();
517
+ // Return a reactive function child so status changes update the DOM.
518
+ // Components run once, so reading status() outside a reactive wrapper
519
+ // would snapshot the value and never update.
520
+ return () => {
521
+ const currentStatus = status();
456
522
 
457
- if (currentStatus === 'pending') {
458
- return loading ? h(loading, {}) : null;
459
- }
523
+ if (currentStatus === 'pending') {
524
+ return loading ? h(loading, {}) : null;
525
+ }
460
526
 
461
- if (currentStatus === 'allowed') {
462
- return h(Component, props);
463
- }
527
+ if (currentStatus === 'allowed') {
528
+ return h(Component, props);
529
+ }
464
530
 
465
- if (typeof fallback === 'string') {
466
- navigate(fallback, { replace: true });
467
- return null;
468
- }
469
- return h(fallback, props);
531
+ if (typeof fallback === 'string') {
532
+ navigate(fallback, { replace: true });
533
+ return null;
534
+ }
535
+ return h(fallback, props);
536
+ };
470
537
  };
471
538
  };
472
539
  }
@@ -572,6 +639,7 @@ export function FileRouter({
572
639
  _mode: r.mode || 'client',
573
640
  }));
574
641
 
642
+ // Router already returns a reactive function child — just delegate
575
643
  return Router({
576
644
  routes: routerRoutes,
577
645
  globalLayout,