Why HTMX Should Be In Your Webdev Toolkit
Tokyo Clojure
This is a webdev talk�(because I’m a webdev, plz contact me if you want to present something)
Who am I?
My name is Aaron Burdick
I want to talk about Hypermedia
I got most of these ideas from this book
SPA World
This JavaScript runtime is so powerful, in fact, that today many developers ignore the hypermedia features of the browser, in favor of building their web applications entirely in JavaScript. Applications built in this manner have come to be called Single Page Applications (SPAs). …[which] communicate with a server, these applications typically use JSON API calls via AJAX. And they often update the user interface using a “reactive” style frontend JavaScript library.
In these applications HTML becomes a…graphical interface description language that is used because, for historical reasons, that’s what happens to be there, in the browser.
Applications built in this style are not hypermedia-driven: they do not take advantage of the underlying hypermedia system of the web.
An General Example
The JavaScript will issue an AJAX HTTP GET request to /api/v1/contacts/1 using fetch(). …unlike requests issued by those hypermedia controls, it is up to the JavaScript code to handle the response from the server.
The JavaScript code above converts the JSON text received from the server into a JavaScript object... [the Javascript] is responsible for updating the UI based on the data encoded in the JavaScript Object, perhaps by displaying the contact in a bit of HTML generated via a client-side template in the JavaScript application.
The JSON API being used here does not return a hypermedia response…This JSON API is, rather, a Data API. Because the response is in JSON and is not hypermedia, the JavaScript… must understand how to turn this contact data into HTML. the code…needs to know about the internal structure and meaning of the data.
It needs to know:
In short, the logic in updateUI() needs to have intimate knowledge of the API endpoint at /api/v1/contact/1... As a result, the updateUI() code and the API have a strong relationship, known as tight coupling: if the format of the JSON response changes, then the code for updateUI() will almost certainly also need to be changed as well.
Roy Fielding, the creator of REST
I am getting frustrated by the number of people calling any HTTP-based interface a REST API. Today’s example is the SocialSite REST API. That is RPC. It screams RPC. There is so much coupling on display that it should be given an X rating.
Why do we have SPAs in the first place then?
General trend
Amount of SPA-like code cost
Interactivity
General trend
Amount of SPA-like code cost
Interactivity
Coupling, state, complexity, cost…
More interactivity w/ lower cost?
Amount of SPA-like code cost
Interactivity
HTMX
htmx gives you access to AJAX, CSS Transitions, WebSockets and Server Sent Events directly in HTML, using attributes, so you can build modern user interfaces with the simplicity and power of hypertext
htmx is small (~14k min.gz’d), dependency-free, extendable, IE11 compatible & has reduced code base sizes by 67% when compared with react
<script src="https://unpkg.com/htmx.org@1.9.11"></script>
<!-- have a button POST a click via AJAX -->
<button hx-post="/clicked" hx-swap="outerHTML">
Click Me
</button>
Source: https://htmx.org/
Drawbacks, cost
Advantages, payoff
Clojure <3 HTMX
Biff - https://biffweb.com/docs/reference/htmx/
SimpleUI - https://simpleui.io/
Examples from Hypermedia Book in Clojure (reitit, hiccup) - https://github.com/aburd/hypermedia-systems-clj-examples
HTMX Docs - https://htmx.org/docs/#introduction
So why should HTMX be in your toolkit?