@tanstack/react-router 1.39.4 → 1.39.7

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
@@ -82,6 +82,7 @@ interface ResolvePathOptions {
82
82
  to: string
83
83
  trailingSlash?: 'always' | 'never' | 'preserve'
84
84
  }
85
+
85
86
  export function resolvePath({
86
87
  basepath,
87
88
  base,
@@ -196,6 +197,7 @@ interface InterpolatePathOptions {
196
197
  leaveWildcards?: boolean
197
198
  leaveParams?: boolean
198
199
  }
200
+
199
201
  export function interpolatePath({
200
202
  path,
201
203
  params,
@@ -203,21 +205,33 @@ export function interpolatePath({
203
205
  leaveParams,
204
206
  }: InterpolatePathOptions) {
205
207
  const interpolatedPathSegments = parsePathname(path)
208
+ const encodedParams: any = {}
209
+
210
+ for (const [key, value] of Object.entries(params)) {
211
+ const isValueString = typeof value === 'string'
212
+
213
+ if (['*', '_splat'].includes(key)) {
214
+ // the splat/catch-all routes shouldn't have the '/' encoded out
215
+ encodedParams[key] = isValueString ? encodeURI(value) : value
216
+ } else {
217
+ encodedParams[key] = isValueString ? encodeURIComponent(value) : value
218
+ }
219
+ }
206
220
 
207
221
  return joinPaths(
208
222
  interpolatedPathSegments.map((segment) => {
209
223
  if (segment.type === 'wildcard') {
210
- const value = params._splat
224
+ const value = encodedParams._splat
211
225
  if (leaveWildcards) return `${segment.value}${value ?? ''}`
212
226
  return value
213
227
  }
214
228
 
215
229
  if (segment.type === 'param') {
216
230
  if (leaveParams) {
217
- const value = params[segment.value]
231
+ const value = encodedParams[segment.value]
218
232
  return `${segment.value}${value ?? ''}`
219
233
  }
220
- return params![segment.value.substring(1)] ?? 'undefined'
234
+ return encodedParams![segment.value.substring(1)] ?? 'undefined'
221
235
  }
222
236
 
223
237
  return segment.value
@@ -241,7 +255,36 @@ export function matchPathname(
241
255
  }
242
256
 
243
257
  export function removeBasepath(basepath: string, pathname: string) {
244
- return basepath != '/' ? pathname.replace(basepath, '') : pathname
258
+ switch (true) {
259
+ // default behaviour is to serve app from the root - pathname
260
+ // left untouched
261
+ case basepath === '/':
262
+ return pathname
263
+
264
+ // shortcut for removing the basepath from the equal pathname
265
+ case pathname === basepath:
266
+ return ''
267
+
268
+ // in case pathname is shorter than basepath - there is
269
+ // nothing to remove
270
+ case pathname.length < basepath.length:
271
+ return pathname
272
+
273
+ // avoid matching partial segments - strict equality handled
274
+ // earlier, otherwise, basepath separated from pathname with
275
+ // separator, therefore lack of separator means partial
276
+ // segment match (`/app` should not match `/application`)
277
+ case pathname[basepath.length] !== '/':
278
+ return pathname
279
+
280
+ // remove the basepath from the pathname in case there is any
281
+ case pathname.startsWith(basepath):
282
+ return pathname.slice(basepath.length)
283
+
284
+ // otherwise, return the pathname as is
285
+ default:
286
+ return pathname
287
+ }
245
288
  }
246
289
 
247
290
  export function matchByPath(
package/src/router.ts CHANGED
@@ -1170,16 +1170,6 @@ export class Router<
1170
1170
  })
1171
1171
  }
1172
1172
 
1173
- // encode all path params so the generated href is valid and stable
1174
- Object.keys(nextParams).forEach((key) => {
1175
- if (['*', '_splat'].includes(key)) {
1176
- // the splat/catch-all routes shouldn't have the '/' encoded out
1177
- nextParams[key] = encodeURI(nextParams[key])
1178
- } else {
1179
- nextParams[key] = encodeURIComponent(nextParams[key])
1180
- }
1181
- })
1182
-
1183
1173
  pathname = interpolatePath({
1184
1174
  path: pathname,
1185
1175
  params: nextParams ?? {},