Lorixen.
EN

Back to the list: restoring scroll position in web apps

Short answer: A shopper scrolls halfway down a long product list, opens an item, taps Back, and lands at the top of the list again. In a single-page app or a page that loads more items with JavaScript, that happens because the browser cannot restore a position in content that does not exist yet. Fix it by letting your router save and restore scroll positions per history entry, rendering the list before restoring, keeping loaded pages and filters in the URL or in session storage, and leaving the browser's own restoration alone on ordinary pages.

Why the list jumps back to the top

On a traditional, server-rendered site, browsers are good at this. When you go back, they reload or reuse the previous page and put you where you were. MDN's documentation for history.scrollRestoration describes the default value, auto, as restoring the location to which the user had scrolled.

Problems appear when JavaScript builds the page:

  • Single-page apps swap views without real page loads, so the browser's restoration may run before the list has re-rendered, or not at all.
  • "Load more" and infinite scroll mean the item the user clicked was on page five of results, but after going back only page one is loaded, so there is nothing to scroll to.
  • Data fetched on arrival makes the list empty for a moment; the browser restores to a position that is past the end of the page, and the user ends up at the top or bottom.
  • Filters and sorting kept only in memory are lost, so the list itself is different.

For an online shop, a property listings site or a job board, this is one of the most irritating experiences on mobile. People give up on browsing because every product they look at costs them their place.

Step one: let the router manage it

Most modern routers have a built-in solution. React Router's ScrollRestoration component, for example, is documented as emulating the browser's scroll restoration on location changes. It stores scroll positions in sessionStorage under a configurable storageKey, and its getKey option decides how positions are remembered: by default per location key, meaning per history entry, or by pathname if you want any later visit to a path to restore its position. Vue Router, Angular's router and other frameworks offer similar scroll behaviour settings.

When you take control in JavaScript, the browser may also need to be told not to interfere. MDN explains that setting history.scrollRestoration to manual means the position is not restored automatically and the user would have to scroll manually; routers that manage scrolling themselves often set this for you.

Step two: restore after the list is ready

Restoring the number of pixels only works if the content above is the same height as before. So:

  • Cache the list data for the previous view, in memory or session storage, so going back renders immediately without waiting for the network.
  • Restore after render, once the items are in the page, not before.
  • Reserve space for images with width and height attributes or aspect-ratio, so the page does not grow after restoration and push the user down.

Step three: remember how much was loaded

With "load more" or infinite scroll, store how many pages of results were showing. When the user goes back, load the same number before restoring. A practical pattern is to keep the page count in the URL, for example ?page=5 or a cursor, so going back, refreshing or sharing the link all give the same list.

An alternative is to restore to the clicked item rather than a pixel offset: remember the product ID, render enough results to include it, then scroll it into view. That survives small layout changes better.

Step four: keep filters and sort in the state

If the user had filtered to "size 10, under $100, newest first", that must still be true when they return. Store filters in the URL query string or in history state, so the router restores the exact same results before restoring the scroll.

Do not fight the browser on ordinary pages

On server-rendered pages without client-side routing, the browser usually does the right job. Avoid scripts that scroll to the top on load, and be careful with layout shifts from late-loading banners, cookie notices or ads, which move content after the browser has restored your position.

Testing it properly

Test on real phones as well as desktop:

  1. Scroll deep into a long list, including after loading more.
  2. Open an item, then use the browser Back button, the phone's back gesture and your in-app back link.
  3. Check you return to the same item, with the same filters.
  4. Repeat on a slow connection setting.
  5. Refresh the list page and check it still makes sense.

A worked example

Picture a hypothetical Napier furniture retailer whose online catalogue used infinite scroll in a single-page app. Analytics showed shoppers opening a few products and then leaving, and session recordings showed them returning to the top of the list after every product.

The developers enabled their router's scroll restoration keyed per history entry, cached the loaded results for the previous view, stored the number of loaded pages and the active filters in the URL, and added image dimensions to stop layout shifts. Back navigation now returns shoppers to the product they opened, with the same filters, and browsing sessions became noticeably longer.

FAQ

Should I switch from infinite scroll to pagination?

Pagination makes restoration simpler and helps search engines, but either works if position and loaded pages are remembered.

Does the back/forward cache solve this?

It helps on multi-page sites by restoring pages from memory. Single-page apps still need their own handling.

What about scroll position inside a scrolling panel, not the whole page?

Save and restore that element's scroll offset yourself, since browsers and routers usually handle only the window.

If shoppers lose their place every time they go back to your product lists, send us a link to a listing page.

Sources

WhatsAppBook a call