Date: 2024.08.02 | create | fsharp | giraffe | htmx | tech |
DISCLOSURE: If you buy through affiliate links, I may earn a small commission. (disclosures)
I build most of my projects with F# / Giraffe and HTMX. In this post I'll share how I get HTMX Targets from a web request in F# / Giraffe as well as how I use that to build apps with HTMX.
Want to see HTMX in action in real projects? HAMINIONs members get access to the full source code of dozens of my projects including 3 using F# + Giraffe + HTMX.
An HTMX Target (declared with hx-target) allows you to target a specific element on the page to swap returned HTML with. I like to build my apps with isolated Interactive Islands powered by HTMX so I use targets as a unique identifier for a specific element.
<div>
<div id="iamanelement">
<button hx-post="/do-something" hx-target="#iamanelement">
Do something!
</button>
</div>
</div>
The way this works then is:
Now let's look at how to get an HTMX Target from an F# / Giraffe web request. Giraffe uses an HttpContext to access data on the request.
HTMX itself sends two Headers on requests that we care about:
HX-Request - Whether this is an HTMX request or notHX-Target - The Target on the elementSo we can create a little function that will get the target off of an HttpContext if it exists:
let getHtmxTargetFromContext
(ctx : HttpContext)
: 'a option
=
ctx.TryGetRequestHeader "HX-Request"
|> Option.bind (
fun _ ->
ctx.TryGetRequestHeader "HX-Target"
)
I usually setup my Server-side rendering code like this:
This makes it easy for me to reuse components, get the power of HTMX for partial re-rendering where necessary, all while keeping most of the simplicity of MPAs.
Then you can do something like this to pass the target into your rendering code.
let myPageHttpHandler
=
handleContext(fun (ctx : HttpContext) ->
task {
let target =
getHtmxTargetFromContext
ctx
return renderMyComponent target
}
)
Your rendering code could be smth very simple like:
This is exactly what I did in my latest project 1000 Checkboxes - if the target is the CheckboxGrid we only return that.
HAMINIONs Members get full access to project source code like 1000 checkboxes (Project Repo).
I've been happily building little websites with F# + Giraffe + HTMX for the past several months. More to come.
Q: How are you building webapps with HTMX? What are you finding difficult to implement?
If you liked this post you might also like:
The best way to support my work is to like / comment / share for the algorithm and subscribe for future updates.