Building
htmx
The htmx version, its extensions, and the error handling this template commits to.
Versions
A project self-hosts four libraries in static/js. make js downloads the latest release of each, and htmx stays on its 2.x line. Pin a version with the variable beside it, for example HTMX_VERSION=2.0.11 make js. Production serves the committed copies and never downloads anything at build time.
| File | Library | Pin with |
|---|---|---|
| htmx.min.js | htmx | HTMX_VERSION |
| htmx-ext-preload.min.js | htmx preload extension | PRELOAD_VERSION |
| alpine-csp.min.js | Alpine.js (CSP build) | ALPINE_VERSION |
| hx-optimistic.min.js | hx-optimistic | HX_OPTIMISTIC_VERSION |
download-js.sh prints each file's SHA-384 hash. Compare it with the upstream release when you upgrade, and record it in the README.
Configuration
The layout sets htmx's configuration with a meta tag: selfRequestsOnly on, allowEval off, and includeIndicatorStyles off, so the strict CSP needs no inline styles. The body sets hx-ext="optimistic, preload" and the CSRF token in hx-headers.
Errors
htmx ignores 4xx and 5xx responses by default, so a failure would be silent. app.js changes that: a response with an HX-Reswap header is swapped as the server asked, and network failures or unlabelled errors show a generic toast. Check xhr.status before you reset a form or close a dialog on success.
Preloading and boosting
Add preload to a link or an hx-get element to fetch it on mousedown, or preload="mouseover" to fetch it after a short hover. A preload is a real request, sent with an HX-Preloaded: true header, so only use it for cheap GETs that change nothing. The extension is loaded on every page, but a new project doesn't use it anywhere yet. Good places are navigation, tabs and pagination.
<nav hx-boost="true" preload="mouseover">
<a href="/docs/cli">The project generator</a>
</nav>Optimistic updates
hx-optimistic makes the page change the moment someone clicks, before the server answers, and undoes the change if the request fails. A toggle such as a done checkbox or a like button feels instant even on a slow connection, while the server stays the source of truth: its response still replaces the target when it arrives. The extension is loaded on every page, and an element opts in with a data-optimistic attribute that holds a JSON config saying what to change on its hx-target.
For toggles, optimistic.OptimisticFlip builds that config. The target carries its state as a data attribute, and flipping that attribute is the whole optimistic update, so style the target from it with Tailwind's data-[done=true]: variants. If the request fails on the network, the extension rolls the change back. If the server answers with an error toast, app.js restores the old value instead, because htmx then treats the response as a swap rather than an error, and the extension's own rollback never runs. A native checkbox flips itself on click, so app.js also sets a checkbox inside the target back from its data-done attribute. Use done as the key for checkbox toggles, or extend that line in app.js.
<li id="task-1" data-done="false" class="data-[done=true]:line-through">
<input type="checkbox" hx-post="/tasks/1/toggle" hx-target="#task-1" hx-swap="outerHTML"
data-optimistic={ optimistic.OptimisticFlip("done", false) }/>
Write the docs
</li>Only use it where the server almost always agrees, and where a brief flicker back on failure is acceptable. Never use it for anything the user must not see succeed before it really has, such as a payment.
Was this page helpful?
Be the first to answer.