| 1 | --- |
| 2 | title: "Hypermedia-Driven Applications" |
| 3 | description: "In this essay, Carson Gross explains the Hypermedia-Driven Application (HDA) architecture, which combines the simplicity of traditional Multi-Page Applications with the enhanced user experience of Single-Page Applications by extending HTML infrastructure through declarative syntax and hypermedia-based server interactions." |
| 4 | created: 2022-02-06 |
| 5 | modified: 2022-10-18 |
| 6 | authors: ["Carson Gross"] |
| 7 | tags: ["foundations"] |
| 8 | --- |
| 9 | |
| 10 | ## Genesis |
| 11 | |
| 12 | > thesis: MPA - multi-page application |
| 13 | > |
| 14 | > antithesis: SPA - single-page application |
| 15 | > |
| 16 | > synthesis: HDA - hypermedia-driven application |
| 17 | > |
| 18 | > \-\-[@htmx_org](https://twitter.com/htmx_org/status/1490318550170357760) |
| 19 | |
| 20 | ## The Hypermedia-Driven Application Architecture |
| 21 | |
| 22 | The **Hypermedia Driven Application (HDA)** architecture is a new/old approach to building web applications. It combines |
| 23 | the simplicity & flexibility of traditional Multi-Page Applications (MPAs) with the better user experience of |
| 24 | [Single-Page Applications](https://en.wikipedia.org/wiki/Single-page_application) (SPAs). |
| 25 | |
| 26 | The HDA architecture achieves this goal by extending the existing HTML infrastructure of the web to allow hypermedia |
| 27 | developers to create more powerful hypermedia-driven interactions. |
| 28 | |
| 29 | Following the REST notion of |
| 30 | architectural [constraints](https://www.ics.uci.edu/~fielding/pubs/dissertation/rest_arch_style.htm), |
| 31 | two such constraints characterize the HDA architecture: |
| 32 | |
| 33 | * An HDA uses *declarative, HTML-embedded syntax* rather than imperative scripting to achieve better front-end |
| 34 | interactivity |
| 35 | |
| 36 | * An HDA interacts with the server **in terms of hypermedia** (i.e. HTML) rather than a non-hypermedia format (e.g. |
| 37 | JSON) |
| 38 | |
| 39 | By adopting these two constraints, the HDA architecture stays within the original |
| 40 | [REST-ful](https://developer.mozilla.org/en-US/docs/Glossary/REST) architecture of the web in a way that the SPA |
| 41 | architecture |
| 42 | does not. |
| 43 | |
| 44 | In particular, HDAs continue to use [Hypermedia As The Engine of Application State (HATEOAS)](/essays/hateoas), whereas |
| 45 | most SPAs abandon HATEOAS in favor of a client-side model and data (rather than hypermedia) APIs. |
| 46 | |
| 47 | ## An Example HDA fragment |
| 48 | |
| 49 | Consider the htmx [Active Search](/patterns/forms/active-search) example: |
| 50 | |
| 51 | ```html |
| 52 | <h3> |
| 53 | Search Contacts |
| 54 | <span class="htmx-indicator"> |
| 55 | <img src="/img/bars.svg" alt=""/> Searching... |
| 56 | </span> |
| 57 | </h3> |
| 58 | <input class="form-control" type="search" |
| 59 | name="search" placeholder="Begin Typing To Search Users..." |
| 60 | hx-post="/search" |
| 61 | hx-trigger="keyup changed delay:500ms, search" |
| 62 | hx-target="#search-results" |
| 63 | hx-indicator=".htmx-indicator"> |
| 64 | |
| 65 | <table class="table"> |
| 66 | <thead> |
| 67 | <tr> |
| 68 | <th>First Name</th> |
| 69 | <th>Last Name</th> |
| 70 | <th>Email</th> |
| 71 | </tr> |
| 72 | </thead> |
| 73 | <tbody id="search-results"> |
| 74 | </tbody> |
| 75 | </table> |
| 76 | ``` |
| 77 | |
| 78 | This is a UX pattern that would typically be associated with an SPA: as the user types, after a slight pause, search |
| 79 | results will populate the result table below. However, in this case, it is being achieved entirely within HTML, |
| 80 | in a manner consonant with HTML. |
| 81 | |
| 82 | This example effectively demonstrates the essential characteristic of an HDA: |
| 83 | |
| 84 | * The front end of the feature is specified entirely in declarative htmx attributes, directly in HTML |
| 85 | |
| 86 | * The interaction with the server is done via HTTP and HTML: an HTTP `POST` request is sent to the server, HTML is |
| 87 | returned by the server and htmx inserts this HTML into the DOM |
| 88 | |
| 89 | ## Scripting In An HDA |
| 90 | |
| 91 | [Code-On-Demand](https://www.ics.uci.edu/~fielding/pubs/dissertation/rest_arch_style.htm#sec_5_1_7) is an optional |
| 92 | constraint of the original REST-ful architecture of the web. |
| 93 | |
| 94 | Similarly, the HDA architecture has a final, optional constraint: |
| 95 | |
| 96 | * Code-On-Demand (i.e. scripting) should, as much as is practical, be done *directly in* the primary hypermedia |
| 97 | |
| 98 | This addresses the concern regarding Code-On-Demand that Roy Fielding mentions in his thesis: |
| 99 | |
| 100 | > However, (Code-On-Demand) also reduces visibility, and thus is only an optional constraint within REST. |
| 101 | |
| 102 | By embedding Code-On-Demand (scripts) directly in HTML, visibility is enhanced, satisfying the |
| 103 | [Locality of Behavior](/essays/locality-of-behaviour) software design principle. |
| 104 | |
| 105 | Three approaches to scripting that satisfy this third constraint are [hyperscript](https://hyperscript.org), |
| 106 | [AlpineJS](https://alpinejs.dev) and [VanillaJS](http://vanilla-js.com/) (when embedded directly on HTML elements). |
| 107 | |
| 108 | Here is an example of each of these approaches: |
| 109 | |
| 110 | ```html |
| 111 | <!-- hyperscript --> |
| 112 | <button _="on click toggle .red-border"> |
| 113 | Toggle Class |
| 114 | </button> |
| 115 | |
| 116 | <!-- Alpine JS --> |
| 117 | <button @click="open = !open" :class="{'red-border' : open, '' : !open}"> |
| 118 | Toggle Class |
| 119 | </button> |
| 120 | |
| 121 | <!-- VanillaJS --> |
| 122 | <button onclick="this.classList.toggle('red-border')"> |
| 123 | Toggle Class |
| 124 | </button> |
| 125 | ``` |
| 126 | |
| 127 | In an HDA, hypermedia (HTML) is the primary medium for building the application, which means that: |
| 128 | |
| 129 | * All communication with the server is still managed via HTTP requests with hypermedia (HTML) responses |
| 130 | * Scripting is used mainly to enhance the *front-end experience* of the application |
| 131 | |
| 132 | Scripting augments the existing hypermedia (HTML) but does not *supersede* it or subvert the fundamental REST-ful |
| 133 | architecture of the HDA. |
| 134 | |
| 135 | ## HDA-style libraries |
| 136 | |
| 137 | The following libraries allow developers to create HDAs: |
| 138 | |
| 139 | * <https://htmx.org> |
| 140 | * <https://unpoly.com/> |
| 141 | * <https://piranha.github.io/twinspark-js/> |
| 142 | * <https://hotwire.dev> |
| 143 | * <https://hyperview.org/> (a mobile hypermedia!) |
| 144 | |
| 145 | The following scripting libraries, when used appropriately, complement the HDA approach: |
| 146 | |
| 147 | * <https://hyperscript.org> |
| 148 | * <https://alpinejs.dev/> |
| 149 | * <http://vanilla-js.com/> (embedded directly in HTML) |
| 150 | |
| 151 | ## Conclusion |
| 152 | |
| 153 | The HDA architecture is a synthesis of two preceding architectures: the original Multi-Page Application (MPA) |
| 154 | architecture |
| 155 | and the (relatively) newer Single-Page Application architecture. |
| 156 | |
| 157 | It attempts to capture the advantages of both: the simplicity and reliability of MPAs, with a |
| 158 | [REST-ful Architecture](https://developer.mozilla.org/en-US/docs/Glossary/REST) that uses |
| 159 | [Hypermedia As The Engine Of Application State](/essays/hateoas), while providing a better user experience, on par |
| 160 | with SPAs in many cases. |