Skip to content

Commit 26963b6

Browse files
committed
Fix navigation for hash routers on manual URL changes
1 parent adc63c6 commit 26963b6

File tree

3 files changed

+24
-19
lines changed

3 files changed

+24
-19
lines changed

.changeset/six-tigers-fold.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"react-router-dom": patch
3+
"@remix-run/router": patch
4+
---
5+
6+
Fix navigation for hash routers on manual URL changes

packages/router/history.ts

+6-18
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ export interface Update {
8585
/**
8686
* The delta between this location and the former location in the history stack
8787
*/
88-
delta: number;
88+
delta: number | null;
8989
}
9090

9191
/**
@@ -612,24 +612,12 @@ function getUrlBasedHistory(
612612
}
613613

614614
function handlePop() {
615-
let nextAction = Action.Pop;
615+
action = Action.Pop;
616616
let nextIndex = getIndex();
617-
618-
if (nextIndex != null) {
619-
let delta = nextIndex - index;
620-
action = nextAction;
621-
index = nextIndex;
622-
if (listener) {
623-
listener({ action, location: history.location, delta });
624-
}
625-
} else {
626-
warning(
627-
false,
628-
`You are trying to perform a POP navigation to a location that was not ` +
629-
`created by @remix-run/router. This will fail silently in production. ` +
630-
`You should navigate via the router to avoid this situation (instead of ` +
631-
`using window.history.pushState/window.location.hash).`
632-
);
617+
let delta = nextIndex == null ? null : nextIndex - index;
618+
index = nextIndex;
619+
if (listener) {
620+
listener({ action, location: history.location, delta });
633621
}
634622
}
635623

packages/router/router.ts

+12-1
Original file line numberDiff line numberDiff line change
@@ -781,12 +781,23 @@ export function createRouter(init: RouterInit): Router {
781781
return;
782782
}
783783

784+
warning(
785+
activeBlocker != null && delta === null,
786+
"You are trying to use a blocker on a POP navigation to a location " +
787+
"that was not created by @remix-run/router. This will fail silently in " +
788+
"production. This can happen if you are navigating outside the router " +
789+
"via `window.history.pushState`/`window.location.hash` instead of using " +
790+
"router navigation APIs. This can also happen if you are using " +
791+
"createHashRouter and the user manually changes the URL."
792+
);
793+
784794
let blockerKey = shouldBlockNavigation({
785795
currentLocation: state.location,
786796
nextLocation: location,
787797
historyAction,
788798
});
789-
if (blockerKey) {
799+
800+
if (blockerKey && delta != null) {
790801
// Restore the URL to match the current UI, but don't update router state
791802
ignoreNextHistoryUpdate = true;
792803
init.history.go(delta * -1);

0 commit comments

Comments
 (0)