htmx 4.0 Ships: Explicit Attribute Inheritance, fetch() Internals, Morph Swaps
After eight months of work, the htmx team has released version 4.0, a major update that nonetheless leaves most day-to-day usage untouched. The three changes that matter: attributes are no longer inherited by child elements automatically — developers must now opt in with an :inherited suffix — event names have been standardized to a htmx:phase:action scheme, and back-button history support no longer snapshots pages into localStorage. Under the hood, the library finally moves off XMLHttpRequest to the modern fetch() API, which the maintainers say is transparent to users but opened the door to a rethink of how extensions and streaming work.
The inheritance change is the biggest migration burden and carries a subtle security wrinkle. In htmx 2, a hx-headers attribute holding a CSRF token on a parent element would flow down to child elements making requests; in htmx 4 it won’t unless explicitly marked :inherited, and the server will reject the unauthenticated request. To ease this, htmx ships a upgrade-check CLI tool that scans templates and JavaScript for spots needing the new suffix, flags renamed and removed attributes (hx-disable becomes hx-ignore, hx-vars and hx-prompt are gone), and catches old event names. The history rework is also framed as a bug fix — re-fetching pages instead of restoring stale localStorage snapshots avoids the third-party-DOM-mutation issues that generated a lot of support tickets.
On the feature side, htmx 4 folds in morphing swaps (a refined idiomorph algorithm) and a new <hx-partial> tag for clearer out-of-band updates, plus a batch of fetch-based extensions covering preloading, downloads, SSE, WebSocket and multipart streaming, and Alpine.js compatibility. There’s even a jQuery/hyperscript-inspired scripting layer, hx-live. Notably, the team is deliberately not tagging 4.0 as latest on NPM to avoid force-upgrading anyone pinned to unversioned CDN URLs — 2.x stays latest while 4.0 rides the next channel until early 2027, reflecting htmx’s stated goal of supporting “100-year” web services.
Read the full article
Continue reading at Hacker News →This is an AI-generated summary. Read the original for the full story.