@tanstack/react-router 1.26.21 → 1.27.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/path.ts CHANGED
@@ -54,7 +54,18 @@ export function trimPath(path: string) {
54
54
  // /a/b/c + d = /a/b/c/d
55
55
  // /a/b/c + d/ = /a/b/c/d
56
56
  // /a/b/c + d/e = /a/b/c/d/e
57
- export function resolvePath(basepath: string, base: string, to: string) {
57
+ interface ResolvePathOptions {
58
+ basepath: string
59
+ base: string
60
+ to: string
61
+ trailingSlash?: 'always' | 'never' | 'preserve'
62
+ }
63
+ export function resolvePath({
64
+ basepath,
65
+ base,
66
+ to,
67
+ trailingSlash = 'never',
68
+ }: ResolvePathOptions) {
58
69
  base = base.replace(new RegExp(`^${basepath}`), '/')
59
70
  to = to.replace(new RegExp(`^${basepath}`), '/')
60
71
 
@@ -85,9 +96,18 @@ export function resolvePath(basepath: string, base: string, to: string) {
85
96
  }
86
97
  })
87
98
 
88
- const joined = joinPaths([basepath, ...baseSegments.map((d) => d.value)])
99
+ if (baseSegments.length > 1) {
100
+ if (last(baseSegments)?.value === '/') {
101
+ if (trailingSlash === 'never') {
102
+ baseSegments.pop()
103
+ }
104
+ } else if (trailingSlash === 'always') {
105
+ baseSegments.push({ type: 'pathname', value: '/' })
106
+ }
107
+ }
89
108
 
90
- return cleanPath(trimPathRight(joined))
109
+ const joined = joinPaths([basepath, ...baseSegments.map((d) => d.value)])
110
+ return cleanPath(joined)
91
111
  }
92
112
 
93
113
  export function parsePathname(pathname?: string): Array<Segment> {
package/src/router.ts CHANGED
@@ -150,6 +150,7 @@ export interface RouterOptions<
150
150
  defaultNotFoundComponent?: NotFoundRouteComponent
151
151
  transformer?: RouterTransformer
152
152
  errorSerializer?: RouterErrorSerializer<TSerializedError>
153
+ trailingSlash?: 'always' | 'never' | 'preserve'
153
154
  }
154
155
 
155
156
  export interface RouterTransformer {
@@ -591,7 +592,13 @@ export class Router<
591
592
  }
592
593
 
593
594
  resolvePathWithBase = (from: string, path: string) => {
594
- return resolvePath(this.basepath, from, cleanPath(path))
595
+ const resolvedPath = resolvePath({
596
+ basepath: this.basepath,
597
+ base: from,
598
+ to: cleanPath(path),
599
+ trailingSlash: this.options.trailingSlash,
600
+ })
601
+ return resolvedPath
595
602
  }
596
603
 
597
604
  get looseRoutesById() {