-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Anchor links broken when navigating across pages #2664
Comments
I noticed this on my personal website and had to add a workaround here. |
Nice, thanks for narrowing this down to these versions - and for posting the workaround 🙏. I'm about to debug the SvelteKit source code to figure out what exactly causes it. I'm going to start my investigation around here (diff). Will report back with findings. |
Actually I think I was wrong about the versions. The first version of my site with the issue appears to be rosslh/rosshill.ca@d4834d4, so the bug must have been introduced between and |
Alright, https://github.com/sveltejs/kit/blob/master/packages/kit/src/runtime/client/renderer.js#L285 returns Once navigation completes, the 👀 |
Found a fix 🎉. PR incoming |
FYI for anyone else blocked by this. Inspired by @rosslh, I ended up adding the following to $: if ($page.path) {
// Workaround until https://github.com/sveltejs/kit/issues/2664 is fixed
if (typeof window !== "undefined" && window.location.hash) {
const deepLinkedElement = document.getElementById(
window.location.hash.substring(1)
);
if (deepLinkedElement) {
deepLinkedElement.scrollIntoView();
}
}
} |
I have updated my app to use the latest version of kit ( |
With Setup
❌ Does not work
✅ Does work
@mquandalle could you try to reload the page twice in your project and let me know that I'm not crazy 😕?! If you see the same, I'd feel a bit better - although can't explain why it happens. |
I tried testing this again but I'm not experiencing the bug, it all seems to work as usual. We may need another repro repo for this. |
After upgrading from 192 to 193 I'm seeing a bug introduced where normal scroll behavior on app navigation is not setting the scroll to the top of the document (when it should be). E.g. i'm scrolled half way down my home page, I could on a link to view my about page, I'm brought to half way down my about page (not the top of my about page). Commenting on this here since it seems to be a regression caused by #2720 (which was intended to fix this issue). I can open a new issue for this if need be. |
@yet-another-dev Looks like the the issue you described is opened at #2733. I think it's good the separate these issues at the moment since they're explaining conflicting scrolling scenarios (?). Maybe it's the same core issue but I'll try to look into this again today. |
Does this work any better with the latest version |
I have updated to The page is correctly scrolled on first load (probably SSR + browser handling) and then jumps to the top. This is a simple anchor without any specific logic. |
@mquandalle Can you provide a minimal reproduction to help isolate the bug? Probably the time where it would scroll to the top is if:
But otherwise the change we did for routers recently only affects inter-route navigation, not on initlal page load, so I'm not sure how it happened. |
This was actually an issue with a third-party library. |
Thanks for the update. @mikenikles Can you confirm that this still happens? Otherwise we can go ahead and close the issue. |
I can confirm, this works in my case on |
Thanks everyone who was involved in resolving this 🙏! |
Hello everyone, Not exactly sure where to post this. routes/pageA.svelte
routes/pageB.svelte
Clicking on the link in page A would bring me to the top of page B, ignoring the anchor. The only working solution was to use the workaround presented above, adding a small timeout before the scroll intoView
NB: It works fine if the anchor is on the same page Please let me know if you need me to open a new issue |
Hi! Same here with @sveltejs/[email protected], external links with # anchor land at the top of the page while links within the same page work as expected. IMHO this issue should be reopened. Thanks. |
I can't reproduce the issue. Please provide a repository with steps to reproduce |
FYI - it seems that this issue is only resolved for Chromium-based browsers. In Firefox (108.0.2), the issue still remains. I've installed @sveltejs/[email protected], @sveltejs/[email protected], and @sveltejs/[email protected] and none of them seem to resolve the issue for me in Firefox and I do not see any errors or other console output. As I said, it works fine in Chromium. |
I wound up here because I was having this problem in an inconsistent, "peek-a-boo" fashion for days and it was driving me insane; I had occasional success navigating with cross-page anchor links but, the overwhelming majority of the time, I would try to go to a specific id on a separate internal page and the window would scroll a few pixels on arrival and then hang up. I did find a fix, though I have no idea how broadly applicable it is. I'm just posting it for the next person who might or might not have the same problem. On the target page, I was tracking the vertical scroll with...
That's Pug syntax, but you get the point. Getting rid of that special element (and, of course, all related code) un-broke the cross-page anchor links. Bringing it back broke them again. So far, over a period of days, I've re-tested this observation and it's one-to-one. I don't understand the deeper mechanics, but I'm fairly confident that this vertical scroll binding to the svelte:window element is the isolated x-factor because I found a workaround with vanilla JS to accomplish what I was doing with the binding, and the cross-page anchor links still work with the workaround in place. |
I'm also running into this behavior when I directly navigate to a URL with an anchor that is meant to scroll the page down to that id. I haven't found any The behavior I see is:
The workaround mentioned above (and pasted below) isn't ideal since it happens after the improper Svelte scrolling and causes visual jitter on the page. But it's better than nothing. Including @jjagielka workaround from the linked PR: // from https://github.com/themesberg/flowbite-svelte/pull/923/files
onMount(() => {
// Workaround until https://github.com/sveltejs/kit/issues/2664 is fixed
if (typeof window !== 'undefined' && window.location.hash) {
const deepLinkedElement = document.getElementById(window.location.hash.substring(1))
if (deepLinkedElement) {
window.setTimeout(() => deepLinkedElement.scrollIntoView(), 100)
}
}
}) |
Dont know if related, but if I want navigate from anchor link to a page without an anchor link, the scroll to top does only work the second time in firefox, in chrome it seems to work fine: #2733 (comment) |
Describe the bug
I upgraded to next.188 yesterday and notice anchor links no longer work across pages.
Steps to reproduce
Actual result
The homepage loads, scrolled to the top
Expected result
The homepage loads, scrolled down to the Get Started Now section.
Note, if you click "Sign Up" again after step 2 (i.e. when you're on the homepage), the anchor link works.
Has anyone else seen this? I've been looking through Discord and GitHub, not much luck so far but will continue to search.
Reproduction
npm init svelte@next
& follow the instructionssrc/routes/index.html
add a bunch of<br />
elements to make the page scrollable, then add<h1 id="jump-here">Here is a page anchor</h1>
at the bottom of the pagesrc/routes/about.html
add a link:<a href="/#jump-here">Jump to section on homepage</a>
Logs
No response
System Info
Severity
blocking an upgrade
Additional Information
No response
The text was updated successfully, but these errors were encountered: