| 1 | --- |
| 2 | title: "Is htmx Just Another JavaScript Framework?" |
| 3 | description: "Alexander Petros give a thoughtful exploration of htmx's relationship to traditional JavaScript frameworks, examining how its HTML-first approach and narrow focus on network requests sets it apart. He argues that while htmx exhibits framework-like qualities in how it shapes application architecture, its deep integration with HTML's native capabilities and lack of dependencies makes it a more sustainable choice for building long-lasting web applications." |
| 4 | created: 2024-01-10 |
| 5 | authors: ["Alexander Petros"] |
| 6 | tags: ["the-case-for-hypermedia"] |
| 7 | --- |
| 8 | |
| 9 | One of the most common criticisms of htmx, usually from people hearing about it for the first time, goes like this: |
| 10 | |
| 11 | > You're complaining about the complexity of modern frontend frameworks, but your solution is just another complex |
| 12 | > frontend framework. |
| 13 | |
| 14 | This is an excellent objection! It's the right question to ask about *any* third-party (3P) code that you introduce into |
| 15 | your project. Even though you aren't writing the 3P code yourself, by including it in your project you are committed to |
| 16 | understanding it—and refreshing that understanding if you want to upgrade it. That's a big commitment. |
| 17 | |
| 18 | Let's break this criticism down into its constituent parts, and determine exactly how much htmx indulges in the harms it |
| 19 | claims to solve. |
| 20 | |
| 21 | ## The difference between a library and a framework |
| 22 | |
| 23 | Some htmx defenders jump to our aid with: "htmx isn't a framework, it's a library." This is probably incorrect. |
| 24 | |
| 25 | "Framework" is a colloquial term—there's no hard rule for the point at which some third-party code evolves from a " |
| 26 | library" into a "framework"—but we should still try to define it. In this context: |
| 27 | |
| 28 | * **Library** - 3P code whose API does not significantly influence the rest of the application |
| 29 | * **Framework** - 3P code whose API dictates the overall structure of the application |
| 30 | |
| 31 | If you prefer metaphors: a library is a cog that you add to your machine, a framework is a pre-built machine that you |
| 32 | control by customizing its cogs. |
| 33 | |
| 34 | This distinction, fuzzy though it may be, is important because it describes how easily some third-party code can be |
| 35 | replaced. For example, a JavaScript service that uses a CSV parsing library can probably swap in a different CSV parsing |
| 36 | library without too much trouble; a JavaScript service that uses the NextJS framework, however, is probably going to |
| 37 | depend on NextJS for its entire useful life, since an enormous chunk of the code is written with the assumption that it |
| 38 | is interacting with NextJS constructs. |
| 39 | |
| 40 | Therefore, if your service is built atop a framework, its useful lifespan is tied to the useful lifespan of that |
| 41 | framework. If that framework is abandoned, or despised, or otherwise undesirable to work on, the difficulty of modifying |
| 42 | your project will steadily increase until you give up modifying it, and eventually, mothball it altogether. |
| 43 | |
| 44 | That's what people are worried about when they ask is "is htmx just another JavaScript framework?" They want to be sure |
| 45 | that they're not committing to a system that will be obsolete soon, like so many of the past web development frameworks. |
| 46 | |
| 47 | So: is htmx a framework? And is it going to be fast made obsolete, leaving a trail of un-maintainable websites in the |
| 48 | wake of its meteoric demise? |
| 49 | |
| 50 | ## htmx is (usually) a framework |
| 51 | |
| 52 | With apologies to our community's ongoing debate about this question—I think htmx is pretty clearly a framework, at |
| 53 | least in the majority use-case. But it does depend on how you use it. |
| 54 | |
| 55 | Wherever you make use of htmx in your project, you're including htmx attributes in your HTML (i.e. `hx-post`, |
| 56 | `hx-target`), writing endpoints that are called with htmx-formatted data (with certain request headers), and returning |
| 57 | data from those endpoints that is formatted in ways that htmx expects (HTML with `hx-*` controls). All of these |
| 58 | attributes and headers and endpoints interact with each other to create a system by which elements enter and exit the |
| 59 | DOM via network request. |
| 60 | |
| 61 | If you use htmx to handle a non-trivial number of your website's network requests, then the inclusion of htmx in your |
| 62 | application has significant implications for the project's structure, from the way you structure your frontend markup, |
| 63 | to the database queries your endpoints make. That is framework-like behavior, and in that scenario, htmx cannot be |
| 64 | trivially replaced. |
| 65 | |
| 66 | You can definitely use htmx in a library-like manner, to add dynamic functionality to just a few sections of your web |
| 67 | page. But you can write [React in this library-like manner too](https://www.patterns.dev/vanilla/islands-architecture) |
| 68 | and nobody argues that React isn't a framework. Suffice to say that many people who use htmx in their applications are |
| 69 | doing so in a way that bends to the demands of htmx, as a framework for building hypermedia applications. |
| 70 | |
| 71 | As they should! Building with htmx works a lot better if you play to its strengths. You can send JSON-formatted form |
| 72 | bodies, [if you really insist](https://github.com/bigskysoftware/htmx-extensions/blob/main/src/json-enc/README). But you |
| 73 | shouldn't! It's simpler to just use `application/x-www-form-urlencoded` bodies, and write an endpoint that accepts them. |
| 74 | You can write an endpoint that is re-used across multiple different |
| 75 | clients, [if you really insist](/essays/why-tend-not-to-use-content-negotiation). But you shouldn't! It's simpler |
| 76 | to [split your data and your hypermedia APIs into separate URLs](/essays/splitting-your-apis). Yes, htmx can be used as |
| 77 | a library, but maybe let it be your framework too. |
| 78 | |
| 79 | That does not mean, however, that htmx is Just Another JavaScript Framework, because htmx has a huge advantage that the |
| 80 | other frameworks do not: HTML. |
| 81 | |
| 82 | ## htmx is for writing HTML |
| 83 | |
| 84 | Let's say you're using htmx as a framework—is it a *JavaScript* framework? In one obvious sense, yes: htmx is |
| 85 | implemented with ~4k lines of JS. But in another, much more important sense, it is not: React, Svelte, Solid, and so on |
| 86 | have you write JS(X) that the framework converts into HTML; htmx just has you write HTML. This removes entire categories |
| 87 | of maintenance that might make you abandon other frameworks with time. |
| 88 | |
| 89 | Codebases tend to get stuck when you want to upgrade or change some dependency, but the framework you use is |
| 90 | incompatible with that change. Java is the most notorious offender here—there are untold millions of lines of Java in |
| 91 | production that will never leave Java 8 because upgrading Spring is too hard—but the npm package ecosystem is a close |
| 92 | second. When you use the htmx "framework" you will never have this problem, because htmx is |
| 93 | a [zero-dependency, client-loaded JavaScript file](/essays/no-build-step), so it is guaranteed to never conflict with |
| 94 | whatever build process or dependency chain your server *does* depend on. |
| 95 | |
| 96 | Browsers render HTML, so no compiler or transpiler is ever necessary to work with htmx. While many htmx users happily |
| 97 | render API responses with JSX, htmx works very well |
| 98 | with [classic](https://jinja.palletsprojects.com) [template](https://ejs.co/) [engines](https://docs.ruby-lang.org/en/2.3.0/ERB.html), |
| 99 | making it portable to [whatever language you like](/essays/hypermedia-on-whatever-youd-like). Say what you will about |
| 100 | Django and Rails, but they were relevant in 2008 and they're relevant today—htmx integrates seamlessly with them both. |
| 101 | This is a recurring theme with htmx-driven development: htmx works well with development tools old and new, because the |
| 102 | common denominator in all these tools is HTML, and htmx is for writing HTML. |
| 103 | |
| 104 | <div style="text-align:center; width:100%"> |
| 105 | <img width=500 |
| 106 | src="/img/memes/htmxanddjango.png" |
| 107 | alt="A monkey labeled 'HTMX' protecting a cute dog named 'Django' from 'all that compilated JS noise'" |
| 108 | > |
| 109 | </div> |
| 110 | |
| 111 | Pushing the user to define the behavior of their application primarily in HTML, rather than JS, has too many advantages |
| 112 | to cover in this essay, so I'll stick to the one people hate most about JavaScript fameworks: churn. Depending on when |
| 113 | you wrote your React application, you might have written your form |
| 114 | with [controlled class components](https://legacy.reactjs.org/docs/forms.html), |
| 115 | or [react hooks](https://blog.logrocket.com/react-hook-form-complete-guide/), or this [experimental |
| 116 | `<form>` extension](https://react.dev/reference/react-dom/components/form). This is genuinely maddening, especially if |
| 117 | you—like me—first learned how to make a web form with class components. |
| 118 | |
| 119 | No matter when you wrote your htmx application, however, the behavior of an htmx form has always been defined in largely |
| 120 | the same way a regular HTML form is: with `<form>`. With htmx adding additional network functionality, you can finally |
| 121 | use `PUT` requests and control where the response goes, but in all other respects—validation, inputs, labels, |
| 122 | autocomplete—you have default `<form>` element behavior. |
| 123 | |
| 124 | Finally, because htmx simply extends HTML in a very narrow domain (network requests and DOM replacements), most of the " |
| 125 | htmx" you write is just plain old HTML. When you have access to complex state management mechanisms, it's incredibly |
| 126 | easy to implement a custom collapsible div; when you don't, you might stop long enough to search up the [ |
| 127 | `<details>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/details) element. Whenever a problem can be |
| 128 | solved by native HTML elements, the longevity of the code improves tremendously as a result. This is a much less |
| 129 | alienating way to learn web development, because the bulk of your knowledge will remain relevant as long as HTML does. |
| 130 | |
| 131 | In this respect, htmx is much more like JQuery than React (htmx's |
| 132 | predecessor, [intercooler.js](https://intercoolerjs.org/), was a JQuery extension), but it improves on JQuery by using a |
| 133 | declarative, HTML-based interface: where JQuery made you go to the `<script>` tag to specify AJAX behavior, htmx |
| 134 | requires only a simple `hx-post` attribute. |
| 135 | |
| 136 | In short, while htmx can be used as a framework, it's a framework |
| 137 | that [deviates far less from the web's semantics](https://unplannedobsolescence.com/blog/custom-html-has-levels) than |
| 138 | the JavaScript frameworks do, and will benefit from improvements in those semantics with no additional work from the |
| 139 | user, thanks to the |
| 140 | web's [excellent backwards compatibility guarantees](https://developer.mozilla.org/en-US/docs/Learn/Getting_started_with_the_web/The_web_and_web_standards#dont_break_the_web). |
| 141 | If you want to build a website that lasts for a long time, these qualities make htmx a substantially better bet than |
| 142 | many of its contemporaries. |
| 143 | |
| 144 | *NOTE: Despite agreeing with this analysis, finding no logical flaws in the essay, and allowing me to publish it on his |
| 145 | website, Carson continues to insist that htmx is a library.* |
| 146 | |
| 147 | <div style="text-align:center; width:100%"> |
| 148 | <img width=500 |
| 149 | src="/img/memes/istudiedhtml.png" |
| 150 | alt="A man holding a sword. He says: 'When you wrote class components, I studied HTML. When you were converting classes to hooks, I mastered the HTML. While you wasted time moving all your client-side logic to server components, I cultivated inner HTML. And now that the browser won't hydrate your thick client JSON API you have the audacity to come to me for help?'" |
| 151 | > |
| 152 | </div> |
| 153 |