I am using the scrollRestoration feature of Next.js to restore the page position when a back button is pressed. The problem, is that it doesn't restore the horizontal scroll position of the carousels on that page. So I have created my own session variables to track the position of each carousel and restore them on page load. The problem, is that I ONLY want to restore these scroll positions if the back/forward button was pressed. If there is an indicator that scrollRestoration was used on the page I could use that, otherwise, if there is an indication that the page was visited using the forward/back buttons that would also solve it.
Any ideas?
Never done this but my instinct would be:
useEffect that updates on router.pathName changesThat kind of goes around the framework, but it doesn't look like Next exposes what you're looking for.
i have figured out how to determine if scrollRestoration is set for a page.
React.useEffect(() => {
const hasScrollHistory = sessionStorage.getItem(`__next_scroll_${window.history.state.idx}`);
if (hasScrollHistory) {
// do your restore logic here
}
}, []);