From React to htmx on a real-world SaaS product: we did it, and it's awesome!
Transcript
By David Guillot. From DjangoCon Europe 2022. Published October 15, 2022.
Watch the video Markdown version 3,059 words · auto-generated · en
Summary
David Guillot explains how his team replaced React with Django templates, HTMX, and Django Components in a production B2B SaaS product. HTMX handles server-triggered updates by fetching and inserting small HTML fragments, while Django Components combine templates, CSS, and JavaScript into reusable UI elements; this supports rich interactions without duplicating application state in the browser. The migration improved first-load performance, reduced client memory use, enabled lists of thousands of items without lag, increased team and product agility, added tests and cleaner code, and removed about 15,000 lines of code. He argues that HTMX is a strong choice for small teams that value fast end-to-end feature delivery, while teams requiring strict front-end/back-end separation may prefer a different architecture.
Transcript
Automatically transcribed, so expect mistakes in names and technical terms. Filler words are removed.
0:00 Introduction to HTMX
Well hello everyone. I hope you can hear me. My name is David Guillot, I'm from France, as you can hear. I've been in the web industry for 15 years. I've been a Django developer for five years. I work at context. Yes, it's the French word for context. And I'm here to talk to you about an experiment we we've been running at my work It's about replacing React with HTMX on a SAS product that was already in production. And it's a good experience. So this talk is about realizing what is possible when it comes to
A modern UI, a rich UI. on the web. So I'll start by breaking some misbeliefs about front-end development. I will show you our UI, I will show you some code. Then I will tell you our story and finally I will share some thoughts and learning about all this. So this is the cover of HTMX Twitter account. It's Creative Commons, by the way. You've heard for too many years now that for a reach UX on the web
1:36 Rich User Interfaces Without React
you need you have to go the React way. You have to build an API and build a single page app and you will have everything you need. Probably the reality is it's a bit more open than that. Because what makes a UX rich? Probably there is something about making the user forgetting they are looking at a web browser. For example, when the user acts on your interface and they changes the application state. You want to react to this change and you don't want to reload a full page because it's so
20 uh05 Another part is you want to have some beautiful UI elements, smooth, reliable, reusable, accessible, like the ones you've got on your phone. And you've heard that to get all this you need to go the JavaScript application framework way, like React or something else. But when it comes to reacting to application state changes, you will hear that you need an application on the client side to duplicate the application state on the client side to
be able to react very smoothly Probably there are other ways and HTMX is one of them. So HTMX, what is it? Basically it's listening to JavaScript events in order to fire some Ajax calls, getting some HTML fragment and inserting this HTML fragment into the DOM. That's it. When it comes to web components, because that's I will that's what I was talking about when I said rich UI elements The best way, the design pattern to get rich UI elements
is to build isolated reusable components that that are pieces of HTML code, CSS code, and JavaScript code. that will work together in order to get some beautiful UI element that you will be able to reuse anywhere in your application and even in other applications. And we were told that in order to achieve that, we need JavaScript application frameworks that force you to think component oriented. and to get web components. Now is it possible on the server side in Django world? Yes it is. This guy
4:44 Production UI Demonstration
implemented some library that that allows you to get web components within Django templating and I will show you some code about it. Now it's demo time. I will show you our UI in our product and production. It's a quite sophisticated screen that I will show you when it comes to UX. I will present it briefly first and then I will Dive into two features I want to show you to illustrate application state changes and UI elements
So there it comes. So this is a screen that is quite central in our user experience. At the top you've got a navigation timeline. So it's a series of steps when you Click on one of them, you get a beautiful animation, you get some stuff that reloads on the bottom of the page. I can close that for now. On the right column you get a text. It can be a huge text, but it's just text. The rendering is very quick. This text is split into articles. The articles can be seen
just when opening the table of contents. I can mark an article as favorite. When I close my table of contents, my article has been marked as the favorite here. Is it client-side application state management? No, it isn't. It's HTMX. It's just a JavaScript event triggering some very, very located reload On the left side you've got a list of items that are related to the text. This list of items can be quite huge. Here we have more than 1,000 items in this list. It's huge
and it works, it doesn't lag. And this list can be filtered For example, by articles, there are two ten items related to article two. Easy HTMX stuff I can also search something. They all match. And let's reset. Now let's dive into one feature that illustrates quite sophisticated application state management.
that you could think it is at least. So in this face set filter I have all my articles with counters of results And at the top of the of the filter I have a special bucket here which is all the articles I'm interested in. All the articles I marked as favorite. So for now I have only article one and article two as favorites So the counter is 127 results, because it's 117 plus 10. What happens if I'm not interested in article first anymore?
8:34 Django Components
Just here, the counter has been updated. Is it some client-side application state management? No. Is there some JavaScript for achieving this? No. Zero line of JavaScript. This is just HTMX all the way. Do you want to see how it works? Let's dive in. So this is the Django view for the watch button. I hope this is readable on big screen. Obviously I have a post method and when I build my HTTP response with the star becoming yellow or green according
According the post or delete method. Let's join this HTTP header, which is a special HTTP header that HTMX understands on the client side. Now my small counter here, actually it's the whole line in my HTML template, which is here, it's just an empty div With a bunch of HTMX attributes, and this MTDiv is about to get its content from the server on page load and also
when an article is watched and when an article is unwatched. This is quite simple, isn't it? And then you have application state management without writing a single line of client-side code. The second feature I wanted to show you is about rich UI elements. Actually you've seen them already. It's these face ed filters. We have three of them It's actually a quite sophisticated UI element because it's a custom drop-down. You've already did probably such things. I can close it when I click anywhere, I can close it by hitting escape.
There is some internal search Oh, there is no article I mean not an article one and article nine for example This is some JavaScript, of course, because it is client-side pure user interaction. But these elements are actually web components made on Django side. So I have a face set template which is a regular Django template that will take some input, the buckets, the values,
what the user selected, etc. And this face set, this HTML template is working with in my static folder, components, facet, I have some CSS code. And I also have some JavaScript, for example, to handle the internal search stuff. We use stimulus on JavaScript, but you can use whatever you want. And all of this is working together through Django components and Django components allow you to declare a component here that make
that bring together the template, the CSS and the JavaScript, and that makes you able to pass some data into it. And this is how you achieve UI web components on server side with Django components. And how do you use it? I will open my base template again and here it goes we were there two minutes ago you have your component And so it's really completely reusable. I have another one here for another field in my data.
And I pass the buckets into it, I pass whatever you the user selected in order to mark them This way and I have slots, slots allowing me to do special stuff to Customize for example this filter you've got some color dotside the values aside the bucket names Here it goes, I have my slot and I have the possibility to customize some stuff in there. So it's reusable, it's isolated.
We can unit test it if we want, we can upgrade it, add some features independently from its usage in our application. And that's the whole point of web components. So that's the end of the demo part. I hope at this point you are convinced that Yes, our UI is quite rich, offers a rich user experience, and no our code is not spaghetti code because we don't have code. HTMX does everything, almost everything for us. Now, about our experience at context. The road wasn't always like this. The project
14:44 Migrating from React to HTMX
this project started in 2017. It's a quite complex project, especially on the back-end side. There are many domain complex domain stuff. But on the front-end side, in 2017 we were a very small team. It was our first SaaS product. And we asked around us, how do we do that? And everybody told us the same thing. It's 2017 guys, go the React way build an API, build an SPA, and you will achieve client-side application state management, you will get separation of concerns
Between your back end and your front end, you will get web components. It's the modern way to go. So we tried it. We hired a JavaScript developer. I was in charge of the API among other things. And It was quite of a kind of okay. We were able to deploy it to production. But the UI vas was very, very slow. Because the API contract was not optimal because the dumb tree was way too deep and as you
as you have seen we have lots of information to put into the browser And we had very low team velocity and that was a big problem because we were trying to launch a product. We were trying to have people buy our product so we needed so to be agile about it. And there were no code quality on the front end side because the JavaScript developer was alone and he was overwhelmed with all the complexity of making an SPA. So when I heard about
Phoenix LiveView in the Elixir world, when I heard about Hotwired in the Ruby world, when I heard about Unpoly HTMX I had an idea, I created a proof of concept, I duplicated our UI, which was all React, I duplicated it with several rendered Django templates and just a bit of HTMX. In one month of work, the code was ugly as hell, but the proof was there. It was working, the performances were good. Let's refactor it. One month later we had web components, we had code quality, we had
we even had tests that we didn't have before on the on the React world. So Consequences on the UX were there some negative consequences on the UX? No, we made no trade-offs. I can't think of a of a feature we used to have and we don't have anymore And it open it even opens some new possibilities, some new opportunities. For example, on the left column you've seen earlier, which has many, many, many items on the React world, the dumb tree was so deep and so heavy that This column
was able to display only 50 items at a time and so you had to scroll, to display, to replace the 50 items, etc. It was terrible. Now that we have a quite clean and as light as possible dumb tree We are able to display thousands of elements and no lags. What about webperf? We measured time to interactive On three scenarios, Django and React, Django API and React on first load, then on second load. And with HTMX. HTMX is
18:48 Performance and Team Impact
at least as fast as the React second load. On first load is way faster. And it happens that in our use case, users often get to our UI through a diplink that they receive by email. So the first load is really important to us And React was just not the right fit for it. The memory usage on the client side is also very lower, much lower. So this is good news because we will We will stop hearing our users crying for a new laptop every six months, which is good for the planet. What happened to the team?
Obviously, the JavaScript developer left because we didn't need here anymore. But the other two, we were quite stuck in our backend only role. The most beautiful demonst demonstration I did after each sprint was an API endpoint with JSON. Now we are full stack and we are able to collaborate with designers. and also obviously continue working on our compli complex back end issues
but We have a much wider scope and this is much more exciting as a developer, at least from my perspective. And this allowed also us to explore new project management methods because we are now able to make the developer in charge of a feature from the beginning to the end by exploring data, by exploring user scenario, working with the developers It's great. I don't even need to comment this slide, I let you enjoy it.
That this is these are lines of code in our code base. We deleted fifteen thousand lines of code Now, when it comes to you because I'm a bit here to convince you but I'm here to give you all the keys all the keys I think you need to make your own decision You need to ask yourself what do we need more as a team, as a as a business? Because
21:41 Choosing a Front-End Architecture
in the beginning of this presentation I told you that people who recommended React to me talked about reacting to application state management. Okay, this is over. They talked to me about web components. Okay, this is handled. They talked to me about separation of concerns and from that perspective with HTMX we have less separated code base and to me it's a good thing because i like to be a full stack developer but If your team is used to separation, heavy separation of concerns between back-end and front
end, maybe HTMX is not the best way for you. But what HTMX gave us is product agility. We are now able to ship much faster, much, much faster. We have no more endless debates about API contracts etc so if product agility is A big thing for you. Maybe you should consider HTMX. Another question is, what is front-end development? If some of you hate CSS, maybe
maybe you should consider an architecture that allow you to focus on the back end and let other people work on the front end. But that doesn't necessarily mean React or JavaScript application framework. But if you think that front-end development is about taking some data from the database and exposing the data to the user in a beautiful way, make the users interact with your data. and saw store the data into the database, maybe HTMX is the way to go. I'm I think I'm done almost. Here are the key takeaways for
24:43 Key Takeaways
for you. The first one if is if you had some doubts about HTMX, does it work on a real project, a real product? Yes, it works. People are paying for the UI that you've seen earlier, so it works. For a small team like us, on a B2B SaaS product It makes us very happy. So if you're in the same position or in if you're in a similar position, maybe it would make you happy. And the most important thing is that with HTMX, with Django components as well, now you're able to make your own choice
about What architecture will we implement for our front end? This time I'm really done I would like to give a special thanks to my colleagues at Context. I'd like to tell you that this awesome company gave me the opportunity to work on this presentation in my working time my working time so this is great And the people you see here gave me feedback about the presentation. They helped me rewrite it three times but it was great. And the girls of the tech team took care of
our tech stuff while I was working on this presentation. So this is good. And thank you for your attention. You can't find me anywhere else than on GitHub. I have no social network. And I have one more thing. If there are if there are some French here. We are hiring so let's meet later