Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jasko's avatar
Level 3

Resetting Infinite Scroll Page upon return back on the page

Basically I have a page List/Show.vue that contains books that are in reading list of current user. I am utilizing pagination functionality in Laravel to send 10 books per page, and then they are rendered in frontend Vue component by scrolling. It all works fine regarding the infinite scroll functionality, the issue I am having is when I click on one of those books and the link takes me to that page, upon clicking return button in the browser and returning to the Show.vue page, only the last page of infinite scroll pagination is shown. What I want to achieve is when a user returns back to the page, he sees the page in its initial state before any scrolling (and book loading) occurred. I don't know how to do that, so I could really use your help.

This is useInfiniteScroll.js composable:

This is Show.vue:

0 likes
2 replies
LaryAI's avatar
Level 58

To reset the infinite scroll page to its initial state when the user navigates back to the page, you can leverage the beforeRouteEnter and beforeRouteLeave navigation guards provided by Vue Router. These guards allow you to execute code before entering or leaving a route, which is perfect for resetting the state of your component.

Here's how you can modify your Show.vue component to achieve this:

  1. Store the initial state: Save the initial state of your component when it is first loaded.
  2. Reset the state on route enter: Reset the component's state to the initial state when the user navigates back to the page.

Here's the updated Show.vue component:

Explanation:

  1. Initial State: The initial state of the books and the next page URL is stored in initialBooks and initialNextPageUrl respectively.
  2. Router Guard: The beforeEach navigation guard checks if the user is navigating back to the Show page from the BookDetail page. If so, it resets the items and nextPageUrl to their initial values.
  3. Template: The template remains mostly unchanged, but make sure to add a key to the v-for loop to help Vue efficiently update the DOM.

This approach ensures that when the user navigates back to the Show.vue page, the infinite scroll is reset to its initial state.

jasko's avatar
Level 3

@LaryAI I am using Inertia router, so I can't use methods from Vue router

1 like

Please or to participate in this conversation.