Skip to content

Commit 4663034

Browse files
authored
refactor(router-core): parseLocation fast path w/ no rewrites (#6516)
1 parent 846fffc commit 4663034

2 files changed

Lines changed: 26 additions & 7 deletions

File tree

packages/react-router/tests/router.test.tsx

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -706,12 +706,6 @@ describe('encoding/decoding: wildcard routes/params', () => {
706706

707707
await router.load()
708708

709-
expect(
710-
router.state.location.href.endsWith(
711-
'/framework/react/guide/file-based-routing%20tanstack',
712-
),
713-
).toBe(true)
714-
715709
expect(router.state.location.href).toBe(
716710
'/framework/react/guide/file-based-routing%20tanstack',
717711
)

packages/router-core/src/router.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1252,9 +1252,34 @@ export class RouterCore<
12521252
previousLocation,
12531253
) => {
12541254
const parse = ({
1255+
pathname,
1256+
search,
1257+
hash,
12551258
href,
12561259
state,
12571260
}: HistoryLocation): ParsedLocation<FullSearchSchema<TRouteTree>> => {
1261+
// Fast path: no rewrite configured and pathname doesn't need encoding
1262+
// Characters that need encoding: space, high unicode, control chars
1263+
// eslint-disable-next-line no-control-regex
1264+
if (!this.rewrite && !/[ \x00-\x1f\x7f\u0080-\uffff]/.test(pathname)) {
1265+
const parsedSearch = this.options.parseSearch(search)
1266+
const searchStr = this.options.stringifySearch(parsedSearch)
1267+
1268+
return {
1269+
href: pathname + searchStr + hash,
1270+
publicHref: href,
1271+
pathname: decodePath(pathname),
1272+
external: false,
1273+
searchStr,
1274+
search: replaceEqualDeep(
1275+
previousLocation?.search,
1276+
parsedSearch,
1277+
) as any,
1278+
hash: decodePath(hash.slice(1)),
1279+
state: replaceEqualDeep(previousLocation?.state, state),
1280+
}
1281+
}
1282+
12581283
// Before we do any processing, we need to allow rewrites to modify the URL
12591284
// build up the full URL by combining the href from history with the router's origin
12601285
const fullUrl = new URL(href, this.origin)
@@ -1276,7 +1301,7 @@ export class RouterCore<
12761301
external: !!this.rewrite && url.origin !== this.origin,
12771302
searchStr,
12781303
search: replaceEqualDeep(previousLocation?.search, parsedSearch) as any,
1279-
hash: decodePath(url.hash.split('#').reverse()[0] ?? ''),
1304+
hash: decodePath(url.hash.slice(1)),
12801305
state: replaceEqualDeep(previousLocation?.state, state),
12811306
}
12821307
}

0 commit comments

Comments
 (0)