-
-
Couldn't load subscription status.
- Fork 2.1k
fix: prevent history change when clicking same hash link #10032
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
Conversation
🦋 Changeset detectedLatest commit: ba4354d The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
|
@trueadm @dummdidumm I'm using #contact and #cart hashes to open a popin over my site (a contact form and a shopping cart), but since this fix I can only open and close them once. In order to open one, I now have to close one to be able to open the other. Also, I now cannot open the cart/contact popin again after closing it, for instance. Using hashes allows me to be able to share an URL and still open the popin needed from the hash present in the URL. Ideally I'd need the new Shallow routing feature but it's still in WIP. Is there any workaround for this issue until then? Here is how I implement it: // +layout.svelte - detect hashchange to change my stores that open each popin
const handleHashChange = () => {
$showContact = window.location.hash === '#contact'
$cartOpen = window.location.hash === '#cart'
}
// Header.svelte - close function when clicking on a button
const handleButtonClick = (event: MouseEvent, name: string) => {
// Contact/Cart: Close popin
if (
(name === 'contact' && $showContact) ||
(name === 'cart' && $cartOpen)
) {
closePopin()
event.preventDefault()
}
}
<Button
size="small"
href="#cart"
label={$cartAmount > 0 ? String($cartAmount) : null}
on:click={event => handleButtonClick(event, 'cart')}
/>
// utils/functions.ts - closing the popin
export const closePopin = () => {
// Remove hash from url
history.pushState({}, '', window.location.href.split('#')[0])
// Trigger
window.dispatchEvent(new HashChangeEvent('hashchange'))
} |
|
@flayks What happens if you remove the |
|
@trueadm unfortunately nothing… 😅 I've tried that before thinking that it would block the click situation |
|
You shouldn't need to do |
|
I think |
|
If I'm not mistaken, your logic was working before because of a bug in the routing logic. I think the best approach you'd want to take, is to capture the scroll position and then re-instate it after the route change. I'm very new to SvelteKit, but maybe there's some API exposed that you can use that allows for a route change that keeps scroll position the same without needing to |
|
I feel that just using |
|
I did a little test here for the contact hash: if (name === 'contact') {
if ($showContact) {
closePopin()
} else {
window.location.hash = 'contact'
}
}When triggered manually like so, it allows to "force" the hash to change. It looks a bit counterproductive regarding the hash link of my button but that's the only workaround I found so far 🤷 This is fine on my Header, although I have these buttons in other places and need to implement the same workaround everywhere which I really would like to avoid. |
|
This patch introduces a killer bug in my app. |
Fixes #8725.
This PR fixes an outstanding issue with routing hash links in Firefox. Specifically, clicking a link that contains as hash and where the link is the same as the current page URL, Firefox clears the history
state, causing browser navigation to fail thereafter. In order to tackle this, we need to detect where the URL for a hash link is the same and manually prevent any client-side routing to take place. Instead we just apply the same scrolling heuristics – fixing the issue in FF.Please don't delete this checklist! Before submitting the PR, please make sure you do the following:
Tests
pnpm testand lint the project withpnpm lintandpnpm checkChangesets
pnpm changesetand following the prompts. Changesets that add features should beminorand those that fix bugs should bepatch. Please prefix changeset messages withfeat:,fix:, orchore:.