| 1 | --- |
| 2 | title: "hx-optimistic" |
| 3 | description: "Apply optimistic UI updates before the server responds" |
| 4 | category: "UX" |
| 5 | icon: "icon-[mdi--lightning-bolt-outline]" |
| 6 | keywords: ["optimistic", "ui", "instant", "updates"] |
| 7 | --- |
| 8 | |
| 9 | The `hx-optimistic` extension shows a preview of the expected result immediately when a request is made, before the server responds. When the response arrives (or on error), the optimistic content is removed and replaced with the real content. |
| 10 | |
| 11 | ## Installing |
| 12 | |
| 13 | ```html |
| 14 | <script src="https://cdn.jsdelivr.net/npm/htmx.org@__VERSION__/dist/htmx.min.js"></script> |
| 15 | <script src="https://cdn.jsdelivr.net/npm/htmx.org@__VERSION__/dist/ext/hx-optimistic.js"></script> |
| 16 | ``` |
| 17 | |
| 18 | Add `hx-optimistic` to your extensions whitelist: |
| 19 | |
| 20 | ```html |
| 21 | <meta name="htmx-config" content='extensions:"hx-optimistic"'> |
| 22 | ``` |
| 23 | |
| 24 | ## Usage |
| 25 | |
| 26 | Add `hx-optimistic` to any element that makes a request, pointing to a template element: |
| 27 | |
| 28 | ```html |
| 29 | <ul id="messages"> |
| 30 | <li>Hello world</li> |
| 31 | </ul> |
| 32 | |
| 33 | <template id="msg-opt"> |
| 34 | <li>Sending...</li> |
| 35 | </template> |
| 36 | |
| 37 | <form hx-post="/message" hx-target="#messages" hx-swap="beforeend" hx-optimistic="#msg-opt"> |
| 38 | <input name="body" placeholder="Message..."> |
| 39 | <button type="submit">Send</button> |
| 40 | </form> |
| 41 | ``` |
| 42 | |
| 43 | When the form submits, the template content is immediately inserted into the target. When the server responds, the optimistic content is removed and the real response is swapped in. |
| 44 | |
| 45 | ## Request Parameters as Data Attributes |
| 46 | |
| 47 | The extension captures all string request parameters (form inputs, `hx-vals`, `hx-include`) and sets them as `data-*` attributes on the optimistic element. This makes the submitted values available to CSS and to the [hx-live](/extensions/hx-live) extension. |
| 48 | |
| 49 | For a form with `<input name="author" value="You">`, the optimistic div gets `data-author="You"`. |
| 50 | |
| 51 | Multi-value fields (checkboxes, multi-selects) are stored as JSON arrays: `data-tags='["js","css"]'`. |
| 52 | |
| 53 | ## Dynamic Templates with hx-live |
| 54 | |
| 55 | When used with the `hx-live` extension, optimistic templates can display the submitted values using `:text` bindings and the `data` proxy: |
| 56 | |
| 57 | ```html |
| 58 | <template id="msg-opt"> |
| 59 | <li><strong :text="data.author"></strong>: <span :text="data.body"></span></li> |
| 60 | </template> |
| 61 | |
| 62 | <form hx-post="/message" hx-target="#messages" hx-swap="beforeend" hx-optimistic="#msg-opt"> |
| 63 | <input name="author" value="You"> |
| 64 | <input name="body" placeholder="Message..."> |
| 65 | <button type="submit">Send</button> |
| 66 | </form> |
| 67 | ``` |
| 68 | |
| 69 | The `data` proxy reads `data-*` attributes from the nearest ancestor, and hx-live's `:text` binding sets `textContent` safely (no XSS risk). Full JavaScript expressions are supported: |
| 70 | |
| 71 | ```html |
| 72 | <template id="order-opt"> |
| 73 | <div> |
| 74 | <span :text="data.item"></span> |
| 75 | <span :text="'$' + (data.price * data.qty).toFixed(2)"></span> |
| 76 | <span :text="Array.isArray(data.tags) ? data.tags.join(', ') : data.tags"></span> |
| 77 | </div> |
| 78 | </template> |
| 79 | ``` |
| 80 | |
| 81 | ## Styling |
| 82 | |
| 83 | The optimistic element receives a `hx-optimistic` class, which you can style with CSS: |
| 84 | |
| 85 | ```css |
| 86 | .hx-optimistic { |
| 87 | opacity: 0.6; |
| 88 | font-style: italic; |
| 89 | } |
| 90 | |
| 91 | .hx-optimistic::after { |
| 92 | content: " (sending...)"; |
| 93 | font-size: 0.8em; |
| 94 | color: #888; |
| 95 | } |
| 96 | ``` |
| 97 | |
| 98 | You can also style based on the request parameters: |
| 99 | |
| 100 | ```css |
| 101 | .hx-optimistic[data-priority="high"] { |
| 102 | border-left: 3px solid red; |
| 103 | } |
| 104 | ``` |
| 105 | |
| 106 | ## How It Works |
| 107 | |
| 108 | 1. On `htmx:config:request` — captures the raw `FormData` before htmx transforms it |
| 109 | 2. On `htmx:before:request` — clones the template, sets `data-*` for each param, inserts it into the target (respecting swap style), and calls `htmx.process()` so hx-live bindings activate |
| 110 | 3. On `htmx:before:swap` or `htmx:error` — removes the optimistic content and unhides any hidden elements |
| 111 | |
| 112 | ## Swap Style Behavior |
| 113 | |
| 114 | The extension respects the `hx-swap` value: |
| 115 | |
| 116 | | Swap Style | Behavior | |
| 117 | |---|---| |
| 118 | | `innerHTML` | Hides target's children, appends optimistic content | |
| 119 | | `beforebegin`, `afterbegin`, `beforeend`, `afterend` | Inserts optimistic content at that position | |
| 120 | | `outerHTML` / other | Hides target, inserts optimistic content after it | |
| 121 | |
| 122 | ## Without hx-live |
| 123 | |
| 124 | The extension works without hx-live — static template content displays as-is, and the `data-*` attributes are still available for CSS selectors. You only need hx-live if you want dynamic `:text` / `:class` / `:style` bindings in the template. |