slicejs-web-framework 3.3.7 → 3.3.8

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.
@@ -1,760 +1,775 @@
1
- /**
2
- * @typedef {Object} RouteConfig
3
- * @property {string} path
4
- * @property {string} component
5
- * @property {RouteConfig[]} [children]
6
- * @property {Object} [metadata]
7
- * @property {string} [fullPath]
8
- * @property {string|null} [parentPath]
9
- * @property {RouteConfig|null} [parentRoute]
10
- */
11
-
12
- /**
13
- * @typedef {Object} RouteInfo
14
- * @property {string} path
15
- * @property {string} component
16
- * @property {Object} params
17
- * @property {Object} query
18
- * @property {Object} metadata
19
- */
20
-
21
- /**
22
- * @typedef {Object} GuardRedirect
23
- * @property {string} path
24
- * @property {boolean} [replace]
25
- */
26
-
27
- /**
28
- * @typedef {Object} RouteMatch
29
- * @property {RouteConfig|null} route
30
- * @property {Object} params
31
- * @property {RouteConfig} [childRoute]
32
- */
33
-
34
- /**
35
- * @callback RouterNext
36
- * @param {void|false|string|{ path: string, replace?: boolean }} [arg]
37
- * @returns {void}
38
- */
39
-
40
- export default class Router {
41
- /**
42
- * @param {RouteConfig[]} routes
43
- */
44
- constructor(routes) {
45
- this.routes = routes;
46
- this.activeRoute = null;
47
- this.pathToRouteMap = this.createPathToRouteMap(routes);
48
-
49
- // O(1) case-insensitive lookup for static routes, so a miss on the exact map
50
- // doesn't scan every route on each navigation. Param routes (which carry a
51
- // precompiled `compiled` regex) are excluded — they go through the param loop.
52
- this._staticLowerIndex = new Map();
53
- for (const [pattern, route] of this.pathToRouteMap.entries()) {
54
- if (!route.compiled) this._staticLowerIndex.set(pattern.toLowerCase(), route);
55
- }
56
-
57
- // Navigation Guards
58
- this._beforeEachGuard = null;
59
- this._afterEachGuard = null;
60
-
61
- // Router state
62
- this._started = false;
63
- this._autoStartTimeout = null;
64
-
65
- // Sistema de caché optimizado
66
- this.routeContainersCache = new Map();
67
- this.lastCacheUpdate = 0;
68
- this.CACHE_DURATION = 100; // ms - caché muy corto pero efectivo
69
-
70
- // Observer para invalidar caché automáticamente
71
- this.setupMutationObserver();
72
- }
73
-
74
- /**
75
- * Inicializa el router
76
- * Si el usuario no llama start() manualmente, se auto-inicia despues de un delay
77
- * @returns {void}
78
- */
79
- init() {
80
- window.addEventListener('popstate', this.onRouteChange.bind(this));
81
-
82
- // Auto-start después de 50ms si el usuario no llama start() manualmente
83
- // Esto da tiempo para que el usuario configure guards si lo necesita
84
- this._autoStartTimeout = setTimeout(async () => {
85
- if (!this._started) {
86
- slice.logger.logInfo('Router', 'Auto-starting router (no manual start() called)');
87
- await this.start();
88
- }
89
- }, 50);
90
- }
91
-
92
- /**
93
- * Inicia el router y carga la ruta inicial
94
- * OPCIONAL: Solo necesario si usas guards (beforeEach/afterEach)
95
- * Si no lo llamas, el router se auto-inicia despues de 50ms
96
- * @returns {Promise<void>}
97
- */
98
- async start() {
99
- // Prevenir múltiples llamadas
100
- if (this._started) {
101
- slice.logger.logWarning('Router', 'start() already called');
102
- return;
103
- }
104
-
105
- // Cancelar auto-start si existe
106
- if (this._autoStartTimeout) {
107
- clearTimeout(this._autoStartTimeout);
108
- this._autoStartTimeout = null;
109
- }
110
-
111
- this._started = true;
112
- await this.loadInitialRoute();
113
- }
114
-
115
- // ============================================
116
- // NAVIGATION GUARDS API
117
- // ============================================
118
-
119
- /**
120
- * Registra un guard que se ejecuta ANTES de cada navegacion.
121
- * Puede bloquear o redirigir la navegacion mediante next().
122
- * @param {(to: RouteInfo, from: RouteInfo, next: RouterNext) => void|Promise<void>} guard
123
- * @returns {void}
124
- */
125
- beforeEach(guard) {
126
- if (typeof guard !== 'function') {
127
- slice.logger.logError('Router', 'beforeEach expects a function');
128
- return;
129
- }
130
- this._beforeEachGuard = guard;
131
- }
132
-
133
- /**
134
- * Registra un guard que se ejecuta DESPUES de cada navegacion.
135
- * No puede bloquear la navegacion.
136
- * @param {(to: RouteInfo, from: RouteInfo) => void} guard
137
- * @returns {void}
138
- */
139
- afterEach(guard) {
140
- if (typeof guard !== 'function') {
141
- slice.logger.logError('Router', 'afterEach expects a function');
142
- return;
143
- }
144
- this._afterEachGuard = guard;
145
- }
146
-
147
- /**
148
- * Crea un objeto con información de ruta para los guards
149
- * @param {Object} route - Objeto de ruta
150
- * @param {Object} params - Parámetros de la ruta
151
- * @param {String} requestedPath - Path original solicitado
152
- * @returns {Object} Objeto con path, component, params, query, metadata
153
- */
154
- /**
155
- * Build route info used by guards and events.
156
- * @param {RouteConfig|null} route
157
- * @param {Object} [params]
158
- * @param {string|null} [requestedPath]
159
- * @returns {RouteInfo}
160
- */
161
- _createRouteInfo(route, params = {}, requestedPath = null) {
162
- if (!route) {
163
- return {
164
- path: requestedPath || '/404',
165
- component: 'NotFound',
166
- params: {},
167
- query: this._parseQueryParams(),
168
- metadata: {},
169
- };
170
- }
171
-
172
- return {
173
- path: requestedPath || route.fullPath || route.path,
174
- component: route.parentRoute ? route.parentRoute.component : route.component,
175
- params: params,
176
- query: this._parseQueryParams(),
177
- metadata: route.metadata || {},
178
- };
179
- }
180
-
181
- /**
182
- * Parsea los query parameters de la URL actual
183
- * @returns {Object} Objeto con los query params
184
- */
185
- /**
186
- * Parse query params from current URL.
187
- * @returns {Object}
188
- */
189
- _parseQueryParams() {
190
- const queryString = window.location.search;
191
- if (!queryString) return {};
192
-
193
- const params = {};
194
- const urlParams = new URLSearchParams(queryString);
195
-
196
- for (const [key, value] of urlParams) {
197
- params[key] = value;
198
- }
199
-
200
- return params;
201
- }
202
-
203
- /**
204
- * Ejecuta el beforeEach guard si existe
205
- * @param {Object} to - Información de ruta destino
206
- * @param {Object} from - Información de ruta origen
207
- * @returns {Object|null} Objeto con redirectPath y options, o null si continúa
208
- */
209
- /**
210
- * Execute beforeEach guard if defined.
211
- * @param {RouteInfo} to
212
- * @param {RouteInfo} from
213
- * @returns {Promise<{ path: string|false, options: { replace?: boolean } }|null>}
214
- */
215
- async _executeBeforeEachGuard(to, from) {
216
- if (!this._beforeEachGuard) {
217
- return null;
218
- }
219
-
220
- let redirectPath = null;
221
- let redirectOptions = {};
222
- let nextCalled = false;
223
-
224
- const next = (arg) => {
225
- if (nextCalled) {
226
- slice.logger.logWarning('Router', 'next() called multiple times in guard');
227
- return;
228
- }
229
- nextCalled = true;
230
-
231
- // Caso 1: Sin argumentos - continuar navegación
232
- if (arg === undefined) {
233
- return;
234
- }
235
-
236
- // Caso 2: false - cancelar navegación
237
- if (arg === false) {
238
- redirectPath = false;
239
- return;
240
- }
241
-
242
- // Caso 3: String - redirección simple (backward compatibility)
243
- if (typeof arg === 'string') {
244
- redirectPath = arg;
245
- redirectOptions = { replace: false };
246
- return;
247
- }
248
-
249
- // Caso 4: Objeto - redirección con opciones
250
- if (typeof arg === 'object' && arg.path) {
251
- redirectPath = arg.path;
252
- redirectOptions = {
253
- replace: arg.replace || false,
254
- };
255
- return;
256
- }
257
-
258
- // Argumento inválido
259
- slice.logger.logError(
260
- 'Router',
261
- 'Invalid argument passed to next(). Expected string, object with path, false, or undefined.'
262
- );
263
- };
264
-
265
- try {
266
- await this._beforeEachGuard(to, from, next);
267
-
268
- // Si no se llamó next(), loguear advertencia pero continuar
269
- if (!nextCalled) {
270
- slice.logger.logWarning('Router', 'beforeEach guard did not call next(). Navigation will continue.');
271
- }
272
-
273
- // Retornar tanto el path como las opciones
274
- return redirectPath ? { path: redirectPath, options: redirectOptions } : null;
275
- } catch (error) {
276
- slice.logger.logError('Router', 'Error in beforeEach guard', error);
277
- return null; // En caso de error, continuar con la navegación
278
- }
279
- }
280
-
281
- /**
282
- * Ejecuta el afterEach guard si existe
283
- * @param {Object} to - Información de ruta destino
284
- * @param {Object} from - Información de ruta origen
285
- */
286
- /**
287
- * Execute afterEach guard if defined.
288
- * @param {RouteInfo} to
289
- * @param {RouteInfo} from
290
- * @returns {void}
291
- */
292
- _executeAfterEachGuard(to, from) {
293
- if (!this._afterEachGuard) {
294
- return;
295
- }
296
-
297
- try {
298
- this._afterEachGuard(to, from);
299
- } catch (error) {
300
- slice.logger.logError('Router', 'Error in afterEach guard', error);
301
- }
302
- }
303
-
304
- // ============================================
305
- // ROUTING CORE (MODIFICADO CON GUARDS)
306
- // ============================================
307
-
308
- /**
309
- * Navigate to a route path (guards run automatically).
310
- * @param {string} path
311
- * @param {{ replace?: boolean }} [options] - `{ replace: true }` replaces history instead of pushing.
312
- * @returns {Promise<void>}
313
- */
314
- async navigate(path, options = {}, legacyOptions) {
315
- // Backward compatibility with the previous signature navigate(path, _redirectChain, _options):
316
- // if the 2nd argument is the internal redirect chain (an array) or a 3rd argument is passed,
317
- // use the 3rd argument as the options object.
318
- if (Array.isArray(options)) {
319
- options = legacyOptions || {};
320
- } else if (legacyOptions !== undefined) {
321
- options = legacyOptions || options;
322
- }
323
- return this._navigateWithGuards(path, options || {}, []);
324
- }
325
-
326
- /**
327
- * Internal navigation that tracks the guard redirection chain (loop protection).
328
- * @param {string} path
329
- * @param {{ replace?: boolean }} options
330
- * @param {string[]} redirectChain
331
- * @returns {Promise<void>}
332
- */
333
- async _navigateWithGuards(path, options, redirectChain) {
334
- const currentPath = window.location.pathname;
335
-
336
- // Detectar loops infinitos: si ya visitamos esta ruta en la cadena de redirecciones
337
- if (redirectChain.includes(path)) {
338
- slice.logger.logError('Router', `Guard redirection loop detected: ${redirectChain.join(' → ')} → ${path}`);
339
- return;
340
- }
341
-
342
- // Límite de seguridad: máximo 10 redirecciones
343
- if (redirectChain.length >= 10) {
344
- slice.logger.logError('Router', `Too many redirections: ${redirectChain.join(' ')} ${path}`);
345
- return;
346
- }
347
-
348
- // Obtener información de ruta actual
349
- const { route: fromRoute, params: fromParams } = this.matchRoute(currentPath);
350
- const from = this._createRouteInfo(fromRoute, fromParams, currentPath);
351
-
352
- // Obtener información de ruta destino
353
- const { route: toRoute, params: toParams } = this.matchRoute(path);
354
- const to = this._createRouteInfo(toRoute, toParams, path);
355
-
356
- // EJECUTAR BEFORE EACH GUARD
357
- const guardResult = await this._executeBeforeEachGuard(to, from);
358
-
359
- // Si el guard redirige
360
- if (guardResult && guardResult.path) {
361
- return this._navigateWithGuards(guardResult.path, guardResult.options || {}, [...redirectChain, path]);
362
- }
363
-
364
- // Si el guard cancela la navegación (next(false))
365
- if (guardResult && guardResult.path === false) {
366
- slice.logger.logInfo('Router', 'Navigation cancelled by guard');
367
- return;
368
- }
369
-
370
- // No hay redirección - continuar con la navegación normal
371
- // Usar replace o push según las opciones
372
- if (options.replace) {
373
- window.history.replaceState({}, path, window.location.origin + path);
374
- } else {
375
- window.history.pushState({}, path, window.location.origin + path);
376
- }
377
-
378
- await this._performNavigation(to, from);
379
- }
380
-
381
- /**
382
- * Método interno para ejecutar la navegación después de pasar los guards
383
- * @param {Object} to - Información de ruta destino
384
- * @param {Object} from - Información de ruta origen
385
- */
386
- /**
387
- * Perform navigation after guards.
388
- * @param {RouteInfo} to
389
- * @param {RouteInfo} from
390
- * @returns {Promise<void>}
391
- */
392
- async _performNavigation(to, from) {
393
- // Renderizar la nueva ruta
394
- await this.onRouteChange();
395
-
396
- // EJECUTAR AFTER EACH GUARD
397
- this._executeAfterEachGuard(to, from);
398
-
399
- // Emitir evento de cambio de ruta
400
- this._emitRouteChange(to, from);
401
- }
402
-
403
- /**
404
- * React to URL changes and render routes.
405
- * @returns {Promise<void>}
406
- */
407
- async onRouteChange() {
408
- // Cancelar el timeout anterior si existe
409
- if (this.routeChangeTimeout) {
410
- clearTimeout(this.routeChangeTimeout);
411
- }
412
-
413
- // Debounce de 10ms para evitar múltiples llamadas seguidas
414
- this.routeChangeTimeout = setTimeout(async () => {
415
- const path = window.location.pathname;
416
- const routeContainersFlag = await this.renderRoutesComponentsInPage();
417
-
418
- if (routeContainersFlag) {
419
- return;
420
- }
421
-
422
- const { route, params } = this.matchRoute(path);
423
- if (route) {
424
- await this.handleRoute(route, params);
425
- }
426
- }, 10);
427
- }
428
-
429
- /**
430
- * Build or update the active route component.
431
- * @param {RouteConfig} route
432
- * @param {Object} params
433
- * @returns {Promise<void>}
434
- */
435
- async handleRoute(route, params) {
436
- const targetElement = document.querySelector('#app');
437
-
438
- const componentName = route.parentRoute ? route.parentRoute.component : route.component;
439
- const sliceId = `route-${componentName}`;
440
-
441
- const existingComponent = slice.controller.getComponent(sliceId);
442
-
443
- if (slice.loading) {
444
- slice.loading.start();
445
- }
446
-
447
- try {
448
- if (existingComponent) {
449
- targetElement.innerHTML = '';
450
- if (existingComponent.update) {
451
- existingComponent.props = { ...existingComponent.props, ...params };
452
- await existingComponent.update();
453
- }
454
- targetElement.appendChild(existingComponent);
455
- await this.renderRoutesInComponent(existingComponent);
456
- } else {
457
- const component = await slice.build(componentName, {
458
- params,
459
- sliceId: sliceId,
460
- });
461
-
462
- targetElement.innerHTML = '';
463
- targetElement.appendChild(component);
464
-
465
- await this.renderRoutesInComponent(component);
466
- }
467
-
468
- // Invalidar caché después de cambios importantes en el DOM
469
- this.invalidateCache();
470
- slice.router.activeRoute = route;
471
- } finally {
472
- if (slice.loading) {
473
- slice.loading.stop();
474
- }
475
- }
476
- }
477
-
478
- /**
479
- * Load initial route and run guards.
480
- * @returns {Promise<void>}
481
- */
482
- async loadInitialRoute() {
483
- const path = window.location.pathname;
484
- const { route, params } = this.matchRoute(path);
485
-
486
- // Para la carga inicial, también ejecutar guards
487
- const from = this._createRouteInfo(null, {}, null);
488
- const to = this._createRouteInfo(route, params, path);
489
-
490
- // EJECUTAR BEFORE EACH GUARD en carga inicial
491
- const guardResult = await this._executeBeforeEachGuard(to, from);
492
-
493
- if (guardResult && guardResult.path) {
494
- return this.navigate(guardResult.path, guardResult.options || {});
495
- }
496
-
497
- // Si el guard cancela la navegación inicial (caso raro pero posible)
498
- if (guardResult && guardResult.path === false) {
499
- slice.logger.logWarning('Router', 'Initial route navigation cancelled by guard');
500
- return;
501
- }
502
-
503
- await this.handleRoute(route, params);
504
-
505
- // EJECUTAR AFTER EACH GUARD en carga inicial
506
- this._executeAfterEachGuard(to, from);
507
-
508
- // Emitir evento de cambio de ruta
509
- this._emitRouteChange(to, from);
510
- }
511
-
512
- /**
513
- * Emitir evento de cambio de ruta
514
- * @param {Object} to
515
- * @param {Object} from
516
- */
517
- /**
518
- * Emit route change event.
519
- * @param {RouteInfo} to
520
- * @param {RouteInfo} from
521
- * @returns {void}
522
- */
523
- _emitRouteChange(to, from) {
524
- const payload = { to, from };
525
-
526
- if (slice.eventsConfig?.enabled && slice.events && typeof slice.events.emit === 'function') {
527
- slice.events.emit('router:change', payload);
528
- return;
529
- }
530
-
531
- window.dispatchEvent(new CustomEvent('router:change', { detail: payload }));
532
- }
533
-
534
- // ============================================
535
- // MÉTODOS EXISTENTES (SIN CAMBIOS)
536
- // ============================================
537
-
538
- setupMutationObserver() {
539
- if (typeof MutationObserver !== 'undefined') {
540
- this.observer = new MutationObserver((mutations) => {
541
- let shouldInvalidateCache = false;
542
-
543
- mutations.forEach((mutation) => {
544
- if (mutation.type === 'childList') {
545
- const addedNodes = Array.from(mutation.addedNodes);
546
- const removedNodes = Array.from(mutation.removedNodes);
547
-
548
- const hasRouteNodes = [...addedNodes, ...removedNodes].some(
549
- (node) =>
550
- node.nodeType === Node.ELEMENT_NODE &&
551
- (node.tagName === 'SLICE-ROUTE' ||
552
- node.tagName === 'SLICE-MULTI-ROUTE' ||
553
- node.querySelector?.('slice-route, slice-multi-route'))
554
- );
555
-
556
- if (hasRouteNodes) {
557
- shouldInvalidateCache = true;
558
- }
559
- }
560
- });
561
-
562
- if (shouldInvalidateCache) {
563
- this.invalidateCache();
564
- }
565
- });
566
-
567
- this.observer.observe(document.body, {
568
- childList: true,
569
- subtree: true,
570
- });
571
- }
572
- }
573
-
574
- invalidateCache() {
575
- this.routeContainersCache.clear();
576
- this.lastCacheUpdate = 0;
577
- }
578
-
579
- createPathToRouteMap(routes, basePath = '', parentRoute = null) {
580
- const pathToRouteMap = new Map();
581
-
582
- for (const route of routes) {
583
- const fullPath = `${basePath}${route.path}`.replace(/\/+/g, '/');
584
-
585
- const routeWithParent = {
586
- ...route,
587
- fullPath,
588
- parentPath: parentRoute ? parentRoute.fullPath : null,
589
- parentRoute: parentRoute,
590
- };
591
-
592
- // Compile parameterized patterns once at map-build time instead of on every
593
- // navigation. Static routes leave `compiled` undefined.
594
- if (fullPath.includes('${')) {
595
- routeWithParent.compiled = this.compilePathPattern(fullPath);
596
- }
597
-
598
- pathToRouteMap.set(fullPath, routeWithParent);
599
-
600
- if (route.children) {
601
- const childPathToRouteMap = this.createPathToRouteMap(route.children, fullPath, routeWithParent);
602
-
603
- for (const [childPath, childRoute] of childPathToRouteMap.entries()) {
604
- pathToRouteMap.set(childPath, childRoute);
605
- }
606
- }
607
- }
608
-
609
- return pathToRouteMap;
610
- }
611
-
612
- /**
613
- * Render any Route/MultiRoute components in a container.
614
- * @param {Document|HTMLElement} [searchContainer]
615
- * @returns {Promise<boolean>}
616
- */
617
- async renderRoutesComponentsInPage(searchContainer = document) {
618
- let routerContainersFlag = false;
619
- const routeContainers = this.getCachedRouteContainers(searchContainer);
620
-
621
- for (const routeContainer of routeContainers) {
622
- try {
623
- if (!routeContainer.isConnected) {
624
- this.invalidateCache();
625
- continue;
626
- }
627
-
628
- let response = await routeContainer.renderIfCurrentRoute();
629
- if (response) {
630
- this.activeRoute = routeContainer.props;
631
- routerContainersFlag = true;
632
- }
633
- } catch (error) {
634
- slice.logger.logError('Router', `Error rendering route container`, error);
635
- }
636
- }
637
-
638
- return routerContainersFlag;
639
- }
640
-
641
- getCachedRouteContainers(container) {
642
- const containerKey = container === document ? 'document' : container.sliceId || 'anonymous';
643
- const now = Date.now();
644
-
645
- if (this.routeContainersCache.has(containerKey) && now - this.lastCacheUpdate < this.CACHE_DURATION) {
646
- return this.routeContainersCache.get(containerKey);
647
- }
648
-
649
- const routeContainers = this.findAllRouteContainersOptimized(container);
650
- this.routeContainersCache.set(containerKey, routeContainers);
651
- this.lastCacheUpdate = now;
652
-
653
- return routeContainers;
654
- }
655
-
656
- findAllRouteContainersOptimized(container) {
657
- const routeContainers = [];
658
-
659
- const walker = document.createTreeWalker(container, NodeFilter.SHOW_ELEMENT, {
660
- acceptNode: (node) => {
661
- if (node.tagName === 'SLICE-ROUTE' || node.tagName === 'SLICE-MULTI-ROUTE') {
662
- return NodeFilter.FILTER_ACCEPT;
663
- }
664
- return NodeFilter.FILTER_SKIP;
665
- },
666
- });
667
-
668
- let node;
669
- while ((node = walker.nextNode())) {
670
- routeContainers.push(node);
671
- }
672
-
673
- return routeContainers;
674
- }
675
-
676
- /**
677
- * Render route containers inside a component.
678
- * @param {HTMLElement} component
679
- * @returns {Promise<boolean>}
680
- */
681
- async renderRoutesInComponent(component) {
682
- if (!component) {
683
- slice.logger.logWarning('Router', 'No component provided for route rendering');
684
- return false;
685
- }
686
-
687
- return await this.renderRoutesComponentsInPage(component);
688
- }
689
-
690
- /**
691
- * Match a path to a configured route.
692
- * @param {string} path
693
- * @returns {RouteMatch}
694
- */
695
- matchRoute(path) {
696
- // Normalize a trailing slash ('/about/' -> '/about'); keep root '/' as-is.
697
- path = path.length > 1 ? path.replace(/\/+$/, '') : path;
698
- // Exact match first (fast path), then a case-insensitive match on static paths
699
- // so '/About' resolves to a route declared as '/about'.
700
- let exactMatch = this.pathToRouteMap.get(path);
701
- if (!exactMatch) {
702
- exactMatch = this._staticLowerIndex.get(path.toLowerCase());
703
- }
704
- if (exactMatch) {
705
- if (exactMatch.parentRoute) {
706
- return {
707
- route: exactMatch.parentRoute,
708
- params: {},
709
- childRoute: exactMatch,
710
- };
711
- }
712
- return { route: exactMatch, params: {} };
713
- }
714
-
715
- for (const route of this.pathToRouteMap.values()) {
716
- if (route.compiled) {
717
- const { regex, paramNames } = route.compiled;
718
- const match = path.match(regex);
719
- if (match) {
720
- const params = {};
721
- paramNames.forEach((name, i) => {
722
- params[name] = match[i + 1];
723
- });
724
-
725
- if (route.parentRoute) {
726
- return {
727
- route: route.parentRoute,
728
- params: params,
729
- childRoute: route,
730
- };
731
- }
732
-
733
- return { route, params };
734
- }
735
- }
736
- }
737
-
738
- const notFoundRoute = this.pathToRouteMap.get('/404');
739
- return { route: notFoundRoute, params: {} };
740
- }
741
-
742
- /**
743
- * Compile a path pattern with ${param} segments.
744
- * @param {string} pattern
745
- * @returns {{ regex: RegExp, paramNames: string[] }}
746
- */
747
- compilePathPattern(pattern) {
748
- const paramNames = [];
749
- const regexPattern =
750
- '^' +
751
- pattern.replace(/\$\{([^}]+)\}/g, (_, paramName) => {
752
- paramNames.push(paramName);
753
- return '([^/]+)';
754
- }) +
755
- '$';
756
-
757
- // 'i' flag: paths match case-insensitively. Captured param values keep their original case.
758
- return { regex: new RegExp(regexPattern, 'i'), paramNames };
759
- }
760
- }
1
+ /**
2
+ * @typedef {Object} RouteConfig
3
+ * @property {string} path
4
+ * @property {string} component
5
+ * @property {RouteConfig[]} [children]
6
+ * @property {Object} [metadata]
7
+ * @property {string} [fullPath]
8
+ * @property {string|null} [parentPath]
9
+ * @property {RouteConfig|null} [parentRoute]
10
+ */
11
+
12
+ /**
13
+ * @typedef {Object} RouteInfo
14
+ * @property {string} path
15
+ * @property {string} component
16
+ * @property {Object} params
17
+ * @property {Object} query
18
+ * @property {Object} metadata
19
+ */
20
+
21
+ /**
22
+ * @typedef {Object} GuardRedirect
23
+ * @property {string} path
24
+ * @property {boolean} [replace]
25
+ */
26
+
27
+ /**
28
+ * @typedef {Object} RouteMatch
29
+ * @property {RouteConfig|null} route
30
+ * @property {Object} params
31
+ * @property {RouteConfig} [childRoute]
32
+ */
33
+
34
+ /**
35
+ * @callback RouterNext
36
+ * @param {void|false|string|{ path: string, replace?: boolean }} [arg]
37
+ * @returns {void}
38
+ */
39
+
40
+ export default class Router {
41
+ /**
42
+ * @param {RouteConfig[]} routes
43
+ */
44
+ constructor(routes) {
45
+ this.routes = routes;
46
+ this.activeRoute = null;
47
+ this.pathToRouteMap = this.createPathToRouteMap(routes);
48
+
49
+ // O(1) case-insensitive lookup for static routes, so a miss on the exact map
50
+ // doesn't scan every route on each navigation. Param routes (which carry a
51
+ // precompiled `compiled` regex) are excluded — they go through the param loop.
52
+ this._staticLowerIndex = new Map();
53
+ for (const [pattern, route] of this.pathToRouteMap.entries()) {
54
+ if (!route.compiled) this._staticLowerIndex.set(pattern.toLowerCase(), route);
55
+ }
56
+
57
+ // Navigation Guards
58
+ this._beforeEachGuard = null;
59
+ this._afterEachGuard = null;
60
+
61
+ // Router state
62
+ this._started = false;
63
+ this._autoStartTimeout = null;
64
+
65
+ // Sistema de caché optimizado
66
+ this.routeContainersCache = new Map();
67
+ this.lastCacheUpdate = 0;
68
+ this.CACHE_DURATION = 100; // ms - caché muy corto pero efectivo
69
+
70
+ // Observer para invalidar caché automáticamente
71
+ this.setupMutationObserver();
72
+ }
73
+
74
+ /**
75
+ * Inicializa el router
76
+ * Si el usuario no llama start() manualmente, se auto-inicia despues de un delay
77
+ * @returns {void}
78
+ */
79
+ init() {
80
+ window.addEventListener('popstate', () => {
81
+ try {
82
+ this.onRouteChange();
83
+ } catch (error) {
84
+ slice.logger.error('Router', 'Error in popstate handler', error);
85
+ }
86
+ });
87
+
88
+ this._autoStartTimeout = setTimeout(async () => {
89
+ if (!this._started) {
90
+ try {
91
+ slice.logger.info('Router', 'Auto-starting router (no manual start() called)');
92
+ await this.start();
93
+ } catch (error) {
94
+ slice.logger.error('Router', 'Auto-start failed', error);
95
+ }
96
+ }
97
+ }, 50);
98
+ }
99
+
100
+ /**
101
+ * Inicia el router y carga la ruta inicial
102
+ * OPCIONAL: Solo necesario si usas guards (beforeEach/afterEach)
103
+ * Si no lo llamas, el router se auto-inicia despues de 50ms
104
+ * @returns {Promise<void>}
105
+ */
106
+ async start() {
107
+ // Prevenir múltiples llamadas
108
+ if (this._started) {
109
+ slice.logger.logWarning('Router', 'start() already called');
110
+ return;
111
+ }
112
+
113
+ // Cancelar auto-start si existe
114
+ if (this._autoStartTimeout) {
115
+ clearTimeout(this._autoStartTimeout);
116
+ this._autoStartTimeout = null;
117
+ }
118
+
119
+ this._started = true;
120
+ await this.loadInitialRoute();
121
+ }
122
+
123
+ // ============================================
124
+ // NAVIGATION GUARDS API
125
+ // ============================================
126
+
127
+ /**
128
+ * Registra un guard que se ejecuta ANTES de cada navegacion.
129
+ * Puede bloquear o redirigir la navegacion mediante next().
130
+ * @param {(to: RouteInfo, from: RouteInfo, next: RouterNext) => void|Promise<void>} guard
131
+ * @returns {void}
132
+ */
133
+ beforeEach(guard) {
134
+ if (typeof guard !== 'function') {
135
+ slice.logger.logError('Router', 'beforeEach expects a function');
136
+ return;
137
+ }
138
+ this._beforeEachGuard = guard;
139
+ }
140
+
141
+ /**
142
+ * Registra un guard que se ejecuta DESPUES de cada navegacion.
143
+ * No puede bloquear la navegacion.
144
+ * @param {(to: RouteInfo, from: RouteInfo) => void} guard
145
+ * @returns {void}
146
+ */
147
+ afterEach(guard) {
148
+ if (typeof guard !== 'function') {
149
+ slice.logger.logError('Router', 'afterEach expects a function');
150
+ return;
151
+ }
152
+ this._afterEachGuard = guard;
153
+ }
154
+
155
+ /**
156
+ * Crea un objeto con información de ruta para los guards
157
+ * @param {Object} route - Objeto de ruta
158
+ * @param {Object} params - Parámetros de la ruta
159
+ * @param {String} requestedPath - Path original solicitado
160
+ * @returns {Object} Objeto con path, component, params, query, metadata
161
+ */
162
+ /**
163
+ * Build route info used by guards and events.
164
+ * @param {RouteConfig|null} route
165
+ * @param {Object} [params]
166
+ * @param {string|null} [requestedPath]
167
+ * @returns {RouteInfo}
168
+ */
169
+ _createRouteInfo(route, params = {}, requestedPath = null) {
170
+ if (!route) {
171
+ return {
172
+ path: requestedPath || '/404',
173
+ component: 'NotFound',
174
+ params: {},
175
+ query: this._parseQueryParams(),
176
+ metadata: {},
177
+ };
178
+ }
179
+
180
+ return {
181
+ path: requestedPath || route.fullPath || route.path,
182
+ component: route.parentRoute ? route.parentRoute.component : route.component,
183
+ params: params,
184
+ query: this._parseQueryParams(),
185
+ metadata: route.metadata || {},
186
+ };
187
+ }
188
+
189
+ /**
190
+ * Parsea los query parameters de la URL actual
191
+ * @returns {Object} Objeto con los query params
192
+ */
193
+ /**
194
+ * Parse query params from current URL.
195
+ * @returns {Object}
196
+ */
197
+ _parseQueryParams() {
198
+ const queryString = window.location.search;
199
+ if (!queryString) return {};
200
+
201
+ const params = {};
202
+ const urlParams = new URLSearchParams(queryString);
203
+
204
+ for (const [key, value] of urlParams) {
205
+ params[key] = value;
206
+ }
207
+
208
+ return params;
209
+ }
210
+
211
+ /**
212
+ * Ejecuta el beforeEach guard si existe
213
+ * @param {Object} to - Información de ruta destino
214
+ * @param {Object} from - Información de ruta origen
215
+ * @returns {Object|null} Objeto con redirectPath y options, o null si continúa
216
+ */
217
+ /**
218
+ * Execute beforeEach guard if defined.
219
+ * @param {RouteInfo} to
220
+ * @param {RouteInfo} from
221
+ * @returns {Promise<{ path: string|false, options: { replace?: boolean } }|null>}
222
+ */
223
+ async _executeBeforeEachGuard(to, from) {
224
+ if (!this._beforeEachGuard) {
225
+ return null;
226
+ }
227
+
228
+ let redirectPath = null;
229
+ let redirectOptions = {};
230
+ let nextCalled = false;
231
+
232
+ const next = (arg) => {
233
+ if (nextCalled) {
234
+ slice.logger.logWarning('Router', 'next() called multiple times in guard');
235
+ return;
236
+ }
237
+ nextCalled = true;
238
+
239
+ // Caso 1: Sin argumentos - continuar navegación
240
+ if (arg === undefined) {
241
+ return;
242
+ }
243
+
244
+ // Caso 2: false - cancelar navegación
245
+ if (arg === false) {
246
+ redirectPath = false;
247
+ return;
248
+ }
249
+
250
+ // Caso 3: String - redirección simple (backward compatibility)
251
+ if (typeof arg === 'string') {
252
+ redirectPath = arg;
253
+ redirectOptions = { replace: false };
254
+ return;
255
+ }
256
+
257
+ // Caso 4: Objeto - redirección con opciones
258
+ if (typeof arg === 'object' && arg.path) {
259
+ redirectPath = arg.path;
260
+ redirectOptions = {
261
+ replace: arg.replace || false,
262
+ };
263
+ return;
264
+ }
265
+
266
+ // Argumento inválido
267
+ slice.logger.logError(
268
+ 'Router',
269
+ 'Invalid argument passed to next(). Expected string, object with path, false, or undefined.'
270
+ );
271
+ };
272
+
273
+ try {
274
+ await this._beforeEachGuard(to, from, next);
275
+
276
+ // Si no se llamó next(), loguear advertencia pero continuar
277
+ if (!nextCalled) {
278
+ slice.logger.logWarning('Router', 'beforeEach guard did not call next(). Navigation will continue.');
279
+ }
280
+
281
+ // Retornar tanto el path como las opciones
282
+ return redirectPath ? { path: redirectPath, options: redirectOptions } : null;
283
+ } catch (error) {
284
+ slice.logger.error('Router', `Error in beforeEach guard from "${from?.path}" to "${to?.path}"`, error);
285
+ return { path: false, options: {} };
286
+ }
287
+ }
288
+
289
+ /**
290
+ * Ejecuta el afterEach guard si existe
291
+ * @param {Object} to - Información de ruta destino
292
+ * @param {Object} from - Información de ruta origen
293
+ */
294
+ /**
295
+ * Execute afterEach guard if defined.
296
+ * @param {RouteInfo} to
297
+ * @param {RouteInfo} from
298
+ * @returns {void}
299
+ */
300
+ _executeAfterEachGuard(to, from) {
301
+ if (!this._afterEachGuard) {
302
+ return;
303
+ }
304
+
305
+ try {
306
+ this._afterEachGuard(to, from);
307
+ } catch (error) {
308
+ slice.logger.logError('Router', 'Error in afterEach guard', error);
309
+ }
310
+ }
311
+
312
+ // ============================================
313
+ // ROUTING CORE (MODIFICADO CON GUARDS)
314
+ // ============================================
315
+
316
+ /**
317
+ * Navigate to a route path (guards run automatically).
318
+ * @param {string} path
319
+ * @param {{ replace?: boolean }} [options] - `{ replace: true }` replaces history instead of pushing.
320
+ * @returns {Promise<void>}
321
+ */
322
+ async navigate(path, options = {}, legacyOptions) {
323
+ // Backward compatibility with the previous signature navigate(path, _redirectChain, _options):
324
+ // if the 2nd argument is the internal redirect chain (an array) or a 3rd argument is passed,
325
+ // use the 3rd argument as the options object.
326
+ if (Array.isArray(options)) {
327
+ options = legacyOptions || {};
328
+ } else if (legacyOptions !== undefined) {
329
+ options = legacyOptions || options;
330
+ }
331
+ return this._navigateWithGuards(path, options || {}, []);
332
+ }
333
+
334
+ /**
335
+ * Internal navigation that tracks the guard redirection chain (loop protection).
336
+ * @param {string} path
337
+ * @param {{ replace?: boolean }} options
338
+ * @param {string[]} redirectChain
339
+ * @returns {Promise<void>}
340
+ */
341
+ async _navigateWithGuards(path, options, redirectChain) {
342
+ const currentPath = window.location.pathname;
343
+
344
+ // Detectar loops infinitos: si ya visitamos esta ruta en la cadena de redirecciones
345
+ if (redirectChain.includes(path)) {
346
+ slice.logger.logError('Router', `Guard redirection loop detected: ${redirectChain.join(' → ')} → ${path}`);
347
+ return;
348
+ }
349
+
350
+ // Límite de seguridad: máximo 10 redirecciones
351
+ if (redirectChain.length >= 10) {
352
+ slice.logger.logError('Router', `Too many redirections: ${redirectChain.join(' → ')} → ${path}`);
353
+ return;
354
+ }
355
+
356
+ // Obtener información de ruta actual
357
+ const { route: fromRoute, params: fromParams } = this.matchRoute(currentPath);
358
+ const from = this._createRouteInfo(fromRoute, fromParams, currentPath);
359
+
360
+ // Obtener información de ruta destino
361
+ const { route: toRoute, params: toParams } = this.matchRoute(path);
362
+ const to = this._createRouteInfo(toRoute, toParams, path);
363
+
364
+ // EJECUTAR BEFORE EACH GUARD
365
+ const guardResult = await this._executeBeforeEachGuard(to, from);
366
+
367
+ // Si el guard redirige
368
+ if (guardResult && guardResult.path) {
369
+ return this._navigateWithGuards(guardResult.path, guardResult.options || {}, [...redirectChain, path]);
370
+ }
371
+
372
+ // Si el guard cancela la navegación (next(false))
373
+ if (guardResult && guardResult.path === false) {
374
+ slice.logger.logInfo('Router', 'Navigation cancelled by guard');
375
+ return;
376
+ }
377
+
378
+ // No hay redirección - continuar con la navegación normal
379
+ // Usar replace o push según las opciones
380
+ if (options.replace) {
381
+ window.history.replaceState({}, path, window.location.origin + path);
382
+ } else {
383
+ window.history.pushState({}, path, window.location.origin + path);
384
+ }
385
+
386
+ await this._performNavigation(to, from);
387
+ }
388
+
389
+ /**
390
+ * Método interno para ejecutar la navegación después de pasar los guards
391
+ * @param {Object} to - Información de ruta destino
392
+ * @param {Object} from - Información de ruta origen
393
+ */
394
+ /**
395
+ * Perform navigation after guards.
396
+ * @param {RouteInfo} to
397
+ * @param {RouteInfo} from
398
+ * @returns {Promise<void>}
399
+ */
400
+ async _performNavigation(to, from) {
401
+ // Renderizar la nueva ruta
402
+ await this.onRouteChange();
403
+
404
+ // EJECUTAR AFTER EACH GUARD
405
+ this._executeAfterEachGuard(to, from);
406
+
407
+ // Emitir evento de cambio de ruta
408
+ this._emitRouteChange(to, from);
409
+ }
410
+
411
+ /**
412
+ * React to URL changes and render routes.
413
+ * @returns {Promise<void>}
414
+ */
415
+ async onRouteChange() {
416
+ if (this.routeChangeTimeout) {
417
+ clearTimeout(this.routeChangeTimeout);
418
+ }
419
+
420
+ this.routeChangeTimeout = setTimeout(async () => {
421
+ try {
422
+ const path = window.location.pathname;
423
+ const routeContainersFlag = await this.renderRoutesComponentsInPage();
424
+
425
+ if (routeContainersFlag) {
426
+ return;
427
+ }
428
+
429
+ const { route, params } = this.matchRoute(path);
430
+ if (route) {
431
+ await this.handleRoute(route, params);
432
+ }
433
+ } catch (error) {
434
+ slice.logger.error('Router', `Route change failed for ${window.location.pathname}`, error);
435
+ }
436
+ }, 10);
437
+ }
438
+
439
+ /**
440
+ * Build or update the active route component.
441
+ * @param {RouteConfig} route
442
+ * @param {Object} params
443
+ * @returns {Promise<void>}
444
+ */
445
+ async handleRoute(route, params) {
446
+ const targetElement = document.querySelector('#app');
447
+
448
+ const componentName = route.parentRoute ? route.parentRoute.component : route.component;
449
+ const sliceId = `route-${componentName}`;
450
+
451
+ const existingComponent = slice.controller.getComponent(sliceId);
452
+
453
+ if (slice.loading) {
454
+ slice.loading.start();
455
+ }
456
+
457
+ try {
458
+ if (existingComponent) {
459
+ targetElement.innerHTML = '';
460
+ if (existingComponent.update) {
461
+ existingComponent.props = { ...existingComponent.props, ...params };
462
+ await existingComponent.update();
463
+ }
464
+ targetElement.appendChild(existingComponent);
465
+ await this.renderRoutesInComponent(existingComponent);
466
+ } else {
467
+ const component = await slice.build(componentName, {
468
+ params,
469
+ sliceId: sliceId,
470
+ });
471
+
472
+ targetElement.innerHTML = '';
473
+ targetElement.appendChild(component);
474
+
475
+ await this.renderRoutesInComponent(component);
476
+ }
477
+
478
+ // Invalidar caché después de cambios importantes en el DOM
479
+ this.invalidateCache();
480
+ slice.router.activeRoute = route;
481
+ } finally {
482
+ if (slice.loading) {
483
+ slice.loading.stop();
484
+ }
485
+ }
486
+ }
487
+
488
+ /**
489
+ * Load initial route and run guards.
490
+ * @returns {Promise<void>}
491
+ */
492
+ async loadInitialRoute() {
493
+ const path = window.location.pathname;
494
+ const { route, params } = this.matchRoute(path);
495
+
496
+ // Para la carga inicial, también ejecutar guards
497
+ const from = this._createRouteInfo(null, {}, null);
498
+ const to = this._createRouteInfo(route, params, path);
499
+
500
+ // EJECUTAR BEFORE EACH GUARD en carga inicial
501
+ const guardResult = await this._executeBeforeEachGuard(to, from);
502
+
503
+ if (guardResult && guardResult.path) {
504
+ return this.navigate(guardResult.path, guardResult.options || {});
505
+ }
506
+
507
+ // Si el guard cancela la navegación inicial (caso raro pero posible)
508
+ if (guardResult && guardResult.path === false) {
509
+ slice.logger.logWarning('Router', 'Initial route navigation cancelled by guard');
510
+ return;
511
+ }
512
+
513
+ await this.handleRoute(route, params);
514
+
515
+ // EJECUTAR AFTER EACH GUARD en carga inicial
516
+ this._executeAfterEachGuard(to, from);
517
+
518
+ // Emitir evento de cambio de ruta
519
+ this._emitRouteChange(to, from);
520
+ }
521
+
522
+ /**
523
+ * Emitir evento de cambio de ruta
524
+ * @param {Object} to
525
+ * @param {Object} from
526
+ */
527
+ /**
528
+ * Emit route change event.
529
+ * @param {RouteInfo} to
530
+ * @param {RouteInfo} from
531
+ * @returns {void}
532
+ */
533
+ _emitRouteChange(to, from) {
534
+ const payload = { to, from };
535
+
536
+ if (slice.eventsConfig?.enabled && slice.events && typeof slice.events.emit === 'function') {
537
+ slice.events.emit('router:change', payload);
538
+ return;
539
+ }
540
+
541
+ window.dispatchEvent(new CustomEvent('router:change', { detail: payload }));
542
+ }
543
+
544
+ // ============================================
545
+ // MÉTODOS EXISTENTES (SIN CAMBIOS)
546
+ // ============================================
547
+
548
+ setupMutationObserver() {
549
+ if (typeof MutationObserver !== 'undefined') {
550
+ this.observer = new MutationObserver((mutations) => {
551
+ try {
552
+ let shouldInvalidateCache = false;
553
+
554
+ mutations.forEach((mutation) => {
555
+ if (mutation.type === 'childList') {
556
+ const addedNodes = Array.from(mutation.addedNodes || []);
557
+ const removedNodes = Array.from(mutation.removedNodes || []);
558
+
559
+ const hasRouteNodes = [...addedNodes, ...removedNodes].some(
560
+ (node) =>
561
+ node.nodeType === Node.ELEMENT_NODE &&
562
+ (node.tagName === 'SLICE-ROUTE' ||
563
+ node.tagName === 'SLICE-MULTI-ROUTE' ||
564
+ node.querySelector?.('slice-route, slice-multi-route'))
565
+ );
566
+
567
+ if (hasRouteNodes) {
568
+ shouldInvalidateCache = true;
569
+ }
570
+ }
571
+ });
572
+
573
+ if (shouldInvalidateCache) {
574
+ this.invalidateCache();
575
+ }
576
+ } catch (error) {
577
+ slice.logger.error('Router', 'Error in MutationObserver callback', error);
578
+ }
579
+ });
580
+
581
+ this.observer.observe(document.body, {
582
+ childList: true,
583
+ subtree: true,
584
+ });
585
+ }
586
+ }
587
+
588
+ invalidateCache() {
589
+ this.routeContainersCache.clear();
590
+ this.lastCacheUpdate = 0;
591
+ }
592
+
593
+ createPathToRouteMap(routes, basePath = '', parentRoute = null) {
594
+ const pathToRouteMap = new Map();
595
+
596
+ for (const route of routes) {
597
+ const fullPath = `${basePath}${route.path}`.replace(/\/+/g, '/');
598
+
599
+ const routeWithParent = {
600
+ ...route,
601
+ fullPath,
602
+ parentPath: parentRoute ? parentRoute.fullPath : null,
603
+ parentRoute: parentRoute,
604
+ };
605
+
606
+ // Compile parameterized patterns once at map-build time instead of on every
607
+ // navigation. Static routes leave `compiled` undefined.
608
+ if (fullPath.includes('${')) {
609
+ routeWithParent.compiled = this.compilePathPattern(fullPath);
610
+ }
611
+
612
+ pathToRouteMap.set(fullPath, routeWithParent);
613
+
614
+ if (route.children) {
615
+ const childPathToRouteMap = this.createPathToRouteMap(route.children, fullPath, routeWithParent);
616
+
617
+ for (const [childPath, childRoute] of childPathToRouteMap.entries()) {
618
+ pathToRouteMap.set(childPath, childRoute);
619
+ }
620
+ }
621
+ }
622
+
623
+ return pathToRouteMap;
624
+ }
625
+
626
+ /**
627
+ * Render any Route/MultiRoute components in a container.
628
+ * @param {Document|HTMLElement} [searchContainer]
629
+ * @returns {Promise<boolean>}
630
+ */
631
+ async renderRoutesComponentsInPage(searchContainer = document) {
632
+ let routerContainersFlag = false;
633
+ const routeContainers = this.getCachedRouteContainers(searchContainer);
634
+
635
+ for (const routeContainer of routeContainers) {
636
+ try {
637
+ if (!routeContainer.isConnected) {
638
+ this.invalidateCache();
639
+ continue;
640
+ }
641
+
642
+ let response = await routeContainer.renderIfCurrentRoute();
643
+ if (response) {
644
+ this.activeRoute = routeContainer.props;
645
+ routerContainersFlag = true;
646
+ }
647
+ } catch (error) {
648
+ const containerName = routeContainer?.tagName || routeContainer?.constructor?.name || 'unknown';
649
+ slice.logger.error('Router', `Error rendering route container "${containerName}"`, error);
650
+ }
651
+ }
652
+
653
+ return routerContainersFlag;
654
+ }
655
+
656
+ getCachedRouteContainers(container) {
657
+ const containerKey = container === document ? 'document' : container.sliceId || 'anonymous';
658
+ const now = Date.now();
659
+
660
+ if (this.routeContainersCache.has(containerKey) && now - this.lastCacheUpdate < this.CACHE_DURATION) {
661
+ return this.routeContainersCache.get(containerKey);
662
+ }
663
+
664
+ const routeContainers = this.findAllRouteContainersOptimized(container);
665
+ this.routeContainersCache.set(containerKey, routeContainers);
666
+ this.lastCacheUpdate = now;
667
+
668
+ return routeContainers;
669
+ }
670
+
671
+ findAllRouteContainersOptimized(container) {
672
+ const routeContainers = [];
673
+
674
+ const walker = document.createTreeWalker(container, NodeFilter.SHOW_ELEMENT, {
675
+ acceptNode: (node) => {
676
+ if (node.tagName === 'SLICE-ROUTE' || node.tagName === 'SLICE-MULTI-ROUTE') {
677
+ return NodeFilter.FILTER_ACCEPT;
678
+ }
679
+ return NodeFilter.FILTER_SKIP;
680
+ },
681
+ });
682
+
683
+ let node;
684
+ while ((node = walker.nextNode())) {
685
+ routeContainers.push(node);
686
+ }
687
+
688
+ return routeContainers;
689
+ }
690
+
691
+ /**
692
+ * Render route containers inside a component.
693
+ * @param {HTMLElement} component
694
+ * @returns {Promise<boolean>}
695
+ */
696
+ async renderRoutesInComponent(component) {
697
+ if (!component) {
698
+ slice.logger.logWarning('Router', 'No component provided for route rendering');
699
+ return false;
700
+ }
701
+
702
+ return await this.renderRoutesComponentsInPage(component);
703
+ }
704
+
705
+ /**
706
+ * Match a path to a configured route.
707
+ * @param {string} path
708
+ * @returns {RouteMatch}
709
+ */
710
+ matchRoute(path) {
711
+ // Normalize a trailing slash ('/about/' -> '/about'); keep root '/' as-is.
712
+ path = path.length > 1 ? path.replace(/\/+$/, '') : path;
713
+ // Exact match first (fast path), then a case-insensitive match on static paths
714
+ // so '/About' resolves to a route declared as '/about'.
715
+ let exactMatch = this.pathToRouteMap.get(path);
716
+ if (!exactMatch) {
717
+ exactMatch = this._staticLowerIndex.get(path.toLowerCase());
718
+ }
719
+ if (exactMatch) {
720
+ if (exactMatch.parentRoute) {
721
+ return {
722
+ route: exactMatch.parentRoute,
723
+ params: {},
724
+ childRoute: exactMatch,
725
+ };
726
+ }
727
+ return { route: exactMatch, params: {} };
728
+ }
729
+
730
+ for (const route of this.pathToRouteMap.values()) {
731
+ if (route.compiled) {
732
+ const { regex, paramNames } = route.compiled;
733
+ const match = path.match(regex);
734
+ if (match) {
735
+ const params = {};
736
+ paramNames.forEach((name, i) => {
737
+ params[name] = match[i + 1];
738
+ });
739
+
740
+ if (route.parentRoute) {
741
+ return {
742
+ route: route.parentRoute,
743
+ params: params,
744
+ childRoute: route,
745
+ };
746
+ }
747
+
748
+ return { route, params };
749
+ }
750
+ }
751
+ }
752
+
753
+ const notFoundRoute = this.pathToRouteMap.get('/404');
754
+ return { route: notFoundRoute, params: {} };
755
+ }
756
+
757
+ /**
758
+ * Compile a path pattern with ${param} segments.
759
+ * @param {string} pattern
760
+ * @returns {{ regex: RegExp, paramNames: string[] }}
761
+ */
762
+ compilePathPattern(pattern) {
763
+ const paramNames = [];
764
+ const regexPattern =
765
+ '^' +
766
+ pattern.replace(/\$\{([^}]+)\}/g, (_, paramName) => {
767
+ paramNames.push(paramName);
768
+ return '([^/]+)';
769
+ }) +
770
+ '$';
771
+
772
+ // 'i' flag: paths match case-insensitively. Captured param values keep their original case.
773
+ return { regex: new RegExp(regexPattern, 'i'), paramNames };
774
+ }
775
+ }