HTMX niggles: Polling

Tags:

I'm doing more stuff with HTMX.

HTMX allows for almost-convenient polling by adding hx-trigger="every 1s" to an attribute. This allows you to kick off some processing on your server after having served a page and automagically update the page with the status and a download link when processing completes.

<div class="preview-card" id="preview-div"
    hx-trigger="every 1s"
    hx-get="/preview"
    hx-swap="outerHTML"
>
Please stand by while we prepare your content
</div>

The polling only works if the element has a hx-get or hx-post attribute. It does not work on <form> elements with action="..." , surprisingly.

The workaround is to add an explicit hx-get or hx-post="..." attribute to the element:

<form method="POST" action="/submit"
    hx-trigger="every 1s"
    hx-post="/preview"
    hx-swap="none"
>
<input name="message" type="text" />
...
</form>