kit: Anchor links broken when navigating across pages
Describe the bug
I upgraded to next.188 yesterday and notice anchor links no longer work across pages.
Steps to reproduce
- Navigate to https://deploy-preview-1131--gitpod-kumquat.netlify.app/blog
- Click “Sign up” at the top right
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 instructions- In
src/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 page - In
src/routes/about.html
add a link:<a href="/#jump-here">Jump to section on homepage</a>
Logs
No response
System Info
System:
OS: macOS 11.2.3
CPU: (8) x64 Apple M1
Memory: 136.04 MB / 16.00 GB
Shell: 5.8 - /bin/zsh
Binaries:
Node: 14.17.4 - /usr/local/bin/node
npm: 7.7.6 - /opt/homebrew/bin/npm
Browsers:
Brave Browser: 94.1.30.87
Chrome: 95.0.4638.54
Edge: 93.0.961.47
Firefox: 92.0
Firefox Developer Edition: 88.0
Safari: 14.0.3
npmPackages:
@sveltejs/kit: next => 1.0.0-next.188
svelte: ^3.34.0 => 3.44.0
Severity
blocking an upgrade
Additional Information
No response
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 1
- Comments: 25 (14 by maintainers)
Commits related to this issue
- fix cross-document scroll bug (#4386) (#4640) * fix cross-document scroll bug (#4386) * make test fail semi-reliably without fix — committed to sveltejs/kit by Rich-Harris 2 years ago
Found a fix 🎉. PR incoming
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…
svelte:window(bind:scrollY)
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.
Hello everyone,
Not exactly sure where to post this. I have encountered the exact same issue.
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 Thank you,
Thanks everyone who was involved in resolving this 🙏!
This was actually an issue with a third-party library. I reported the issue in https://github.com/flekschas/svelte-simple-modal/issues/53, sorry for the noise here!
@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.
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.
FYI for anyone else blocked by this. Inspired by @rosslh, I ended up adding the following to
__layout.svelte
to unblock myself until the PR gets merged.Actually I think I was wrong about the versions. The first version of my site with the issue appears to be https://github.com/rosslh/rosshill.ca/commit/d4834d45748c553e1fe046373617d10b8d6a1b59, so the bug must have been introduced between and
1.0.0-next.169
and1.0.0-next.179