Remix.run Logo
fvdessen 3 days ago

HTMX is indeed simple and lean, but unfortunately that doesn't mean solving common frontend problems with HTMX is simple either. After a decade of frontend dev and being tired of react/vue/etc, I tried HTMX, wanting for something leaner, but IMHO it has big problems, and I am back to react/vue.

The two biggest problems with HTMX is that being fully server side controlled you need to put the whole app state in the URL and that quickly becomes a nightmare.

The other is that the code of components is split in two, the part that is rendered on the first time, and the endpoint that returns the updated result. You need a lot of discipline to prevent it from turning that into a mess.

The final nail on the coffin for me was that the thing I wanted to avoid by picking HTMX; making a rest api to separate the frontend and the backend, was actually a good thing to have. After a while I was missing the clean and unbreakable separation of the back and front. Making the rest api was very quickly done, and the frontend was quicker to write as a result. So HTMX ended up slower than react / vue. Nowadays react/vue provide server side rendering as well so i'm not sure what Htmx has to bring.

withinboredom 2 days ago | parent | next [-]

> you need to put the whole app state in the URL and that quickly becomes a nightmare.

You should be doing this anyway ... it's so annoying when my wife sends me a link at work and it just goes to a generic page instead of the search results she wanted to share with me. She ends up mostly sending me screenshots these days because sharing links don't work.

Cthulhu_ 2 days ago | parent | next [-]

Depends on what the commenter means by app state. Anything bookmarkable - like search results - should be in the URL, but "state" should not (I consider things like partially filled in forms, shopping carts, etc to be state).

PaulHoule 2 days ago | parent | next [-]

You have choices, especially all the choices that 1999-style web applications have.

The shopping cart can be kept on the back end and referenced by an id stored in a cookie.

You can keep partially filled out forms in hidden form variables and can send them back in either GET or POST.

Not all requests require all the form data, for instance my RSS reader YOShInOn is HTMX based -- you can see two forms from it here:

https://mastodon.social/@UP8/114887102728039235

in the one at the upper left there is a main form where you can view one item and evaluate it which involves POSTing a form with hidden input fields but above that I can change the judgements of the past five items by just flipping one of the <selects> which needs to only submit the id and the judgement selected in the select. I guess on clicking one of the buttons in the bottom section I could redraw the the bottom section, insert a <select> row at the bottom of the list and the delete the one at the top, but it just redraws the whole form which is OK because I don't have 200k worth of open graph and other meta data in the <head> and endless <script> tags and any CSS other than bootstrap and maybe 5k of my own, which all caches properly.

naasking 2 days ago | parent | prev | next [-]

> Depends on what the commenter means by app state. Anything bookmarkable - like search results - should be in the URL, but "state" should not (I consider things like partially filled in forms, shopping carts, etc to be state).

Yes, shopping cart state should be in the URL in the form of a server-side token under which the cart state is stored. Ditto for partially filled in forms, if that's something your app needs.

All page state should be transitively reachable from the URL used to access that page, just like all state in a function in your favourite programming language should be transitively reachable from the parameters passed into that function, eg. no global variables. The arguments for each are basically the same.

foobarbecue 2 days ago | parent | prev [-]

Presumably you mean search query input, not search results, right?

LeFantome 2 days ago | parent [-]

I think you are saying the same thing. They mean that their wife is trying to send them search results. You are pointing out that the link would contain the search query.

foobarbecue 2 days ago | parent [-]

Not really same thing. If you share the search query, you'll be re-submitting the query at a later time, so the results are likely to be different when the receiver uses that URL. It would be possible (but pretty terrible) to embed the actual search results in the url.

A more reasonable implementation of sharing search results would be to store results on the server and have a storedResults id key in the url.

fvdessen 2 days ago | parent | prev | next [-]

That is just the location of a page, and indeed that should be put in the URL. But a modern js app has much more state than that: partially filled fields, drafted documents, scroll position of lists, multi-path navigation history, widget display toggles, etc. etc. In react/vue you have 'stores' to hold and manipulate those info. In HTMX you need to choose between the url, the session cookie and the dom. And what you can usually keep just keep in the dom with client vue/react can't be with HTMX since the backend needs to be aware of the state to correctly render the new widget.

withinboredom 2 days ago | parent [-]

Are people really reimplementing browser history in JavaScript? Why are people implementing “navigation history”??!!

All of this stuff needs to be stored on the server anyway… otherwise how will you get it back on the page when I switch computers or pull it up on my phone.

fvdessen 2 days ago | parent | next [-]

Because app navigation is not linear like the url history. Think of a popup with tabs within a page. There's the navigation within the popup, and the navigation within the page. When you close the popup, you don't want 'back' to bring the popup back, you want it to go to the page before the popup. This is hard to replicate with just urls and server side rendered html.

Also you don't want that store server side because there can be multiple parallel tabs and you don't get notified server side when the tab is closed to properly cleanup the associated resources.

withinboredom 2 days ago | parent [-]

This is why we are cooked. Just because you can, doesn’t mean you should.

?tab[0]=/some/url&tab[1]=/some/resource&activeTab=0

Bam, you have tabs in the url. I can duplicate the tab, share my view, or whatever. Assuming the other user/tab/window/profile has access to these resources, it’ll show exactly the same thing. I can even bookmark it!

You can even add popups:

?popupModal=saveorleave

This state probably won’t be applied on an entry, but what’s great is that pushing [back] in the browser is the same effect as cancel! If you click “leave” then you do a “replace state” instead of a “push state” navigation, so the user doesn’t go back to a modal…

This was, at one point, decently standard logic. Then people who don’t know how browsers work started creating frameworks and reinventing things.

I digress. I’m just so glad I left the front end 15 years ago, I’d lose my shit if I were dealing with this kind of stuff every day.

javcasas 2 days ago | parent | prev | next [-]

Scroll positions of lists, toggleable widget status, partial form fills.

You say all of that needs to be stored in the server?

That is how you make a big server crawl with just 100 users, regardless of the programming language of the backend.

naasking 2 days ago | parent | next [-]

> That is how you make a big server crawl with just 100 users, regardless of the programming language of the backend.

This is a myth, servers and round trips can be way faster than most people seem to believe: https://www.youtube.com/watch?v=0K71AyAF6E4#t=21m21s

That's thousands of DOM updates per second being sent and rendered at 144fps.

withinboredom 2 days ago | parent | prev [-]

Those particular things are already handled by the browser.

Even mobile browsers handle this just fine: https://share.icloud.com/photos/022RMgNZWot7w6AXurHPKC_Nw

joseda-hg 2 days ago | parent | prev [-]

Depending of your flavor of SPA framework, Browser History might not work because there's no actual page change

Some will manually push a History entry, but not all

threatofrain 2 days ago | parent | prev [-]

For easy apps, sure, like shopping carts and search results. What about all the other apps?

bookofcooks 3 days ago | parent | prev | next [-]

Yes, this was what I wanted to get across with the article (although I utterly failed to do so). I think I would stick to HTMX for other reasons (which I've made clear in my top-level comment on this thread), but I now I see it an occasional tool to use for something simple and not long-term.

> The two biggest problems with HTMX is that being fully server side controlled you need to put the whole app state in the URL and that quickly becomes a nightmare.

Or you can create a large session object (which stores all the state) on the server, and have a sessionId in the URL (although I'd prefer a cookie) to associate the user with that large session object.

2 days ago | parent [-]
[deleted]
chrisandchris 2 days ago | parent | prev | next [-]

> HTMX is indeed simple and lean, but unfortunately that doesn't mean solving common frontend problems with HTMX is simple either.

Then it is not simple (as in simple I understand it) it's just the same we already have, reinvented.

If it would be simple and lean, it would solve the most common problems by itself (like I don't need to care about the HTTP part in .Net - it's a one liner and the framework solves it for me).

mpweiher 2 days ago | parent | prev | next [-]

> The other is that the code of components is split in two, the part that is rendered on the first time, and the endpoint that returns the updated result.

Yeah, that also bothered me. To me it looks like the page (template) should fetch that partial from the same endpoint that will deliver the partial via the wire to HTMX.

L3viathan 2 days ago | parent [-]

You can do that if you want to. It'll just be slower.

mpweiher 2 days ago | parent [-]

Will it?

I haven't gotten around to it yet, but my plan is to use in-process REST with Objective-S, so that accessing the internal endpoint will be the cost of a function call.

The HTTP wrapper for external access is generic.

jgalt212 3 days ago | parent | prev | next [-]

Server side rendering requires JavaScript on the server. So then you very often, but not always, violate "the clean and unbreakable separation of the back and front."

colejohnson66 2 days ago | parent [-]

Not necessarily JavaScript, but some kind of rendering or templating engine. As shown in the blog post, Go works.

jgalt212 2 days ago | parent [-]

right, but the person I was responding to mentioned JavaScript frameworks.

> Nowadays react/vue provide server side rendering as well so i'm not sure what Htmx has to bring.

nsonha 2 days ago | parent | prev [-]

> HTMX is indeed simple and lean, but unfortunately that doesn't mean solving common frontend problems with HTMX is simple

I think it should be obvious that if a software is easy and convenient to the author to write (simple and lean), then the complexity falls onto the users.