Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix navigation for hash routers on manual URL changes #9980

Merged
merged 1 commit into from Jan 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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).`
);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should only apply when blockers were present, so lift it up to the router. We'll send delta === null when we can't determine it

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."
);
chaance marked this conversation as resolved.
Show resolved Hide resolved

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