Htmx 4.0.0: Explicit Attribute Inheritance, Morph Swaps and the Move to fetch()

Translated by a model; the original is in Russian.

· Программы
The htmx team has shipped the fourth major version of the popular library. On the surface it looks almost identical to htmx 2, but under the hood it has been reworked — from events and attribute inheritance to the networking layer.

cover.svg

After eight months of work (and one game built along the way), the htmx team has unveiled version 4.0.0. The developers stress that, from a user's point of view, the new release is all but indistinguishable from htmx 2, and the few differences that do exist are designed so that applications keep working with a “hundred-year web service” in mind.

What's changed for developers

The main change: attribute inheritance is now explicit by default. In htmx 2 an attribute placed on a parent element automatically propagated to its descendants — a mechanism inherited all the way back from intercooler.js and inspired by CSS. It turned out to be as powerful as it was confusing. In htmx 4 you have to opt into inheritance by hand, adding the :inherited suffix to the attribute name. This is the most conspicuous item when migrating, so a console utility ships with the release to find the spots that need fixing.

The second novelty is a unified naming scheme for events, following the htmx:phase:action pattern. So htmx:beforeRequest has become htmx:before:request. Some events have been dropped: htmx:xhr:* is gone (because of the move to fetch()), as is htmx:validation:* (in favor of the browser's native form validation).

The third change concerns history. Previously htmx cached page snapshots in localStorage, which caused no end of trouble: when a page was restored, edits made by third-party scripts remained, but the logic behind them did not. Now, when you navigate back, htmx simply re-requests the page and drops it into the <body>. For anyone who needs local caching, there is a separate hx-history-cache extension built on sessionStorage.

Internals and new capabilities

The library has finally moved from XMLHttpRequest to fetch() — imperceptible for most users, but it was precisely this step that made it possible to rethink the extension system. Among the new features are built-in support for morph swaps based on the idiomorph algorithm and the <hx-partial> tag, a more intuitive alternative to out-of-band updates.

Separately, there is now a set of extensions for streaming HTML (via SSE, WebSockets and multipart/mixed), as well as its own small solution for frontend scripting, hx-live, inspired by Alpine.js, jQuery and hyperscript.

Upgrading is optional

Version 4.0 is deliberately not tagged as latest on npm: the latest tag stays with 2.x so as not to break projects that reference unversioned CDN URLs. The switch to 4.0 as the main line is planned for early 2027, and htmx 2 will be supported indefinitely.

Nor is the release without a sign of the times: the developers put out a set of skill files for large language models — from a general guide to migration help — noting with some irony that they would rather leave open the question of whether releasing a new version of a library in the age of LLMs is a good idea at all.


Source: Hacker News

Comments

Sign in, to comment.

  • No comments yet.