| 1 | --- |
| 2 | name: htmx-migration |
| 3 | description: Use when migrating a web application from React, Vue, Angular, or other SPA frameworks to htmx, or when converting client-side rendered components to server-rendered htmx patterns. Also covers htmx 2 to htmx 4 migration. |
| 4 | argument-hint: "[framework name or component to migrate]" |
| 5 | --- |
| 6 | |
| 7 | # Migrating to htmx |
| 8 | |
| 9 | ## SPA to htmx: The Core Shift |
| 10 | |
| 11 | htmx returns to the original web architecture: |
| 12 | - **Server renders HTML**, browser displays it |
| 13 | - **State lives on the server**, not in the browser |
| 14 | - **No build step**, no virtual DOM, no component lifecycle |
| 15 | - Server returns **HTML fragments**, not JSON |
| 16 | - The server framework matters more than the frontend |
| 17 | |
| 18 | ## Conceptual Mapping |
| 19 | |
| 20 | | SPA Concept | htmx Equivalent | |
| 21 | |---|---| |
| 22 | | Component state (`useState`, reactive data) | Server-side session/database state | |
| 23 | | Client-side routing (React Router, Vue Router) | Server routes + `hx-boost` or `hx-push-url` | |
| 24 | | API calls (`fetch`/`axios` to JSON endpoints) | `hx-get`/`hx-post` returning HTML fragments | |
| 25 | | Virtual DOM diffing | `innerMorph`/`outerMorph` swap styles | |
| 26 | | Component re-rendering | Server re-renders HTML fragment, htmx swaps it in | |
| 27 | | Props drilling | Server templates have access to all data | |
| 28 | | Global state (Redux, Vuex, Pinia) | Server-side state (session, database) | |
| 29 | | Client-side form validation | Server-side validation + `hx-status:422` | |
| 30 | | Loading spinners | `hx-indicator` + `htmx-indicator` class | |
| 31 | | Event bus | `HX-Trigger` response header + `hx-trigger="eventName from:body"` | |
| 32 | | Conditional rendering (`v-if`, `{cond && ...}`) | Server sends different HTML based on state | |
| 33 | | List rendering (`v-for`, `.map()`) | Server renders the list; use morph/OOB for updates | |
| 34 | | Lifecycle hooks (`useEffect`, `onMounted`) | `htmx:after:init`, `htmx:after:swap`, or `htmx.onLoad()` | |
| 35 | | Code splitting / lazy loading | `hx-trigger="load"` or `hx-trigger="revealed"` | |
| 36 | | Optimistic updates | `hx-optimistic` attribute (with extension) | |
| 37 | |
| 38 | ## Step-by-Step Migration Strategy |
| 39 | |
| 40 | 1. **Identify your server stack** -- htmx needs a server that renders HTML. Any backend works: Django, Rails, Express, Laravel, Spring Boot, Go, ASP.NET, etc. |
| 41 | |
| 42 | 2. **Identify your JSON API endpoints** -- these need HTML-returning variants. You can: |
| 43 | - Create new HTML endpoints alongside existing JSON ones |
| 44 | - Check for `HX-Request` header to return HTML vs JSON from the same endpoint |
| 45 | - Use `HX-Request-Type` header to distinguish partial (`"partial"`) vs full page (`"full"`) requests |
| 46 | |
| 47 | 3. **Convert one component at a time** -- replace the SPA component with: |
| 48 | - Server-rendered HTML + htmx attributes |
| 49 | - A server endpoint that returns an HTML fragment |
| 50 | |
| 51 | 4. **Handle interactivity with htmx patterns:** |
| 52 | - Search → `hx-get` with `hx-trigger="input changed delay:500ms"` |
| 53 | - Modals → `hx-get` to load modal content, `hx-target` to place it |
| 54 | - Tabs → `hx-get` per tab with `hx-push-url` |
| 55 | - Infinite scroll → `hx-trigger="revealed"` with `hx-swap="afterend"` |
| 56 | - Forms → `hx-post` with `hx-status:422` for validation errors |
| 57 | |
| 58 | 5. **For multi-region updates**, use one of: |
| 59 | - Expand the target (wrap both regions) |
| 60 | - `<hx-partial>` tags in the response |
| 61 | - `hx-swap-oob` for ID-based replacement |
| 62 | - `HX-Trigger` header + event-driven refresh |
| 63 | |
| 64 | 6. **Progressive enhancement** -- with `hx-boost`, links and forms work without JavaScript too |
| 65 | |
| 66 | ## React Migration Patterns |
| 67 | |
| 68 | ### useState + render → server endpoint + htmx |
| 69 | |
| 70 | **React:** |
| 71 | ```jsx |
| 72 | function Counter() { |
| 73 | const [count, setCount] = useState(0); |
| 74 | return <button onClick={() => setCount(c => c + 1)}>Count: {count}</button>; |
| 75 | } |
| 76 | ``` |
| 77 | |
| 78 | **htmx:** |
| 79 | ```html |
| 80 | <button hx-post="/counter/increment" hx-swap="outerHTML">Count: 0</button> |
| 81 | ``` |
| 82 | |
| 83 | Server returns: `<button hx-post="/counter/increment" hx-swap="outerHTML">Count: 1</button>` |
| 84 | |
| 85 | ### useEffect for data fetching → hx-trigger="load" |
| 86 | |
| 87 | **React:** |
| 88 | ```jsx |
| 89 | function UserProfile({ id }) { |
| 90 | const [user, setUser] = useState(null); |
| 91 | useEffect(() => { fetch(`/api/users/${id}`).then(r => r.json()).then(setUser); }, [id]); |
| 92 | return user ? <div>{user.name}</div> : <div>Loading...</div>; |
| 93 | } |
| 94 | ``` |
| 95 | |
| 96 | **htmx:** |
| 97 | ```html |
| 98 | <div hx-get="/users/123" hx-trigger="load" hx-swap="innerHTML"> |
| 99 | Loading... |
| 100 | </div> |
| 101 | ``` |
| 102 | |
| 103 | ### React Router → hx-boost |
| 104 | |
| 105 | **React:** |
| 106 | ```jsx |
| 107 | <Link to="/about">About</Link> |
| 108 | <Route path="/about" component={About} /> |
| 109 | ``` |
| 110 | |
| 111 | **htmx:** |
| 112 | ```html |
| 113 | <div hx-boost:inherited="true"> |
| 114 | <a href="/about">About</a> |
| 115 | <a href="/contact">Contact</a> |
| 116 | </div> |
| 117 | ``` |
| 118 | |
| 119 | Server returns full page content; htmx swaps just the body. |
| 120 | |
| 121 | ### Form with validation → hx-post + hx-status:422 |
| 122 | |
| 123 | **React:** |
| 124 | ```jsx |
| 125 | const [errors, setErrors] = useState({}); |
| 126 | const handleSubmit = async (e) => { |
| 127 | const res = await fetch('/api/register', { method: 'POST', body: formData }); |
| 128 | if (!res.ok) setErrors(await res.json()); |
| 129 | }; |
| 130 | ``` |
| 131 | |
| 132 | **htmx:** |
| 133 | ```html |
| 134 | <form hx-post="/register" hx-target="#result" hx-status:422="target:#errors"> |
| 135 | <input name="email" type="email"> |
| 136 | <div id="errors"></div> |
| 137 | <div id="result"></div> |
| 138 | <button type="submit">Register</button> |
| 139 | </form> |
| 140 | ``` |
| 141 | |
| 142 | Server returns 422 with error HTML, or 200 with success HTML. |
| 143 | |
| 144 | ## Vue Migration Patterns |
| 145 | |
| 146 | ### v-model → standard HTML form inputs |
| 147 | |
| 148 | State stays on the server. Form submission sends the data. |
| 149 | |
| 150 | ### v-if / v-show → server conditional rendering |
| 151 | |
| 152 | Server decides what HTML to return based on state. No client-side conditionals needed. |
| 153 | |
| 154 | ### v-for → server loop rendering |
| 155 | |
| 156 | Server renders the list. For updates, use `innerMorph` to preserve DOM state, or `hx-swap-oob` to update specific items. |
| 157 | |
| 158 | ### Vuex/Pinia → server state |
| 159 | |
| 160 | All application state lives on the server. No client-side state store needed. |
| 161 | |
| 162 | ## Angular Migration Patterns |
| 163 | |
| 164 | ### Services → server-side services |
| 165 | |
| 166 | Business logic moves to the server. htmx endpoints call server services directly. |
| 167 | |
| 168 | ### RxJS observables → SSE extension or polling |
| 169 | |
| 170 | ```html |
| 171 | <!-- Polling --> |
| 172 | <div hx-get="/updates" hx-trigger="every 5s">...</div> |
| 173 | |
| 174 | <!-- SSE (with extension) --> |
| 175 | <div hx-sse:connect="/events" hx-sse:swap="message">...</div> |
| 176 | ``` |
| 177 | |
| 178 | ### Angular forms → HTML forms + server validation |
| 179 | |
| 180 | Standard `<form>` with `hx-post`. Validation happens server-side. |
| 181 | |
| 182 | ### Angular routing → hx-boost + hx-push-url |
| 183 | |
| 184 | Same pattern as React Router migration above. |
| 185 | |
| 186 | ## Server-Side Endpoint Pattern |
| 187 | |
| 188 | The key to any migration -- your endpoints must return HTML: |
| 189 | |
| 190 | ``` |
| 191 | # Detect htmx request |
| 192 | if request.headers["HX-Request"]: |
| 193 | # Return just the HTML fragment |
| 194 | return render_template("_partial.html", data=data) |
| 195 | else: |
| 196 | # Return full page (for direct navigation, SEO, etc.) |
| 197 | return render_template("full_page.html", data=data) |
| 198 | ``` |
| 199 | |
| 200 | For validation errors, return status 422 with error HTML. |
| 201 | For no-content responses, return 204. |
| 202 | For redirects after form submission, use `HX-Redirect` header instead of HTTP 302. |
| 203 | |
| 204 | --- |
| 205 | |
| 206 | # htmx 2 to htmx 4 Migration |
| 207 | |
| 208 | ## Quick Fix: Two Config Changes |
| 209 | |
| 210 | These two settings make most htmx 2 apps work in htmx 4: |
| 211 | |
| 212 | ```html |
| 213 | <meta name="htmx-config" content='{ |
| 214 | "implicitInheritance": true, |
| 215 | "noSwap": [204, 304, "4xx", "5xx"] |
| 216 | }'> |
| 217 | ``` |
| 218 | |
| 219 | - `implicitInheritance: true` restores automatic attribute inheritance |
| 220 | - `noSwap` with 4xx/5xx restores htmx 2 swap behavior (only swap 2xx) |
| 221 | |
| 222 | ## Attribute Changes |
| 223 | |
| 224 | ### Renamed |
| 225 | |
| 226 | | htmx 2 | htmx 4 | Notes | |
| 227 | |---------|---------|-------| |
| 228 | | `hx-disabled-elt` | `hx-disable` | **IMPORTANT:** First rename old `hx-disable` → `hx-ignore`, then `hx-disabled-elt` → `hx-disable` | |
| 229 | |
| 230 | ### Removed |
| 231 | |
| 232 | | Removed | htmx 4 Alternative | |
| 233 | |---------|---------------------| |
| 234 | | `hx-vars` | `hx-vals` with `js:` prefix | |
| 235 | | `hx-params` | `htmx:config:request` event to filter parameters | |
| 236 | | `hx-prompt` | `hx-confirm` with `js:` prefix | |
| 237 | | `hx-ext` | Load extensions via `<script>` + `<meta name="htmx-config" content='{"extensions":"..."}'>` | |
| 238 | | `hx-disinherit` | Not needed (inheritance is explicit by default) | |
| 239 | | `hx-inherit` | Not needed (inheritance is explicit by default) | |
| 240 | | `hx-request` | `hx-config` | |
| 241 | | `hx-history` | Removed (no localStorage history) | |
| 242 | | `hx-history-elt` | Removed | |
| 243 | |
| 244 | ### Inheritance |
| 245 | |
| 246 | Add `:inherited` modifier to parent attributes: |
| 247 | |
| 248 | ```html |
| 249 | <!-- htmx 2 --> |
| 250 | <div hx-target="#output">...</div> |
| 251 | |
| 252 | <!-- htmx 4 --> |
| 253 | <div hx-target:inherited="#output">...</div> |
| 254 | ``` |
| 255 | |
| 256 | ### GET/DELETE form data |
| 257 | |
| 258 | GET and DELETE no longer include enclosing form data. Add `hx-include="closest form"` if needed. |
| 259 | |
| 260 | ### OOB swap order |
| 261 | |
| 262 | In htmx 4, main content swaps FIRST, then OOB/partial elements. (Reversed from htmx 2.) |
| 263 | |
| 264 | ## Config Changes |
| 265 | |
| 266 | ### Renamed |
| 267 | |
| 268 | | htmx 2 | htmx 4 | |
| 269 | |---------|---------| |
| 270 | | `defaultSwapStyle` | `defaultSwap` | |
| 271 | | `globalViewTransitions` | `transitions` | |
| 272 | | `historyEnabled` | `history` | |
| 273 | | `includeIndicatorStyles` | `includeIndicatorCSS` | |
| 274 | | `timeout` | `defaultTimeout` | |
| 275 | |
| 276 | ### Changed Defaults |
| 277 | |
| 278 | | Config | htmx 2 | htmx 4 | |
| 279 | |--------|--------|--------| |
| 280 | | `defaultTimeout` | `0` (no timeout) | `60000` (60s) | |
| 281 | | `defaultSettleDelay` | `20` | `1` | |
| 282 | |
| 283 | ### `data-hx-*` prefix |
| 284 | |
| 285 | Only `hx-*` recognized by default. For `data-hx-*`: set `htmx.config.prefix = "data-hx-"`. |
| 286 | |
| 287 | ## Event Name Changes |
| 288 | |
| 289 | htmx 4 naming: `htmx:phase:action` |
| 290 | |
| 291 | | htmx 2 | htmx 4 | |
| 292 | |---------|---------| |
| 293 | | `htmx:beforeRequest` | `htmx:before:request` | |
| 294 | | `htmx:afterRequest` | `htmx:after:request` | |
| 295 | | `htmx:beforeSwap` | `htmx:before:swap` | |
| 296 | | `htmx:afterSwap` | `htmx:after:swap` | |
| 297 | | `htmx:afterSettle` | `htmx:after:swap` | |
| 298 | | `htmx:configRequest` | `htmx:config:request` | |
| 299 | | `htmx:beforeOnLoad` | `htmx:before:init` | |
| 300 | | `htmx:afterOnLoad` | `htmx:after:init` | |
| 301 | | `htmx:afterProcessNode` | `htmx:after:init` | |
| 302 | | `htmx:beforeProcessNode` | `htmx:before:process` | |
| 303 | | `htmx:load` | `htmx:after:init` | |
| 304 | | `htmx:beforeCleanupElement` | `htmx:before:cleanup` | |
| 305 | | `htmx:beforeHistorySave` | `htmx:before:history:update` | |
| 306 | | `htmx:beforeHistoryUpdate` | `htmx:before:history:update` | |
| 307 | | `htmx:pushedIntoHistory` | `htmx:after:push:into:history` | |
| 308 | | `htmx:replacedInHistory` | `htmx:after:replace:into:history` | |
| 309 | | `htmx:beforeTransition` | `htmx:before:viewTransition` | |
| 310 | | `htmx:beforeSend` | `htmx:before:request` | |
| 311 | | `htmx:historyRestore` | `htmx:before:restore:history` | |
| 312 | | `htmx:responseError` | `htmx:error` | |
| 313 | | `htmx:sendError` | `htmx:error` | |
| 314 | | `htmx:sendAbort` | `htmx:error` | |
| 315 | | `htmx:swapError` | `htmx:error` | |
| 316 | | `htmx:targetError` | `htmx:error` | |
| 317 | | `htmx:timeout` | `htmx:error` | |
| 318 | |
| 319 | ## HTTP Header Changes |
| 320 | |
| 321 | ### Request Headers |
| 322 | |
| 323 | | htmx 2 | htmx 4 | Notes | |
| 324 | |---------|---------|-------| |
| 325 | | `HX-Trigger` | `HX-Source` | Format changed: `tagName#id` (not just ID) | |
| 326 | | `HX-Trigger-Name` | _Removed_ | Use `HX-Source` | |
| 327 | | `HX-Target` | `HX-Target` | Format changed: `tagName#id` | |
| 328 | | `HX-Prompt` | _Removed_ | Use `hx-confirm` with `js:` prefix | |
| 329 | | _(new)_ | `HX-Request-Type` | `"partial"` or `"full"` | |
| 330 | |
| 331 | ### Response Headers |
| 332 | |
| 333 | | htmx 2 | htmx 4 | |
| 334 | |---------|---------| |
| 335 | | `HX-Trigger-After-Swap` | _Removed_ (use `HX-Trigger`) | |
| 336 | | `HX-Trigger-After-Settle` | _Removed_ (use `HX-Trigger`) | |
| 337 | |
| 338 | ## JavaScript API Changes |
| 339 | |
| 340 | ### Removed (use native equivalents) |
| 341 | |
| 342 | | Removed | Use Instead | |
| 343 | |---------|-------------| |
| 344 | | `htmx.addClass()` | `element.classList.add()` | |
| 345 | | `htmx.removeClass()` | `element.classList.remove()` | |
| 346 | | `htmx.toggleClass()` | `element.classList.toggle()` | |
| 347 | | `htmx.closest()` | `element.closest()` | |
| 348 | | `htmx.remove()` | `element.remove()` | |
| 349 | | `htmx.off()` | `removeEventListener()` | |
| 350 | | `htmx.values()` | `new FormData(form)` | |
| 351 | | `htmx.defineExtension()` | `htmx.registerExtension()` | |
| 352 | | `htmx.logAll()` | `htmx.config.logAll = true` | |
| 353 | | `htmx.logNone()` | `htmx.config.logAll = false` | |
| 354 | |
| 355 | ## Extension Changes |
| 356 | |
| 357 | Extensions no longer use `hx-ext` attribute. Load via script + config: |
| 358 | |
| 359 | ```html |
| 360 | <!-- htmx 2 --> |
| 361 | <div hx-ext="sse">...</div> |
| 362 | |
| 363 | <!-- htmx 4 --> |
| 364 | <script src="/ext/hx-sse.js"></script> |
| 365 | <meta name="htmx-config" content='{"extensions": "sse"}'> |
| 366 | ``` |
| 367 | |
| 368 | Custom extensions need a full rewrite: `defineExtension()` → `registerExtension()`, callback-based → event-based hooks. |
| 369 | |
| 370 | ## Compatibility Extension |
| 371 | |
| 372 | For gradual migration, load the `htmx-2-compat` extension which bridges old event names and patterns: |
| 373 | |
| 374 | ```html |
| 375 | <script src="/ext/htmx-2-compat.js"></script> |
| 376 | ``` |
| 377 | |
| 378 | ## Instructions for Claude |
| 379 | |
| 380 | When helping with migrations: |
| 381 | |
| 382 | 1. **Ask about the server framework** -- htmx needs a server that returns HTML |
| 383 | 2. **For SPA→htmx:** convert one component at a time, don't try to migrate everything at once |
| 384 | 3. **For htmx 2→4:** suggest the two-line config fix first, then incrementally update attributes/events |
| 385 | 4. **Show both HTML and server response** for each pattern |
| 386 | 5. **Search for htmx 2 patterns** (camelCase events, `hx-ext`, `hx-vars`, etc.) and provide htmx 4 equivalents |
| 387 | 6. **Suggest the compat extension** as a bridge for large htmx 2 codebases |
| 388 | 7. **Preserve existing URL structure** where possible |