HeadlinesBriefing favicon HeadlinesBriefing.com

htmx 4.0 Released: Key Changes & Migration

Hacker News •
×

The htmx team has released version 4.0.0 after 8 months of work. The idea originated from creating fixi, which led to deeper familiarity with the fetch() API and async programming. JavaScript previously relied on XMLHttpRequest due to backwards compatibility issues. A chance contact from Christian sparked interest in streaming HTML, prompting a shift to fetch() internally. With help from Michael and Alex, the team ported fixi and the htmx test suite, rediscovering why htmx did many things as it did, and aligning the new implementation closely with the old.

Behavioral differences between 2.x and 4.x are relatively small, with deliberate choices made to support 100-year web services. Notably, 4.0 is not marked as latest on NPM to avoid forcing upgrades for users relying on non-versioned CDN URLs. Instead, 2.x remains latest, while 4.0 stays as next until early 2027. The website will reference 4.0.

Three major changes stand out. First, attribute inheritance is now explicit: in htmx 2, attributes like hx-confirm were inherited by default; in 4.0, you must add :inherited (e.g., hx-confirm:inherited). This is the largest migration burden, but a command-line tool helps identify needed changes, and hx-disinherit is no longer required. Second, event names are standardized to follow htmx:phase:action[:sub-action] (e.g., htmx:before:request instead of htmx:beforeRequest). Third, history support no longer uses localStorage by default, which most users won't notice. Internally, the migration from XMLHttpRequest to fetch() is transparent to users.

Additional changes: most error events collapse into htmx:error, HTTP errors fire htmx:response:error, htmx:xhr:* events are removed, and htmx:validation:* events are replaced by native browser validation. The upgrade checker flags old event names in hx-on attributes and JavaScript. Overall, htmx 4.0 maintains compatibility while modernizing internals and clarifying behavior.