How do I optimize LiveView when using pagination?

LiveView tracks things by DOM ID. So as long you make the container with phx-update=“prepend” have a different DOM ID whenever the page changes, for example by append the current page to the DOM ID, then everything will work:

  1. whenever the page changes, you start with a fresh container
  2. you can still prepend to the container

However, I think that pagination is most likely a poor fit for something that also receives updates live. If you are on page 2 and there is new content, what do you do?

Maybe a better fit is this:

  1. you have two containers (one for prepending and another for appending)
  2. LV updates go to the prepend container
  3. You have a button at the bottom that says “load more”. whenever you click “load more”, you append new entries to the append container

Of coruse, there are UI/UX considerations here that are outside of using LV or not, but whatever approach you choose should be doable!

5 Likes