| 1 |
--- |
| 2 |
title: "When Should You Use Hypermedia?" |
| 3 |
description: "This essay by Carson Gross explores when to use hypermedia in web development, highlighting its advantages and trade-offs. Carson discusses scenarios where hypermedia is a good fit, such as text and image-heavy UIs, CRUD applications, and nested UIs with well-defined areas. He also addresses when hypermedia may not be ideal, including situations with frequent UI state updates, offline requirements, or dynamic interdependencies. Using a 'Transitional' approach (as suggested by Rich Harris), Carson advocates for combining hypermedia with other strategies to optimize web application development, emphasizing its practical benefits and alignment with the web's architecture." |
| 4 |
created: 2022-10-23 |
| 5 |
modified: 2023-02-03 |
| 6 |
authors: [ "Carson Gross" ] |
| 7 |
tags: ["the-case-for-hypermedia"] |
| 8 |
--- |
| 9 |
|
| 10 |
import { Image } from 'astro:assets'; |
| 11 |
import nestingExample from '../../assets/img/nesting-example.png'; |
| 12 |
|
| 13 |
> The trade-off, though, is that a uniform interface degrades efficiency, since information is transferred in a |
| 14 |
> standardized form rather than one which is specific to an application's needs. The REST interface is designed to be |
| 15 |
> efficient for large-grain hypermedia data transfer, optimizing for the common case of the Web, but resulting in an |
| 16 |
> interface that is not optimal for other forms of architectural interaction. |
| 17 |
|
| 18 |
_-Roy Fielding, https://www.ics.uci.edu/~fielding/pubs/dissertation/rest_arch_style.htm#sec_5_1_5_ |
| 19 |
|
| 20 |
We are obviously fans of hypermedia and think that it can address, at least in part, many of the problems that the web |
| 21 |
development world is facing today: |
| 22 |
|
| 23 |
* Hypermedia is often [significantly less complex](/essays/a-real-world-react-to-htmx-port) than an SPA approach would |
| 24 |
be for many problems |
| 25 |
* Hypermedia allows your application API to be [much more aggressively refactored and optimized](/essays/hateoas) |
| 26 |
* Hypermedia takes pressure off adopting a particular server technology, since you do not have an extensive JavaScript |
| 27 |
front-end code base |
| 28 |
|
| 29 |
With [htmx](/) and the additional UX possibilities that it gives you, we believe that many modern web applications |
| 30 |
can be built |
| 31 |
using HTML and the hypermedia paradigm. |
| 32 |
|
| 33 |
With that being said, as with all technical choices, there are tradeoffs associated with hypermedia. In this article |
| 34 |
we will give you some ways to think about if hypermedia will be a good fit for an application or feature you are |
| 35 |
building. |
| 36 |
|
| 37 |
## Transitional Applications & Hypermedia |
| 38 |
|
| 39 |
Before we get into the details of when hypermedia is a good choice, we'd like to clarify that adopting hypermedia is not |
| 40 |
an [either/or](https://en.wikipedia.org/wiki/Either/Or) decision when building a web application. Even the most Single-y |
| 41 |
of Single Page Applications utilizes hypermedia after all: as a bootstrap mechanism, to start the application. |
| 42 |
|
| 43 |
In his talk, [Have SPAs Ruined The Web](https://www.youtube.com/watch?v=860d8usGC0o), Rich Harris gives us the term |
| 44 |
"Transitional" Applications, that is applications that _mix_ both hypermedia and non-hypermedia (SPA) concepts. We |
| 45 |
have responded to Mr. Harris' talk [in more detail here](/essays/a-response-to-rich-harris), but suffice to say we |
| 46 |
violently agree with him that a pragmatic "Transitional" approach to web development is best: you should use the |
| 47 |
right tool for the particular job you are working on. |
| 48 |
|
| 49 |
Where we would likely disagree with Mr. Harris is where "the line" is between features that can be implemented |
| 50 |
effectively in hypermedia and features that require a more sophisticated client-side approach. We feel that, with htmx, |
| 51 |
hypermedia can go much, much further than many web developers today believe is possible. And, further, that, for many |
| 52 |
applications, it can address many or all of their UX needs. |
| 53 |
|
| 54 |
## Hypermedia: A Good Fit If... |
| 55 |
|
| 56 |
### _...If your UI is mostly text & images_ |
| 57 |
|
| 58 |
In [The Mother Of All htmx Demos](/essays/a-real-world-react-to-htmx-port), David Guillot of Contexte shows how |
| 59 |
replacing |
| 60 |
React with htmx lead to a 67% reduction in the total codebase, along with numerous other eye-popping results. |
| 61 |
|
| 62 |
As much as we would like to claim that every team moving from React to htmx would experience these results, the fact is |
| 63 |
that the |
| 64 |
Contexte web application is *extremely amenable* to the hypermedia style. |
| 65 |
|
| 66 |
What makes Contexte so perfect for hypermedia is that it is a media-oriented web application, showing articles |
| 67 |
consisting |
| 68 |
of text and images for reading. It has a sophisticated filtering mechanism and other niceties, but the crux of the |
| 69 |
application is displaying and categorizing articles. This is exactly the sort of thing that hypermedia was designed to |
| 70 |
do, and this is why htmx and hypermedia worked so well for their application. |
| 71 |
|
| 72 |
### _...If your UI is CRUD-y_ |
| 73 |
|
| 74 |
Another area where hypermedia has a long track-record of success |
| 75 |
is [CRUD](https://en.wikipedia.org/wiki/Create,_read,_update_and_delete)-y |
| 76 |
web applications, in the [Ruby on Rails](https://rubyonrails.org/) style. If your main application mechanic is showing |
| 77 |
forms and saving the forms into a database, hypermedia can work very well. |
| 78 |
|
| 79 |
And, with htmx, it can also be [very smooth](/patterns/records/edit-in-place), and not just constrained |
| 80 |
to the simple list view/detail view approach many server side applications take. |
| 81 |
|
| 82 |
### _...If your UI is "nested", with updates mostly taking place within well-defined blocks_ |
| 83 |
|
| 84 |
One area where hypermedia can start to go a little wobbly is when you have UI dependencies that span structural |
| 85 |
areas of the screen. A good example of this, and one that often comes up when discussing the hypermedia approach, is the |
| 86 |
issue |
| 87 |
count number shown on the ["Issues" tab](https://github.com/bigskysoftware/htmx/issues) in GitHub. For a long time, |
| 88 |
when you closed an issue on GitHub, the issue count on the tab did not update properly. GitHub, in general (although |
| 89 |
not exclusively), uses a hypermedia-style application. |
| 90 |
|
| 91 |
"Ah ha!", exclaims the SPA enthusiast, "See, even GitHub can't get this right!" |
| 92 |
|
| 93 |
Well, GitHub has fixed the issue, but it does demonstrate a problem with the hypermedia approach: how do you update |
| 94 |
disjoint parts of the UI cleanly? htmx offers [a few techniques for making this work](/patterns/advanced/update-other-content), |
| 95 |
and Contexte, in their talk, discuss handling this situation very cleanly, using the event approach. |
| 96 |
|
| 97 |
But, let us grant that this is an area where the hypermedia approach can get into trouble. To avoid this problem, one |
| 98 |
potential strategy is to colocate dependent elements for a given resource within a given region or area on the screen in |
| 99 |
an |
| 100 |
application. |
| 101 |
|
| 102 |
As an example, consider a contact application whose detail screen for displaying and editing a contact has: |
| 103 |
|
| 104 |
* An area for basic contact information |
| 105 |
* An area for the contact's emails, and the count of those emails |
| 106 |
* An area for the contact's phone numbers, and the count of those phone numbers |
| 107 |
|
| 108 |
This UI could be laid out in the following manner: |
| 109 |
|
| 110 |
<Image src={nestingExample} alt="Nested UI example" /> |
| 111 |
|
| 112 |
In this scenario, each subsection can have its own dedicated hypermedia end-points: |
| 113 |
|
| 114 |
* `/contacts/<id>/details` for the first name/last name/ etc. info |
| 115 |
* `/contacts/<id>/emails` for the email section |
| 116 |
* `/contacts/<id>/phonenumbers` for the phone numbers section |
| 117 |
|
| 118 |
The trick here is that the email and phone counts are co-located on the screen with their collections, which allows you |
| 119 |
to |
| 120 |
[target](/reference/attributes/hx-target) just that particular area for update when a modification is made to the respective |
| 121 |
collections. All the data dependencies are co-located within a single area that can be updated via a single, simple |
| 122 |
and obvious target, and that, further, don't interfere with one another when they are replaced. |
| 123 |
|
| 124 |
Each area effectively forms a sort of server-side component, independent of the other areas on the screen, and they are |
| 125 |
all nested within a broader contact detail user interface. |
| 126 |
|
| 127 |
#### A Side Node: UI Driven Hypermedia APIs |
| 128 |
|
| 129 |
Note that our hypermedia API (i.e. our end-points) in this case is _driven by the UI_: we have a particular UI layout |
| 130 |
that we want to achieve, and we adapt our API to that. If the UI changed, we would have no qualms with completely |
| 131 |
changing |
| 132 |
our API to satisfy the new requirements. This is a [unique aspect](/essays/hateoas) of developing with |
| 133 |
hypermedia, and we [discuss it in more detail here](/essays/hypermedia-apis-vs-data-apis). |
| 134 |
|
| 135 |
Of course, there may be UI requirements that do not allow for grouping of dependent element in this manner and, if |
| 136 |
the techniques [mentioned above](/patterns/advanced/update-other-content) aren't satisfactory, then it may be |
| 137 |
time to consider an alternative approach. |
| 138 |
|
| 139 |
### _...If you need "deep links" & good first-render performance_ |
| 140 |
|
| 141 |
A final area where hypermedia outperforms other options is when you need "deep links", that is, links into your |
| 142 |
application that go beyond the landing page, or when you need excellent first-render performance. |
| 143 |
|
| 144 |
Since hypermedia is the natural language of the web, and since browsers are very good at rendering HTML given a URL, |
| 145 |
using this approach is hard to beat for "traditional" web features such as these. |
| 146 |
|
| 147 |
## Hypermedia: Not A Good Fit If... |
| 148 |
|
| 149 |
### _...If your UI has many, dynamic interdependencies_ |
| 150 |
|
| 151 |
As we discussed above in the section on "nested" UIs, one area where hypermedia can have trouble is when there are |
| 152 |
many UI dependencies spread across your UI and you can't afford to "update the whole UI". This is what Roy Fielding was |
| 153 |
getting at in the quote at the top of this article: the web was designed for large-grain hypermedia data transfers, not |
| 154 |
for lots of small data exchanges. |
| 155 |
|
| 156 |
Particularly difficult for hypermedia to handle is when these dependencies are dynamic, that is, they depend on |
| 157 |
information |
| 158 |
that cannot be determined at server-side render-time. A good example of this is something like a spreadsheet: a user can |
| 159 |
enter an arbitrary function into a cell and introduce all sorts of dependencies on the screen, on the fly. |
| 160 |
|
| 161 |
(Note, however, that for many applications, the "editable row" pattern is an |
| 162 |
acceptable alternative to more general spreadsheet-like behavior, and this pattern does play well with hypermedia by |
| 163 |
isolating edits within a bounded area.) |
| 164 |
|
| 165 |
### _...If you require offline functionality_ |
| 166 |
|
| 167 |
The hypermedia distributed architecture leans heavily on the server side for rendering representations of resources. |
| 168 |
When |
| 169 |
a server is down or unreachable, the architecture will obviously have trouble. It is possible to use Service Workers |
| 170 |
to handle offline requests (although this is a complex option), and it is also easy to detect when a hypermedia |
| 171 |
application is offline and show an offline message, as many thick-client application do. |
| 172 |
|
| 173 |
But if your application requires full functionality in an offline environment, then the hypermedia approach is not |
| 174 |
going to be an acceptable one. |
| 175 |
|
| 176 |
### _...If your UI state is updated extremely frequently_ |
| 177 |
|
| 178 |
Another situation where hypermedia is not going to be a good approach is if your UI state is updated frequently. A good |
| 179 |
example is an online game that needs to capture mouse movements. Putting a hypermedia network request in-between a mouse |
| 180 |
move and a UI update will not work well, and you would be far better off writing your own client-side state management |
| 181 |
for the game and syncing with a server using a different technology. |
| 182 |
|
| 183 |
Of course, your game may also have a setting page and that setting page might be better done with hypermedia than |
| 184 |
whatever solution you use for the core of your game. There is nothing wrong with mixing approaches, in the Transitional |
| 185 |
style! |
| 186 |
|
| 187 |
We should note, however, that it is typically easier to embed SPA components _within_ a larger hypermedia |
| 188 |
architecture, than vice-versa. Isolated client-side components can communicate with a broader hypermedia application |
| 189 |
via [events](https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Building_blocks/Events), in the manner |
| 190 |
demonstrated |
| 191 |
in the [drag-and-drop Sortable.js + htmx](/patterns/records/drag-to-reorder) example. |
| 192 |
|
| 193 |
### _...If you want integrated copy & paste components_ |
| 194 |
|
| 195 |
Recently we have seen the rise of "Copy & Paste" friendly components such as [ShadCN](https://ui.shadcn.com/). |
| 196 |
These components are typically designed for a specific front end framework such as React, and choosing htmx means that |
| 197 |
you can't use them. |
| 198 |
|
| 199 |
There are front-end library neutral component libraries such as [lit](https://lit.dev/), but they are not as integrated |
| 200 |
with htmx as ShadCN is with React. |
| 201 |
|
| 202 |
### _...If your team is not on board_ |
| 203 |
|
| 204 |
A final reason to not choose hypermedia isn't technical, but rather sociological: currently, hypermedia simply isn't |
| 205 |
in favor in web development. Many companies have adopted React as their standard library for building web |
| 206 |
applications. |
| 207 |
Many developers and consultants have bet their careers on it. Many hiring managers have never heard of hypermedia, let |
| 208 |
alone htmx, but put React on every job they post out of habit. It is certainly much easier to hire for! |
| 209 |
|
| 210 |
While this is frustrating, it is a real phenomenon and should be borne in mind with humility. Although Contexte |
| 211 |
was able to rewrite their application quickly and effectively in htmx, not all teams are as small, agile and |
| 212 |
passionate, nor are all applications such slam dunks for the approach. It may be better to adopt hypermedia around |
| 213 |
the edges, perhaps for internal tools first, to prove its value first, before taking a broader look at it. |
| 214 |
|
| 215 |
## Conclusion |
| 216 |
|
| 217 |
We are often asked: "OK, so what sorts of applications **wouldn't** htmx be good for". We prefer to think about |
| 218 |
things on a feature-by-feature basis using the "Transitional" application concept, but it is useful to have some |
| 219 |
broad, popular applications in mind when thinking about just how much might be done in hypermedia versus other |
| 220 |
approaches. |
| 221 |
|
| 222 |
To give an example of two famous applications that we think _could_ be implemented cleanly in hypermedia, consider |
| 223 |
[Twitter](https://twitter.com) or [GMail](https://gmail.com). Both web applications are text-and-image heavy, with |
| 224 |
coarse-grain updates and, thus, would be quite amenable to a hypermedia approach. |
| 225 |
|
| 226 |
Two famous examples of web applications that would _not_ be amenable to a hypermedia approach are |
| 227 |
[Google Sheets](https://www.google.com/sheets/about/) and [Google Maps](https://maps.google.com). Google Sheets can have |
| 228 |
a large amounts of state within and interdependencies between many cells, making it untenable to issue a server request |
| 229 |
on every |
| 230 |
cell update. Google Maps, on the other hand, responds rapidly to mouse movements and simply can't afford a server round |
| 231 |
trip for |
| 232 |
every one of them. Both of these applications require a much more sophisticated client-side setup than what hypermedia |
| 233 |
can provide. |
| 234 |
|
| 235 |
Of course, the vast majority of web applications are nowhere near the scale and complexity of these examples. And almost |
| 236 |
every web application, even Google Sheets or Google Maps, has parts where, potentially, the hypermedia approach would be |
| 237 |
better: simpler, faster and cleaner. |
| 238 |
|
| 239 |
Having hypermedia as a tool in your tool-chest will improve your ability to address engineering problems as a web |
| 240 |
developer, even if it doesn't become your favorite hammer. There is a good [theoretical basis](/essays/hateoas) |
| 241 |
for the approach, [practical benefits for many applications](/essays/a-real-world-react-to-htmx-port), |
| 242 |
and it is "with the grain" of the web in a way that other approaches are not. |