diff --git a/packages/react-router/tests/router.test.tsx b/packages/react-router/tests/router.test.tsx index 585c641297a..052dafa3774 100644 --- a/packages/react-router/tests/router.test.tsx +++ b/packages/react-router/tests/router.test.tsx @@ -706,12 +706,6 @@ describe('encoding/decoding: wildcard routes/params', () => { await router.load() - expect( - router.state.location.href.endsWith( - '/framework/react/guide/file-based-routing%20tanstack', - ), - ).toBe(true) - expect(router.state.location.href).toBe( '/framework/react/guide/file-based-routing%20tanstack', ) diff --git a/packages/router-core/src/router.ts b/packages/router-core/src/router.ts index e783bb7f0a2..9fbb6744bb8 100644 --- a/packages/router-core/src/router.ts +++ b/packages/router-core/src/router.ts @@ -1252,9 +1252,34 @@ export class RouterCore< previousLocation, ) => { const parse = ({ + pathname, + search, + hash, href, state, }: HistoryLocation): ParsedLocation> => { + // Fast path: no rewrite configured and pathname doesn't need encoding + // Characters that need encoding: space, high unicode, control chars + // eslint-disable-next-line no-control-regex + if (!this.rewrite && !/[ \x00-\x1f\x7f\u0080-\uffff]/.test(pathname)) { + const parsedSearch = this.options.parseSearch(search) + const searchStr = this.options.stringifySearch(parsedSearch) + + return { + href: pathname + searchStr + hash, + publicHref: href, + pathname: decodePath(pathname), + external: false, + searchStr, + search: replaceEqualDeep( + previousLocation?.search, + parsedSearch, + ) as any, + hash: decodePath(hash.slice(1)), + state: replaceEqualDeep(previousLocation?.state, state), + } + } + // Before we do any processing, we need to allow rewrites to modify the URL // build up the full URL by combining the href from history with the router's origin const fullUrl = new URL(href, this.origin) @@ -1276,7 +1301,7 @@ export class RouterCore< external: !!this.rewrite && url.origin !== this.origin, searchStr, search: replaceEqualDeep(previousLocation?.search, parsedSearch) as any, - hash: decodePath(url.hash.split('#').reverse()[0] ?? ''), + hash: decodePath(url.hash.slice(1)), state: replaceEqualDeep(previousLocation?.state, state), } }