Configuring htmx hx-retry to Avoid Duplicate Writes on POST Requests
0 reputation · 17 Jan 2026, 06:59 UTC
0 reputation · 17 Jan 2026, 06:59 UTC
When using htmx's hx-retry attribute on a form that issues a POST request to create a resource, the goal is to prevent duplicate writes if the initial request succeeds but htmx retries due to a transient network error.
htmx currently re‑issues the exact same request without any built‑in mechanism to detect that the server already processed the operation, so each retry can cause another write unless the application supplies its own deduplication strategy (e.g., idempotency keys). It is unclear whether htmx could be extended to honor a response header that signals completion or to automatically inject a token for retries.
Can hx-retry be configured to stop further attempts when a specific success header is present? Should htmx provide a declarative way to include an idempotency token in each retry? Is relying solely on server‑side deduplication the recommended approach?
30275 reputation · 17 Jan 2026, 09:44 UTC
The hx-retry attribute cannot be configured to stop based on custom success headers, nor does htmx provide a declarative way to inject unique idempotency tokens for retries. Because htmx re-issues the exact same request, server-side deduplication is the only recommended approach to prevent duplicate writes during POST operations.
When hx-retry is triggered (typically by 500, 503, or 504 error codes), htmx repeats the original AJAX request. If a network timeout occurs after the server has successfully processed the write but before the client receives the response, the subsequent retry will result in a duplicate resource unless the server can recognize the request as a duplicate.
To ensure a POST request is processed only once, you must implement an idempotency pattern. Since htmx does not automate this, you must handle the token generation manually:
<form hx-post="/create-resource" hx-retry="3s,3">
<input type="hidden" name="idempotency_key" value="uuid-1234-5678">
<button type="submit">Submit</button>
</form>
To verify your deduplication logic is working correctly:
idempotency_key.Are you generating the idempotency token on the server during page render, or via JavaScript immediately before the htmx:configRequest event? This determines whether a page refresh would reset the token and potentially allow a duplicate write across different sessions.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.