htmx interview question · Most asked

What happens in htmx 2 when the server returns 204, 404 or 500?

Create a free account to save this page, take notes and track your progress. Sign in

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

← All htmx questions

Was this page helpful?