October 7th, 2024

Building a Single-Page App with Htmx

Jake Lazaroff explores htmx for developing single-page applications, highlighting its client-side operation, offline capabilities, and IndexedDB for data retention, while noting challenges with service worker support in browsers.

Read original articleLink Icon
Building a Single-Page App with Htmx

Jake Lazaroff discusses the development of a single-page application (SPA) using htmx, a framework that aims to simplify web development compared to traditional SPAs like React. He highlights the concept of a Hegelian dialectic in web applications, where traditional multi-page applications (MPAs) and SPAs converge into a new form of hypermedia-driven applications. Lazaroff created a simple todo list app as a proof of concept, which operates entirely on the client side after the initial page load, utilizing a service worker to manage network requests and business logic. This approach allows the app to function offline and retain data between sessions using IndexedDB for storage. While service workers offer advantages, such as improved offline capabilities and state retention, they also present challenges, including poor developer tool support and limitations in certain browsers. Despite these hurdles, Lazaroff successfully demonstrates the functionality of his htmx-based SPA, emphasizing the potential of htmx to provide a more straightforward alternative to complex frameworks.

- htmx aims to simplify the development of single-page applications compared to frameworks like React.

- The application operates entirely on the client side after the initial load, using a service worker for network requests.

- IndexedDB is utilized for stateful data storage, allowing data retention between sessions.

- Service workers enhance offline capabilities but come with challenges, including limited support in developer tools.

- Lazaroff's project serves as a proof of concept for the effectiveness of htmx in building SPAs.

Link Icon 27 comments
By @swyx - 6 months
imo any experienced frontend framework person will be able to pick out the issues with this impl. OP is returning entire strings of HTML (App, Todo, Icon) to rerender on state changes. this works when 1) you dont care about keeping UI state on the parts that are replaced (incl any stateful children of the UI element that happen to be there in your DOM structure), and 2) when you dont have to update the app in any other places when your state changes. go ahead and build your whole app by replacing innerHtml, frontend frameworks will be right here when you get back from speedrunning the last 10 years.

in other words, this todo app is just about the most complex of a frontend you can easily* build with htmx, i'm afraid. try to fix either 1 or 2 and you end up building components and your own little framework.

as an exercise to demonstrate, try taking OPs code and adding a count of todos on each All/Active/Completed tab that should update every time u add/edit/delete the todos. see how much extra ui code that takes. compare with equivalent [framework of choice] impl (in most it will just involve 1 state update, thats it). this is htmx's explosion of complexity that makes it not [ optimized for change ] (https://overreacted.io/optimized-for-change/). code that is hard to change eventually calcifies and consumes code that is easy to change if you do not consistently garbage collect (nobody does)

i bought the hype too until i tried building something nontrivial in htmx and im afraid the aforementioned islands of interactivity you can build are very very smol islands indeed.

happy to revisit my opinion if there are componentlike design patterns in htmx i am not aware of.

*emphasis on easily; with enough elbow grease u can do anything ofc. but then you fall out of htmx's very narrow [ pit of success ](obligatory codinghorror dot com link)

By @extr - 6 months
I am a backend/ML engineer and tried using HTMX to create a website with:

* Search box

* Typeahead

* Instantly updating search results

It was super instructive. In the end, I realized HTMX was probably not the best tool for that job, but it really helped me bridge the gap between "I get in theory why we use JS on the FE" and "Ah, I can see why client side JS is the obvious choice for this".

By @rutierut - 6 months
I see a couple of people here bashing on the practicality of this project. That's obviously not the point, it's an interesting weird use case that's more explorative/educational than practical. I thought it was an interesting and inspiring read!
By @davidedicillo - 6 months
FWIW, as a hobbyist developer who never had a chance to learn React, I found HTMX really helpful to make my Flask projects more reactive without adding much complexity.
By @turtlebits - 6 months
IME, Htmx is best for enriching server side rendered (non-SPA) apps. I'm having an great time with single file web apps using FastHTML (python).

I've rewritten a bunch of my JS framework apps for simplicity and don't miss much.

By @sgt - 6 months
I'm not sure about this approach but it does look interesting. I think it'll work fine.

My method is however to use Django and templates to build a regular MPA, and then switch out link changes (between pages) with htmx functionality so there is no browser reload. At least then you'll have a webapp that acts mostly like an SPA.

Next up, you can add more interactivity using htmx as much as you want (with some kind of Django components, ideally). You can even add VueJS to one of the pages if you want, but full blown SPA frameworks tend to eat into development time, so rather not unless absolutely needed.

By @leephillips - 6 months
I don’t know if the article describes the best use of HTMX, but when I wanted to experiment with building an interactive physics application on the web that used Julia on the backend, it was a perfect fit. I could design the whole page without any javascript (almost):

https://lee-phillips.org/pluckit/

By @koolala - 6 months
I hope we build Single-page apps with iFrames one day like how the web originally used <frame>. Everyone only talks about iFrames as if their only purpose is cross-origin content and <frame> is forgotten.
By @uhtred - 6 months
I think 68% of SPAs don't need to be SPAs. The end user doesn't care if the browser page refreshes if it is quick.
By @JodieBenitez - 6 months
It's already bad enough that too many devs think only in SPA mode, now you're trying to use the wrong tool for the wrong solution !

Fun experiment though, in the true "hacker" spirit ;-)

By @altbdoor - 6 months
I made a somewhat similar prototype by mocking XHR instead of service workers, in https://stackblitz.com/edit/typescript-h3wfwx?file=index.ts

It was fun for a bit to quickly experiment with how htmx works, but I find it difficult to scale in terms of state.

By @zoezoezoezoe - 6 months
HTMX is an idea that I never understood. HTML over the wire sounds like the most egregious idea I could have ever thought of. If you want a sub par SPA experience just build an SPA with PHP ;), and if you want a real SPA that doesnt have 600 ms of latency when I click a button that for some god forsaken reason calls an api endpoint, use React or Vue or Svelte like we always have. Is it perfect? Trust me, as someone who is trying to build something that delivers an actual SPA experience with SSR without Node.js, it's not, but a real SPA is miles better than streaming HTML over the wire.
By @VagabundoP - 6 months
For my own hobby useage - ie apps I shared with a few people - htmx is a godsend. It lets me focus most of my time in python/flask.

I do have a slightly more complicated TTRPG live character sheet that I use for a game I run. Its got five users at once and sqlite backend. Its gotten moderately more complicated over the last year and now sports htmx, discord integration, and will soon have SSE for me to push events to their character sheets, like ability point refreshes etc and other fun stuff without them having to refresh the page all the time.

I'm just thankful I don't have to dive into JS frameworks and can mostly stay in html, maybe a little hyperscript.

By @pier25 - 6 months
I'm thinking of starting a project with HTMX and islands of interactivity (probably web components). Anyone used this pattern in production?
By @v3ss0n - 6 months
This is really bad abomination .. please keep SPA to SPA Frameworks and leave HTMX out of it..
By @BiteCode_dev - 6 months
Which means offline htmx is possible, althought I wouldn't love to maintain that.
By @maddalax - 6 months
imo htmx is good but its a bit too low level to use directly, you sort of have to rebuild all the features frameworks like React have, such as components in your server programming language.

I'm building https://htmgo.dev to do that with go + htmx, and http://fastht.ml seems like a good contender for python

By @baggachipz - 6 months
"Your scientists were so preoccupied with whether they could, they didn't stop to think if they should"

At that point, maybe stop fighting with service workers and simply use a framework like Vue. It allows html templates to be swapped in, in much the same way. Except you can actually debug it and store in localStorage if you desire.

By @Bengalilol - 6 months
I wanted to send a UX feedback : Multiline text shrinks the 'x' button (the more text, the more shrunk the button gets).
By @sinanisler - 6 months
it's sucks man who even uses this useless thing btw https://swag.htmx.org/products/htmx-sucks-mug
By @synergy20 - 6 months
seems to me a stretch for what it's best for and designed for
By @pyzn - 6 months
Seems like lots of code for a todo list
By @davedx - 6 months
Htmx - the hipster tech of the decade, honestly
By @nsonha - 6 months
"Simple"
By @tzahifadida - 6 months
Was wondering if any1 embeds react spa apps in golang binaries to build a lean SaaS?
By @kccqzy - 6 months
Anyone who says React has mired developers in complexity needs to go back ten years and use jQuery to build a single-page app and remind themselves how much less complex React is. I know that because I rewrote a jQuery-based SPA in React back in 2015 and that experience was so revelatory that I will never forget it.

It is then that they will realize complexity comes from single-page apps. Pushing back against that complexity should result in not building SPAs, which is where Htmx comes in. There are still plenty of very successful web apps that are not SPAs.

If a product owner asks to build a SPA, a developer's job is to stop and ask why. Most likely what the product owner really has in mind can be accomplished without a SPA.