Frontend Patterns

Pattern

Scroll Restoration

Remember and restore scroll positions when navigating backward.

Scroll Restoration

Problem

Users who navigate back from a detail page return to the top of long lists, losing their place entirely. They must scroll through hundreds of items to find where they left off, making browsing catalog or feed-based interfaces frustrating and inefficient.

Solution

Restore scroll positions when users navigate back to previous pages. This preserves context and prevents users from losing their place in lists or articles.

Example

This demonstrates saving and restoring scroll positions when navigating between pages, preserving user context and preventing them from losing their place in long lists or articles.

// Save scroll position before navigation
sessionStorage.setItem('scrollPos', window.scrollY);

// Restore scroll position when returning to the page
useEffect(() => {
  const savedPos = sessionStorage.getItem('scrollPos');
  if (savedPos) {
    // Scroll to the saved position
    window.scrollTo(0, parseInt(savedPos));
  }
}, []);

Benefits

  • Preserves user context when navigating back to previous pages.
  • Improves browsing experience for catalog and feed-based interfaces.
  • Reduces frustration by eliminating need to scroll back to original position.
  • Makes back button behave more like users expect.

Tradeoffs

  • Can be confusing if content has changed since the user left the page.
  • Requires storing scroll positions for each route, adding memory overhead.
  • May conflict with frameworks that reset scroll on navigation.
  • Doesn’t work well with dynamically loaded content that changes page height.
Stay Updated

Get New Patterns
in Your Inbox

Join thousands of developers receiving weekly insights on frontend architecture patterns

No spam. Unsubscribe anytime.