Skip to content

Commit

Permalink
Fix navigation for hash routers on manual URL changes (#9980)
Browse files Browse the repository at this point in the history
  • Loading branch information
brophdawg11 committed Jan 26, 2023
1 parent adc63c6 commit e9fa13f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 19 deletions.
6 changes: 6 additions & 0 deletions .changeset/six-tigers-fold.md
@@ -0,0 +1,6 @@
---
"react-router-dom": patch
"@remix-run/router": patch
---

Fix navigation for hash routers on manual URL changes
24 changes: 6 additions & 18 deletions packages/router/history.ts
Expand Up @@ -85,7 +85,7 @@ export interface Update {
/**
* The delta between this location and the former location in the history stack
*/
delta: number;
delta: number | null;
}

/**
Expand Down Expand Up @@ -612,24 +612,12 @@ function getUrlBasedHistory(
}

function handlePop() {
let nextAction = Action.Pop;
action = Action.Pop;
let nextIndex = getIndex();

if (nextIndex != null) {
let delta = nextIndex - index;
action = nextAction;
index = nextIndex;
if (listener) {
listener({ action, location: history.location, delta });
}
} else {
warning(
false,
`You are trying to perform a POP navigation to a location that was not ` +
`created by @remix-run/router. This will fail silently in production. ` +
`You should navigate via the router to avoid this situation (instead of ` +
`using window.history.pushState/window.location.hash).`
);
let delta = nextIndex == null ? null : nextIndex - index;
index = nextIndex;
if (listener) {
listener({ action, location: history.location, delta });
}
}

Expand Down
13 changes: 12 additions & 1 deletion packages/router/router.ts
Expand Up @@ -781,12 +781,23 @@ export function createRouter(init: RouterInit): Router {
return;
}

warning(
activeBlocker != null && delta === null,
"You are trying to use a blocker on a POP navigation to a location " +
"that was not created by @remix-run/router. This will fail silently in " +
"production. This can happen if you are navigating outside the router " +
"via `window.history.pushState`/`window.location.hash` instead of using " +
"router navigation APIs. This can also happen if you are using " +
"createHashRouter and the user manually changes the URL."
);

let blockerKey = shouldBlockNavigation({
currentLocation: state.location,
nextLocation: location,
historyAction,
});
if (blockerKey) {

if (blockerKey && delta != null) {
// Restore the URL to match the current UI, but don't update router state
ignoreNextHistoryUpdate = true;
init.history.go(delta * -1);
Expand Down

0 comments on commit e9fa13f

Please sign in to comment.