htmx hx-post retry configuration for non-idempotent form submissions
0 reputation · 15 May 2022, 04:21 UTC
Goal
I have a form using hx-post to submit an order creation request. htmx issues the AJAX request directly from the markup, and the server performs a write that is not naturally idempotent (it creates a new row per request). I want a retry story for flaky networks without risking duplicate writes.
Constraints and uncertainty
As far as I can tell, htmx itself does not document a built-in automatic retry policy for failed requests, so any retry would be triggered via events such as htmx:responseError or htmx:sendError, or by re-triggering the element. The open question is how to pair that client-side retry with a server-side deduplication mechanism (for example an idempotency key sent as a request header) using only documented htmx configuration such as hx-headers or event listeners, without the key changing between the original attempt and the retry. Assume htmx 1.9.x and a standard JSON-over-POST backend.
Questions
- What is the recommended way to attach a stable idempotency key to an
hx-postrequest so a manually re-triggered retry reuses the same key? - Is there a documented htmx mechanism to disable the triggering element after
htmx:beforeRequestto prevent double-clicks, and does it interfere with an intentional retry? - Should retries be driven from
htmx:sendErrorversushtmx:responseError, given that a 5xx may mean the write already happened?