htmx interview question · Most asked
What happens in htmx 2 when the server returns 204, 404 or 500?
Summary
None of them are swapped by default. 204 means 'nothing to swap'. 4xx and 5xx fire htmx:responseError and leave the page unchanged; a network failure fires htmx:sendError and a timeout fires htmx:timeout. htmx:afterRequest fires in every case with detail.successful.
Longer Answer
Without handling, failures are silent to the user. Add a global listener (or hx-on::after-request) that shows a message and keeps the user's input. Note the default timeout in htmx 2 is 0 (none): set htmx.config.timeout or hx-request='{"timeout": ...}'. Delete endpoints should return an empty 200, not 204, when the row should disappear.
Key Points
- 204, 4xx, 5xx aren't swapped by default.
- responseError, sendError, timeout events.
- afterRequest with detail.successful.
- No default timeout in htmx 2.
Deep dive: htmx: Build Interactive Web Apps with HTML: Loading States, Races and Failures
More most-asked htmx questions
- How do you handle form validation errors with htmx?
- How does htmx compare with React or Vue?
- What is htmx?
- What do hx-get, hx-post, hx-put, hx-patch and hx-delete do, and how are values sent?
- What is hx-target, and what relative selectors does it support?
- What swap styles does hx-swap support?
- How does hx-trigger work? Give examples of modifiers and special triggers.
- What security risks are specific to htmx, and how do you mitigate them?