| 1 |
--- |
| 2 |
title: "SPA Alternative" |
| 3 |
description: "In this essay, Carson Gross explores alternatives to Single Page Applications (SPAs), advocating for an 'HTML-Centric' development approach. He highlights the drawbacks of SPA complexity and the benefits of embracing HTML as the core medium for web development, offering a simpler, more efficient way to build applications without sacrificing interactivity. Carson discusses how tools like htmx enhance HTML's capabilities and encourages developers to reconsider the dominant SPA paradigm with technical bravery." |
| 4 |
created: 2020-10-29 |
| 5 |
modified: 2022-02-06 |
| 6 |
authors: ["Carson Gross"] |
| 7 |
tags: ["the-case-for-hypermedia"] |
| 8 |
--- |
| 9 |
|
| 10 |
import { Image } from 'astro:assets'; |
| 11 |
import whatIf from '../../assets/img/what-if.png'; |
| 12 |
|
| 13 |
Recently [Tom MacWright](https://macwright.com) has written a few posts on Single Page Applications and their |
| 14 |
discontents: |
| 15 |
|
| 16 |
* [Second-guessing the modern web](https://macwright.com/2020/05/10/spa-fatigue.html) |
| 17 |
* [If not SPAs, What?](https://macwright.com/2020/10/28/if-not-spas.html) |
| 18 |
|
| 19 |
> The emerging norm for web development is to build a React single-page application, with server rendering. The two key |
| 20 |
> elements of this architecture are something like: |
| 21 |
> |
| 22 |
>1. The main UI is built & updated in JavaScript using React or something similar. |
| 23 |
>2. The backend is an API that that application makes requests against. |
| 24 |
> |
| 25 |
> This idea has really swept the internet. It started with a few major popular websites and has crept into corners |
| 26 |
> like marketing sites and blogs. |
| 27 |
|
| 28 |
In these two articles Tom lays out the problem associated with the React/SPA everywhere mindset. If I can summarize |
| 29 |
them in one sentence: SPA frameworks tend to be complex, and you don't get a lot of benefit for all that |
| 30 |
complexity in many cases. |
| 31 |
|
| 32 |
## An Alternative |
| 33 |
|
| 34 |
Tom outlines a few alternatives to the SPA approach in the second article and, I'm happy to say, mentions htmx. However, |
| 35 |
he classifies htmx (as well as [Stimulus](https://stimulusjs.org/) and [Alpine.js](https://github.com/alpinejs/alpine/)) |
| 36 |
as "progressive-enhancement" libraries. This is a good description, but, at least in the case of htmx, I think there |
| 37 |
is a better term to help describe this style of library: *HTML-Centric* (or, perhaps, *Hypertext-Centric*) |
| 38 |
|
| 39 |
### HTML-Centric Development |
| 40 |
|
| 41 |
In HTML-Centric Development, rather than being an afterthought, HTML is embraced as the primary medium of application |
| 42 |
development. This is in contrast to most SPA frameworks, where a client-side model & the javascript that manipulates |
| 43 |
it is the central focus. |
| 44 |
|
| 45 |
HTML-Centric Development builds on the original model of the web, as outlined in |
| 46 |
[Roy Fielding's PhD dissertation](https://www.ics.uci.edu/~fielding/pubs/dissertation/top.htm), describing the web |
| 47 |
architecture. In particular, by embracing HTML as a hypertext, you get the benefits of |
| 48 |
[REST and HATEOAS](https://www.ics.uci.edu/~fielding/pubs/dissertation/rest_arch_style.htm), all without needing to |
| 49 |
be an expert in either of those topics. |
| 50 |
|
| 51 |
(Recall, Roy was *describing* the web architecture, so the original web was |
| 52 |
largely REST-ful, without any particular effort on the part of the original participants) |
| 53 |
|
| 54 |
By picking HTML-Centric Development, you accrue many benefits: |
| 55 |
|
| 56 |
* A simpler front end allows you to save your [complexity budget](/essays/complexity-budget) for the back end |
| 57 |
functionality |
| 58 |
that differentiates your application from others. |
| 59 |
* You do not face pressure to adopt javascript on the back end "since the front end is written in javascript". This |
| 60 |
allows |
| 61 |
you to use the best backend framework for your particular application. |
| 62 |
* With a simpler front end, a "full stack" developer can more easily manage and optimize front-to-back optimization in |
| 63 |
your application, leading to much better system tuning |
| 64 |
* Your web application is going to have HTML in it anyway, so by maximizing its utility you are boosting the power of |
| 65 |
an existing component, rather than adding another layer of complexity between the end user and your application code. |
| 66 |
* The stateless network model of the web has proven very resilient and easy to develop for. Many mature and |
| 67 |
battle-tested |
| 68 |
technologies and techniques (e.g. [caching](https://developer.mozilla.org/en-US/docs/Web/HTTP/Caching)) exist for |
| 69 |
building HTML-based applications. |
| 70 |
|
| 71 |
### HTML: The Bad Parts |
| 72 |
|
| 73 |
With all these benefits of the HTML-Centric model, one may wonder why it has been abandoned (and is often mocked) by |
| 74 |
many web developers. At a high level, the answer is: |
| 75 |
|
| 76 |
*HTML-Centric applications have historically offered a limited |
| 77 |
amount of interactivity when compared with javascript-based applications*. |
| 78 |
|
| 79 |
This is in large part because HTML is a limited hypertext. In particular: |
| 80 |
|
| 81 |
* Only `<a>` and `<form>` can make HTTP requests |
| 82 |
* Only `click` & `submit` events can trigger them |
| 83 |
* Only GET & POST [HTTP Methods](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods) are widely available |
| 84 |
* A request must replace the entire screen, leading to a clunky and sometimes jarring user experience |
| 85 |
|
| 86 |
Of course, none of the constraints are inherent in the concept of a hypertext, and the goal of [htmx](/) |
| 87 |
is to remove each of them. |
| 88 |
|
| 89 |
By removing these constraints and completing HTML as a fully-functional and high-powered hypertext, HTML-Centric |
| 90 |
applications can compete with SPAs in many application domains, while at the same time accruing the technical |
| 91 |
and complexity benefits mentioned above. |
| 92 |
|
| 93 |
## Being Brave, Technically |
| 94 |
|
| 95 |
Tom closes his first article with this: |
| 96 |
|
| 97 |
> What if everyone’s wrong? We’ve been wrong before. |
| 98 |
|
| 99 |
Web development has gone down blind alleys quite a few times: GWT, Java Server Faces, Angular 1, FlatUI, etc. |
| 100 |
During the height of the hype cycle around each of these technologies, it was difficult to go against the grain. It is |
| 101 |
particularly difficult to do in the technology world , where the being left behind technically is not only a threat to |
| 102 |
our ego, but also to our employment. |
| 103 |
|
| 104 |
> "No One Ever Got Fired For Using React" |
| 105 |
|
| 106 |
is today's |
| 107 |
|
| 108 |
> "No One Ever Got Fired For Buying IBM" |
| 109 |
|
| 110 |
That's a reality that we must accept, even if we feel that React/etc. aren't appropriate for many (or even most) web |
| 111 |
applications being built today. |
| 112 |
|
| 113 |
However, we are starting to see a reconsideration of the SPA approach. With a bit of technical bravery, a willingness |
| 114 |
to stand against the crowd, you may be able to make your application much less complex, and focus your development |
| 115 |
efforts on what your application does, rather than on how it does it. |
| 116 |
|
| 117 |
From the [htmx developer's starter kit](https://twitter.com/htmx_org/status/1306234341056344065): |
| 118 |
|
| 119 |
<Image src={whatIf} alt="What if meme" /> |