Infinite Scroll — HTMX

Infinite scroll in htmx: an element at the end of the list with hx-get="/history?page=2" and hx-trigger="revealed", which fires once when it's seen. Use…

Here's what you'll build: Mrs. Rini's order history that grows by itself as you scroll. The first five orders come with the page. As soon as you reach the last row, the next five appear under it, with no "Load more" button to find and click.

Infinite scroll loads the next page by itself when the user scrolls to the bottom. In htmx, it's one element at the end of the list with hx-trigger="revealed": as soon as that element is seen, it asks for the next page. Mrs. Rini has a paper version of this. She copies her order history book for a customer, five lines per sheet. On the last line of the sheet you're reading, she slips in a bookmark. As soon as your eyes reach that bookmark, the next sheet is fetched and placed under it, with a new bookmark at its end. The last sheet comes with no bookmark: done.

The last row triggers it

<ul id="history">
  <li id="order-11">#11 Mrs. Sari, 2 kg: <span>Picked up</span></li>
  <li id="order-10">#10 Mr. Joko, 4 kg: <span>Picked up</span></li>
  <li id="order-9">#9 Ms. Tia, 3 kg: <span>Picked up</span></li>
  <li id="order-8">#8 Mrs. Ani, 6 kg: <span>Picked up</span></li>
  <li id="order-7" hx-get="/history?page=2" hx-trigger="revealed" hx-swap="afterend">
    #7 Mr. Dodi, 2 kg: <span>Picked up</span>
  </li>
</ul>

The server answers GET /history?page=2 with the next five rows, and the last one carries the trigger for page 3:

<li id="order-6">#6 Mrs. Wati, 5 kg: <span>Picked up</span></li>
...
<li id="order-2" hx-get="/history?page=3" hx-trigger="revealed" hx-swap="afterend">#2 Mr. Ujang, 3 kg: <span>Picked up</span></li>

Stopping at the last page

You scroll down to Request Rows in the list
(the page opens) none yet, page 1 comes with the page 5
#7 GET /history?page=2 10
#2 GET /history?page=3 11
#1 none, #1 has no hx-get 11

Page 3 holds only #1, and the server gives it no trigger. There are no triggers left, so the requests stop: 2 requests in total. In Laravel, the trigger is written only while there are pages left: @if ($orders->hasMorePages()), with the address from $orders->nextPageUrl().

The sentinel pattern: a "Loading..." element that replaces itself

<ul id="history">
  ...five rows...
  <li hx-get="/history?page=2" hx-trigger="revealed" hx-swap="outerHTML">Loading...</li>
</ul>

revealed or intersect

⚠️ Lists that nest, or never stop loading

TL;DR: Infinite scroll in htmx: an element at the end of the list with hx-get="/history?page=2" and hx-trigger="revealed", which fires once when it's seen. Use hx-swap="afterend" on the last row (the reply goes after it), or a "Loading..." sentinel with hx-swap="outerHTML" (the sentinel is replaced). Every reply carries a new trigger for the next page, except the last page: that's where the requests stop. Without hx-swap, the new rows go inside the trigger. Inside a scrolling box, revealed doesn't work: use intersect once.

⚡ The last row triggers it

Write:

<li id="order-7" hx-get="/history?page=2" hx-trigger="revealed" hx-swap="afterend">
  #7 Mr. Dodi, 2 kg: <span>Picked up</span>
</li>

Meaning: revealed sends the request once, when this element first comes into view by scrolling. afterend puts the reply after row #7, still inside the same <ul>.

⚡ Stopping at the last page

Guess first: the history has 3 pages. How many requests are sent by the time you scroll down to #1?

Two. Page 1 comes with the page, then pages 2 and 3 take one request each. Page 3 comes with no trigger, so the requests stop.

Meaning: Every reply carries a new trigger for the next page, except the last page.

⚡ A sentinel that replaces itself

Write:

<li hx-get="/history?page=2" hx-trigger="revealed" hx-swap="outerHTML">Loading...</li>

Meaning: outerHTML replaces the sentinel with the new rows plus the next sentinel. Without hx-swap, the new rows go inside the trigger. Inside a scrolling box, revealed doesn't work: use intersect once.

What you will learn