htmx-protocol-pitfalls
Step 6 of the pipeline (WORKFLOW.md), for the htmx behaviours that are decided by the HTTP exchange rather
than by the markup: what the server must send, what the browser eats before htmx sees it, and what htmx does
to the page around the swap. The sections share one premise: htmx only sees what the browser hands it, and
it only wires up what is in the DOM when it looks. Each rule says what you see when it is missed.
Version scope: htmx 2.0.x, read at the 2.0.11 release. The repository documentation also mentions
material of unclear version scope (an hx-partial swap command, form validation on by default in a later
line); none of it is used here, and every rule below applies to 2.0 only. Check the installed version before relying on a point.
Overlaps with skills/htmx-alpine-conventions, which owns which tool does what, swap targets, escaping and Alpine state;
this block owns the HTTP exchange. Cross-referenced, not merged (see references/origin.md).
When
- A server endpoint answers an htmx request, redirects after a form post, or has to tell the page to do
something (navigate, refresh, fire an event, swap elsewhere).
- A page polls, or shows progress for a long task.
- A button double-submits, a spinner is wanted, a video or field must survive a swap, or a child element
behaves as if it had inherited something nobody wrote on it.
- A third-party widget stops working after a swap, a back button shows a fragment instead of a page, a cached
response is the wrong variant, or htmx calls fail across origins.
Steps
Read only the section the task meets. They are independent.
| § |
Covers |
Read it when |
File |
| 1 |
Response side: redirects and lost headers, response headers, polling and 286 |
the server answers an htmx request, or an element polls |
01-responses-and-polling.md |
| 2 |
Request side: indicators, double submit, preserve, inheritance |
a request needs UX, or an attribute applies where it should not |
02-request-ux-and-inheritance.md |
| 3 |
Integration and delivery: widgets after swap, caching, history, CORS, pinned script |
htmx meets other JavaScript, a cache, another origin or a CDN |
03-integration-and-delivery.md |
Output / checkpoint
Each rule was exercised, not read off the file: the endpoint was called once with and once without the htmx
request header and the two bodies compared (§3), a redirect was followed in a browser with the network panel
open and the header seen to arrive (§1), a poll was stopped by the server and the requests stopped (§1), a
button was clicked twice quickly and one request went out (§2). A rule only read is not verified.
Guardrails
- Never rely on a response header sent with a 3xx status: the browser follows the redirect internally and
htmx never sees the header (§1).
- Never send
286 from an endpoint that is not a polling target: it cancels the polling of the element that
made the request (§1).
- Never hoist
hx-target or hx-confirm onto a container without checking every htmx element beneath it (§2).
- Never return a fragment from a URL a user can open directly without
Vary or a distinct validator (§3).
- Never push a URL into history that cannot return a full page (§3).
- Security of swapped HTML (escaping, content security policy) belongs to
security-hardening.
Origin
Rewritten from the htmx repository documentation and source (BSD Zero-Clause), read 2026-10-08 at the 2.0.11
release, cross-checked against one MIT htmx skill set. 🟡: never run by us; open points are in
references/origin.md.
1---2name: htmx-protocol-pitfalls3description: "Use when a server sends HTML fragments to htmx 2.x (response headers HX-Redirect, HX-Location, HX-Trigger, HX-Refresh, HX-Retarget, HX-Reswap, the lost headers on 3xx redirects, polling with every Ns and the 286 status, load polling), when an htmx request needs UX around it (htmx-indicator, hx-disabled-elt against double submit, hx-preserve, attribute inheritance leaking hx-confirm or hx-target to children), or when htmx meets other code (htmx.onLoad and htmx.process for widgets, Vary and ETag for fragment versus full-page responses, history restore, CORS exposed headers, a pinned CDN script with an integrity hash). Which tool owns what, swap targets, escaping and Alpine state are htmx-alpine-conventions."4metadata:5 source: https://skillmd.com/skills/techmefr/htmx-protocol-pitfalls6---78# htmx-protocol-pitfalls910Step 6 of the pipeline (`WORKFLOW.md`), for the htmx behaviours that are decided by the HTTP exchange rather11than by the markup: what the server must send, what the browser eats before htmx sees it, and what htmx does12to the page around the swap. The sections share one premise: **htmx only sees what the browser hands it, and13it only wires up what is in the DOM when it looks**. Each rule says what you see when it is missed.1415**Version scope: htmx 2.0.x**, read at the 2.0.11 release. The repository documentation also mentions16material of unclear version scope (an `hx-partial` swap command, form validation on by default in a later17line); none of it is used here, and every rule below applies to 2.0 only. Check the installed version before relying on a point.1819Overlaps with `skills/htmx-alpine-conventions`, which owns which tool does what, swap targets, escaping and Alpine state;20this block owns the HTTP exchange. Cross-referenced, not merged (see [`references/origin.md`](./references/origin.md)).2122## When23- A server endpoint answers an htmx request, redirects after a form post, or has to tell the page to do24 something (navigate, refresh, fire an event, swap elsewhere).25- A page polls, or shows progress for a long task.26- A button double-submits, a spinner is wanted, a video or field must survive a swap, or a child element27 behaves as if it had inherited something nobody wrote on it.28- A third-party widget stops working after a swap, a back button shows a fragment instead of a page, a cached29 response is the wrong variant, or htmx calls fail across origins.3031## Steps3233**Read only the section the task meets.** They are independent.3435| § | Covers | Read it when | File |36|---|---|---|---|37| 1 | Response side: redirects and lost headers, response headers, polling and 286 | the server answers an htmx request, or an element polls | [`01-responses-and-polling.md`](./references/01-responses-and-polling.md) |38| 2 | Request side: indicators, double submit, preserve, inheritance | a request needs UX, or an attribute applies where it should not | [`02-request-ux-and-inheritance.md`](./references/02-request-ux-and-inheritance.md) |39| 3 | Integration and delivery: widgets after swap, caching, history, CORS, pinned script | htmx meets other JavaScript, a cache, another origin or a CDN | [`03-integration-and-delivery.md`](./references/03-integration-and-delivery.md) |4041## Output / checkpoint42Each rule was exercised, not read off the file: the endpoint was called once with and once without the htmx43request header and the two bodies compared (§3), a redirect was followed in a browser with the network panel44open and the header seen to arrive (§1), a poll was stopped by the server and the requests stopped (§1), a45button was clicked twice quickly and one request went out (§2). A rule only read is not verified.4647## Guardrails48- Never rely on a response header sent with a 3xx status: the browser follows the redirect internally and49 htmx never sees the header (§1).50- Never send `286` from an endpoint that is not a polling target: it cancels the polling of the element that51 made the request (§1).52- Never hoist `hx-target` or `hx-confirm` onto a container without checking every htmx element beneath it (§2).53- Never return a fragment from a URL a user can open directly without `Vary` or a distinct validator (§3).54- Never push a URL into history that cannot return a full page (§3).55- Security of swapped HTML (escaping, content security policy) belongs to `security-hardening`.5657## Origin58Rewritten from the htmx repository documentation and source (BSD Zero-Clause), read 2026-10-08 at the 2.0.1159release, cross-checked against one MIT htmx skill set. 🟡: never run by us; open points are in60[`references/origin.md`](./references/origin.md).