Skip to content

Commit bfba2da

Browse files
authored
Do not throw during scroll restoration if the url hash is not a valid URI component (#13247)
1 parent 1396c7f commit bfba2da

File tree

3 files changed

+20
-7
lines changed

3 files changed

+20
-7
lines changed

.changeset/proud-scissors-tickle.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+
Do not throw if the url hash is not a valid URI component

contributors.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,7 @@
386386
- ValiantCat
387387
- vdusart
388388
- vesan
389+
- vezaynk
389390
- VictorElHajj
390391
- vijaypushkin
391392
- vikingviolinist

packages/react-router/lib/dom/lib.tsx

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2090,14 +2090,21 @@ export function useScrollRestoration({
20902090
}
20912091

20922092
// try to scroll to the hash
2093-
if (location.hash) {
2094-
let el = document.getElementById(
2095-
decodeURIComponent(location.hash.slice(1))
2096-
);
2097-
if (el) {
2098-
el.scrollIntoView();
2099-
return;
2093+
try {
2094+
if (location.hash) {
2095+
let el = document.getElementById(
2096+
decodeURIComponent(location.hash.slice(1))
2097+
);
2098+
if (el) {
2099+
el.scrollIntoView();
2100+
return;
2101+
}
21002102
}
2103+
} catch {
2104+
warning(
2105+
false,
2106+
`"${location.hash.slice(1)}" is not a decodable element ID. The view will not scroll to it.`
2107+
);
21012108
}
21022109

21032110
// Don't reset if this navigation opted out

0 commit comments

Comments
 (0)