@@ -1186,15 +1186,14 @@ function getNodeMatch<T extends RouteLike>(
11861186 if ( casePart !== suffix ) continue
11871187 }
11881188 // wildcard matches consume the rest of the URL and cannot have children
1189- const consumed = partsLength - index
11901189 stack . push ( {
11911190 node : segment ,
11921191 index : partsLength ,
11931192 skipped,
11941193 depth : depth + 1 ,
1195- statics : shiftScore ( statics , consumed ) ,
1196- dynamics : shiftScore ( dynamics , consumed ) ,
1197- optionals : shiftScore ( optionals , consumed ) ,
1194+ statics,
1195+ dynamics,
1196+ optionals,
11981197 extract,
11991198 rawParams,
12001199 parsedParams,
@@ -1238,9 +1237,9 @@ function getNodeMatch<T extends RouteLike>(
12381237 index : index + 1 ,
12391238 skipped,
12401239 depth : nextDepth ,
1241- statics : shiftScore ( statics ) ,
1242- dynamics : shiftScore ( dynamics ) ,
1243- optionals : shiftScore ( optionals ) + 1 ,
1240+ statics,
1241+ dynamics,
1242+ optionals : optionals + segmentScore ( partsLength , index ) ,
12441243 extract,
12451244 rawParams,
12461245 parsedParams,
@@ -1266,9 +1265,9 @@ function getNodeMatch<T extends RouteLike>(
12661265 index : index + 1 ,
12671266 skipped,
12681267 depth : depth + 1 ,
1269- statics : shiftScore ( statics ) ,
1270- dynamics : shiftScore ( dynamics ) + 1 ,
1271- optionals : shiftScore ( optionals ) ,
1268+ statics,
1269+ dynamics : dynamics + segmentScore ( partsLength , index ) ,
1270+ optionals,
12721271 extract,
12731272 rawParams,
12741273 parsedParams,
@@ -1287,9 +1286,9 @@ function getNodeMatch<T extends RouteLike>(
12871286 index : index + 1 ,
12881287 skipped,
12891288 depth : depth + 1 ,
1290- statics : shiftScore ( statics ) + 1 ,
1291- dynamics : shiftScore ( dynamics ) ,
1292- optionals : shiftScore ( optionals ) ,
1289+ statics : statics + segmentScore ( partsLength , index ) ,
1290+ dynamics,
1291+ optionals,
12931292 extract,
12941293 rawParams,
12951294 parsedParams,
@@ -1306,9 +1305,9 @@ function getNodeMatch<T extends RouteLike>(
13061305 index : index + 1 ,
13071306 skipped,
13081307 depth : depth + 1 ,
1309- statics : shiftScore ( statics ) + 1 ,
1310- dynamics : shiftScore ( dynamics ) ,
1311- optionals : shiftScore ( optionals ) ,
1308+ statics : statics + segmentScore ( partsLength , index ) ,
1309+ dynamics,
1310+ optionals,
13121311 extract,
13131312 rawParams,
13141313 parsedParams,
@@ -1353,8 +1352,13 @@ function getNodeMatch<T extends RouteLike>(
13531352 return null
13541353}
13551354
1356- function shiftScore ( score : number , amount = 1 ) : number {
1357- return score * 2 ** amount
1355+ function segmentScore ( partsLength : number , index : number ) : number {
1356+ // The specificity scores are bitmasks over consumed URL segments. Earlier
1357+ // URL segments should dominate later ones when comparing scores, so the
1358+ // first real segment gets the highest bit and the last gets bit 0. Since
1359+ // `parts[0]` is the empty string before the leading slash, real URL segments
1360+ // are [1, partsLength), making this segment's bit `partsLength - index - 1`.
1361+ return 2 ** ( partsLength - index - 1 )
13581362}
13591363
13601364function isPerfectStaticMatch ( statics : number , partsLength : number ) : boolean {
0 commit comments