From 8744bbae7789e236e5308f939fb4b2d946a8ca6d Mon Sep 17 00:00:00 2001 From: Namysh Date: Fri, 25 Feb 2022 19:01:12 +0100 Subject: [PATCH] fix(matcher): add child before parent when using `addRoute` --- src/matcher/index.ts | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/matcher/index.ts b/src/matcher/index.ts index 18b752c90..994af7ec3 100644 --- a/src/matcher/index.ts +++ b/src/matcher/index.ts @@ -211,12 +211,24 @@ export function createRouterMatcher( return matchers } + function isChildOf( + child: RouteRecordMatcher, + parent: RouteRecordMatcher + ): Boolean { + return parent.children.some(currChild => { + if (currChild === child) return true + + return isChildOf(child, currChild) + }) + } + function insertMatcher(matcher: RouteRecordMatcher) { let i = 0 // console.log('i is', { i }) while ( i < matchers.length && - comparePathParserScore(matcher, matchers[i]) >= 0 + comparePathParserScore(matcher, matchers[i]) >= 0 && + !isChildOf(matcher, matchers[i]) ) i++ // console.log('END i is', { i })