Skip to content

impr(nav): avoid duplicate browser history entries when re-clicking same nav button (@byseif21) #6624

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

Merged

Conversation

byseif21
Copy link
Contributor

@byseif21 byseif21 commented Jun 6, 2025

Description

Closes #6623

@monkeytypegeorge monkeytypegeorge added the frontend User interface or web stuff label Jun 6, 2025
@Miodec Miodec added the waiting for review Pull requests that require a review before continuing label Jun 16, 2025
Comment on lines 163 to 171
// only push to history if we're navigating to a different URL
const currentPath =
window.location.pathname + window.location.search + window.location.hash;
const targetUrl = new URL(url, window.location.origin);
const targetPath = targetUrl.pathname + targetUrl.search + targetUrl.hash;

if (currentPath !== targetPath) {
history.pushState(null, "", url);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Curious if there’s a reason we’re not directly comparing window.location.href with url — since the click handler calls this navigate function with target.href, it might work out cleanly and will simplify this. What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hi @pran01, I thought about using window.location.href !== url, but I went with this approach because url might be relative (like /settings), while href is always absolute. That mismatch could cause false negatives.

By comparing just the path, search, and hash, I make sure we only push when the actual navigable part changes—regardless of environment or base URL. It's a bit more verbose, but safer for typical SPA routing. that what I thought of.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

but rethinking this about simplifing thing, i think we could simplify this to be like that

const currentUrl = new URL(window.location.href);
const targetUrl = new URL(url, window.location.origin);

if (
  currentUrl.pathname + currentUrl.search + currentUrl.hash !==
  targetUrl.pathname + targetUrl.search + targetUrl.hash
) {
  history.pushState(null, "", url);
}

Same behavior, just a little cleaner. I will wait for mio tho.

Copy link
Member

Choose a reason for hiding this comment

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

but rethinking this about simplifing thing, i think we could simplify this to be like that

const currentUrl = new URL(window.location.href);
const targetUrl = new URL(url, window.location.origin);

if (
  currentUrl.pathname + currentUrl.search + currentUrl.hash !==
  targetUrl.pathname + targetUrl.search + targetUrl.hash
) {
  history.pushState(null, "", url);
}

Same behavior, just a little cleaner. I will wait for mio tho.

Yeah i would prefer that.

@Miodec Miodec added waiting for update Pull requests or issues that require changes/comments before continuing and removed waiting for review Pull requests that require a review before continuing labels Jun 23, 2025
@github-actions github-actions bot removed the waiting for update Pull requests or issues that require changes/comments before continuing label Jun 24, 2025
Copy link
Member

@Miodec Miodec left a comment

Choose a reason for hiding this comment

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

Thanks

@Miodec Miodec merged commit 7be7fbe into monkeytypegame:master Jun 24, 2025
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
frontend User interface or web stuff
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Navigation buttons add redundant history entries on repeated clicks
4 participants