Module
server.htmx_safe_target
Safe target — auto-add hx-target="this" to event-driven elements.
When hx-boost sets hx-target on the
, that target propagates to every child element via htmx's attribute inheritance. Self-updating elements (counters, badges, live regions) that usehx-triggerwith
from:to listen for server-sent events inherit this global target
and silently swap their tiny fragment response into the main content
area, wiping the page.
This script fixes the footgun automatically. On everyhtmx:load
(initial page load + after each swap), it finds elements that:
- Listen for events from elsewhere (
hx-triggercontainsfrom:) - Make an HTTP request (
hx-get,hx-post, etc.) - Have no explicit
hx-target
…and addshx-target="this"so the response targets the element
itself instead of the inherited layout target.
The attribute is visible in DevTools — it looks like the developer
wrote it. If a developer setshx-targetexplicitly, the
:not([hx-target])selector skips the element.
Injected into every full-page HTML response viaHTMLInject
middleware. Disabled withAppConfig(safe_target=False).