Htmx in a Nutshell
htmx.org
htmx.org
Django, DRF, Postgres, tailwind and HTMX.
I am so tired of all the front end frameworks and all the complexity that gets added. At some point I think you need it and you get returns from it but hearing more people in the industry recognize and talk about how JS everything isn't always the answer gives me hope.
I like what HTMX has to offer and I am excited to see it continue to get air time.
The final peace of the puzzle is getting component scoped styles, which you can do by packaging your components into a web component and using the shadow DOM. CSS custom properties actually penetrate the shadow boundaries so it is a nice catch-all. If web components scare you (which is reasonable, they are scary) then there are other systems (as simple as class naming conventions) which you can use to reach component scoped styles without tailwind.
I'm updating my CSS understanding after having left the front end for ~6 years (but having done a lot of front-end work for the previous 10), loving custom properties and am intrigued with some of the systems built around them (e.g. Pollen), trying to figure out where else I should be directing attention.
As for the shadow DOM, it is pretty advanced, I don’t recommend going there unless you really want to learn it. Using web components without understanding can cause way more problems then it solves. And at the end of the journey you get a very verbose system with a lot of boilerplate just to get component scoped styles, which is not worth it. But if you still want to learn it, this is a good place to start: https://developer.mozilla.org/en-US/docs/Web/Web_Components
I have completely ignored web components and need to learn all about them.
Is HTMX really mature and stable for production and massive UI heavy apps ?
I am asking because we are starting a big project in Django, Postgres, Tailwind and for frontend, still trying to use VueJS and we hate SPAs so curious what our options are.
I don't have much experience with HTMX, but it seems to be aimed at sites with a little bit of interactivity, especially if you want to be "forced" to maintain compatibility with no-js. (I mean "forced" in the positive sense. It keeps you honest.)
I'm an experienced ios dev, but in at the deepens with all this js frontend stuff. There are so many options! The one thing I'm sure of is I don't want to use react.
https://htmx.org/essays/a-real-world-react-to-htmx-port/ is about porting a serious production Django application from React to htmx.
If you hate SPAs and are chafing against Vue for building your app, it's probably worth considering if your app absolutely needs to be a JS/frontend-heavy product, or if it would be viable using a more traditional HTML+AJAX design.
In my experience this distinction has been a source of complexity in server-rendered apps that consume the same API as the client but at a different address, leading to problems like untrusted self-signed certificates, mismatching hostnames, and leaky proxy routes.
Or perhaps none of your server rendered pages even call the API directly, and you just expose it for other clients?
[1] https://django-ninja.rest-framework.com
edit: typo
1. Python with htmx has less accidental complexity. It doesn't get rid of intrinsic complexity of the domain, but it does reduce technology-induced overhead.
2. Dotnet/Angular tech split has organisational implications, tending to encourage a division between front end and back end developers. Communication and coordination overhad goes up correspondingly (Conway's law).
3. htmx is stable. There's a lot less churn than with Angular. I haven't measured this, but subjectively, there's less time spent updating packages and re-testing. Keeping FastAPI/Django/Python update is approximately equivalent keeping the dotnet back end up to date.
4. State management is simpler. There's no front end caching to deal with, and so no corresponding cache invalidation questions.
I'm not building tech unicorn sites, so can't comment on whether the technical overhead is justified at that scale. I _have_ been pleasantly surprised at the level of UI sophistication that can be achieved with server side rendering and htmx.
I'm not a big javascript fan generally, so definitely have bias. I also have an increasingly allergic reaction to accidental complexity, so am constantly looking for ways to minimise it.
Based on those biases and experiences, my default starting point is SSR+htmx. In my experience the accidental complexity is lower, productivity higher, and intra-team communication better. YMMV obviously.
Do this in a static functional language and your whole app will more “deterministically pure”.
So yes, it causes more work on backend but, given more tooling, it can provide more opportunities for less code written overall via code-generation (like how creating a model in Rails generates lots of helpers).
Equivalently, my first impression of HTMX is that I kind of hate it. But I could see it being similar to Tailwind, where you just need to try it out and then it'll click. Is that fair to say?
The other part is that I'm trying to simply "fix HTML", and I believe HTMX is pretty great. I'm taking a different approach as I have a reactive backend, and I can make the HTML simply be a template of a streaming JSON object. It's like mustache with reactive data binding.
I'm enjoying, but I'm not a consultancy so I don't need to push the framework for every usecase.
Another big advantage of the Tailwind approach is that you can copy any component you see on the web, paste it in your project, and it will look the same (unless custom classes are used). No need to hunt down and adapt the whole cascade of styles for each tag.
In that sense, Tailwind has some of the earlier-Internet charm where you learned by seeing what others were doing.
If you’re in a scenario where you’re tasked with building out UI, but don’t have design or FE skills, my recommendation would be something like bootstrap or MUI.
Maybe it's not for everyone, but it really is worth a try, with an open mind.
So when you write something like `hx-trigger="click" hx-put="/api/my"` you're actually actively thinking of an `addEventListener("click", ...)`, `this.innerHTML` assignment and `fetch("/api/my", { method: "PUT" }))`. Can't use HTMX without knowing the underlying principles and primitives.
And then you still have to do JS because how else you're supposed to handle `htmx:responseError` and stuff. And maybe I'm wrong but it feels risky because the logic could end up all around the place and not in a single nice function/code block.
So, basically, it's a nice DSL providing a bunch of shortcuts, but it doesn't magically alleviate knowing any underlying principles and nuances of JS and DOM. Again, it's even defined in terms of those systems (`hx-swap="outerHTML"` being a very clear indicator example).
Unless you only consider the happiest path, as in "I develop on localhost with zero latency, 100% uptime, and my backend validation logic perfectly matches my frontend policies" (and probably a lot more other "if"s and "only"s).
alpinejs does make the claim that it is a "better jquery". What is the development experience here?
especially for backend teams (python, java, whatever) who are now also building HTMX.
I'm not aware of a major Javascript engine which is written in C, except for QuickJS, which is not "major" in any sense of popularity but is an efficient engine for embedded use cases (and anything from Fabrice Bellard is always work a look).
So instead of a backend language transpiling to JavaScript (like clojurescript), I can just generate htmx markup.
I wish I had known about it when I started my most recent project a yard and a half who.
I am all in for something that keeps us on server side templates/HTML with dynamic capabilities.
I think we’re still suffering from “everything with React” syndrome, I’ve never used htmx but if it can help as an antidote I’m all in.
(Edited for typos)
It doesn't.
For example in the Java world and in the last 10 years there were only two "frameworks". Just Spring and Oracle/Java EE
Now two more have appeared (Quarkus and Micronaut) bringing the total number of frameworks to 4.
If the former, totally agree. If the latter, Java frameworks are plenty complicated.
Also not sure how you've missed a lot of other frameworks; Spring and J2EE are certainly not the only ones that have been around for 10+ years. Dropwizard, Vert.x, akka-http, GWT, Play... just to name a few.
Frontend wise it seems similar at a glance, in that you could use react for the last 10 years and be fine. Or you could jump from react to vue to svelte and make life hard for yourself.
As someone who came into webdev only recently with a long experience in just-development, it feels like there is no problem with frontend at all. The problem is that the frontend industry chose the worst perverted defaults and those defaults pay the most money. That’s the whole drama. It’s just a “five monkeys experiment” sort of situation.
This is what hypermedia architecture with true HATEOAS is all about. It feels like we took a wrong turn a decade ago, and we have been trying to reinvent rich clients with JSON RPC designs from the 90s. It has resulted in too much churn and complexity IMO.
I ask because I know ClojureScript is built on Google Closure which has pretty advanced dead code elimination (and typically for React functionality people use libraries like reagant that take advantage of this - I think?). Presumably with htmx users are having to download the whole htmx lib.
Still, in general, because we are generating fragments of hypermedia (HTML) on the server and returning that to target elements in the browser directly, the need for JavaScript directly in our code lessens dramatically. As you mentioned, though, you do need to include the hmtx JavaScript libary with a script tag. That library is very small though when min - something like 12k.
Amazingly, you can build some pretty dynamic web apps using htmx and sprinkling in a little vanilla JavaScript or _hyperscript if you are daring! The main key for us though is thinking from a hypermedia point of view with SSR; that coupled with the elimination of the complicated development configurations, is a huge win in simplicity and productivity. No more versioning our endpoints too!
The reason being, is we needed to do some code gen of Svg components in React Native, and in the build process (via shadow-cljs), the closure compiler was munging the definitions in the.
Off the top of my head an svgr component which looked like: // generated component import React from 'react'
function SomeSvg(props) { return React.createElement(..., ...merged props..., ...children...) }
was getting "optimized" to be used as: function $SomeSvg(props) { return $R.$c($$..., $$...merged props..., $$...children...) }
The above is psuedo-ish code, but I think covers the point. There were also some edge cases where not includeing a ^js reader macro before using js values caused crashes, due to the symbol being munged in the background. As a result, whenever we used any sort of js, we needed to mark it w/ a ^js reader macro, which began to get really noisey and error prone if you forgot. :simple has made the code based more stable. Could be user error on our end, but the fact that we ran into so much with :advanced and our application stabilized significantly after that one configuration change, it makes me think using :advanced can be too involved.
In addition (to an already winded comment), cljs needs to include the cljs runtime. I haven't benched the size, but it is no negligible (think in the 100's of KB), on top of what react/reagent/re-frame may bring in, you can easily end up w/ a compiled cljs app of > 1MB.
Why not clj-thymeleaf ? or even clojurescript ? this is very interesting that you find HTMX better than clojurescript - typically clojure devs prefer to stay within the lisp world for any markup.
ive been getting pushback in a java team against htmx. cos the value prop is unclear vs jsp or thymeleaf. Would love to hear ur perspective.
Also like bnert, when using Clojure we prefer to use a library on the server-side called hiccup to generate HTML from Clojure data structures instead of a template library. Either way, template engine or hiccup HTML generation, the principle is the same. You are coding everything on the server-side and rendering HTML back to the browser - something it is VERY good at rendering. :) As part of that HTML rendering, you are putting attributes on some HTML elements that htmx understands from the browser side.
ClojureScript is a totally different concept, as it basically allows a Clojure developer to write JavaScript in Clojure - it generates JS. With the server-side rendering model above, we still code 100% in Clojure, but we get to jettison a lot of the complexity of the ClojureScript model (Reagent etc), and development set-up. It is far simpler.
Other benefits of the hypermedia approach provided by htmx: 1. It is obviously true HATEAOS, so we don't worry about end-point versioning issues. 2. The barriers of the UI team and Server team negotiating and re-negotiating end-points is gone. We have all transitioned to "full stack developers" - sanely. 3. As mentioned above, it is language and server-side agnostic, so use what you prefer and have competitive advantage with using. 4. Simpler mental model. The team thinks in hypermedia terms from a server rendering perspective only. That lets you wrap your brain more easily around the business problems. 5. You have the full power and capability of your database at your disposal instead of going through and desigining the correct endpoints to get there first.
Hope that made sense
what im not sold on is HTMX specifically. im kind of wondering things like "hey what if we just used alpine.js with JSP or thymeleaf and skip htmx entirely", etc
what im getting incrementally with htmx is a bit unclear...while staying in the server side rendering of things.
W/ htmx + clojure, you can define your ui like so:
(def counter (atom 0))
(defn partial-count-markup [c]
[:span (str "Pressed: " c)])
; Handler for /partial/count
(defn partial-count []
(swap! counter inc)
(partial-count-markup @counter))
; Handler for index.html
(defn handler []
[:button {:hx-put "/partial/count" :hx-swap "innerHtml"}
(partial-count-markup @counter)])
And like that you have a page with a button that tracks a counter and updates the ui (I haven't tested this, YMMV).Also if it isn't clear, you can also keep all your markup as Clojure data structures, which means you can write an `html` function which has the common styles/scripts/etc.. necessary so you get a ton of re-use with an already similar syntax vs needing to learn a new templating syntax w/ its own conventions.
W/ clojurescript, to get the same behavior you need:
- clojurescript toolchain w/ some configuration of how you'll bundle/package it
- an idea of how you'll distribute your application (serve spa from same API service? S3/Object store? another web service? How to reconcile state?)
- an idea of how you'll reconcile state between local/server, if you want to go that route. If only local, nbd. If server, you add a handler and fetch data once SPA or cljs has loaded.
- and idea of what format you want to consume (JSON, HTML, XML,text) and then write the translation between that format and your markup.
etc...I hope the above answers your question, or at the very least offers another perspective.
As a quick postscript, I think it is underestimated how convoluted templates/templating engines are, given they have the tendency to implement their own language/semantics outside of the PL you're using. I have much respect for the authors of template engines/spec, the engineering that goes into them is impressive, however, most I have come across tend to be a leaky abstraction. Once I experienced writing markup as Clojure data structures, it ruined me for templates permanently. I don't want to go back to writing templates, and doing an assessment of "what language does this template engine implement, and is it simple/easy to learn?" is an exercise I do not miss.
Note: I've been Clojure/ClojureScript developing professionally for almost two years now and have debated most of the above internally during that time. Done some templating w/ JS, Go.
im not able to figure out what is it that htmx is saving you in the example above. with clojurescript, wouldnt you have written very similar code ? i mean all you are doing is calling an api. is it automatically doing conversion of JSON to ur DTO/business object. that cant be right can it ?
my mind is telling me that it is some kind of lifecycle management - like before/after hooks. make sure that the html loads after server is loaded, etc. is that what it is ?
But it does seem like this would be fun to play around with using PHP as your backend. No more JSON, or GraphQL, or bloated frameworks, just intermixed code and markup in the same PHP file...just like the good ol' days! For the situations where it does work, this could simplify the architecture of an app significantly.
Also, 10/10 meme page.
EDIT: are there any large and complex projects using this that are publicly accessible?
PS. I just believe people have to try it, because it's so much easier to do web apps this way.
https://htmx.org/essays/hateoas/
it really tries to focus on extending HTML & the original model of the web, rather than replacing it w/something else
good for some stuff, not good for other stuff
i like using events when I need to tie things together with some scripting:
I found HTMX a few months after, and had it doing AJAX in probably 10 minutes. It has been unbelievably easy to use and helpful.
https://lassebomh.github.io/htmx-sandbox/
It runs a small mock server inside the browser which is very Django inspired, so it should be very familiar if you have experience with it.
The mock server runs using PollyJS and Nunjucks as the templating engine. There is sadly no mobile support or the ability to save yet, but it's still a great place to start tinkering without needing a full setup.
So much about modern FE dev requires knowing stuff that was intentionally abstracted to purer forms years if not _decades_ ago and easy to pull off in a browser<>server relationship with a little bit of JS for state management or swapping content. The author even uses the phrase "AJAX," which I feel like I haven't seen used in earnest in years. This library is for the OGs.
Obviously, trade offs etc etc. If you're starting a new web based project, something that's just submitting forms and showing results, why would you really use anything more complicated than this? It's sorta hilarious that JSX took off; it shows that the declarative nature of HTML was right all along.
2. REST/GraphQL/gRPC API. Often, if you want to open up your API to internal or external users it's a lot easier if the frontend was already written to deal with protobuf or JSON endpoints. Seems like it would be painful to add an API to an htmlx backend.
Is there a good way to address these with htmlx?
Of course, the (potential) drawback is in using Elixir for both front-end and back-end which may be a tough sell to a client or employer.
-----
[1]: https://blog.appsignal.com/2021/09/28/real-time-form-validat...
[2]: https://hexdocs.pm/ecto/Ecto.Schema.html#module-redacting-fi...
2. So far in my experience a rest API for my own front end has been pretty specific, so I'd want to add a separate API anyway for that purpose.
I highly recommend using HTMX.
Also: How much has the backend's responsibility in pre-rendering grown in response to the move to HTMX?
Like: what's the point of using this over HTML5+js? I've spent 15 minutes reading the website and I still don't understand why I should want this, especially if I already know HTML/CSS/JS. All it seems to do is add a SGML DSL into your HTML, so you can be all declarative about `onclick` attributes.
I am sure I'm missing something - what is it?
You can have a page with a list of items. The page is one template, and it includes a sub-template which is just the <li> items. Then you have a separate view for "get list fragment" which just returns the updated/sorted/filtered <li>, rendered with that same sub-template. If you toggle the ordering, or filter the list, HTMX will automatically call the fragment renderer and replace just the <li> items, without reloading the page.
See this example: https://github.com/adamchainz/django-htmx/blob/8054f049f53f0...
This approach solves the common interactivity use-cases requiring JS in a server-rendered app, without having to write any JS, and without having to build a REST API. Instead you just render HTML, which your framework is excellent at.
The false dichotomy here is that it's either htmx or React. Why not... neither?
Typically, for a SPA: your backend would return some data, and your frontend would then figure out how to update your html in response to it.
For a multi-page-app: your backend will redirect the user to a whole new page with new HTML.
This is somewhere in between: Your backend will send just a little bit of HTML and you can update your existing HTML with it. It should feel like coding an MPA, but get the benefits of an SPA.
EDIT: Sorry, nevermind. The Bulk Edit example does work, I think it either does not like Safari, or something failed to load before, but it works in Chrome.
Also if you build stuff on top of hypermedia APIs then bye bye performance. It's probably fine for web pages, because you're competing with JS+JSON/REST frameworks that are written by people who have never heard the term "L2 cache", but I strongly suspect the right move is to drop the framework completely, not replace it with a new flavour-of-the-week thing.
Just write HTML5. It's fine. Add javascript for the 2-3 bits of interactivity you actually need. If you hate JSON, use protobufs. Your web page will work forever, because it'll sit on the actual API provided by the browser, and it won't make my laptop overheat when I load your site.
The price to pay for this is it does not work offline. For quickly prototyping and app it is very fast.
Why write any JS at all if you don't have to? Htmx extends HTML to replace the need for a lot of the JS that's out there. It gives you client/server interaction and dynamic page behaviours without a single line of code.
Sure you can do without it. You could always do it all in vanilla JS or jQuery, at the cost of debugging it yourself.
This is not obvious to me.
HTTP is named Hypertext Transfer Protocol after all. Exchanging schema-less JSONs is conceptually pretty lame. It's plain old RPC architecture. That's why we end up needing to write API docs to line up the frontend and backend, or otherwise inspect backend code to figure out what's being handed over.
Browsers are built to understand HTML. Whereas in the modern world we largely ignore that and write double the code we'd have to write otherwise passing arbitrary messages and it introduces a whole new set of problems making sure those codebases line up properly. Whereas HTML produced by the backend (properly) contains all the information someone could need.
Things that don't require request to server - managed by alpine, things that do require - by htmx. There are situations where both tools can be used, one has to pick.
I did make https://hyperscript.org, which is kinda in the same space but very different than pretty much everything else
What HTMX does should've been a W3C standard a decade ago, and I've my fingers crossed that some day I'll be able to do what I've done with jQuery and remove it.
Thank you for pushing the state of the art forward in a meaningful (and RESTful!) way.
One bit I've found slightly unintuitive is the swap and settled states. Took my a while to animate in a sidebar how I wanted. It was my fault for not groking it faster but perhaps some more examples or guides in that regard could be useful.
and here is a perfect place for you to contribute to the project! :)
As someone who is very interested in the space, are there other hypermedia implementation (if is the right word) that you found interesting? Or do you have some pointer for those who want to explore this space?
Personally I really want some kind of hypermedia framework for terminal application, since I often need to work with hardware (or bandwidth) where the only interface is a terminal, and I never feel productive with manually writing the application code, having to reimplement a new client every time.
Also thank you for htmx, I started using for a couple of project, and it really is refreshing. Especially kudos for the various example, that really helped making it "click".
maybe gopher as a prototype example?
adam stepinski (creator of hyperview) is one of the heroes of hypermedia in that he didn't just develop a hypermedia format but also developed a hypermedia client, and you need both for a proper hypermedia system
so, I would say... maybe you are lookin' at the person in the mirror who is going to make that terminal app hypermedia?
Sorry for the newb and lazy questions.
Is there any material on successfully combining htmx with web components? It doesn't look to me like the original design takes this possibility into consideration...
(note: at a surface glance it doesn't look to me like components are in conflict with hypermedia - one could treat them as a side channel that "enhances" the standard set of elements)
My components look like this. The JS and CSS is included in the application's bundles. Ruby renders HTML on the server side.
app/components/breadcrumbs
- _breadcrumbs.scss
- breadcrumbs_component.rb
...
app/components/search_field
- _search_field.scss
- search_field_component.rb
- search_field.js
- selections.html.hamlIt's not really Web Components as specified, but iirc those are generally pretty much JS-dependent? But hypermedia's reliance on server side rendering means that you can just move this logic to the server.
When I started using them, one of the hardest parts was to actually have it imported and built with webpack, since the site focuses on cdn and webpack example is really brief. Same goes for hyperscript
Do you see cdn as a preferred way of usage?
https://htmx.org/events/#htmx:configRequest
you could jam the outerHTML of the detail.target into the detail.parameters map, maybe based on the presence of an attribute...
he's me
I stopped scrolling as soon as I noticed I have to ask myself what am I posting to this URL?
Maybe digging deeper I could find the reference docs explaining in very vague terms where the payload is coming from. But this was supposed to be simple.
Simple doesn't mean "I should be able to intuitively understand this with under a minute of reading"...
hopefully over time the problem will subside
The most interesting part of it to me is that the mainstream has gotten so far out over to react and data payloads that the paradigm of "send some HTML and stick it in a container" is seen as revolutionary. Not to say that a well-designed library for doing this is a bad idea, just that... it's interesting to see how it's talked about.
I've been picking up Python for the past 6 months and adding as little as possible and slowly as possible. About 2 months ago I started building a Twitter client with HTMx + Flask and it's been nice. It now supports multilogins for Twitter and Mastodon, as well as Twitter Archive and RSS/Atom backends. Very similar to programming with PHP back in the day. Minimal JS, just a couple functions to integrate with my little Notes app that's not running HTMx (yet). Not trying to boast, just showing how far one can take it.
Using the server as an engine of state with the Facade pattern is something I've already become happy to do, having successfully used it for a pretty delicate use case involving multiple iOS App Store versions and a backend migration without impacting old App versions. I'm a little cautious about offline apps (SPA-domain), but have done a Service Worker demo and have some ideas to mess around with in mind. It's the one possible downside to not using JS (Node) on the backend that I can see: no shared code that can run on the backend and client/edge alike (but a language subset transpiler fills in the gap, eg. for validation/pure/non-mutant logic).
Documentation is pretty clear as well, I've been able to go a long way just reading it over and over and trying out features as I think of them.
I've normally extremely skeptical of anything like this, but HTMX just feels right when using it.
So while, the value proposition of HTMX for the larger nodejs world (or anyone using React) is "back to Server Side Rendering". I'm kind of struggling to build a value prop on the JSP/Thymeleaf side which is already popular.
So is HTMX a "better JSP" ?
Btw if you are interested in the reasoning of HTMX: https://htmx.org/essays/
I use it for http://golfcourse.wiki and I’d recommend it to others who prefer to avoid JS when they can.
I'm not sure if that's the best course of action, but I'm just a guy who's a bit embarrassed to really like to programming in python and playing golf.
It seems now the majority of commenters are positive about htmx now and I feel happy for recursivedoubts.
Most web applications today would benefit from using htmx more than JavaScript-heavy frameworks with the exception of applications like Figma and the like of it.
Moving from React to htmx - https://news.ycombinator.com/item?id=33218439 - Oct 2022 (315 comments)
An SPA Alternative - https://news.ycombinator.com/item?id=32148253 - July 2022 (138 comments)
Reimagining front-end web development with htmx and hyperscript - https://news.ycombinator.com/item?id=31949621 - July 2022 (207 comments)
Building a fullstack app with Flask and Htmx - https://news.ycombinator.com/item?id=30843275 - March 2022 (140 comments)
The Renaissance of server side rendering with Alpine and HTMX - https://news.ycombinator.com/item?id=30577181 - March 2022 (9 comments)
How a hypermedia approach can address usability concerns with multi-page apps - https://news.ycombinator.com/item?id=29693571 - Dec 2021 (128 comments)
Building a simple room-based chat application in Nim (using HTMX) - https://news.ycombinator.com/item?id=29594763 - Dec 2021 (28 comments)
Django, HTMX and Alpine.js: Modern websites, JavaScript optional - https://news.ycombinator.com/item?id=29319034 - Nov 2021 (274 comments)
Htmx 1.2.0 Release - https://news.ycombinator.com/item?id=26128229 - Feb 2021 (25 comments)
Htmx 1.0.0 Release - https://news.ycombinator.com/item?id=25232719 - Nov 2020 (58 comments)
Htmx – high power tools for HTML - https://news.ycombinator.com/item?id=23330881 - May 2020 (166 comments)
It is almost the kind of web application that I built in 2001 but I find it a lot easier to write today than I did back then thanks to my own maturity and numerous refinements to the web platform. The backend is written in Python and uses aiohttp without any "framework" other than some very basic stuff I write myself.
I worked for a company that had built something similar (that one was named "Themis", this one is named "Nemesis") that had parts written in Python, Scala and Typescript and we faced the problem of having to update three codebases whenever we made some change, having to deal with three kinds of "dependency hell", etc. It is all the more acute for an R&D project where adding a new task or model should be a day's work.
With very little Javascript I am getting performance close to a desktop application when my server is on localhost, it helps that I am paging out small screenfuls of data. I have to admit that a library like Htmx could be a big help for this kind of application but I've yet to really wrap my head around fitting it into my application.
At the end of the day, I want my backend API to be restful json(or grpc, graphql) any client can consume. I want the client to be written with as few dependencies as possible. Maybe I'm misunderstanding though. Put another way: suppose I have an existing backend REST API where input/output is json. Can I use htmx without any changes to the api or server-side code to translate json to markup?
https://htmx.org/essays/hypermedia-apis-vs-data-apis/
https://htmx.org/essays/splitting-your-apis/
Generally, I would recommend splitting your data and hypermedia APIs up, so you can take advantage of the features of hypermedia (the uniform interface) and still provide a good data API to clients that want it.
Also, this is pedantic, but a graphql JSON API is almost certainly not REST-ful, in the original sense of that term:
https://htmx.org/essays/how-did-rest-come-to-mean-the-opposi...
https://htmx.org/essays/hateoas/
More essays and other silliness here:
I know, it's a ridiculous thought, right? There's always going to be challenging work to do, but some people are entrenched in VueJS/React/Angular, and relish spending 10x the time building CRUD apps.
And there's no need, because there are multiple ways in HTML they could already express this without violating HTML. Here are 2 ways that work, and then the way they've done it which is in violation:
Possible method 1: Data attributes. Any time you as an author want to add additional data to an element, you are free to invent any custom attribute names you want, so long as they begin with "data-".
Possible method 2: Custom elements. Any time you as an author want to add a custom element to HTML, you are free to invent any tag name so long as it follows a simple naming criteria (essentially [a-z]+-[a-z]+) and if you invent any custom element, you are allowed to define whatever attributes on it you like.
Invalid method chosen: To pollute standard elements with invalid attribute names in violation of the HTML standard.
From the linked docs:
> It's worth mentioning that, if you prefer, you can use the data- prefix when using htmx:
<a data-hx-post="/click">Click Me!</a>After dealing with these issues for years I am really happy to return HTML from a backend API that uses a language where [1, 2, 12, 3].sort() return what I expect from a sorting function sorting integers.
That is all, and that is what htmx gives us. I am not sure if we can replace our UIs that are written in Vue for now but at least it I have a good time porting some of the web things to htmx + Rust from Vue + Python.
> Now any HTTP verb, not just GET and POST, can be used
requires care. I'm assuming HTMX is just a JS library that attaches all these behaviors to HTML. That's fine, as that won't change anything as far as cross-site scripting goes. It's just that if this were coded in the browser, and the responses to these requests made available to the page, then bad things would happen.
Also regarding this:
> Note that when you are using htmx, on the server side you typically respond with HTML, not JSON. This keeps you firmly within the original web programming model, using Hypertext As The Engine Of Application State without even needing to really understand that concept.
it's much easier to write server-side APIs that produce JSON and static HTML + JS that consumes it than it is to generate HTML (and then also JSON for non-browser apps).
On the whole I like this a lot.
What i like with htmx is that it's very easy to upgrade step by step a legacy SSR app. And the opposite. I mean it will be easy to switch to an other ajax lib or if w3c add similar functionalities in 10 years. To use htmx is mostly to replace whole pages with fragments, that's interesting even when we don't use htmx, sort of server side web components.
In other world, just try !
I'm fighting myself to don't give back examples to the community but i'm so exited to upgrade my very old legacy apps !
- Douglas Crockford, creator of JSON
https://www.youtube.com/watch?v=LRrrxQXWdhI
He mentions that "With HTMX we use a little bit of JavaScript so that we don't have to use any JavaScript."
OTOH React immediately clicked, as it is just javascript.
I've tried it both back in the Angular 1.0 days, but React came ahead both in the composability and maintainability departments.
HTMLX is great and all, but loading it from a CDN is insecure and wrought with problems. Namely, the JS file in question could be hijacked and replaced with a malicious version that can steal data. It's unlikely, but still a plausible scenario. Also: the resource could just 404 and not be available due to work-in-progress scenarios on the CDN side.
I would much rather host it on my own domain that I control.
<script
src="https://example.com/example-framework.js"
integrity="sha384-oqVuAfXRKap7fdgcCY5uykM6+R9GqQ8K/uxy9rx7HNQlGYl1kPzQho1wx4JwY8wC"
crossorigin="anonymous"></script>You can ensure you're always running the version of the script you validated, assuming you even validate the code (which presumably no one does).
You can and they encourage users to consider not using CDNs in production
For example if a user clicks a link in their email to /movies/2001, it does not know to show the movies frame with the 2001 content. We have to implement some kind of url parsing to determine what divs need updating and then update those on first page load.
If so, what do people pair it with on the backend?
I can see how this approach allows for much more dynamic pages with just static content, but what about when there is a database involved?
Also, what do people use for "published" static sites (I mean where the site is static once published, but the publishing step can pull content from dynamic source. Not sure the common terminology for that, but in this case, there tends to still be app logic to be developed, but it's just executed at publishing time rather than run time.) I'd imagine a publishing framework that is htmx-aware/friendly would be powerful.
Just curious how people round out the tech stack.
We call this the HOWL (Hypermedia On Whatever you'd Like) stack.
In most cases, this means your choice of backend framework in virtually any language, anything from Django to RoR to Spring to http4k to Laravel, plus some templating framework/language such as Handlebars, Jinja, Thymeleaf, Moustache, JTE, etc.
Really easy to use.
Why do you want that?
Sounds a bit like the hacky stuff PHP did in the first half of the 00s.
However, looking at some of the more complex features it starts to feel like they are not all suited to HTML, but seem more fitting to CSS. Perhaps, even, we need something akin to CSS but dedicated to coordinating events and requests? I’m not sure. I get the feeling Htmx is a big step in right direction, but I don’t think we’re quite “there” yet.
I also think JSON has its place (b/c it’s pure data), and wouldn’t be too quick to completely chuck it out in favor of an all HTML replacement (data with markup).
Very cool, I used intercooler for some stuff a while ago and I loved it, I'll have to check this out.
100% plain HTML, CSS, and JavaScript based components (zero attribute hacks or compiler tricks). No external libraries for basic functionality (state, data fetching, url handling—all included or relies on native browser functionality). All a part of one full-stack framework that works out of the box.
> It's worth mentioning that, if you prefer, you can use the data- prefix when using htmx
Why is this worth mentioning and what difference does adding the prefix make?
My biggest pain point lies with the docs, there are not many of them and it takes time to figure out what’s happening
For example, there was no simple way to understand that both libraries are loaded correctly, also it wasn’t immediately obvious where to use htmx and where to start using hyperscript
However, I believe in a better way if you have a better backend. This is what I'm working on as I build a "vertically integrated online board game studio" where I own the infrastructure, the clients, the frameworks, and now gaming runtimes.
I typically use mako for templates and htmx partials or components or whatever you want to call them would seem to cleanly map to mako includes - but I'm not sure how I might connect the render context which is typically collected for the top-level page in a tidy way.
Appreciate hearing others thoughts.
then you have alpine.js that is more a simple jquery for frontend js work.
if you do django, use htmx, I'm not sure how it works for other scenarios. For rich desktop like apps, SPA is still probably the only option.
How does htmx differ against hotwire and livewire?
For anyone curious, I used Quarkus & Qute templates for the backend and Bulma for CSS.
You'd be better off comparing https://mrujs.com to HTMX
Stimulus is a way to attach behaviours to DOM elements. It has no remote concept and no rendering concept. It's freaking awesome, IMO. But it has nothing to do with the problems HTMX wants to solve.
Doing it in htmx. Works great.
Burn the frontend js frameworks!