@@ -27,23 +27,24 @@ export function href<Path extends keyof Args>(
27
27
...args : Args [ Path ]
28
28
) : string {
29
29
let params = args [ 0 ] ;
30
- let result = path . replace (
31
- / \/ : ( [ \w - ] + ) ( \? ) ? / g, // same regex as in .\router\utils.ts: compilePath().
32
- ( _ : string , param : string , isOptional ) => {
33
- const value = params ? params [ param ] : undefined ;
34
- if ( isOptional == null && value == null ) {
35
- throw new Error (
36
- `Path '${ path } ' requires param '${ param } ' but it was not provided`
37
- ) ;
30
+ let result = path
31
+ . replace ( / \/ * \* ? $ / , "" ) // Ignore trailing / and /*, we'll handle it below
32
+ . replace (
33
+ / \/ : ( [ \w - ] + ) ( \? ) ? / g, // same regex as in .\router\utils.ts: compilePath().
34
+ ( _ : string , param : string , isOptional ) => {
35
+ const value = params ? params [ param ] : undefined ;
36
+ if ( isOptional == null && value == null ) {
37
+ throw new Error (
38
+ `Path '${ path } ' requires param '${ param } ' but it was not provided`
39
+ ) ;
40
+ }
41
+ return value == null ? "" : "/" + value ;
38
42
}
39
- return value == null ? "" : "/" + value ;
40
- }
41
- ) ;
43
+ ) ;
42
44
43
- if ( result . endsWith ( "*" ) ) {
45
+ if ( path . endsWith ( "*" ) ) {
44
46
// treat trailing splat the same way as compilePath, and force it to be as if it were `/*`.
45
47
// `react-router typegen` will not generate the params for a malformed splat, causing a type error, but we can still do the correct thing here.
46
- result = result . slice ( 0 , result . endsWith ( "/*" ) ? - 2 : - 1 ) ;
47
48
if ( params && params [ "*" ] != null ) {
48
49
result += "/" + params [ "*" ] ;
49
50
}
0 commit comments