| 1 | --- |
| 2 | name: htmx-upgrade-from-htmx2 |
| 3 | description: Use when helping a user upgrade or migrate their codebase from htmx 2.x to htmx 4.x. Covers attribute renames, event name changes, config updates, header changes, extension migration, and step-by-step upgrade workflow. |
| 4 | --- |
| 5 | |
| 6 | # Upgrading from htmx 2 to htmx 4 |
| 7 | |
| 8 | htmx 4 is a ground-up rewrite. This guide covers the practical steps to migrate a codebase. |
| 9 | |
| 10 | ## Instructions for Claude |
| 11 | |
| 12 | When helping with an htmx 2 → 4 upgrade: |
| 13 | |
| 14 | 1. Search the codebase for all htmx usage: `hx-` attributes, `data-hx-` attributes, `htmx:` event |
| 15 | listeners, `htmx.` API calls, and server-side `HX-` header handling |
| 16 | 2. Identify the scope: how many files, how complex the usage |
| 17 | 3. Work through the steps below in order, making changes file by file |
| 18 | 4. Pay special attention to the hx-disable/hx-ignore rename order (Step 1) |
| 19 | 5. For attribute inheritance (Step 3), use codebase analysis to find likely-inherited attributes |
| 20 | rather than blindly adding `:inherited` everywhere — see the detailed instructions in that step |
| 21 | 6. Check server-side code for header handling changes (often in middleware or base controllers) |
| 22 | 7. Check for custom extensions — these need a full rewrite |
| 23 | |
| 24 | ## Step 1: Attribute Renames |
| 25 | |
| 26 | Search and replace across the codebase. **Order matters for hx-disable.** |
| 27 | |
| 28 | ``` |
| 29 | # IMPORTANT: Do hx-disable FIRST (it means something different in htmx 2 vs 4) |
| 30 | # In htmx 2, hx-disable stops htmx processing. In htmx 4, hx-ignore does that. |
| 31 | hx-disable → hx-ignore (htmx 2's "disable htmx processing") |
| 32 | |
| 33 | # Now safe to rename hx-disabled-elt |
| 34 | hx-disabled-elt → hx-disable (htmx 2's "disable elements during request") |
| 35 | ``` |
| 36 | |
| 37 | ## Step 2: Remove Deleted Attributes |
| 38 | |
| 39 | | Find | Replace with | |
| 40 | |-----------------------|-------------------------------------------------------------| |
| 41 | | `hx-vars='...'` | `hx-vals='js:...'` (wrap value in `js:` prefix) | |
| 42 | | `hx-params="..."` | Remove; use `htmx:config:request` event to filter params | |
| 43 | | `hx-prompt="..."` | Load the `hx-prompt` extension (same syntax as htmx 2) | |
| 44 | | `hx-ext="..."` | Remove (just including the extension script is enough) | |
| 45 | | `hx-disinherit="..."` | Remove (inheritance is explicit by default) | |
| 46 | | `hx-inherit="..."` | Remove (use `:inherited` modifier on individual attributes) | |
| 47 | | `hx-request='...'` | `hx-config='...'` (same JSON format) | |
| 48 | | `hx-history="false"` | Remove (history no longer uses localStorage) | |
| 49 | |
| 50 | ## Step 3: Update Attribute Inheritance |
| 51 | |
| 52 | In htmx 2, all attributes inherited implicitly from parent elements. In htmx 4, inheritance must |
| 53 | be explicit using the `:inherited` modifier. |
| 54 | |
| 55 | ### How to find inherited attributes |
| 56 | |
| 57 | **Do not blindly add `:inherited` to everything.** Instead, analyze the codebase to find attributes |
| 58 | that are actually being inherited. Look for this pattern: a parent element has an htmx attribute, |
| 59 | and child/descendant elements rely on it without declaring it themselves. |
| 60 | |
| 61 | Common attributes that are frequently inherited: |
| 62 | |
| 63 | - **`hx-target`** — very common. Look for a container with `hx-target` and multiple child elements |
| 64 | with `hx-get`/`hx-post`/etc. that don't have their own `hx-target` |
| 65 | - **`hx-include`** — common in form-heavy UIs where a parent sets a shared include |
| 66 | - **`hx-swap`** — when a group of elements should all swap the same way |
| 67 | - **`hx-boost`** — typically set on a parent `<div>` or `<body>` to boost all links within |
| 68 | - **`hx-confirm`** — set on a container to confirm all actions within it |
| 69 | - **`hx-headers`** — set on a parent to attach auth tokens or CSRF headers to all requests within |
| 70 | - **`hx-indicator`** — set on a parent to share a loading indicator |
| 71 | - **`hx-sync`** — set on a parent to coordinate request timing for children |
| 72 | - **`hx-config`** — set on a parent to configure timeouts, etc. for children |
| 73 | - **`hx-encoding`** — set on a parent for multipart encoding across children |
| 74 | - **`hx-validate`** — set on a parent to enable validation for all children |
| 75 | |
| 76 | ### What to search for |
| 77 | |
| 78 | For each inheritable attribute, search for elements that have the attribute but **don't** have |
| 79 | their own `hx-get`, `hx-post`, `hx-put`, `hx-patch`, or `hx-delete`. These are likely |
| 80 | inheritance parents. Add `:inherited` to them: |
| 81 | |
| 82 | ```html |
| 83 | <!-- htmx 2 --> |
| 84 | <div hx-target="#output" hx-headers='{"X-Token":"abc"}'> |
| 85 | <button hx-get="/items">Load</button> |
| 86 | <button hx-delete="/item/1">Delete</button> |
| 87 | </div> |
| 88 | |
| 89 | <!-- htmx 4 --> |
| 90 | <div hx-target:inherited="#output" hx-headers:inherited='{"X-Token":"abc"}'> |
| 91 | <button hx-get="/items">Load</button> |
| 92 | <button hx-delete="/item/1">Delete</button> |
| 93 | </div> |
| 94 | ``` |
| 95 | |
| 96 | For `hx-boost`, it is almost always inherited — a `hx-boost="true"` on a non-link, non-form |
| 97 | element is always meant for its descendants. |
| 98 | |
| 99 | ## Step 4: Update `data-hx-*` Attributes |
| 100 | |
| 101 | In htmx 2, both `hx-get` and `data-hx-get` worked. In htmx 4, only `hx-*` works by default. |
| 102 | |
| 103 | Option A: Replace all `data-hx-` with `hx-`: |
| 104 | |
| 105 | ``` |
| 106 | data-hx-get → hx-get |
| 107 | data-hx-post → hx-post |
| 108 | data-hx-target → hx-target |
| 109 | (etc.) |
| 110 | ``` |
| 111 | |
| 112 | Option B: Set the prefix config: |
| 113 | |
| 114 | ```html |
| 115 | <meta name="htmx-config" content='{"prefix": "data-hx-"}'> |
| 116 | ``` |
| 117 | |
| 118 | Note: with a custom prefix, ALL attributes must use it. |
| 119 | |
| 120 | ## Step 5: Update Event Listeners |
| 121 | |
| 122 | htmx 2 uses camelCase event names. htmx 4 uses colon-separated names. |
| 123 | |
| 124 | **In JavaScript:** |
| 125 | |
| 126 | | Find | Replace | |
| 127 | |-----------------------------|-----------------------------------| |
| 128 | | `htmx:configRequest` | `htmx:config:request` | |
| 129 | | `htmx:beforeRequest` | `htmx:before:request` | |
| 130 | | `htmx:afterRequest` | `htmx:after:request` | |
| 131 | | `htmx:beforeSwap` | `htmx:before:swap` | |
| 132 | | `htmx:afterSwap` | `htmx:after:swap` | |
| 133 | | `htmx:afterSettle` | `htmx:after:swap` | |
| 134 | | `htmx:beforeSend` | `htmx:before:request` | |
| 135 | | `htmx:load` | `htmx:after:init` | |
| 136 | | `htmx:beforeOnLoad` | `htmx:before:init` | |
| 137 | | `htmx:afterOnLoad` | `htmx:after:init` | |
| 138 | | `htmx:beforeProcessNode` | `htmx:before:process` | |
| 139 | | `htmx:afterProcessNode` | `htmx:after:init` | |
| 140 | | `htmx:beforeCleanupElement` | `htmx:before:cleanup` | |
| 141 | | `htmx:beforeHistorySave` | `htmx:before:history:update` | |
| 142 | | `htmx:beforeHistoryUpdate` | `htmx:before:history:update` | |
| 143 | | `htmx:historyCacheMiss` | `htmx:before:history:restore` | |
| 144 | | `htmx:historyRestore` | `htmx:before:history:restore` | |
| 145 | | `htmx:pushedIntoHistory` | `htmx:after:history:push` | |
| 146 | | `htmx:replacedInHistory` | `htmx:after:history:replace` | |
| 147 | | `htmx:beforeTransition` | `htmx:before:viewTransition` | |
| 148 | | `htmx:oobBeforeSwap` | `htmx:before:swap` | |
| 149 | | `htmx:oobAfterSwap` | `htmx:after:swap` | |
| 150 | | `htmx:responseError` | `htmx:response:error` | |
| 151 | | `htmx:sendError` | `htmx:error` | |
| 152 | | `htmx:sendAbort` | `htmx:error` | |
| 153 | | `htmx:swapError` | `htmx:error` | |
| 154 | | `htmx:targetError` | `htmx:error` | |
| 155 | | `htmx:timeout` | `htmx:error` | |
| 156 | |
| 157 | **Removed events (no htmx 4 equivalent):** |
| 158 | |
| 159 | - `htmx:validation:validate`, `htmx:validation:failed`, `htmx:validation:halted` — use native form validation |
| 160 | - `htmx:xhr:loadstart`, `htmx:xhr:loadend`, `htmx:xhr:progress`, `htmx:xhr:abort` — XHR is gone |
| 161 | |
| 162 | **In `hx-on:` attributes:** |
| 163 | |
| 164 | ```html |
| 165 | <!-- htmx 2 --> |
| 166 | <div hx-on:htmx:afterSwap="console.log('done')"> |
| 167 | |
| 168 | <!-- htmx 4 --> |
| 169 | <div hx-on:htmx:after:swap="console.log('done')"> |
| 170 | ``` |
| 171 | |
| 172 | **In `hx-trigger` attributes referencing htmx events:** |
| 173 | |
| 174 | ```html |
| 175 | <!-- htmx 2 --> |
| 176 | <div hx-get="/data" hx-trigger="htmx:afterSwap from:body"> |
| 177 | |
| 178 | <!-- htmx 4 --> |
| 179 | <div hx-get="/data" hx-trigger="htmx:after:swap from:body"> |
| 180 | ``` |
| 181 | |
| 182 | ## Step 6: Update Event Handler Code |
| 183 | |
| 184 | The event detail structure has changed. Key differences: |
| 185 | |
| 186 | ```js |
| 187 | // htmx 2: event.detail contained XHR object |
| 188 | document.addEventListener('htmx:configRequest', (evt) => { |
| 189 | evt.detail.headers['X-Custom'] = 'value'; |
| 190 | evt.detail.parameters['key'] = 'value'; |
| 191 | evt.detail.path = '/modified-url'; |
| 192 | }); |
| 193 | |
| 194 | // htmx 4: event.detail.ctx contains request context |
| 195 | document.addEventListener('htmx:config:request', (evt) => { |
| 196 | evt.detail.ctx.request.headers['X-Custom'] = 'value'; |
| 197 | evt.detail.ctx.request.body.set('key', 'value'); // FormData |
| 198 | evt.detail.ctx.request.action = '/modified-url'; |
| 199 | }); |
| 200 | ``` |
| 201 | |
| 202 | ## Step 7: Update Configuration |
| 203 | |
| 204 | | htmx 2 config | htmx 4 config | |
| 205 | |--------------------------|-----------------------------------------| |
| 206 | | `defaultSwapStyle` | `defaultSwap` | |
| 207 | | `globalViewTransitions` | `transitions` | |
| 208 | | `historyEnabled` | `history` | |
| 209 | | `includeIndicatorStyles` | `includeIndicatorCSS` | |
| 210 | | `timeout` | `defaultTimeout` (new default: 60000ms) | |
| 211 | |
| 212 | Removed configs (no equivalent): `refreshOnHistoryMiss`, `historyCacheSize`, `defaultSwapDelay`, |
| 213 | `addedClass`, `settlingClass`, `swappingClass`, `allowEval`, `allowScriptTags`, `attributesToSettle`, |
| 214 | `useTemplateFragments`, `wsReconnectDelay`, `wsBinaryType`, `disableSelector`, `withCredentials`, |
| 215 | `scrollBehavior`, `getCacheBusterParam`, `methodsThatUseUrlParams`, `selfRequestsOnly`, `ignoreTitle`, |
| 216 | `scrollIntoViewOnBoost`, `triggerSpecsCache`, `allowNestedOobSwaps`, `responseHandling`. |
| 217 | |
| 218 | ## Step 8: Update Server-Side Header Handling |
| 219 | |
| 220 | **Request headers your server reads:** |
| 221 | |
| 222 | | htmx 2 header | htmx 4 header | Format change | |
| 223 | |-------------------|---------------|------------------------------------------------------| |
| 224 | | `HX-Trigger` | `HX-Source` | Was element ID → now `tag#id` (e.g. `button#submit`) | |
| 225 | | `HX-Trigger-Name` | Removed | Use `HX-Source` | |
| 226 | | `HX-Target` | `HX-Target` | Was element ID → now `tag#id` | |
| 227 | | `HX-Prompt` | Via extension | Load the `hx-prompt` extension to restore the header | |
| 228 | |
| 229 | New request header: `HX-Request-Type` (`"full"` or `"partial"`). |
| 230 | |
| 231 | **Response headers your server sends:** |
| 232 | |
| 233 | | htmx 2 header | htmx 4 status | |
| 234 | |---------------------------|---------------------------| |
| 235 | | `HX-Trigger-After-Swap` | Removed; use `HX-Trigger` | |
| 236 | | `HX-Trigger-After-Settle` | Removed; use `HX-Trigger` | |
| 237 | |
| 238 | Still supported: `HX-Trigger`, `HX-Push-Url`, `HX-Replace-Url`, `HX-Redirect`, `HX-Location`, |
| 239 | `HX-Refresh`, `HX-Retarget`, `HX-Reswap`, `HX-Reselect`. |
| 240 | |
| 241 | ## Step 9: Update JavaScript API Calls |
| 242 | |
| 243 | | htmx 2 | htmx 4 | |
| 244 | |------------------------------|------------------------------------| |
| 245 | | `htmx.defineExtension(...)` | `htmx.registerExtension(...)` | |
| 246 | | `htmx.addClass(elt, cls)` | `elt.classList.add(cls)` | |
| 247 | | `htmx.removeClass(elt, cls)` | `elt.classList.remove(cls)` | |
| 248 | | `htmx.toggleClass(elt, cls)` | `elt.classList.toggle(cls)` | |
| 249 | | `htmx.closest(elt, sel)` | `elt.closest(sel)` | |
| 250 | | `htmx.remove(elt)` | `elt.remove()` | |
| 251 | | `htmx.off(elt, evt, fn)` | `elt.removeEventListener(evt, fn)` | |
| 252 | | `htmx.values(elt)` | `new FormData(elt)` | |
| 253 | |
| 254 | ## Step 10: Update Extensions |
| 255 | |
| 256 | Extensions need a full rewrite for htmx 4. The API changed from callback-based to event-based: |
| 257 | |
| 258 | ```js |
| 259 | // htmx 2 |
| 260 | htmx.defineExtension('my-ext', { |
| 261 | onEvent: function (name, evt) { |
| 262 | if (name === 'htmx:configRequest') { /* ... */ } |
| 263 | }, |
| 264 | transformResponse: function (text, xhr, elt) { /* ... */ } |
| 265 | }); |
| 266 | |
| 267 | // htmx 4 |
| 268 | htmx.registerExtension('my-ext', { |
| 269 | init(api) { /* receive internal API */ }, |
| 270 | htmx_config_request(elt, detail) { |
| 271 | // detail.ctx has request context |
| 272 | }, |
| 273 | htmx_after_request(elt, detail) { |
| 274 | // detail.ctx.text has response text |
| 275 | } |
| 276 | }); |
| 277 | ``` |
| 278 | |
| 279 | To restrict which extensions can load, use the `extensions` config as a whitelist: |
| 280 | |
| 281 | ```html |
| 282 | <meta name="htmx-config" content='{"extensions": "my-ext"}'> |
| 283 | ``` |
| 284 | |
| 285 | ## Step 11: Handle GET/DELETE Form Data Change |
| 286 | |
| 287 | In htmx 4, `hx-delete` (like `hx-get`) no longer includes the enclosing form's inputs. If your |
| 288 | delete buttons relied on form data: |
| 289 | |
| 290 | ```html |
| 291 | <!-- htmx 2: form data included automatically --> |
| 292 | <form> |
| 293 | <input type="hidden" name="token" value="abc"> |
| 294 | <button hx-delete="/item/1">Delete</button> |
| 295 | </form> |
| 296 | |
| 297 | <!-- htmx 4: must explicitly include form --> |
| 298 | <form> |
| 299 | <input type="hidden" name="token" value="abc"> |
| 300 | <button hx-delete="/item/1" hx-include="closest form">Delete</button> |
| 301 | </form> |
| 302 | ``` |
| 303 | |
| 304 | ## Step 12: Handle OOB Swap Order Change |
| 305 | |
| 306 | In htmx 2, OOB swaps happened before the main content swap. In htmx 4, main content swaps first, |
| 307 | then OOB/partial elements swap after. If you have code that depends on OOB elements being present |
| 308 | when the main content is swapped, you may need to restructure. |
| 309 | |
| 310 | ## Step 13: Handle Non-200 Response Swapping |
| 311 | |
| 312 | In htmx 2, 4xx and 5xx responses did not swap by default. In htmx 4, all responses swap except |
| 313 | 204 (No Content) and 304 (Not Modified). |
| 314 | |
| 315 | If your server returns error HTML in 4xx/5xx responses and you don't want it swapped in, either: |
| 316 | |
| 317 | - Set `htmx.config.noSwap = [204, 304, "4xx", "5xx"]` to restore htmx 2 behavior |
| 318 | - Use `hx-status:4xx="swap:none"` and `hx-status:5xx="swap:none"` on specific elements |
| 319 | - Return 204 No Content when you want no swap to occur |
| 320 | |
| 321 | ## Compatibility Options |
| 322 | |
| 323 | For large codebases where a full migration isn't practical all at once, there are two options to |
| 324 | ease the transition: |
| 325 | |
| 326 | **Config flags** — add to your htmx config meta tag to restore htmx 2 defaults: |
| 327 | |
| 328 | ```html |
| 329 | <meta name="htmx-config" content='{ |
| 330 | "implicitInheritance": true, |
| 331 | "noSwap": [204, 304, "4xx", "5xx"] |
| 332 | }'> |
| 333 | ``` |
| 334 | |
| 335 | - `implicitInheritance: true` restores automatic attribute inheritance (skipping Step 3) |
| 336 | - `noSwap: [204, 304, "4xx", "5xx"]` restores htmx 2's 4xx/5xx no-swap behavior (skipping Step 13) |
| 337 | |
| 338 | **Compatibility extension** — `htmx-2-compat.js` fires old event names alongside new ones and |
| 339 | handles old attribute names: |
| 340 | |
| 341 | ```html |
| 342 | <script src="/path/to/htmx.js"></script> |
| 343 | <script src="/path/to/ext/htmx-2-compat.js"></script> |
| 344 | ``` |
| 345 | |
| 346 | These are bridges for incremental migration, not long-term solutions. |