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:

  1. Listen for events from elsewhere (hx-trigger contains from:)
  2. Make an HTTP request (hx-get, hx-post, etc.)
  3. Have no explicithx-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).