@@ -20,6 +20,22 @@ function normalizePath(path: string) {
2020 return path
2121}
2222
23+ function chooseBestMatcher (
24+ firstMatcher : RouteRecordMatcher | undefined ,
25+ secondMatcher : RouteRecordMatcher | undefined
26+ ) {
27+ if ( secondMatcher ) {
28+ if (
29+ ! firstMatcher ||
30+ comparePathParserScore ( firstMatcher , secondMatcher ) > 0
31+ ) {
32+ firstMatcher = secondMatcher
33+ }
34+ }
35+
36+ return firstMatcher
37+ }
38+
2339export function createMatcherTree ( ) : MatcherTree {
2440 const root = createMatcherNode ( )
2541 const exactMatchers : Record < string , RouteRecordMatcher [ ] > =
@@ -53,15 +69,10 @@ export function createMatcherTree(): MatcherTree {
5369 find ( path ) {
5470 const matchers = exactMatchers [ normalizePath ( path ) ]
5571
56- if ( matchers ) {
57- for ( const matcher of matchers ) {
58- if ( matcher . re . test ( path ) ) {
59- return matcher
60- }
61- }
62- }
63-
64- return root . find ( path )
72+ return chooseBestMatcher (
73+ matchers && matchers . find ( matcher => matcher . re . test ( path ) ) ,
74+ root . find ( path )
75+ )
6576 } ,
6677
6778 toArray ( ) {
@@ -127,24 +138,24 @@ function createMatcherNode(depth = 1): MatcherTree {
127138 find ( path ) {
128139 const tokens = path . split ( '/' )
129140 const myToken = tokens [ depth ]
141+ let matcher : RouteRecordMatcher | undefined
130142
131143 if ( segments && myToken != null ) {
132144 const segmentMatcher = segments [ myToken . toUpperCase ( ) ]
133145
134146 if ( segmentMatcher ) {
135- const match = segmentMatcher . find ( path )
136-
137- if ( match ) {
138- return match
139- }
147+ matcher = segmentMatcher . find ( path )
140148 }
141149 }
142150
143151 if ( wildcards ) {
144- return wildcards . find ( matcher => matcher . re . test ( path ) )
152+ matcher = chooseBestMatcher (
153+ matcher ,
154+ wildcards . find ( matcher => matcher . re . test ( path ) )
155+ )
145156 }
146157
147- return
158+ return matcher
148159 } ,
149160
150161 toArray ( ) {
0 commit comments