| 1 | --- |
| 2 | title: "The fetch()ening" |
| 3 | description: "You know, technically, I never said anything about a version four" |
| 4 | created: 2025-11-01 |
| 5 | authors: ["Carson Gross"] |
| 6 | tags: [] |
| 7 | --- |
| 8 | |
| 9 |  |
| 10 | |
| 11 | OK, I said there would never be a version three of htmx. |
| 12 | |
| 13 | But, _technically_, I never said anything about a version *four*... |
| 14 | |
| 15 | ## htmx 4: The fetch()ening |
| 16 | |
| 17 | In [The Future of htmx](/essays/hypermedia-driven-applications) I said the following: |
| 18 | |
| 19 | > We are going to work to ensure that htmx is extremely stable in both API & implementation. This means accepting and |
| 20 | documenting the [quirks](https://htmx.org/quirks/) of the current implementation. |
| 21 | |
| 22 | Earlier this year, on a whim, I created [fixi.js](https://github.com/bigskysoftware/fixi), a hyperminimalist implementation |
| 23 | of the ideas in htmx. That work gave me a chance to get a lot more familiar with the `fetch()` and, especially, the |
| 24 | [async](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function) infrastructure |
| 25 | available in JavaScript. |
| 26 | |
| 27 | In doing that work I began to wonder if that, while the htmx [API](https://htmx.org/reference/#attributes) |
| 28 | is (at least reasonably) correct, maybe there was room for a more dramatic change of the implementation that took |
| 29 | advantage of these features in order to simplify the library. |
| 30 | |
| 31 | Further, changing from ye olde [`XMLHttpRequest`](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest) |
| 32 | (a legacy of htmx 1.0 IE support) to [`fetch()`](https://developer.mozilla.org/en-US/docs/Web/API/Window/fetch) would |
| 33 | be a pretty violent change, guaranteed to break at least *some* stuff. |
| 34 | |
| 35 | So I began thinking: if we are going to consider moving to fetch, then maybe we should _also_ use this update as a |
| 36 | chance address at least _some_ of the [quirks & cruft](https://htmx.org/quirks/) that htmx has acquired over its lifetime. |
| 37 | |
| 38 | So, eventually & reluctantly, I have changed my mind: there _will_ be another major version of htmx. |
| 39 | |
| 40 | However, in order to keep my word that there will not be a htmx 3.0, the next release will instead be htmx 4.0. |
| 41 | |
| 42 | ## Project Goals |
| 43 | |
| 44 | With htmx 4.0 we are rebuilding the internals of htmx, based on the lessons learned from |
| 45 | fixi.js and [five+ years](https://www.npmjs.com/package/htmx.org/v/0.0.1) of supporting htmx. |
| 46 | |
| 47 | There are three major simplifying changes: |
| 48 | |
| 49 | ### The fetch()ening |
| 50 | |
| 51 | The biggest internal change is that `fetch()` will replace `XMLHttpRequest` as the core ajax infrastructure. This |
| 52 | won't actually have a huge effect on most usages of htmx _except_ that the events model will necessarily change due |
| 53 | to the differences between `fetch()` and `XMLHttpRequest`. |
| 54 | |
| 55 | ### Explicit Inheritance By Default |
| 56 | |
| 57 | I feel that the biggest mistake in htmx 1.0 & 2.0 was making attribute inheritance implicit. I was inspired by CSS in |
| 58 | doing this, and the results have been roughly the same as CSS: powerful & maddening. |
| 59 | |
| 60 | In htmx 4.0, attribute inheritance will be explicit by default rather than implicit. Explicit inheritance will |
| 61 | be done via the `:inherited` modifier: |
| 62 | |
| 63 | ```html |
| 64 | <div hx-target:inherited="#output"> |
| 65 | <button hx-post="/up">Like</button> |
| 66 | <button hx-post="/down">Dislike</button> |
| 67 | </div> |
| 68 | <output id="output">Pick a button...</output> |
| 69 | ``` |
| 70 | |
| 71 | Here the `hx-target` attribute is explicitly declared as `inherited` on the enclosing `div` and, if it wasn't, the |
| 72 | `button` elements would not inherit the target from it. |
| 73 | |
| 74 | You will be able to revert to htmx 2.0 implicit inheritance behavior via a configuration variable. |
| 75 | |
| 76 | ### No Locally Cached History |
| 77 | |
| 78 | Another source of pain for both us and for htmx users is history support. htmx 2.0 stores history in local |
| 79 | cache to make navigation faster. Unfortunately, snapshotting the DOM is often brittle because of third-party |
| 80 | modifications, hidden state, etc. There is a terrible simplicity to the web 1.0 model of blowing everything away and |
| 81 | starting over. There are also security concerns storing history information in session storage. |
| 82 | |
| 83 | In htmx 2.0, we often end up recommending that people facing history-related issues simply disable the cache entirely, |
| 84 | and that usually fixes the problems. |
| 85 | |
| 86 | In htmx 4.0, history support will no longer snapshot the DOM and keep it locally. It will, rather, issue a network |
| 87 | request for the restored content. This is the behavior of 2.0 on a history cache-miss, and it works reliably with |
| 88 | little effort on behalf of htmx users. |
| 89 | |
| 90 | We will offer an extension that enables history caching like in htmx 2.0, but it will be opt-in, rather than the default. |
| 91 | |
| 92 | This tremendously simplifies the htmx codebase and should make the out-of-the-box behavior much more plug-and-play. |
| 93 | |
| 94 | ## What Stays The Same? |
| 95 | |
| 96 | Most things. |
| 97 | |
| 98 | The [core](https://dl.acm.org/doi/10.1145/3648188.3675127) functionality of htmx will remain the same, `hx-get`, `hx-post`, |
| 99 | `hx-target`, `hx-boost`, `hx-swap`, `hx-trigger`, etc. |
| 100 | |
| 101 | With a few configuration tweaks, most htmx 2.x based applications should work with htmx 4.x. |
| 102 | |
| 103 | These changes will make the long term maintenance & sustainability of the project much stronger. It will also take |
| 104 | pressure off the 2.0 releases, which can now focus on stability rather than contemplating new features. |
| 105 | |
| 106 | ## Upgrading |
| 107 | |
| 108 | htmx 2.0 users _will_ face an upgrade project when moving to 4.0 in a way that they did not have to in moving |
| 109 | from 1.0 to 2.0. |
| 110 | |
| 111 | I am sorry about that, and want to offer two things to address it: |
| 112 | |
| 113 | * htmx 2.0 (like htmx 1.0 & intercooler.js 1.0) will be supported _in perpetuity_, so there is absolutely _no_ pressure to |
| 114 | upgrade your application: if htmx 2.0 is satisfying your hypermedia needs, you can stick with it. |
| 115 | * We will roll htmx 4.0 out slowly, over a multi-year period. As with the htmx 1.0 -> 2.0 upgrade, there will be a long |
| 116 | period where htmx 2.x is `latest` and htmx 4.x is `next` |
| 117 | |
| 118 | ## New Features |
| 119 | |
| 120 | Beyond simplifying the implementation of htmx significantly, switching to fetch also gives us the opportunity to add |
| 121 | some nice new features to htmx |
| 122 | |
| 123 | ### Streaming Responses & SSE in Core |
| 124 | |
| 125 | By switching to `fetch()`, we can take advantage of its support for |
| 126 | [readable streams](https://developer.mozilla.org/en-US/docs/Web/API/Streams_API/Using_readable_streams), which |
| 127 | allow for a stream of content to be swapped into the DOM, rather than a single response. |
| 128 | |
| 129 | htmx 1.0 had Server Sent Event support integrated into the library. In htmx 2.0 we pulled this functionality out as an |
| 130 | extension. It turns out that SSE is just a specialized version of a streaming response, so in adding streaming |
| 131 | support, it's an almost-free free two-fer to add that back into core as well. |
| 132 | |
| 133 | This will make incremental response swapping much cleaner and well-supported in htmx. |
| 134 | |
| 135 | ### Morphing Swap in Core |
| 136 | |
| 137 | [Three years ago](https://github.com/bigskysoftware/idiomorph/commit/7760e89d9f198b43aa7d39cc4f940f606771f47b) I had |
| 138 | an idea for a DOM morphing algorithm that improved on the initial algorithm pioneered by [morphdom](https://github.com/patrick-steele-idem/morphdom). |
| 139 | |
| 140 | The idea was to use "id sets" to make smarter decisions regarding which nodes to preserve and which nodes to delete when |
| 141 | merging changes into the DOM, and I called this idea "idiomorph". Idiomorph has gone on to be adopted by many other |
| 142 | web project such as [Hotwire](https://hotwired.dev/). |
| 143 | |
| 144 | We strongly considered including it in htmx 2.0, but I decided not too because it worked well as an extension and |
| 145 | htmx 2.0 had already grown larger than I wanted. |
| 146 | |
| 147 | In 4.0, with the complexity savings we achieved by moving to `fetch()`, we can now comfortably fit a `morphInner` and |
| 148 | `morphOuter` swap into core, thanks to the excellent work of Michael West. |
| 149 | |
| 150 | ### Explicit <htmx-partial> Tag Support |
| 151 | |
| 152 | htmx has, since very early on, supported a concept of "Out-of-band" swaps: content that is removed from the main HTML |
| 153 | response and swapped into the DOM elsewhere. I have always been a bit ambivalent about them, because they move away |
| 154 | from [Locality of Behavior](https://htmx.org/essays/locality-of-behaviour/), but there is no doubt that they are useful |
| 155 | and often crucial for achieving certain UI patterns. |
| 156 | |
| 157 | Out-of-band swaps started off very simply: if you marked an element as `hx-swap-oob='true'`, htmx would swap the element |
| 158 | as the outer HTML of any existing element already in the DOM with that id. Easy-peasy. |
| 159 | |
| 160 | However, over time, people started asking for different functionality around Out-of-band swaps: prepending, appending, |
| 161 | etc. and the feature began acquiring some fairly baroque syntax to handle all these needs. |
| 162 | |
| 163 | We have come to the conclusion that the problem is that there are really _two_ use cases, both currently trying to be |
| 164 | filled by Out-of-band swaps: |
| 165 | |
| 166 | * A simple, id-based replacement |
| 167 | * A more elaborate swap of partial content |
| 168 | |
| 169 | Therefore, we are introducing the notion of `<htmx-partial>`s in htmx 4.0 |
| 170 | |
| 171 | A partial element is, under the covers, a template element and, thus, can contain any sort of content you like. It |
| 172 | specifies on itself all the standard htmx options regarding swapping, `hx-target` and `hx-swap` in particular, allowing |
| 173 | you full access to all the standard swapping behavior of htmx without using a specialized syntax. This tremendously |
| 174 | simplifies the mental model for these sorts of needs, and dovetails well with the streaming support we intend to offer. |
| 175 | |
| 176 | Out-of-band swaps will be retained in htmx 4.0, but will go back to their initial, simple focus of simply replacing |
| 177 | an existing element by id. |
| 178 | |
| 179 | ### Improved View Transitions Support |
| 180 | |
| 181 | htmx 2.0 has had [View Transition](https://developer.mozilla.org/en-US/docs/Web/API/View_Transition_API) support since |
| 182 | [April of 2023](https://github.com/bigskysoftware/htmx/blob/master/CHANGELOG.md#190---2023-04-11). In the interceding |
| 183 | two years, support for the feature has grown across browsers (c'mon, safari, you can do it) and we've gained experience |
| 184 | with the feature. |
| 185 | |
| 186 | One thing that has become apparent to us while using them is that, to use them in a stable manner, it is important |
| 187 | to establish a _queue_ of transitions, so each can complete before the other begins. If you don't do this, you can get |
| 188 | visually ugly transition cancellations. |
| 189 | |
| 190 | So, in htmx 4.0 we have added this queue which will ensure that all view transitions complete smoothly. |
| 191 | |
| 192 | CSS transitions will continue to work as before as well, although the swapping model is again made much simpler by the |
| 193 | async runtime. |
| 194 | |
| 195 | We may enable View Transitions by default, the jury is still out on that. |
| 196 | |
| 197 | ### Stabilized Event Ordering |
| 198 | |
| 199 | A wonderful thing about `fetch()` and the async support in general is that it is _much_ easier to guarantee a stable |
| 200 | order of events. By linearizing asynchronous code and allowing us to use standard language features like try/catch, |
| 201 | the event model of htmx should be much more predictable and comprehensible. |
| 202 | |
| 203 | We are going to adopt a new standard for event naming to make things even clearer: |
| 204 | |
| 205 | `htmx:<phase>:<system>[:<optional-sub-action>]` |
| 206 | |
| 207 | So, for example, `htmx:before:request` will be triggered before a request is made. |
| 208 | |
| 209 | ### Improved Extension Support |
| 210 | |
| 211 | Another opportunity we have is to take advantage of the `async` behavior of `fetch()` for much better performance in our |
| 212 | preload extension (where we issue a speculative (`GET` only!) request in anticipation of an actual trigger). We have |
| 213 | also added an optimistic update extension to the core extensions, again made easy by the new async features. |
| 214 | |
| 215 | In general, we have opened up the internals of the htmx request/response/swap cycle much more fully to extension developers, |
| 216 | up to and including allowing them to replace the `fetch()` implementation used by htmx for a particular request. There |
| 217 | should not be a need for any hacks to get the behavior you want out of htmx now: the events and the open "context" object |
| 218 | should provide the ability to do almost anything. |
| 219 | |
| 220 | ### Improved `hx-on` Support |
| 221 | |
| 222 | In htmx 2.0, I somewhat reluctantly added the [`hx-on`](https://htmx.org/attributes/hx-on/) attributes to support light |
| 223 | scripting inline on elements. I added this because HTML does not allow you to listen for arbitrary events via `on` |
| 224 | attributes: only standard DOM events like `onclick` can be responded to. |
| 225 | |
| 226 | We hemmed and hawed about the syntax and so, unfortunately, there are a few different ways to do it. |
| 227 | |
| 228 | In htmx 4.0 we will adopt a single standard for the `hx-on` attributes: `hx-on:<event name>`. Additionally, we are |
| 229 | working to improve the htmx JavaScript API (especially around async operation support) and will make those features |
| 230 | available in `hx-on`: |
| 231 | |
| 232 | ```html |
| 233 | <button hx-post="/like" |
| 234 | hx-on:htmx:after:swap="await timeout('3s'); ctx.newContent[0].remove()"> |
| 235 | Get A Response Then Remove It 3 Seconds Later |
| 236 | </button> |
| 237 | ``` |
| 238 | |
| 239 | htmx will never support a fully featured scripting mechanism in core, we recommend something like |
| 240 | [Alpine.js](https://alpinejs.dev/) for that, but our hope is that we can provide a relatively minimalist API that |
| 241 | allows for easy, light async scripting of the DOM. |
| 242 | |
| 243 | I should note that htmx 4.0 will continue to work with `eval()` disabled, but you will need to forego a few features like |
| 244 | `hx-on` if you choose to do so. |
| 245 | |
| 246 | ### A Better But Familiar htmx |
| 247 | |
| 248 | All in all, our hope is that htmx 4.0 will feel an awful lot like 2.0, but with better features and, we hope, with fewer bugs. |
| 249 | |
| 250 | ## Timeline |
| 251 | |
| 252 | As always, software takes as long as it takes. |
| 253 | |
| 254 | However, our current planned timeline is: |
| 255 | |
| 256 | * An alpha release is available _today_: `[email protected]` |
| 257 | * A 4.0.0 release should be available in early-to-mid 2026 |
| 258 | * 4.0 will be marked `latest` in early-2027ish |
| 259 | |
| 260 | You can track our progress (and see quite a bit of dust flying around) in the `four` branch on |
| 261 | [github](https://github.com/bigskysoftware/htmx/tree/four) and at: |
| 262 | |
| 263 | <https://four.htmx.org> |
| 264 | |
| 265 | Thank you for your patience and pardon our dust! |
| 266 | |
| 267 | > "Well, when events change, I change my mind. What do you do?" --Paul Samuelson or John Maynard Keynes |