Removing React.js from the codebase and adapting Htmx for UI interactivity (2023)
摘要
Misago 论坛项目当前架构为 Django 渲染 HTML 并嵌入 JSON,再由 React.js 读取 JSON 替换页面内容,导致页面重复实现、性能下降、插件开发复杂等问题。作者对比了两种方案后,倾向采用 HTMX:它允许在服务端渲染的 HTML 中标记动态区域,交互时仅替换该区域的新 HTML,无需 JSON 序列化和前端框架。作者认为对论坛这类交互隔离的场景,HTMX 比 React.js 更简单高效。
荐读理由
作者用具体案例指出 React.js 在论坛这类交互隔离场景下导致页面双实现、性能下降和插件开发复杂,改用 HTMX 可简化架构并减少维护成本;读者在类似项目可考虑服务端渲染 + HTMX 替代 SPA,避免重复实现和性能坑。
原文
Here's how Misago works currently:
You request
https://misago-project.org.Django view gathers data to render list of threads.
Django view renders template that contains almost complete HTML with list of threads, but also includes a JSON with same data used to render this HTML. And there is no interactivity because forms are disabled.
JavaScript downloads.
JavaScript runs, reads JSON embedded in the page's body and replaces most of HTML rendered from Django templates with HTML from React.js components. Buttons become active, and timestamps in UI swap.
This approach has some problems:
A lot of pages in Misago are implemented twice: as Django templates and React.js components.
People who start customizing HTML get burned because they edit Django templates and see their changes flash for a second on a site before being replaced by React.js HTML which they didn't know they also need to customize.
Every view accessible to both users and guests needs to be done twice: as Django view with templates, and as React.js route with components. It also requires an API and JSON serializers to power data fetching.
JSON serialization to pre-bake data for React.js app slows down response generation.
A large part of translation messages is duplicated, living in both
django.poanddjangojs.pofiles. JavaScript translation files also increase the initial download size.Lots of JavaScript can kill performance, especially on older and slower mobile devices.
Enabling plugins to replace or inject custom HTML to the page requires for those plugins to implement both Django templates and React.js components. And Misago will need to implement a JavaScript build step as part of site build in
misago-docker. Plugin devs need to know both.
I've considered two solutions to this problem:
Drop Django views and templates, only keep those in minimal versions to keep search engine bots happy. Focus on implementing an API and having all UI as React.js app.
Reduce Django to API and use JavaScript framework with serverside rendering, eg. Next.js or Remix.run.
But here's a thing: there's a lot of forum software out there that still does the old way of rendering as much as possible on the server and using some JavaScript on client to improve its interactivity here and there. And people are happy with that. And this approach has none of the above issues.
Internet forum software has plenty of interactivity, but this interactivity is isolated to specific places on the page. Moderation actions, watching a thread, writing a reply, seeing last notifications, voting in a poll. All this stuff can be achieved without React.js and was achieved without it for years before we've decided that full page reload is something that needs to be avoided.
Now, what's HTMX? HTMX is a tiny library that lets developers specify parts of HTML as dynamic islands that can be swapped by new server-rendered HTML on interaction. For example, list of actual threads is one such (large) island. With little bit of HTMX included in Django template, changing current category on threads list could pull new HTML from Django only for new threads list for selected category, while keeping rest of the page's HTMX (eg. already loaded search results in navbar) unchanged. Misago's backend code would only need to be changed to return only the HTML for this island when request comes from HTMX, instead of full page. There's no need to do any JSON serialization or write dedicated JavaScript or React.js.
HTMX is declarative way of doing $.get("url", "#outlet") we've did in jQuery 20 years ago. Or Rails Turbolinks. I can't believe I am writing this, but this is the way for forum software if you hate endless scrolling or want to keep things simple otherwise.
这条对你有帮助吗?