Debian Codegraph @

www/src/content/essays/a-response-to-rich-harris.mdx

raw
1 ---
2 title: "A Response To "Have Single-Page Apps Ruined the Web?""
3 description: "In this essay, Carson Gross gives an analysis of Rich Harris's talk 'Have Single-Page Apps Ruined the Web?', exploring the debate between Single-Page Applications (SPAs) and Multi-Page Applications (MPAs). Carson examines Harris's criticisms of both approaches and proposes hypermedia-oriented solutions using htmx, while discussing the broader implications for web development architecture and the future role of JavaScript in web applications."
4 created: 2021-12-24
5 modified: 2022-05-27
6 authors: ["Carson Gross"]
7 tags: ["the-case-for-hypermedia"]
8 ---
9
10 import { Image } from 'astro:assets';
11 import languageTrendsSo from '../../assets/img/language-trends-so.png';
12 import languageTrendsGithub from '../../assets/img/language-trends-github.png';
13 import jsDevsBeThinking from '../../assets/img/js-devs-be-thinking.png';
14
15 [Rich Harris](https://twitter.com/rich_harris) is a well-known web developer who works
16 on [Svelte.js](https://svelte.dev/), a novel
17 Single-Page Application (SPA) framework.
18
19 In October of 2021 he gave a talk at JamStack
20 entitled ["Have Single-Page Apps Ruined the Web?"](https://www.youtube.com/watch?v=860d8usGC0o).
21
22 We have been asked for our opinion on the talk, so this essay is our response.
23
24 The first thing to say about the talk is that it is very well done: well produced, thoughtful, funny, fair to both sides
25 of the debate
26 and very reasonable throughout. We don't agree with a lot that Mr. Harris has to say, as we will detail below, but we
27 respect
28 and appreciate his opinions as well as the technologies he works on.
29
30 ## Problems with SPAs
31
32 The talk begins with some reasonable criticisms of SPAs, particularly focusing on usability issues found
33 with Instagram, a canonical SPA implementation from our friends at Facebook. He takes a very fair look at
34 the drawbacks to SPAs, including but not limited to the following list:
35
36 * You will need a bloated JS framework
37 * Performance will suffer
38 * It will be buggy
39 * There will be accessibility issues
40 * The tooling is complicated
41 * It will be less resilient
42
43 After considering the usability issues with Instagram, Mr. Harris has this to say:
44
45 > Come on people. If the best front end engineers in the world can't make
46 > text and images work without five megabytes of javascript, then maybe
47 > we should just give up on the web platform.
48
49 Here we find ourselves in violent agreement with Mr. Harris, with the caveat that we would substitute "the *javascript*
50 web platform"
51 for just "the web platform", since that is what is in play with Instagram.
52
53 We would further clarify that SPA applications and frameworks often simply *ignore* the *actual* web platform, that is,
54 the original, [REST-ful model](/essays/rest-explained) of the web, except as a bootstrap mechanism.
55
56 ## Problems with MPAs
57
58 Mr. Harris then moves on to problems with Multi-Page Applications (MPAs) which are the "traditional",
59 click-a-link-load-a-page-of-HTML web applications we are all familiar with and that are, to an extent,
60 being supplanted by SPAs.
61
62 Below, we will go through the various problems he outlines, all of which are true of "standard" MPAs,
63 and we will demonstrate how an MPA using a hypermedia-oriented technology, [htmx](/), can solve each of them.
64
65 ### "You Can't Keep A Video Running On Navigations"
66
67 A general problem with standard MPAs is that they issue a full page refresh on
68 every request. This means something like a video or audio player will be replaced and, thus, stop playing, when a
69 request is made.
70
71 This problem can be addressed in htmx via the [`hx-preserve`](/reference/attributes/hx-preserve) attribute, which tells
72 htmx to
73 preserve a particular piece of content between requests.
74
75 ### "Back Button & Infinite Scroll Don't Work"
76
77 In the presence of infinite scroll behavior (presumably implemented via javascript of some sort) the back button will
78 not work properly with an MPA. I would note that the presence of infinite scroll calls into question the term MPA, which
79 would traditionally use paging instead of an infinite scroll.
80
81 That said, [infinite scroll](/patterns/loading/infinite-scroll) can be achieved quite easily using htmx, in a
82 hypermedia-oriented and obvious manner. When combined with the [`hx-push-url`](/reference/attributes/hx-push-url)
83 attribute, history and the back button works properly with very little effort by the developer, all with nice
84 Copy-and-Pasteable URLs, sometimes referred to as "Deep Links" by people in the SPA community.
85
86 ### "What about Nice Navigation Transitions?"
87
88 Nice transitions are, well, nice. We think that designers tend to over-estimate their contribution to application
89 usability, however. Yes, the demo sizzles, but on the 20th click users often just want the UI to get on with it.
90
91 That being said, htmx supports using [standard CSS transitions](/patterns/display/animations) to make animations possible.
92 Obviously there is a limit to what you can achieve with these pure CSS techniques, but we believe this can give you the
93 80 of an 80/20 situation. (Or, perhaps, the 95 of a 95/5 situation.)
94
95 ### "Multipage Apps Load Javascript Libraries Every Request"
96
97 Mr. Harris focuses heavily on "crappy Ad Tech" as a culprit for web usability issues on the web, and who can defend the
98 2.5MB payload of tracking, spyware and adware that most websites deliver to their users today? Mr. Harris points out
99 that SPAs ameliorate this issue by loading up this bundle of garbage once, rather than over and over on every request,
100 as an MPA does.
101
102 Now, a vanilla MPA would typically have said garbage cached after the first request, so the download cost, at least, is
103 about the same as with SPAs. But an MPA must *execute* the bundle of garbage again on each page, which does burn CPU and
104 can lead to poor user experience.
105
106 However, an MPA powered by htmx, we note, has exactly the same characteristics as an SPA: the ad garbage would be
107 downloaded and executed once on the first request, and, after that, all requests will be relatively light-weight
108 replacements of DOM elements.
109
110 ### "MPAs Have Network Latency Issues"
111
112 This is a valid point: with an MPA-style application your UI interactions are gated by how fast your server can respond
113 to requests, its latency. Part of that is network latency, which is hard to overcome without giving up one of the
114 tremendously simplifying aspects of traditional web applications: a centralized data store. However, networks are fast
115 and are getting faster, and there are well-known techniques for optimizing *server* latency (i.e. how fast your server
116 returns a response), developed over decades, for monitoring and optimizing this response time. SQL tuning, Redis caching
117 and so on, all well established and making sub-100ms responses a reasonable goal. Many htmx users remark just how fast
118 htmx-based applications feel, but we won't pretend that latency isn't an issue to be considered.
119
120 Of course the problem with latency issues is that they can make an app feel laggy. But, like you, we have worked with
121 plenty of laggy SPAs, so we must say the problem isn't neatly solved by simply adopting SPA frameworks. On top of that,
122 optimistically synchronizing data with a server can lead to extremely difficult to understand data consistency issues as
123 well as a significant increase in overall application complexity, a topic we will return to later.
124
125 ### "GitHub Has UI Bugs"
126
127 GitHub does, indeed, have UI bugs. However, none of them are particularly difficult to solve.
128
129 htmx offers multiple ways to [update content beyond the target element](/patterns/advanced/update-other-content), all of them
130 quite easy and any of which would work to solve the UI consistency issues Mr. Harris points out.
131
132 Contrast the GitHub UI issues with the Instagram UI issues Mr. Harris pointed out earlier: the Instagram issues would
133 require far more sophisticated engineering work to resolve.
134
135 ## Transitional Applications
136
137 Mr. Harris then discusses the concept of "transitional applications" which are a mix of both SPA and MPA technologies.
138 This terminology is reasonable, and we will see if the term sticks in the industry.
139
140 We often recommend using htmx for the parts of the app where it makes sense to keep things simple, and then using other
141 technologies when needed: [alpine.js](https://alpinejs.dev/), [hyperscript](https://hyperscript.org), a small reactive
142 framework, etc.
143
144 So we can agree with Mr. Harris here to an extent and recommend a "transitional" approach to web development, although
145 we would recommend leaning MPA/hypermedia when possible, whereas it seems fairly certain Mr. Harris would lean
146 SPA/javascript.
147
148 ## The Elephant In The Room: Complexity
149
150 Unfortunately, there is a topic that Mr. Harris does not discuss, and we believe this may be because he doesn't see it.
151 He is a javascript developer who is passionate about that language and who swims in the engineering culture of front end
152 frameworks, so the current *complexity* of javascript front end development seems natural to him. For many of us,
153 however, the javascript ecosystem is simply *insanely* overly-complicated. Comically so, in fact, given the requirements
154 of most web applications.
155
156 Many of the "transitional" technologies that Mr. Harris goes on to
157 mention: [React Server Components](https://vercel.com/blog/everything-about-react-server-components) (which he calls "
158 like html over the wire, but vastly more sophisticated), [Marko](https://markojs.com/) (which is doing "partial
159 hydration"), [Quik](https://github.com/BuilderIO/qwik) (which aggressively lazy loads things, apparently), are all
160 remarkable engineering achievements, but are also all, we must say, quite complicated.
161
162 This is, unfortunately, part of the culture of front end development right now: sky-high levels of complexity are
163 tolerated in application frameworks, in build tool chains, in deployment models and so on, and, when problems arise due
164 to all this complexity, more complexity is often offered as the answer.
165
166 "Simple" is disparaging and "sophisticated" is high praise.
167
168 This complexity is overwhelming many developers and development teams today. As Mr. Harris himself points out when
169 discussing Instagram, even some of
170 the best front-end engineers in the world appear to be unable to keep it all under control.
171
172 So there is a cultural issue here.
173
174 There is a technical issue as well.
175
176 This technical issue can be summarized as "The Hypermedia Approach" vs. "The Remote Procedure Call (RPC) Approach".
177
178 When web applications moved from MPAs to SPAs, they adopted, often unwittingly, an RPC approach to application
179 development:
180 AJAX moved to JSON as a data serialization format and largely ([and correctly](/essays/hypermedia-apis-vs-data-apis))
181 abandoned the hypermedia concept. This abandonment of The Hypermedia Approach was driven by the admitted usability
182 issues with vanilla MPAs.
183
184 It turns out, however, that those usability issues often *can* [be addressed](/patterns) using The Hypermedia
185 Approach:
186 rather than *abandoning* Hypermedia for RPC, what we needed then and what we need today is a *more powerful* Hypermedia.
187
188 This is exactly what htmx gives you.
189
190 By returning to The Hypermedia Approach, you can build reasonably sophisticated web applications that address many of
191 Mr. Harris's concerns regarding MPAs at a fraction of the complexity required by most popular SPA frameworks. Further,
192 without
193 thinking about it very much, you will get
194 all [the benefits](https://en.wikipedia.org/wiki/Representational_state_transfer#Architectural_concepts)
195 that Roy Fielding outlined about truly REST-ful architectures.
196
197 Is The Hypermedia Architecture right for all web applications? Obviously not.
198
199 Is it right for many, and perhaps most, web applications? We certainly think so, at least in part.
200
201 ## Javascript: The Resistance
202
203 Now we get to the most emotionally charged claim made in the talk: that "the ship has sailed" on javascript, and that
204 we should accept that it will be the dominant programming language in web development going forward.
205
206 Mr. Harris believes that it will be [edge computing](https://en.wikipedia.org/wiki/Edge_computing) that will be the
207 driver that finally eliminates the remaining, scattered opposition to javascript.
208
209 We are not so sure about that.
210
211 To the contrary, we do not expect edge computing to figure in the majority of web applications for the foreseeable
212 future.
213 Or, to be frank, ever. CPU is cheap, network speeds are fast and increasing and microservices are a mess.
214
215 And, contra what Mr. Harris says, today
216 the [trend is not obviously in javascripts favor](https://insights.stackoverflow.com/trends?tags=java%2Cc%2Cc%2B%2B%2Cpython%2Cc%23%2Cvb.net%2Cjavascript%2Cassembly%2Cphp%2Cperl%2Cruby%2Cvb%2Cswift%2Cr%2Cobjective-c).
217 Five years ago, we, as founding members
218 of the javascript resistance, were despairing of any hope of stopping the Javascript juggernaut. But then something
219 unexpected happened: Python took off and, at the same time, javascript flat lined:
220
221 <div style="text-align:center">
222
223 <Image src={languageTrendsSo} alt="JavaScript language trends on Stack Overflow" />
224
225 </div>
226
227 This trend of javascript peaking in the mid-2010's can be
228 observed [on GitHub](https://www.benfrederickson.com/ranking-programming-languages-by-github-users/) as well:
229
230 <div style="text-align:center">
231
232 <Image src={languageTrendsGithub} alt="JavaScript language trends on GitHub" />
233
234 </div>
235
236 Now, does this mean javascript will eventually "lose" to Python and go away?
237
238 Of course not. Javascript is a core technology of the web and will be with us forever. Without it, we couldn't have
239 built
240 htmx (or [hyperscript](https://hyperscript.org)) so we are very thankful for javascript.
241
242 But this *does* imply that the future of the web does not *necessarily* belong *entirely* to javascript, as appeared to
243 be the case
244 say five years ago.
245
246 We are fond of talking about the HOWL stack: Hypermedia On Whatever you'd Like. The idea is that, by returning to a (
247 more powerful) Hypermedia Architecture, you can use whatever backend language you'd like: python, lisp, haskell, go,
248 java, c#, whatever. Even javascript, if you like.
249
250 Since you are using hypermedia & HTML for your server interactions, you don't feel that pressure to adopt javascript on
251 the backend that a huge javascript front end produces. You can still use javascript, of course, (perhaps in the form
252 of [alpine.js](https://alpinejs.dev/))
253 but you use it in the manner it was originally intended: as a light, front end scripting language for enhancing your
254 application. Or, if you are brave, perhaps you can try [hyperscript](https://hyperscript.org) for these needs.
255
256 This is a world we would prefer to live in: many programming language options, each with their own strengths, technical
257 cultures and thriving
258 communities, all able to participate in the web development world through the magic of more powerful hypermedia, rather
259 than a
260 monolith of SPAs-talking-to-Node-in-JSON. Diversity, after all, is our strength.
261
262 In conclusion,
263
264 <div style="text-align:center">
265
266 <Image src={jsDevsBeThinking} alt="JavaScript developers be thinking meme" />
267
268 </div>