Skip to content

Commit 59db42b

Browse files
authored
Merge pull request #2739 from headlamp-k8s/fix-createRouteURL
frontend: Fix createRouteURL to match routes by name
2 parents e44b7fc + 1d1323b commit 59db42b

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

frontend/src/lib/router.tsx

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -931,7 +931,27 @@ export interface RouteURLProps {
931931

932932
export function createRouteURL(routeName: string, params: RouteURLProps = {}) {
933933
const storeRoutes = store.getState().routes.routes;
934-
const route = (storeRoutes && storeRoutes[routeName]) || getRoute(routeName);
934+
935+
// First try to find by name
936+
const matchingStoredRouteByName =
937+
storeRoutes &&
938+
Object.entries(storeRoutes).find(
939+
([, route]) => route.name?.toLowerCase() === routeName.toLowerCase()
940+
)?.[1];
941+
942+
// Then try to find by path
943+
const matchingStoredRouteByPath =
944+
storeRoutes &&
945+
Object.entries(storeRoutes).find(([key]) => key.toLowerCase() === routeName.toLowerCase())?.[1];
946+
947+
if (matchingStoredRouteByPath && !matchingStoredRouteByName) {
948+
console.warn(
949+
`[Deprecation] Route "${routeName}" was found by path instead of name. ` +
950+
'Please use route names instead of paths when calling createRouteURL.'
951+
);
952+
}
953+
954+
const route = matchingStoredRouteByName || matchingStoredRouteByPath || getRoute(routeName);
935955

936956
if (!route) {
937957
return '';

0 commit comments

Comments
 (0)