Write Custom Extensions for htmx to Make it Your Own Personal Toolkit

submitted by 💻

thathtml.blog/2025/11/htmx-extensions-your-pers…

Hot take: the killer feature of htmx which transforms it from a nice lil’ opinionated DSL for HTML-based reactivity to a powerhouse full-stack framework with which you can build ambitious, even experimental application architectures is this: Extensions.

Unfortunately, if you look up “Building htmx extensions” in the docs, there’s virtually nothing there. 😒

So I’m guessing the answer to “Why didn’t anyone tell me about this sooner??” is likely “Because nobody knows!”

I’m here to rectify that situation; in particular, explain the ability for you to build your own custom swaps.

6
12

Log in to comment

6 Comments

@jaredwhite Neat! I have some htmx that says hello to an API, and if the response is a 204, the tag then knows to remove the div (along with the reserved whitespace). That logic is currently all defined in the htmx tags, and is a bit hard to parse/reason. I reckon this might help clean it up a bit! Ty :)

Cool, this seems analogous to custom actions in Turbo Steams.

Coming from Turbo, is there anything you’re missing in htmx? (plus maybe Swup in place of Turbo Drive)

@fpsvogel @jaredwhite The only thing I miss in HTMX that Turbo supports is the ActionCable integration (provided by `turbo-rails`) that multiplexes WebSockets. So updates for many elements can be sent over a single WebSocket connection. HTMX’s WebSocket extension only handles a single element per WebSocket.

Everything else I’ve used in Turbo has a direct analogue in HTMX.

Deleted by author

 reply
1

@Cyberbeni @fpsvogel @jaredwhite Oh, nice. It makes sense, to some degree. SSE messages have per-message metadata exposed as part of the protocol, but AFAICT WebSockets don't.

Deleted by author

 reply
1

There’s definitely conceptual overlap here, and that’s actually how I found out about this feature in the first place (experimenting with something akin to actions in Turbo 😏).

htmx does have some Drive-like functionality with the boosting feature, but I’m not sure that’s its strongest suit. I’d probably reach for Swup first as an alternative like you mentioned.

Is this something you knew about? Have you tried writing your own custom htmx swaps before? What might you create with it? Comment here and let me know! (or AMA about htmx!) ☺️

Insert image