Skip to content

Commit 9c2e5ae

Browse files
authored
Ensure backwards-compatibility with navigator.encodeLocation (#9647)
* Ensure backwards-compatibility with navigator.encodeLocation * add changeset
1 parent d4c7399 commit 9c2e5ae

File tree

4 files changed

+16
-4
lines changed

4 files changed

+16
-4
lines changed

.changeset/hungry-eyes-cheat.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"react-router": patch
3+
---
4+
5+
backwards compatibility for encodeLocation

packages/react-router-dom/index.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,9 @@ export const NavLink = React.forwardRef<HTMLAnchorElement, NavLinkProps>(
480480
let routerState = React.useContext(DataRouterStateContext);
481481
let { navigator } = React.useContext(NavigationContext);
482482

483-
let toPathname = navigator.encodeLocation(path).pathname;
483+
let toPathname = navigator.encodeLocation
484+
? navigator.encodeLocation(path).pathname
485+
: path.pathname;
484486
let locationPathname = location.pathname;
485487
let nextLocationPathname =
486488
routerState && routerState.navigation && routerState.navigation.location

packages/react-router/lib/context.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,8 @@ export interface NavigateOptions {
107107
*/
108108
export interface Navigator {
109109
createHref: History["createHref"];
110-
encodeLocation: History["encodeLocation"];
110+
// Optional for backwards-compat with Router/HistoryRouter usage (edge case)
111+
encodeLocation?: History["encodeLocation"];
111112
go: History["go"];
112113
push(to: To, state?: any, opts?: NavigateOptions): void;
113114
replace(to: To, state?: any, opts?: NavigateOptions): void;

packages/react-router/lib/hooks.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -405,15 +405,19 @@ export function useRoutes(
405405
pathname: joinPaths([
406406
parentPathnameBase,
407407
// Re-encode pathnames that were decoded inside matchRoutes
408-
navigator.encodeLocation(match.pathname).pathname,
408+
navigator.encodeLocation
409+
? navigator.encodeLocation(match.pathname).pathname
410+
: match.pathname,
409411
]),
410412
pathnameBase:
411413
match.pathnameBase === "/"
412414
? parentPathnameBase
413415
: joinPaths([
414416
parentPathnameBase,
415417
// Re-encode pathnames that were decoded inside matchRoutes
416-
navigator.encodeLocation(match.pathnameBase).pathname,
418+
navigator.encodeLocation
419+
? navigator.encodeLocation(match.pathnameBase).pathname
420+
: match.pathnameBase,
417421
]),
418422
})
419423
),

0 commit comments

Comments
 (0)