| 1 | --- |
| 2 | title: "10 Tips For Building SSR/HDA applications" |
| 3 | description: "In this guide, Carson Gross provides ten practical tips to help developers transition from Single Page Applications (SPAs) to Server-Side Rendering and Hypermedia-Driven Applications, focusing on essential mindset shifts and architectural advantages." |
| 4 | created: 2022-06-13 |
| 5 | modified: 2023-06-13 |
| 6 | authors: ["Carson Gross"] |
| 7 | tags: ["guides"] |
| 8 | --- |
| 9 | |
| 10 | Building web applications using traditional Server-Side Rendering (SSR) or, saying the same thing another way, building |
| 11 | [Hypermedia-Driven Applications](/essays/hypermedia-driven-applications) (HDAs) requires a mindset shift when |
| 12 | compared with building web applications with Single Page Application frameworks like React. |
| 13 | |
| 14 | If you come at this style of development with an SPA-engineering hat on, you are likely to be frustrated and miss out |
| 15 | on many advantages of this particular architectural choice. |
| 16 | |
| 17 | Here are 10 tip to help you make the mental shift smoothly, taking advantage of the strengths of this approach and |
| 18 | minimizing the weaknesses of it: |
| 19 | |
| 20 | ### Tip 1: Maximize Your Server-Side Strengths |
| 21 | |
| 22 | A big advantage of the hypermedia-driven approach is that it makes the server-side environment far more important when |
| 23 | building your web application. Rather than simply producing JSON, your back end is an integral component in the user |
| 24 | experience of your web application. |
| 25 | |
| 26 | Because of this, it makes sense to look deeply into the functionality available there. Many older web frameworks have |
| 27 | incredibly deep functionality available around producing HTML. Features like |
| 28 | [server-side caching](https://guides.rubyonrails.org/caching_with_rails.html) can make the difference between an |
| 29 | incredibly |
| 30 | snappy web application and a sluggish user experience. |
| 31 | |
| 32 | Take time to learn all the tools available to you. |
| 33 | |
| 34 | A good rule of thumb is to shoot to have responses in your application take less than 100ms to complete, and mature |
| 35 | server side frameworks have tools to help make this happen. |
| 36 | |
| 37 | ### Tip 2: Factor Your Application On The Server |
| 38 | |
| 39 | Server-side environments often have extremely mature mechanisms for factoring (or organizing) your code properly. The |
| 40 | [Model/View/Controller](https://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller) pattern is well-developed |
| 41 | in |
| 42 | most environments, and tools like modules, packages, etc. provide an excellent way to organize your code. |
| 43 | |
| 44 | Whereas SPAs user interfaces are typically organized via _components_, hypermedia-driven applications are typically |
| 45 | organized via _template inclusion_, where the server-side templates are broken up according to the HTML-rendering needs |
| 46 | of the application, and then included in one another as needed. This tends to lead to fewer, chunkier files than you |
| 47 | would find in a component-based application. |
| 48 | |
| 49 | Another technology to look for are [Template Fragments](/essays/template-fragments), which allow you to render only |
| 50 | part of a template file. This can reduce even further the number of template files required for your server-side |
| 51 | application. |
| 52 | |
| 53 | ### Tip 3: Specialize Your API End Points |
| 54 | |
| 55 | Unlike a [JSON API](/essays/hypermedia-apis-vs-data-apis), the hypermedia API you produce for your hypermedia-driven |
| 56 | application _should_ feature end-points specialized for your particular application's UI needs. |
| 57 | |
| 58 | Because hypermedia APIs are [not designed to be consumed by general-purpose clients](/essays/hypermedia-clients) you |
| 59 | can set aside the pressure to keep them generalized and produce the content specifically needed for your application. |
| 60 | Your end-points should be optimized to support your particular applications UI/UX needs, not for a general-purpose |
| 61 | data-access model for your domain model. |
| 62 | |
| 63 | ### Tip 4: Aggressively Refactor Your API End Points |
| 64 | |
| 65 | A related tip is that, when you have a hypermedia-based API, you can aggressively refactor your API in a way that is |
| 66 | heavily discouraged when writing JSON API-based SPAs. Because hypermedia-based applications |
| 67 | use [Hypermedia As The Engine |
| 68 | Of Application State](/essays/hateoas), you are able and, in fact, encouraged, to change the shape of them as your |
| 69 | application developers and as use cases change. |
| 70 | |
| 71 | A great strength of the hypermedia approach is that you can completely rework your API to adapt to new needs over time |
| 72 | without needing to version the API or even document it. |
| 73 | |
| 74 | ### Tip 5: Take Advantage of Direct Access To The Data Store |
| 75 | |
| 76 | When an application is built using the SPA approach, the data store typically lives behind a JSON API. This level of |
| 77 | indirection often prevents front end developers from being able to take full advantage of the tools available in the |
| 78 | data store. GraphQL can help address this issue, but comes |
| 79 | with [security-related issues](https://intercoolerjs.org/2016/02/17/api-churn-vs-security.html) |
| 80 | that do not appear to be well understood by many developers. |
| 81 | |
| 82 | When you produce your HTML on the server side, on the other hand, the developer creating that HTML can have full access |
| 83 | to the data store and take advantage of, for example, [joins](https://www.sqltutorial.org/sql-left-join/) and |
| 84 | [aggregation functions](https://www.sqltutorial.org/sql-aggregate-functions/) in SQL stores. |
| 85 | |
| 86 | This puts far more expressive power directly in the hands of the developer producing the HTML. Because your hypermedia |
| 87 | API can be structured around your UI needs, you can tune each endpoint to issue as few data store requests as possible. |
| 88 | |
| 89 | A good rule of thumb is that every request should shoot to have three or fewer data-store accesses. |
| 90 | |
| 91 | ### Tip 6: Avoid Modals |
| 92 | |
| 93 | [Modal windows](https://en.wikipedia.org/wiki/Modal_window) have become popular, almost standard, in many web |
| 94 | applications |
| 95 | today. |
| 96 | |
| 97 | Unfortunately, [modal windows do not play well with much of the infrastructure of the web](https://youdontneedamodalwindow.dev/) |
| 98 | and introduce client-side state that can be difficult (though not impossible) to integrate cleanly with the |
| 99 | hypermedia-based |
| 100 | approach. |
| 101 | |
| 102 | Consider using alternatives such as [inline editing](https://htmx.org/patterns/click-to-edit/), rather than modals. |
| 103 | |
| 104 | ### Tip 7: Accept "Good Enough" UX |
| 105 | |
| 106 | A problem many SPA developers face when coming to the HDA approach is that they look at their current SPA application |
| 107 | and |
| 108 | imagine implementing it _exactly_ using hypermedia. While htmx and other hypermedia-oriented libraries significantly |
| 109 | close the interactivity gap between hypermedia-based applications and SPAs, that gap still exists. |
| 110 | |
| 111 | As Roy Fielding [said](https://www.ics.uci.edu/~fielding/pubs/dissertation/rest_arch_style.htm#sec_5_1_5) with respect |
| 112 | to the web's REST-ful network architecture: |
| 113 | |
| 114 | > The trade-off, though, is that a uniform interface degrades efficiency, since information is transferred in a |
| 115 | > standardized form rather than one which is specific to an application's needs. |
| 116 | |
| 117 | Accepting a slightly less efficient and interactive solution to a particular UX can save you a tremendous amount of |
| 118 | [complexity](/essays/complexity-budget) when building a web application. |
| 119 | |
| 120 | Do not let the perfect be the enemy of the good. |
| 121 | |
| 122 | ### Tip 8: When Necessary, Create "Islands of Interactivity" |
| 123 | |
| 124 | At some point in your web application, there may come a point where the hypermedia approach, on its own, just doesn't |
| 125 | cut it. |
| 126 | |
| 127 | A good example of this is re-ordering a list of things. This can be done in "pure" hypermedia by clicking up and down |
| 128 | arrows or having order # drop-downs next to items. (I am ashamed to admit I have built both of these!) |
| 129 | |
| 130 | But this experience stinks compared to what people are used to: drag-and-drop. |
| 131 | |
| 132 | In cases like this, it is perfectly fine to use a front-end heavy approach as an "Island of Interactivity". |
| 133 | |
| 134 | Consider the [SortableJS](/patterns/records/drag-to-reorder) example. Here you have a sophisticated area of interactivity that |
| 135 | allows for |
| 136 | drag-and-drop, and that integrates with htmx and the broader hypermedia-driven application via events. |
| 137 | |
| 138 | This is an excellent way to encapsulate richer UX within an HDA. |
| 139 | |
| 140 | ### Tip 9: Don't Be Afraid To Script! |
| 141 | |
| 142 | Scripting |
| 143 | is [explicitly a part of the web architecture](https://www.ics.uci.edu/~fielding/pubs/dissertation/rest_arch_style.htm#sec_5_1_7) |
| 144 | and developers adopting the hypermedia approach shouldn't be afraid to use it. Of course there is scripting and then |
| 145 | there is scripting. |
| 146 | |
| 147 | As much as possible, you should try to use the [hypermedia-friendly scripting](/essays/hypermedia-friendly-scripting) |
| 148 | approach, retaining hypermedia-exchanges as the primary mechanism for communicating system state changes with the |
| 149 | server. |
| 150 | |
| 151 | Inline-style scripting, as enabled by [alpine.js](https://alpinejs.dev/) & [hyperscript](https://hyperscript.org) for |
| 152 | example, |
| 153 | is worth exploring as well, as it refocuses your scripting on the hypermedia (HTML) itself and imposes an aesthetic |
| 154 | constraint on just how much code you can write. |
| 155 | |
| 156 | ### Tip 10: Be Pragmatic |
| 157 | |
| 158 | Finally, do not be dogmatic about using hypermedia. At the end of the day, it is just another technology with its own |
| 159 | [strengths & weaknesses](/essays/when-to-use-hypermedia). If a particular part of an app, or if an entire app, |
| 160 | demands something more interactive than what hypermedia can deliver, then go with a technology that can. |
| 161 | |
| 162 | Just be familiar with [what hypermedia can do](/patterns), so you can make that decision as an informed |
| 163 | developer. |
| 164 | |
| 165 | ## Conclusion |
| 166 | |
| 167 | Hopefully these tips help you adopt hypermedia and server-side rendering as a tool more effectively and smoothly. It |
| 168 | isn't a perfect client-server architecture, and it involves explicit tradeoffs, but it can be extremely effective for |
| 169 | many web applications (far more than most web developers today suspect) and provides a much simpler overall development |
| 170 | experience in those cases. |