From 994921f704071e0204b3f583ce7033414c0774e4 Mon Sep 17 00:00:00 2001 From: Kevin Bulteel Date: Sun, 10 Mar 2024 16:02:16 +0100 Subject: [PATCH] fix: routing when there is no parent --- packages/@hec.js/ui/lib/src/routing.js | 58 ++++++++++++++------------ 1 file changed, 31 insertions(+), 27 deletions(-) diff --git a/packages/@hec.js/ui/lib/src/routing.js b/packages/@hec.js/ui/lib/src/routing.js index ff2b82a..21e8647 100644 --- a/packages/@hec.js/ui/lib/src/routing.js +++ b/packages/@hec.js/ui/lib/src/routing.js @@ -39,34 +39,38 @@ export function routeCompare(a,b) { /** @param { Route } route */ export function addRoute(route) { - let node = route.node, - path = route.path, - parent = node.parentElement?.closest('[data-route]'), - parentRoute = routingNodes.get(parent), - parentPath = parentRoute?.pattern?.pathname, - targetRoutes = parentRoute?.group ?? routes; - route.group = []; - - routingNodes.set(node, route); - - if (parentPath) { - path = path == '/' ? '' : path; - path = parentPath.replaceAll(/[^\/a-zA-Z0-9]+$/gm, '') + path.replaceAll(/^[^\/a-zA-Z0-9]+/gm, ''); - } - - path = path.replaceAll(/\/+/g, '/').replace(/\/$/m, ''); - - route.pattern = new URLPattern({ pathname: !path ? '/' : path }); - - targetRoutes.push(route); - targetRoutes.sort(routeCompare); - - if (!state.updateQueued) { - state.updateQueued = true; - - queueMicrotask(updateRouting); - } + queueMicrotask(() => { + let node = route.node, + path = route.path, + parent = node.parentElement?.closest('[data-route]'), + parentRoute = routingNodes.get(parent), + parentPath = parentRoute?.pattern?.pathname, + targetRoutes = parentRoute?.group ?? routes; + + route.group = []; + + routingNodes.set(node, route); + + if (parentPath) { + path = path == '/' ? '' : path; + path = parentPath.replaceAll(/[^\/a-zA-Z0-9]+$/gm, '') + path.replaceAll(/^[^\/a-zA-Z0-9]+/gm, ''); + } + + path = path.replaceAll(/\/+/g, '/').replace(/\/$/m, ''); + + route.pattern = new URLPattern({ pathname: !path ? '/' : path }); + + targetRoutes.push(route); + targetRoutes.sort(routeCompare); + + if (!state.updateQueued) { + state.updateQueued = true; + + queueMicrotask(updateRouting); + } + }); + } export function navigate(path = '') {