-
-
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
Static template that doesn't parse the response #3082
Comments
As you linked the v1 docs, I suppose you're using htmx v1 and not v2 here ? |
Is the mismatch between ids relevant (unrecoverable vs. unhandled)? |
No - sorry that is a copy paste error from reformatting the real markup to be easier to read for the example. What happens here is the server returns a response with an error status code. HTMX produces a Json Decoding error because the error response isn't formatted with JSON. And then HTMX inserts the server response instead of the static message from the template. |
Perhaps you could add something like: |
Awesome! Thanks! |
I tried this:
It did not solve the issue. Note that it seems to work without |
According to docs, default handling is: responseHandling: [
{code:"204", swap: false}, // 204 - No Content by default does nothing, but is not an error
{code:"[23]..", swap: true}, // 200 & 300 responses are non-errors and are swapped
{code:"[45]..", swap: false, error:true}, // 400 & 500 responses are not swapped and are errors
{code:"...", swap: false} // catch all for any other response code
] So there should be no difference between 4xx and 5xx errors. The
Possibly also need to set You can get value Here is how htmx matches status code: function codeMatches(responseHandlingConfig, status) {
var regExp = new RegExp(responseHandlingConfig.code)
return regExp.test(status.toString(10))
} |
As you said earlier @thenewguy ,
So the issue is not the error code, but rather that the response isn't JSON. htmx/dist/ext/client-side-templates.js Lines 28 to 30 in 30ab5c1
htmx/dist/ext/client-side-templates.js Line 35 in 30ab5c1
If your server properly answers with a JSON on a 4xx error, then it works as expected, if it doesn't answer with JSON (error 5xx in your case) then it can't. What to doI think you have to make it a proper JSON in any case, be it by making sure your server returns a JSON even on a 5xx error, or by adding some custom client-side code to replace the server response if it is not valid JSON. Hope this helps! PS: as you're using htmx 1, make sure to use the V1 docs which doesn't include |
I appreciate your attention to this matter. I understand that returning JSON from the server would prevent this error from occuring but unfortunately that is not a viable solution in this case. I had hoped that I was overlooking a simple workaround. Since it appears that this cannot be easily handled in client code, please consider adding a new template type option that makes it possible to insert static html templates in situations such as this. Thanks! |
Ofc depends on what you consider "easy" or not @thenewguy , but I feel the following would do the trick: document.body.addEventListener("htmx:beforeSwap", function (event) {
const isError = event.detail.xhr.status >= 400
const isHandlebarTemplate = htmx.closest(event.detail.elt, "[handlebars-template], [handlebars-array-template]")
if (isError && isHandlebarTemplate) {
try {
JSON.parse(event.detail.serverResponse)
} catch {
event.detail.serverResponse = "{}"
}
}
}) Disclaimer: I didn't test this at all, but that's what I had in mind when typing the previous message
Hope this helps!
Sounds reasonable to me! If you are interested btw, feel free to investigate & open a PR for that! |
With a current use case, the response from the POST isn't important - the side effect is handled by
path-deps
and that handles the re-render. It is only important to let the user know there if was an error.As such, it would be helpful to be able to provide a static template block that does not parse the response.
For example:
Using the available template engines causes a Parsing error in Javascript when JSON isn't returned by a 500 error and htmx falls back to inserting the response into "#unhandled-error" instead of the static message.
Being able to force the static message to display would be very beneficial.
The text was updated successfully, but these errors were encountered: