Debian Codegraph @

dist/skills/htmx-debugging.md

raw symbols SCIP
1 ---
2 name: htmx-debugging
3 description: Use when diagnosing htmx issues, troubleshooting requests that aren't firing, swaps that aren't happening, events that aren't triggering, or any unexpected htmx behavior.
4 argument-hint: "[description of the problem]"
5 ---
6
7 # htmx 4 Debugging Guide
8
9 ## Quick Diagnostic Checklist
10
11 Work through these in order -- most issues fall in the first few:
12
13 1. **Is htmx loaded?** Check for `htmx` global in console, verify script tag
14 2. **Is the element processed?** Check `element._htmx` property exists
15 3. **What htmx version?** Check `htmx.config.version` -- htmx 4 API is very different from htmx 2
16 4. **Is the trigger correct?** Defaults: `click` for most, `change` for inputs/selects/textareas, `submit` for forms
17 5. **Is the target correct?** Verify the CSS selector matches an existing element
18 6. **Does the server return HTML?** Not JSON -- check Content-Type and response body
19 7. **Check the response status** -- htmx 4 swaps ALL responses except 204 and 304 by default
20 8. **Is inheritance set up?** htmx 4 requires `:inherited` modifier on parent attributes
21 9. **For extensions:** Is the extension approved in `<meta name="htmx-config" content='{"extensions":"..."}'>`?
22
23 ## Enable Debug Logging
24
25 Errors and warnings flow to `console.error` / `console.warn` by default. To also surface every event htmx dispatches:
26
27 ```html
28 <meta name="htmx-config" content='{"logAll": true}'>
29 ```
30
31 or in the console:
32
33 ```js
34 htmx.config.logAll = true;
35 ```
36
37 Observability tools (Sentry, DataDog RUM, LogRocket, etc.) capture `console.*` automatically, so htmx logs flow into your existing pipeline without any extra setup.
38
39 ## Event Monitoring Snippet
40
41 Paste this in the console to monitor the request/swap lifecycle:
42
43 ```js
44 ['htmx:config:request', 'htmx:before:request', 'htmx:after:request',
45 'htmx:before:swap', 'htmx:after:swap', 'htmx:error', 'htmx:finally:request']
46 .forEach(evt => document.body.addEventListener(evt, e => {
47 console.log(evt, e.detail?.ctx?.request?.action, e.detail?.ctx?.response?.status, e.detail);
48 }));
49 ```
50
51 To monitor what events a specific element is firing:
52
53 ```js
54 monitorEvents(htmx.find("#theElement"));
55 ```
56
57 ## Common Issues and Solutions
58
59 ### Request Not Firing
60
61 **Check the trigger:**
62 - Is the event actually happening? Use `monitorEvents()` on the element
63 - Default triggers differ by element type -- an `<input>` won't fire on `click`
64 - If using `hx-trigger="load"`, was the element in the DOM before htmx initialized?
65
66 **Check synchronization:**
67 - `hx-sync` may be dropping or queuing the request
68 - Check for `hx-sync="closest form"` or similar that might block it
69
70 **Check confirmation:**
71 - `hx-confirm` blocks until confirmed -- including `js:` async confirmation
72 - An `htmx:confirm` event listener calling `preventDefault()` without calling `issueRequest()` will block forever
73
74 **Check for `hx-ignore`:**
75 - A parent element with `hx-ignore` disables htmx for all children
76
77 **Dynamic content:**
78 - Elements added to the DOM after page load need `htmx.process(element)` to initialize htmx behavior
79 - Or use `htmx.onLoad()` to set up a callback for new content
80
81 ### Swap Not Happening
82
83 **Check response status:**
84 - `204` and `304` do NOT swap by default (controlled by `htmx.config.noSwap`)
85 - In htmx 4, 4xx and 5xx responses DO swap by default (unlike htmx 2!)
86 - If you need htmx 2 behavior: `htmx.config.noSwap = [204, 304, '4xx', '5xx']`
87
88 **Check the target:**
89 - Does the `hx-target` CSS selector match an existing element?
90 - Use browser devtools to run `document.querySelector("your-selector")` to verify
91
92 **Check `hx-swap`:**
93 - `hx-swap="none"` explicitly prevents swapping
94 - `hx-swap="delete"` deletes the target regardless of response
95
96 **Check `hx-select`:**
97 - If set, only matching elements from the response are used
98 - If nothing matches, nothing gets swapped
99
100 **Check event listeners:**
101 - An `htmx:before:swap` listener calling `preventDefault()` will cancel the swap
102
103 ### Wrong Content Being Swapped
104
105 **Check selectors:**
106 - `hx-select` might be matching the wrong element in the response
107 - `hx-target` might point to the wrong element
108
109 **Check for OOB/partial interference:**
110 - `hx-swap-oob` in the response swaps content by ID independently
111 - `<hx-partial>` tags in the response swap into their own targets
112 - In htmx 4, OOB swaps happen AFTER the main content swap (changed from htmx 2)
113
114 **Check response headers:**
115 - `HX-Retarget`, `HX-Reswap`, `HX-Reselect` override client-side attributes
116 - `hx-status:XXX` attributes can change target/swap based on status code
117
118 ### Extension Not Working
119
120 1. Is the extension script loaded AFTER htmx.js?
121 2. Is the extension name listed in the `extensions` config? Check `<meta name="htmx-config" content='{"extensions": "ext-name"}'>`
122 3. Does the extension name match exactly (case-sensitive)?
123 4. Check console for registration errors
124 5. htmx 4 extensions use `htmx.registerExtension()` not `htmx.defineExtension()` -- make sure you have an htmx 4 compatible extension
125
126 ### Inheritance Not Working
127
128 The #1 gotcha in htmx 4:
129
130 ```html
131 <!-- WRONG: children won't inherit this -->
132 <div hx-target="#output">
133 <button hx-get="/a">A</button>
134 </div>
135
136 <!-- RIGHT: use :inherited modifier -->
137 <div hx-target:inherited="#output">
138 <button hx-get="/a">A</button>
139 </div>
140 ```
141
142 - htmx 4 requires `:inherited` modifier by default
143 - Set `htmx.config.implicitInheritance = true` to get htmx 2 behavior
144 - Check that it's on the PARENT, not the child
145
146 ### History/URL Issues
147
148 - `hx-push-url` and `hx-replace-url` require the URL to return a full page when accessed directly
149 - History restoration in htmx 4 does a full page request (no localStorage/sessionStorage cache)
150 - Set `htmx.config.history = "reload"` to do hard browser reloads on back/forward
151 - `hx-status` attributes with `push:false` can prevent URL updates on errors
152
153 ### CSS Transitions Not Working
154
155 - CSS transitions rely on element ID stability across swaps -- keep `id` attributes consistent
156 - `htmx-swapping` class is applied before swap, `htmx-settling` after
157 - For View Transitions API: enable with `htmx.config.transitions = true` or `hx-swap="... transition:true"`
158 - Morphing (`innerMorph`/`outerMorph`) preserves animations better than `innerHTML`/`outerHTML`
159
160 ### Form Data Not Included
161
162 - `GET` and `DELETE` requests do NOT include enclosing form data by default in htmx 4
163 - Fix: add `hx-include="closest form"` to include form values
164 - Non-GET/DELETE requests (POST, PUT, PATCH) DO include enclosing form values automatically
165 - Check `hx-vals` syntax: must be valid JSON, use `js:` prefix for dynamic values
166
167 ### htmx 2 Code Not Working in htmx 4
168
169 Quick compatibility fixes:
170 1. Add `htmx.config.implicitInheritance = true` (restores automatic inheritance)
171 2. Add `htmx.config.noSwap = [204, 304, '4xx', '5xx']` (restores htmx 2 swap behavior)
172 3. Replace `hx-ext="name"` with `<script src="ext.js">` + `<meta name="htmx-config" content='{"extensions":"name"}'>`
173 4. Update event names: `htmx:beforeRequest` → `htmx:before:request`, `htmx:afterSwap` → `htmx:after:swap`, etc.
174 5. Replace `hx-disabled-elt` → `hx-disable`
175 6. Replace `hx-disable` (old meaning of ignoring) → `hx-ignore`
176 7. Replace `hx-vars` → `hx-vals` with `js:` prefix
177 8. Load the `hx-prompt` extension to keep `hx-prompt` working
178 9. Or load the `htmx-2-compat` extension for gradual migration
179
180 ## Browser DevTools Techniques
181
182 ### Network Tab
183 - Filter by Fetch/XHR requests
184 - Look for `HX-Request: true` in request headers to confirm htmx is making the request
185 - Check response headers for `HX-Trigger`, `HX-Retarget`, `HX-Reswap`
186 - Check response body -- should be HTML, not JSON
187
188 ### Elements Panel
189 - Inspect element and check for `_htmx` property (indicates htmx processed it)
190 - Look for `htmx-request` class during active requests
191 - Look for `htmx-swapping` / `htmx-settling` classes during swaps
192
193 ### Console
194 - `htmx.config.logAll = true` -- log every event (errors and warnings already on by default)
195 - `htmx.find("#selector")` -- test extended CSS selectors
196 - `htmx.trigger(elt, "eventName")` -- manually fire events
197
198 ## Instructions for Claude
199
200 When helping users debug htmx issues:
201
202 1. **Ask about the htmx version first** -- htmx 2 vs 4 is the most common source of confusion
203 2. **Suggest `htmx.config.logAll = true`** as the first step and ask for console output (errors/warnings are visible without it)
204 3. **Check for `:inherited` modifier** when inheritance problems are reported
205 4. **Check the Network tab** -- verify the request is being made and inspect the response
206 5. **Check the response body** -- it should be HTML, not JSON
207 6. **Look for typos** in attribute names (e.g. `hx-triger` instead of `hx-trigger`)
208 7. **Check `hx-status` attributes** that might override swap behavior for specific status codes
209 8. **Verify the server** returns appropriate status codes (200 for success, 422 for validation errors, 204 for no-content)