avergin

avergin

How to recover page state in "load more" pagination or infinite scrolling

Hi,

I have “load more” type of pagination in my LiveView app that I implement through phx-update="append". However, when there is a reconnection, the page state is lost – meaning only the initially fetched items are rendered and all the user loaded items are lost.

I have thought of 2 solutions. I was curious if there is a better solution because both these solutions have certain limitations.

Solution 1: Store page count in a hidden form or a data attribute. When there is a reconnection, fetch all items in a single query. A big downside of this approach is that it may increase the DB load depending on the page count.

Solution 2: Implementing infinite scroll pagination (instead of load more) similar to “Discourse” (e.g. ElixirForum), by keeping the page/section state in the URL. However, I think there may always be some parts of the app where a “load more” type of pagination is needed (where you don’t store the page state in the URL).

How would you tackle this problem? (This question also applies to infinite scrolling)

Also related to this, is there a way to efficiently render the previously fetched items when the user uses the browser’s back functionality to go back to the page with “load more” pagination, without reloading all the items from the previous session? This may be within the domain of javascript, but I would love to learn how a javascript solution would play well with a robust live view solution.

Thanks!

Marked As Solved

chrismccord

chrismccord

Creator of Phoenix

You could use connect params to send the state of the page back up, but a simpler solution which also closes the UX gap for infinite scroll is to use push_patch when loading more results, which updates the URL and gives you the recovery mechanism “for free”, but you’ll also need to handle loading previous results in the other direction if you want to restore the current page.

Also Liked

kiere

kiere

I use a “Load More” type pagination with LiveView. I guess a question I would ask you to help put this in context is “if a user wants to send a link to that view to someone else, how would you do it without including the ‘state’ in the URL”? That’s the question I had to ask myself as I was building this. In our app’s industry, most of our competitors have apps that don’t update the URL when things change and it’s frustrating the customer base. I found that by ensuring that every state in our app is accessible by a unique URL, we can satisfy a LOT of prospect complaints in sales calls.

Unfortunately, I have found that the “load more”/infinite scrolling approach is not ideally suited to this due to the amount of data you could potentially need to load (it would have been better to use the more traditional page number style). However, in our real-world use, and because we have included multiple filters and a search box that helps drill down to just the data needed, we aren’t seeing people load a lot of data to find what they need so this is not a problem for us.

I know this is much later than your original post, but I hope it helps a little. :slight_smile:

chrismccord

chrismccord

Creator of Phoenix

connect params is the way

Last Post!

avergin

avergin

Ok, now I see what you mean by connect params:

let liveSocket = new LiveSocket("/live", Socket, {
  uploaders: Uploaders,
  hooks: Hooks, 
  params: {
    // connect params
    _csrf_token: csrfToken
  }
})

Thanks for pointing out the way to tackle this.

Where Next?

Popular in Questions Top

minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New

Other popular topics Top

minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 31494 112
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 40042 209
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New

We're in Beta

About us Mission Statement