-
-
Notifications
You must be signed in to change notification settings - Fork 433
Sapper treats anchor tag clicks as route changes #331
Comments
I've found a possibly related bug across all browsers which is that if I have a page at I've updated my test repo to also show this bug as well. Check out the instructions in the README to reproduce under Reproduce bug two I've set up a barebones HTML page to show the expected behavior at: https://github.com/mhkeller/anchor-test-two |
This may be because the I tried putting in similar code by modifying my test repo but I must not be triggering the build properly since I can't get my changes to show up. |
I'm starting to dig on this issue right now. |
Shouldn't hash be derived from target? Line 200 in 7aa3e90
It's not being passed in |
@mhkeller, can you test again with the new version? I believe that the PR from DayBr3ak solves this issue as well. |
This is still an issue with the latest version when first loading a page with an anchor. From looking at this, my initial idea is to just change this: sapper/runtime/src/app/start/index.ts Line 43 in 9e60a71
and switch |
leave anchor scrolling on initial load to browser
Based on this thread I would assume that I'm not supposed to be seeing the behavior described in this comment, where clicking a "pure id" anchor tag of the form This is with Sapper 0.27 and Svelte 3.0 – anyone know what might be causing it? Or was this half of the problem just never fixed? |
is this the baseUrl issue? (search for it) |
Yeah it is. It is indeed currently intended that an anchor like that takes you back to the root, and we realize that's not convenient, and there's an issue where we're trying to figure out what to do about it. |
Ah I see. For anyone else coming here, the issue is #904 |
I have the same issue with fragment in URL, when I am trying to use it: cheers! |
Its seems that this workaround on #904 may help, with adjustments from the next comment. |
the workaround of course doesn't hit shadow doms in web components and such..so still largely broken for us. couldn't sapper just tag onto something like rel="internal" to avoid processing the link as a route and instead treat it as a smooth scroll internal link? |
Another workaround for the time being might be to hijack and prevent the click event, using a native JS <script>
function scrollTo({ target }) {
document.querySelector(target.getAttribute('href')).scrollIntoView({
behavior: 'smooth'
});
}
</script>
<a href="#section-1" on:click|preventDefault={scrollTo}>Section 1</a>
<div id="section-1">...</div> This way server-rendered/static apps will still have the correct anchor |
@adamduncan sounds good. Did it work for you? |
@zolotokrylin There are a couple of related-but-different issues being discussed here. This One workaround is to prepend the |
Any word on getting this fix? I bashed a couple of the answers here together and got this working, I like this as a temporary solution (for me anyways).
|
a simple workaround is to put the local path with the href, for example if the path is http://localhost:3000/product/foo this is working on sapper: 0.28.10 and svelte: 3.29.4 on localhost, didn't try yet on a live server |
Here's a repo reproducing the issue, which I'm currently only seeing in Firefox: https://github.com/mhkeller/anchor-test
When I go to a page that has an anchor tag like
localhost:3000/#link-5
, it will initially load at the correct page height but then jump to the top. In Chrome and Safari, the page stays where it's supposed to.The text was updated successfully, but these errors were encountered: