File tree Expand file tree Collapse file tree 2 files changed +46
-3
lines changed Expand file tree Collapse file tree 2 files changed +46
-3
lines changed Original file line number Diff line number Diff line change @@ -3212,18 +3212,36 @@ export type ProcessRouteTreeResult<TRouteLike extends RouteLike> = {
3212
3212
const REQUIRED_PARAM_BASE_SCORE = 0.5
3213
3213
const OPTIONAL_PARAM_BASE_SCORE = 0.4
3214
3214
const WILDCARD_PARAM_BASE_SCORE = 0.25
3215
+ const BOTH_PRESENCE_BASE_SCORE = 0.05
3216
+ const PREFIX_PRESENCE_BASE_SCORE = 0.02
3217
+ const SUFFIX_PRESENCE_BASE_SCORE = 0.01
3218
+ const PREFIX_LENGTH_SCORE_MULTIPLIER = 0.0002
3219
+ const SUFFIX_LENGTH_SCORE_MULTIPLIER = 0.0001
3215
3220
3216
3221
function handleParam ( segment : Segment , baseScore : number ) {
3217
3222
if ( segment . prefixSegment && segment . suffixSegment ) {
3218
- return baseScore + 0.05
3223
+ return (
3224
+ baseScore +
3225
+ BOTH_PRESENCE_BASE_SCORE +
3226
+ PREFIX_LENGTH_SCORE_MULTIPLIER * segment . prefixSegment . length +
3227
+ SUFFIX_LENGTH_SCORE_MULTIPLIER * segment . suffixSegment . length
3228
+ )
3219
3229
}
3220
3230
3221
3231
if ( segment . prefixSegment ) {
3222
- return baseScore + 0.02
3232
+ return (
3233
+ baseScore +
3234
+ PREFIX_PRESENCE_BASE_SCORE +
3235
+ PREFIX_LENGTH_SCORE_MULTIPLIER * segment . prefixSegment . length
3236
+ )
3223
3237
}
3224
3238
3225
3239
if ( segment . suffixSegment ) {
3226
- return baseScore + 0.01
3240
+ return (
3241
+ baseScore +
3242
+ SUFFIX_PRESENCE_BASE_SCORE +
3243
+ SUFFIX_LENGTH_SCORE_MULTIPLIER * segment . suffixSegment . length
3244
+ )
3227
3245
}
3228
3246
3229
3247
return baseScore
Original file line number Diff line number Diff line change @@ -425,5 +425,30 @@ describe('processRouteTree', () => {
425
425
expect ( result . flatRoutes . map ( ( r ) => r . id ) ) . toEqual ( expected )
426
426
} ,
427
427
)
428
+
429
+ it . each ( [
430
+ {
431
+ routes : [ '/f{$param}' , '/foo{$param}' ] ,
432
+ expected : [ '/foo{$param}' , '/f{$param}' ] ,
433
+ } ,
434
+ {
435
+ routes : [ '/{$param}r' , '/{$param}bar' ] ,
436
+ expected : [ '/{$param}bar' , '/{$param}r' ] ,
437
+ } ,
438
+ {
439
+ routes : [ '/f{$param}bar' , '/foo{$param}r' ] ,
440
+ expected : [ '/foo{$param}r' , '/f{$param}bar' ] ,
441
+ } ,
442
+ {
443
+ routes : [ '/foo{$param}r' , '/f{$param}baaaaaar' ] , // very long suffix can "override" prefix
444
+ expected : [ '/f{$param}baaaaaar' , '/foo{$param}r' ] ,
445
+ } ,
446
+ ] ) (
447
+ 'length of prefix and suffix are considered in ranking: $routes' ,
448
+ ( { routes, expected } ) => {
449
+ const result = processRouteTree ( { routeTree : createRouteTree ( routes ) } )
450
+ expect ( result . flatRoutes . map ( ( r ) => r . id ) ) . toEqual ( expected )
451
+ } ,
452
+ )
428
453
} )
429
454
} )
You can’t perform that action at this time.
0 commit comments