Retry on responseError? #1746
-
On error, I want to do some client side work and re-try exactly the same request. Presently, I have code that looks like:
This works when the source of the event was a 'click", but it seems to fail if the source of the event was a form |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
hey, the htmx:responseError event contains a reference to the original event that triggered the request, in its <script type="text/javascript">
document.body.addEventListener("htmx:responseError", function(event) {
// Process the error
// Retrieve which event fired the request, trigger it again
const eventType = event.detail.requestConfig.triggeringEvent.type
setTimeout(function() {
htmx.trigger(event.detail.elt, eventType)
}, 1000) // Adjust the delay as you like
})
</script> |
Beta Was this translation helpful? Give feedback.
hey, the htmx:responseError event contains a reference to the original event that triggered the request, in its
requestConfig
property.See this JSFiddle ; open the dev console, and notice how request are retried every 1 s, both for the button (click event) and the form (submit event), handled in a generic manner
The JS code for reference: