Wat is de beste manier om de scrollpositie op de hoofdpagina (categorieën) te onthouden?

AI-generated summary

In a discussion titled “What is the best way to remember the scroll position on main (categories) page?” under the Dev category, 4ong observes that while many Discourse pages preserve scroll position, the categories page always resets to the top — negatively affecting UX, especially with many categories. Referencing an old topic and a commit by @markvanlan, 4ong asks whether there’s a simple way to replicate scroll preservation on the categories page, or if it’s better to implement it as a theme component. markvanlan suggests using the onPageChange API to detect when the user is on the categories page, retrieve the last scroll position from localStorage, scroll to it, and attach a scroll event listener to save position changes — removing the listener when leaving the page. 4ong thanks markvanlan, acknowledges limited frontend experience, and requests the topic remain open for others who may have already implemented a solution. Later, 4ong shares a quick working solution using sessionStorage and the Discourse plugin API: on visiting / or /categories, it restores the saved scroll position and listens for scroll events to update it; otherwise, it removes the listener. The script uses window.scrollTo and stores positions with sessionStorage.setItem, adding +1 to avoid zero-value issues. This provides a functional, if basic, fix for scroll retention on the categories page.

As I see many pages on Discourse remember the scroll position.
But the categories page opens on top every time. And in case when it’s the main page and there are a lot of categories there - it provides not the best UX.

F.e. I saw FEATURE: Remember scroll position in private message lists by markvanlan · Pull Request #8212 · discourse/discourse · GitHub commit by @markvanlan
Do you know - what is the shortest way to do the same for the categories page?

As I understand from this old topic -

dev team prefers to keep the Categories page to open on top, not like other pages.

So probably it’s better to find a way to implement Categories scroll fix as a separate theme component. What is the best approach in your opinion?

Or maybe someone has already done it?

Thanks!

I believe this could be done with a theme component. I’m not sure how technical you are but I can give a basic outline of how this could be accomplished.

Using the onPageChange API, you could check to see if the current page is the categories page. If so, check localStorage for the user’s last scroll position on the page, and scroll to that position if it exists. At that point add an event listener to the window to store the user’s scroll position in localStorage. In the onPageChange, if the user is not on the categories page, remove the event listener.

Thank you @markvanlan - I will try.
I’m a programmer but less experienced in frontend.

Please keep this topic open for some time - maybe there is someone who already did this.

A quick and dirty solution for now - maybe it may help someone later.

<script type="text/discourse-plugin" version="0.8">

api.onPageChange((url) => {
    window.removeEventListener("scroll", saveScroll, {passive: true,});
    
    if (url === "/" || url === "/categories"){
        restoreScroll();
        window.addEventListener("scroll", saveScroll, {passive: true,});
    }
});


function restoreScroll() {
    var pos = sessionStorage.getItem("categoriesScrollPosition");
    if (pos != 0) {
        window.scrollTo(0, pos);
    } 
}

function saveScroll() {
    if (window.scrollY != 0) {
        sessionStorage.setItem("categoriesScrollPosition", window.scrollY + 1);
    }
}
</script>