vincent15000's avatar

Return back to a page with Infinite Scroll

Hello,

With Laravel / InertiaJS / VueJS.

Step1 : I have a page with an Infinite Scroll to display a list of games. I can click on a game to display the game details on another page.

Step 2 : On this page with the game details, I have a back button. When I click on the back button, I go back to the page with the list of the games.

Step 3 : I'm back on the page with the list of the games.

In Step 3, I'd like to retrieve the same state (scroll position) than I was in step 1.

How can I do that ?

Thanks for your help.

V

0 likes
4 replies
LaryAI's avatar
Level 58

Great question! Preserving scroll position with Infinite Scroll when navigating away and back is an important UX consideration. Here is a well-structured solution for Laravel/Inertia/Vue:


1. Save Scroll Position Before Navigating

When the user clicks a game to view details, save the current scroll position and page state (including loaded items if possible).

In Your Infinite List Component

Add this before navigation (e.g., in your @click handler):

methods: {
  goToGame(gameId) {
    // Save scroll state to localStorage (or Pinia/Store/Reactively as needed)
    localStorage.setItem('games_scroll_position', window.scrollY)

    // Save items loaded so far (optional, but helps avoid re-fetching)
    localStorage.setItem('games_list_state', JSON.stringify(this.games))

    this.$inertia.visit(`/games/${gameId}`)
  }
}

2. Restore Scroll Position When Returning

When the games list component is mounted, restore the state.

mounted() {
  // Optional: Restore loaded games state
  const gamesState = localStorage.getItem('games_list_state')
  if (gamesState && !this.games.length) {
    this.games = JSON.parse(gamesState)
  }

  // Restore scroll position after DOM updates
  this.$nextTick(() => {
    const pos = +localStorage.getItem('games_scroll_position') || 0
    window.scrollTo({ top: pos, behavior: 'auto' })
  })
}

Tip: Clean up state after use (in beforeDestroy/unmounted) to avoid quirks if the user visits directly.


3. Option: Use Inertia preserveState

If pagination/scroll state is embedded in your Vue component's state, you could also benefit from [Inertia's preserveState] and preserveScroll options. For example:

this.$inertia.visit('/games', {
  // These help preserve scroll/component state automatically in some cases
  preserveState: true,
  preserveScroll: true,
})

But, with infinite scroll, manual scroll restoration like above is usually more reliable because the component is remounted and loses state unless actively preserved.


4. Handling Infinite Scroll Pagination

If your infinite scroll loads items from the API as the user scrolls, restoring only the scroll position is not enough—you must also restore the correct batch of items (games). That's why we save the loaded items array above. If your API supports querying by offset/page, use that info in the localStorage as well.


Summary

  • Save the scroll position and loaded items to localStorage (or a Vuex/Pinia store).
  • Restore them on mount of the games list component.
  • Clear the saved position/state after restoring to prevent using stale data.
  • Make sure your infinite scroll logic supports rehydrating the loaded items.

Example Minimal Implementation


Let me know if you have a Pinia/Vuex store or need a store-based example!

alfredscheer's avatar

Yes — this is exactly what Inertia’s built-in scroll/state preservation is for.

Use:

router.get(url, {}, { preserveScroll: true, preserveState: true, })

or with a Link:

For browser back navigation, Inertia usually restores scroll automatically if you use its history system correctly.

If your infinite scroll data is loaded client-side, also store the loaded items/page in:

useRemember() Pinia/Vuex local state cache

Example:

const games = useRemember([], 'games-list')

This preserves the list + scroll position when returning to the page.

1 like
vincent15000's avatar

I will try this.

But it seems to not be possible if the page is reloaded ... and I'm specifically in this situation.

I have a list of games with an infinite scroll. The user scrolls down, new games are added to the page, and then he can click on a game to see its full decription : a new page is loaded with the full description of the game.

Then he can click on a button to go back to the list : it's right now that I want that the user recovers the exact games list and the exact scroll position as before displaying a game description.

ghabriel25's avatar

@vincent15000 The real problem is the browser doesnt know what games has been loaded because infinite scroll behavior, it only knows what data you asked and then append it to the list.

This might be trickier than it seems but you could save the page into session whenever new items appended, when showing game description and go back, you could pull last page from the session (use default value if not exists) and use https://inertiajs.com/docs/v3/advanced/scroll-management

Only store it into session if user scrolling into new items.

1 like

Please or to participate in this conversation.