Turbo Frames - Scroll Position Restoration

Preserve and restore scroll position from sessionStorage.

View Solution on Patreon
Turbo Frames - Scroll Position Restoration

Premise

We are picking up Challenge 6 (Turbo Frames Pagination) again, to look at a known problem that spawned many different solutions: Scroll Position Restoration. While it seems that the issue is largely solved in Turbo Drive, there are cases where this can bite you when navigating with Turbo Frames.

Challenge

If you open https://stackblitz.com/edit/turbo-frames-scroll-position?file=app.js,index.html,page2.html,page1.html and use the pagination at the bottom of the page a few times, then hit your browser’s back button, you’ll notice that you’re taken to the top of the page.

This can be a disrupting user experience, for example when you’re quickly browsing the items in a storefront and expect to be taken back to the place you left off when hitting “Back”:

An Animation Showing a Reset of the Scroll Position after Navigation

Implement a solution that restores the scroll position prior to restoration. There are a couple available on the web, I encourage you to try and see which fits best!

Teaser

In my experience, almost every app has a spot where this can be improved. Does yours?

More from

Faceted Search with Stimulus and Turbo Frames
10 December 2024

Harness the power of Stimulus and Turbo Frames to drive a simple but powerful faceted search solution

Turbo Frames - Markdown Preview
08 October 2024

Typeahead previews of markdown by simple Turbo Frame rerendering

Turbo Frames - Render Flash Messages Upon Form Submission
27 August 2024

Intercept form submission responses to render flash messages that are outside the originating Turbo Frame

Cookies
essential