-
-
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
Page scroll position not reset to top on navigation (regression) #2733
Comments
Almost certainly caused by something in #2668, as that was merged in between the next.192 and next.193 releases and it's a scroll-related feature. |
I'm having same problem. What is the workaround please? |
@raymclee for now, downgrade to 1.0.0-next.192. It's only 1 version back so you shouldn't be missing much (changelog)
|
thanks |
With |
@rosslh I'm not able to reproduce in Firefox with the repro steps given in this issue. Do you have a different repro for the issue? |
My issue was resolved at some point, not sure which version. |
Fixed for me in |
Solve for me with |
i still have this issue with |
I've been having this issue recently in |
Will this be resolved in an upcoming release? It's not a huge problem for me so I'd rather not downgrade to an earlier version, but it is a bit ugly so it would be great if it was solved a some point. |
I have the same issue on MacOs with 1.0.0-next.240 on all available browsers. @benmccann please can we reopen this bug. I have seen that there are some related issues which are already closed. I'm not sure it makes sense to create a new bug every time this problem seems to be fixed. |
Having the same issue on MacOs with SvelteKit v1.0.0-next.232 |
I cannot reproduce this issue. Scroll position is reset to the top as it should be for me. Im also using MacOS and the current SvelteKit Version (v1.0.0-next.242) and tested with Chrome and Safari using links to different routes (explicitly not "/"). It would be great if someone who has this issue could provide a repository to reproduce and detailed system information. |
I can only reproduce it on my iPhone, not on my laptop (Windows). If you have an iPhone, try and visit my site and scroll down a bit, then use the hamburger menu to go to the page "Skapa konto" (means "create account" in Swedish). You'll find that the scroll position isn't reset, since you can scroll up a bit. |
A little bit more info: I just updated to SvelteKit v1.0.0-next.245, and scroll is still not reseting on page change, tried it on firefox, brave, and safari, which are all updated. You can as well visit my website to see it not working [https://painhas.com], just go there, scroll down a bit, and click in "originals" on the header (for example). Also im on macOS Monterey 12.2 beta to be more specific. |
I'm here to report that I'm seeing this issue on the sveltekit sverdle demo (as well as my site) when I use |
Which issue are you referring to that causes scroll to break?
If it's different from both of these, can you provide a reproducible? |
It functions the same way people are describing the iOS issue in that it only appears when I apply the ’scroll-behavior: smooth’. I was able to reproduce it with a fresh install of the sverdle demo by only editing the style.css to add the smooth scrolling. Edit: Here's the recording: |
Thanks for the helpful feedback! I will try to investigate this as well. |
I can't seem to reproduce this on the same version of Firefox (macos). https://stackblitz.com/github/s3812497/sveltejs-kit-2733?file=README.md Can you reproduce it using the repository I've shared, or provide your own reproducible repository? |
@s3812497 I am able to reproduce it with the repo you linked above: I was also to reproduce it in my repository: https://github.com/AaronNBrock/sveltekit-scroll-test With some playing around with it. I'm noticing very consistent, yet odd behavior. You'll see the site has 3 pages
Also, this issue is only on Firefox (my version is 112.0.2 (64-bit) Windows 10) it works as expected on Chrome / Edge. |
I bumped into this issue on my personal repo, using sveltekit here's a minimal reproducible sample https://github.com/crowdozer/svelte-kit-scroll-test the gist is this inner "page" div never scrolls on navigation <div class="flex h-full flex-col overflow-auto">
<header>
<!-- header content -->
</header>
<div id="page" class="flex grow flex-col">
<div class="grow">
<!-- main page content here -->
</div>
<footer>
<!-- footer content -->
</footer>
</div>
</div> my workaround has been to put this in the root layout, now it works fine import { afterNavigate } from '$app/navigation';
afterNavigate(() => {
document.getElementById('page')?.scrollTo(0, 0);
}); |
Yup, this is a known issue. Sveltekit doesn't know which area to scroll if it's not the |
Likely related, or maybe just more noticeable with this CSS: html {
scroll-behavior: smooth;
} Goal behavior:
Actual behavior for #2:
|
This behaviour and the solution are talked more about in |
Thanks @s3812497. I'd consider that a workaround, not a bug fix. But appreciate you pointing to that code. |
Agreed. And it feels like a particularly hacky workaround for a bug -- it has to be included everywhere and doesn't really address the issue. Some things I've noticed in my tests with Svelte 3.59.2 and Kit 1.22.3 (I can't update to 4.1.1 yet).
|
I have a similar problem. When a navigate from the root page to a details page, the scroll position is not reset. I use the following snippet in my {#key $page.url}
<div bind:this={contentContainer} class="flex-1 overflow-auto">
<slot />
</div>
{/key} |
Noting #8723 (comment) as a workaround. Perhaps we should document this as a solution? |
In my case, scroll behavior is only broken when navigating from root to another page, and only on the initial load. Refreshing the page will then scroll to the top. I'm running SvelteKit 2.3.2 and Svelte 4.2.8. I tried many of the suggested fixes in this thread, but the only one that worked for me was using the following, or minor variants (e.g. binding to an element and scrollIntoView(), not checking nav type, etc.). The timeout is necessary, and setting the scroll behavior breaks it again.
However, that fix can result in a content flash that's a bit annoying on some loads due to the nature of setTimeout(). Inspired by that fix, the following also works for me and doesn't result in any content flashing.
Seems odd, since as far as I can tell, svelteKit natively does something very similar (see here). |
Still broken on Firefox. Seems to work on Chrome. |
I still have this problem on both MacOS Safari 16.6 and Chrome 122.0.6261.69. But the weird thing is that it does not do it on every page. But it is consistent in the sense that a link to the "team" page would always keep the previous scroll position. It is really the previous scroll position because I have tried different scroll lengths to verify. The thing is that I cannot pin point what is similar is faulty pages that could cause this. Since I have checked this thread, I have checked and my Here is what I can confirm though: It is related to the fact that I have a I personally have Here you go, I hope it'll help somebody make sense of this whole issue. |
Due to how large the scope of this thread has become, it’s difficult to understand how your app’s scrolling is broken, and what is causing it. It would be a great help for future reports to include a description of the expected behaviour, the current behaviour, and a minimal reproduction in the form a downloadable code repository or stackblitz link so that we can diagnose the root cause. |
Looks to me like setting Here's my dirty fix in <script>
// Fix SvelteKit scrolling issue
import { browser } from "$app/environment";
beforeNavigate(async (nav) => {
if (!browser) return;
document.getElementsByTagName("html")[0].classList.add("pageSwitch");
})
afterNavigate(async (nav) => {
if (!browser) return;
await tick();
document.getElementsByTagName("html")[0].classList.remove("pageSwitch");
});
</script>
<style>
:global(html.pageSwitch) {
scroll-behavior: auto;
}
</style> |
TODO: remove when it will be fixed: sveltejs/kit#2733
Same issue. I haven't found any working solution/hack/work-around. Sadly, none of the solutions mentioned in here seemed to work for me either. I should also mention that I am unable to reproduce this issue in a desktop browser, even if I make the viewport short enough to make the page scroll-able, and scroll down before navigating to the next link. This issue for me only seems to be happening on mobile browsers. ./package.json
|
I had a very similar problem and my issue was that I wasn't actually scrolling in the top level page but inside my content div. To fix this, I bound the content div to a variable, and then invoked <script>
import { onNavigate } from '$app/navigation';
let contentDiv;
onNavigate((navigation) => {
return new Promise((resolve) => {
const transition = document.startViewTransition(async () => {
if(contentDiv) {
// Fix scroll
contentDiv.scrollTop = 0;
}
resolve();
await navigation.complete;
});
});
});
</script>
<div class="header">...</div>
<div class="content" bind:this={contentDiv}>
<slot />
</div>
<div class="footer">...</div> |
In this case it broke for me on firefox:
When removing scroll-behaviour, it will work. |
The problem is not really related to SvelteKit. If you have your using overflow-y-scroll or overflow-x-auto or h-screen (tailwind classes you can lookup the css styles as it also applies) then you will notice this behavior. If you remove those, the behavior returns to normal. This is a known side effect of using these on outer wrappers in CSS. You need to change the outer structure of your HTML to fix the issue. For me I wanted a custom scrollbar styling and without those classes it wasn't applying. The fix was to apply the custom scrollbar classes to the html element. I could then completely remove all of my h-screen, and overflow-y classes entirely and it all just worked. |
I don't know this will help but I got this solution. // +layout.svelte -- I put the code inside layout svelte so that it can affect every page.
// 1. Maka a variable that stores old pathname.
let pathname = '';
// 2. When the layout page mount, store the pathname.
onMount(() => {
pathname = $page.url.pathname;
});
// 4. Store the pathname before navigating.
beforeNavigate(() => {
pathname = $page.url.pathname;
});
// 3. Compare the pathnames and if they are different then make the scroll top.
afterNavigate(() => {
if (pathname === $page.url.pathname) return;
window.scrollTo({ top: 0, behavior: 'auto' });
}); |
Same here. I am only able to reproduce in chrome for iOS. Everything works as expected in Safari and on desktop browsers of all viewport widths and setting the device to iphone in inspector settings. I can't seem to fix a hack or work around. It's like chrome on mobile just wants to retain the scroll position of the previous page no matter what. I remember running into this problem 6 months or so ago, and finding a fix. But I can't, for the life of me remember what I did. |
The
|
@rubenesque-code You might not have considered the case where you don't want to scroll to the very top, so when you press links with url fragment set ( |
@khromov Thank you so much for your suggestion and taking the time to provide a snippet! Sorry I did not see your reply until now. It did not show in the unread messages for some reasons. I will definitely try that when going back to the project I was talking about. |
Describe the bug
When navigating to a new page, if you are already scrolled partway down the page, the next page load will keep the current scroll position and not scroll back to the top.
I believe this broke with next-193. When I install next-192, this does not happen -- navigating to a new page always scrolls to the top, just like regular browser navigation.
Reproduction
Init the SvelteKit demo project. Add the following paragraph in
about.svelte
after the last paragraph.Make the browser window small enough so that the page scrolls and scroll to the bottom of the page. Click the Home link. The Home page loads, but does not scroll to the top (i.e. the nav bar is not visible).
.
Tested in Chrome and Firefox on Windows 10.
Logs
No response
System Info
Severity
serious, but I can work around it
Additional Information
No response
The text was updated successfully, but these errors were encountered: