AJAX pagination, update current page

When you refresh the page, there is currently no state you can use to know you’re on page 2.

You could use the History API to update the page URL without refreshing the page:

history.pushState(data, title, 'https://yourpage.com/?page=2');

You can also set some state on the user browser using the SessionStorage instead of the LocalStorage API, because the session will not persist when the user closes the browser tab:

// Store current page
sessionStorage.setItem('page', 2);

// Get current page on page load/refresh
var currentPage = sessionStorage.getItem('page');

This second method has its issues since the user can go to a different page and then get back to the paginated page and get the second page of results, instead of getting the first one, so you need to be careful about this approach.