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>
hx-trigger="revealed": the request goes once, when this element first comes into view by scrolling. htmx then marks it withdata-hx-revealed="true"so it doesn't fire again.hx-get="/history?page=2": the address of the next page, its number in the query.hx-swap="afterend": the reply goes after row #7, still inside the same<ul>. Row #7 itself stays. With nohx-target, the target is that row itself.
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>
- A sentinel is a marker element at the end of the list.
hx-swap="outerHTML"replaces that sentinel itself with five new rows plus a new sentinel for the next page. The last page comes with no sentinel. - In a table, the sentinel is a
<tr>inside<tbody>:<tr hx-get="/api/rows?page=2" hx-trigger="revealed" hx-swap="outerHTML" hx-target="this"><td>Loading more...</td></tr>.hx-target="this"makes it explicit that the row itself is replaced. - The difference:
afterendkeeps the trigger row,outerHTMLdrops the sentinel. Both stop when the server sends no new trigger.
revealed or intersect
revealedchecks the position against the browser window: htmx listens for scrolling onwindow, then checks every 200 ms. Inside a box withoverflow-y: auto, it's the box that scrolls, not the window. Sorevealednever fires, or fires too early.- For a list inside a box, use
hx-trigger="intersect once".intersectuses an IntersectionObserver, which takes the box into account.oncehas to be written, becauseintersectfires every time the element comes into view.
⚠️ Lists that nest, or never stop loading
- Forgetting
hx-swap. The default isinnerHTML: the new rows go inside the trigger row, so you get an<li>inside an<li>. - The last page still carries a trigger. The server is still asked for page 4, which is empty. Write the trigger only while there's a next page.
revealedinside a scrolling box. It doesn't work. Useintersect once.- A first page that's too short. The trigger is visible as soon as the page opens, so pages 2, 3 and on load one after another without any scrolling. Send enough rows to fill the screen.
- In the exercise: the sentinel
<div hx-get="/api/feed?page=2">inside#feedneedshx-trigger="revealed"andhx-swap="outerHTML". The table row<tr hx-get="/api/rows?page=2">needshx-trigger="revealed",hx-swap="outerHTML"andhx-target="this". The example replies have a last page with no sentinel, so once htmx has finished loading, every item is on the page and nohx-getis left behind.
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.