为什么你应该为你的下一个基于 Web 的副项目选择 HTMX (2024)
Why you should choose HTMX for your next web-based side project (2024)

原始链接: https://hamy.xyz/blog/2024-02_htmx-for-side-projects

## HTMX:一种更快、更经济的构建 Web 应用的方式 传统上,Web 应用都是作为**多页面应用 (MPA)** 构建的——简单但由于完整页面刷新而速度较慢。**单页面应用 (SPA)** 通过动态更新解决了这个问题,但引入了数据管理和大型 JavaScript 包的复杂性。 HTMX 提供了一种引人注目的替代方案。它是一种**为标准 HTML 添加动态能力**的工具,能够在*无需* SPA 复杂性的情况下实现部分页面更新。本质上,它弥合了差距,让开发者能够以 MPA 的简单性实现现代用户体验。 对于侧重速度和成本的副项目来说,HTMX 表现出色。它减少了开发时间和资源需求,同时仍然能提供响应迅速、现代的感觉。作者已将 HTMX 集成到他们的核心技术栈中,并将其命名为“HAM 栈”,因为它具有效率和平衡性。它允许专注于*构建*项目,而不是与复杂的框架作斗争。

## HTMX:回归Web开发的简洁性 这次Hacker News的讨论围绕着HTMX展开,这是一个越来越受欢迎的框架,它专注于构建简单快速的Web应用程序。HTMX采用“一次请求,一次响应”的模式——用户交互触发服务器请求,服务器返回HTML,客户端将其插入到页面中。这与复杂的、依赖大量JavaScript的SPA(单页应用)形成对比。 许多评论者强调HTMX在副项目和小型应用程序中的优势,强调它易于使用,并且与React等框架相比,复杂度降低。虽然可扩展性存在争议,但普遍认为瓶颈在于后端,而不是HTMX本身。 讨论还涉及HTMX与较旧技术(如AJAX)的关系,将其定位为一种现代化且更精简的方法。一些用户分享了在大型项目中与HTMX的负面经验,认为失败的原因在于开发者的思维方式,而不是框架本身。另一些人则指出了Phoenix LiveView和Datastar等替代方案,同时承认HTMX在将前端开发从特定的服务器端语言中解耦方面的成功。 最终,HTMX被认为是一种可行的选择,适合那些寻求更简单、更快速、更传统的Web开发体验的开发者。
相关文章

原文

I build a lot of side projects so I'm always on the lookout for technologies and paradigms that allow me to build them faster and cheaper at a similar quality. For the last several months I've been experimenting with building web apps with low-js tools like HTMX and Alpine.

In this post I'll share why I think HTMX is a great choice for building web apps faster and cheaper and why it's now a core part of my tech stack - The HAM Stack.

MPA vs SPA

Multi-Page Apps (MPA)

In the beginning we had Multi-Page Apps (MPA).

  • Each URL goes to a web page
  • This web-page is rendered server-side and sent back to the user
  • If you need to update something on the page (because a user changes something or it needs to refresh) then the full page needs to reload

This works fine but it gives a very slow / clunky feel because you need to reload EVERYTHING any time one thing on the page changes. For an example of this - think of a State government website, most of those are still old-school MPAs and they feel old and clunky compared to most modern apps.

MPA Pros and Cons

  • Pros: Simple to build
  • Cons: Feels slow and clunky due to refreshes

Single-Page Apps (SPA)

To solve this, Single-Page Apps (SPA) were created.

  • A URL loads a large JS payload containing a bunch of app logic
  • This JS takes over page rendering
  • It uses internal state to determine what the page should look like and does data transfers behind the scenes so it only needs to update the parts of the page that need to change

This feels better to the user because changes feel faster. The page only updates where it needs to and we don't need a full page refresh to change data. Most apps / websites today feel like this because it generally provides a better user experience.

  • Pros: Rich "modern" user experiences
  • Cons: Complex at scale - dealing w data transfers (graphQL was built for this), abstractions, and app logic payload sizes

So in general:

  • Multi-Page Apps (MPA) - Simple to build but hard to get rich "modern" user experiences
  • Single-Page Apps (SPA) - Complex to build but can achieve rich "modern" user experiences

MPA vs HTMX vs SPA

Image inspiration from u/Abhilash26 on r/htmx.

HTMX has been around for several years but has really gained mass hype in the past year or so. The hype is not unfounded - it often allows you to build modern web apps faster and cheaper than a SPA.

HTMX essentially extends HTML so that it can do partial page reloads.

  • Any element can specify how it can retrieve up-to-date data and when it should do that
  • The element can then specify what to do with the new data - replacing itself or other elements on the page

If we look back at the MPA vs SPA comparison, we can see that the lack of partial page reloads is really the main thing holding MPAs back. So adding this capability into an MPA largely allows you to get most of the rich "modern" user experiences of a SPA with the complexity level of an MPA.

The goal of a side project is often just to build the thing. You likely don't have a team of engineers or warchest of VC-money to do so - it's just you and your computer.

So lowering the cost of building - in time and money - is crucial to making a side project successful.

From my perspective, HTMX provides a great balance between allowing you to build modern apps with very low complexity (and thus time and money cost). Thus it's become a core part of my tech stack - the HAM Stack.

If you liked this post you might also like:

联系我们 contact @ memedata.com