Skip to content

Commit 2c7c08c

Browse files
committed
refactor(router-core): buildLocation can skip decoding the next pathname if we dont encode it in the first place
1 parent cdd2de3 commit 2c7c08c

File tree

2 files changed

+18
-12
lines changed

2 files changed

+18
-12
lines changed

packages/router-core/src/path.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -208,8 +208,10 @@ export function resolvePath({
208208
interface InterpolatePathOptions {
209209
path?: string
210210
params: Record<string, unknown>
211-
// Map of encoded chars to decoded chars (e.g. '%40' -> '@') that should remain decoded in path params
211+
/** Map of encoded chars to decoded chars (e.g. '%40' -> '@') that should remain decoded in path params */
212212
decodeCharMap?: Map<string, string>
213+
/** whether to encode the interpolated params (defaults: true) */
214+
encode?: boolean
213215
}
214216

215217
type InterPolatePathResult = {
@@ -248,6 +250,7 @@ export function interpolatePath({
248250
path,
249251
params,
250252
decodeCharMap,
253+
encode = true,
251254
}: InterpolatePathOptions): InterPolatePathResult {
252255
// Tracking if any params are missing in the `params` object
253256
// when interpolating the path
@@ -298,7 +301,9 @@ export function interpolatePath({
298301
continue
299302
}
300303

301-
const value = encodeParam('_splat', params, decodeCharMap)
304+
const value = encode
305+
? encodeParam('_splat', params, decodeCharMap)
306+
: params._splat
302307
joined += '/' + prefix + value + suffix
303308
continue
304309
}
@@ -312,7 +317,9 @@ export function interpolatePath({
312317

313318
const prefix = path.substring(start, segment[1])
314319
const suffix = path.substring(segment[4], end)
315-
const value = encodeParam(key, params, decodeCharMap) ?? 'undefined'
320+
const value =
321+
(encode ? encodeParam(key, params, decodeCharMap) : params[key]) ??
322+
'undefined'
316323
joined += '/' + prefix + value + suffix
317324
continue
318325
}
@@ -335,7 +342,8 @@ export function interpolatePath({
335342

336343
usedParams[key] = valueRaw
337344

338-
const value = encodeParam(key, params, decodeCharMap) ?? ''
345+
const value =
346+
(encode ? encodeParam(key, params, decodeCharMap) : params[key]) ?? ''
339347
joined += '/' + prefix + value + suffix
340348
continue
341349
}

packages/router-core/src/router.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1184,7 +1184,7 @@ export class RouterCore<
11841184
pathname: decodePath(pathname),
11851185
searchStr,
11861186
search: replaceEqualDeep(previousLocation?.search, parsedSearch) as any,
1187-
hash: hash.split('#').reverse()[0] ?? '',
1187+
hash: last(hash.split('#')) ?? '',
11881188
state: replaceEqualDeep(previousLocation?.state, state),
11891189
}
11901190
}
@@ -1681,13 +1681,11 @@ export class RouterCore<
16811681
? // Use the original template path for interpolation
16821682
// This preserves the original parameter syntax including optional parameters
16831683
nextTo
1684-
: decodePath(
1685-
interpolatePath({
1686-
path: nextTo,
1687-
params: nextParams,
1688-
decodeCharMap: this.pathParamsDecodeCharMap,
1689-
}).interpolatedPath,
1690-
)
1684+
: interpolatePath({
1685+
path: nextTo,
1686+
params: nextParams,
1687+
encode: false,
1688+
}).interpolatedPath
16911689

16921690
// Resolve the next search
16931691
let nextSearch = fromSearch

0 commit comments

Comments
 (0)