htmx 2.x → htmx 4.x Migration Guide

The purpose of this guide is to provide instructions for migrations from htmx 2.x to htmx 4.x.

Note: htmx 4 is a significant architectural rewrite with many breaking changes. While we have tried to maintain backwards compatibility where possible, this upgrade will require more work than previous htmx versions.

Biggest Changes

The two most impactful changes in htmx 4 are switching to fetch() for issuing ajax requests and the removal of implicit inheritance - inheritance now requires the :inherited modifier

The inheritance change, in particular, will likely affect most htmx applications and should be addressed first.

New Features


Attribute Changes

Renamed Attributes

htmx 2.xhtmx 4.xNotes
hx-disabled-elthx-disableBefore upgrading, audit usage of hx-disable attribute (see note below)

Important Note on hx-disable:

In htmx 2, hx-disable disables htmx processing. In htmx 4, hx-ignore serves this purpose. Before upgrading:

  1. Search for any usage of hx-disable in your htmx 2 codebase
  2. Rename hx-disablehx-ignore
  3. Then rename hx-disabled-elthx-disable

Removed Attributes

The following attributes have been removed:

Removed Attributehtmx 4 Alternative
hx-varsUse hx-vals with js: prefix
hx-paramsUse htmx:config:request event to filter parameters
hx-promptUse hx-confirm with async JavaScript function
hx-extExtensions now work via event listeners
hx-disinheritNo longer needed (inheritance is explicit)
hx-inheritNo longer needed (inheritance is explicit)
hx-requestUse hx-config
hx-historyRemoved (history is no longer stored in local storage)
hx-history-eltRemoved (history uses target element)
hx-select-oobUse <htmx-action type="partial"> elements or standard OOB swaps

New Attributes

AttributePurpose
hx-actionSpecifies URL (use with hx-method)
hx-methodSpecifies HTTP method (use with hx-action)
hx-configConfigure request behavior with JSON
hx-optimisticShow optimistic content while request is in flight
hx-preloadPreload a request on a trigger event

Attribute Inheritance Changes

Inheritance is now explicit using the :inherited modifier.

Before (htmx 2):

<!-- Attributes inherited automatically -->
<div hx-confirm="Are you sure?">
    <button hx-delete="/item/1">Delete 1</button>
    <button hx-delete="/item/2">Delete 2</button>
</div>

After (htmx 4):

<!-- Must use :inherited modifier -->
<div hx-confirm:inherited="Are you sure?">
    <button hx-delete="/item/1">Delete 1</button>
    <button hx-delete="/item/2">Delete 2</button>
</div>

Any attribute can use the :inherited modifier: hx-boost:inherited, hx-headers:inherited, hx-target:inherited, etc.


Event Name Changes

htmx 4 uses a new event naming convention: htmx:phase:action

Complete Event Mapping

htmx 2.x Eventhtmx 4.x Event
htmx:afterOnLoadhtmx:after:init
htmx:afterProcessNodehtmx:after:init
htmx:afterRequesthtmx:after:request
htmx:afterSettlehtmx:after:swap
htmx:afterSwaphtmx:after:swap
htmx:beforeCleanupElementhtmx:before:cleanup
htmx:beforeHistorySavehtmx:before:history:update
htmx:beforeOnLoadhtmx:before:init
htmx:beforeProcessNodehtmx:before:init
htmx:beforeRequesthtmx:before:request
htmx:beforeSwaphtmx:before:swap
htmx:configRequesthtmx:config:request
htmx:historyCacheMisshtmx:before:restore:history
htmx:historyRestorehtmx:after:restore:history
htmx:loadhtmx:after:init
htmx:oobAfterSwaphtmx:after:oob:swap
htmx:oobBeforeSwaphtmx:before:oob:swap
htmx:pushedIntoHistoryhtmx:after:push:into:history
htmx:replacedInHistoryhtmx:after:replace:into:history
htmx:responseErrorhtmx:error
htmx:sendErrorhtmx:error
htmx:swapErrorhtmx:error
htmx:targetErrorhtmx:error
htmx:timeouthtmx:error

New Events in htmx 4

Event Detail Changes

TBD


Configuration Changes

Default Value Changes

Config Optionhtmx 2.x Defaulthtmx 4.x Default
viewTransitions (was globalViewTransitions)falsetrue

To restore htmx 2 behavior:

htmx.config.viewTransitions = false;

Removed Config Options

These config options have been removed from core:


Upgrade Music

This is the official htmx 2.x -> 4.x upgrade music: