-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
hx-swap is not applied when response is 204 No Content #1130
Comments
I found this note in the code while trying to provide a PR for this: https://github.com/bigskysoftware/htmx/blob/master/src/htmx.js#L3062-L3065
But it seems to me that an explicit mention of |
I came here to create the same issue, with exactly the same use-case. At the very least I think it would be good idea to add a note to the documentation (and the delete row example that a That having been said, a |
This is an ongoing saga, but the MDN resource says this about https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/204
In htmx terms this means "do nothing", so the current default behavior is appropriate. However, you can adjust this behavior if you want by using the https://htmx.org/docs/#modifying_swapping_behavior_with_events |
I don't understand why this means "do nothing" to htmx. Can you explain the rationale? |
Better you tell us what you didn’t fully understand. |
After reading this quote:
it seems to me that htmx might want to take some action upon successful response without navigating away from the current page. For example, to delete a DOM node that represents the deleted resource. So I don't understand why for htmx it is good to "do nothing". |
Ahh. :) I usually do a 204 when I want to trigger an event via response header. This can also be used to delete content. Another approach could be a route that returns content X but depending on the requests has a status code 200 or 204. |
I agree with @nat-418 on this. Returning 204 should work out of the box for hx-swap. |
I interpret this, in normal HTML, as meaning "Do nothing to the current page" not as "replace the current page with nothing." htmx takes this interpretation to the partial level: "do nothing with the currently targeted element". If you wish to replace the current element with nothing, you can return a 204 with an empty body. I admit there is some ambiguity in the language here, but I think the current htmx interpretation of the response code is reasonable. |
I should also note that this behavior can be overridden by catching the https://htmx.org/events/#htmx:beforeSwap https://htmx.org/docs/#modifying_swapping_behavior_with_events so you can make htmx behave however you'd like with respect to this response code. |
Came here to say I ran into this issue as well and my expectation was that an element with |
Interestingly I come accross this thread by searching on "how do I give an htmx response that tells the client not to swap", and glad that htmx just does that by default with an empty 204 response. I understand the difference between 204 and 200 as something like a null and empty string. Just say that to bring some different perspective to the discussion. |
The current functionality is correct based on the docs:
In both |
While trying to reproduce https://htmx.org/examples/delete-row/ in my app, I struggled a bit since it wasn't swapping on my DELETE response (204 No Content).
The example says:
Until I found that answering a 200 with an empty string was doing the job.
It seems to me that "204 No Content" is a much cleaner way to return an empty content and should have the same behavior as 200 with an empty string when used with hx-swap.
The text was updated successfully, but these errors were encountered: