I'm in a Next JS 13 app router page with the following layout:
When clicking on a star icon, I'm updating some state in the DB and the containing card (green client cmp) is moved to the top of the list as a consequence. To update the list (brown server cmp), I'm calling router.refresh()
after performing the DB update.
Unfortunately, this call to router.refresh()
resets the scroll position of the list and I'm "moved" back to the top of the list which is rather unpleasant as a user.
Is there a way for me to actively prevent this change to the scroll position?
I just realized that this has nothing to do with server components & router refresh at all. This also happens in a vanilla react client component without anything Next specific and the reason is that I'm updating the order of key
s in my rendered list when moving the newly starred item to the top.
Can be fixed by simply applying CSS overflow-anchor: none to each list item.
I found the answer here, where you can find more details about why this is happening in the first place.