1

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?

Mike Mintz
  • 273
  • 1
  • 11
  • Regarding handling back/forward actions, does this help: [Want to have an event handler for the browser's back button with next.js](https://stackoverflow.com/questions/61932918/want-to-have-an-event-handler-for-the-browsers-back-button-with-next-js)? – juliomalves May 16 '22 at 22:44

2 Answers2

1

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
    }
}, []);
Mike Mintz
  • 273
  • 1
  • 11
0

Never done this but my instinct would be:

That kind of goes around the framework, but it doesn't look like Next exposes what you're looking for.

serraosays
  • 7,163
  • 3
  • 35
  • 60