构建以 HTML 为先的网站,我们的用户量一夜之间翻了一番。
Building an HTML-first site doubled our users overnight

原始链接: https://mohkohn.co.uk/writing/html-first/

在为一家公用事业公司构建复杂的 React 应用程序两次失败后(导致性能低下、功能损坏以及高跳出率),一位开发者采取了不同的方法:构建一个“HTML 优先”的解决方案。 通过优先考虑可访问性和韧性,该开发者使用 Astro 创建了一个表单驱动的应用程序,即使在没有 JavaScript 的情况下也能完美运行。该架构利用了标准表单提交和后端会话持久化,确保使用低端设备或网络环境较差的用户也能完成申请,而不会丢失数据。为了提升用户体验,开发者构建了一个 1KB 的轻量级 Web 组件,用于增强而非替代浏览器原生的验证功能。 结果是变革性的:用户完成率在一夜之间翻了一番,覆盖到了此前因浏览器无法处理繁重的 JavaScript 实现而无法被分析工具捕捉到的用户群体。 这一案例为软件行业提供了一个至关重要的教训:通过为“最低公分母”(如连接较弱的旧设备)进行设计,你所创建的软件将更具包容性、稳健性,并能为所有人提供服务。走出网页开发的“蛮荒时代”,需要优先考虑长期的可访问性和可靠性,而非不必要的复杂性。

Hacker News 上的讨论围绕着文章《构建“HTML 优先”网站使我们的用户翻倍》展开,该文章强调了优先采用简单、快速、服务器端渲染的网页体验,而非 React 等繁重的客户端框架所带来的益处。 辩论的要点包括: * **性能与用户共情:** 用户压倒性地偏好“HTML 优先”的方案,因为它们加载速度极快。参与者认为,分发臃肿的 JavaScript 包是对用户的不尊重,而业界向复杂的客户端应用转型导致了不必要的阻碍。 * **框架哲学:** 讨论很大程度上对比了“选择加入(opt-in)”与“选择退出(opt-out)”架构。Astro 因默认追求性能而受到赞扬,而 React 则因默认带来客户端复杂性而受到批评。 * **基础技术的“复兴”:** 讨论达成共识,即许多现代开发者已经忘记了如何使用基础的网络原语(如 HTML 表单),转而依赖繁重的 JavaScript 抽象。 * **澄清:** 评论者指出,“用户翻倍”的说法很可能指的是降低了表单放弃率,而非单纯的流量增长。 最终,该讨论帖反映出人们对“过度工程化”的技术栈日益疲惫,并重新开始欣赏简洁、快速和标准的网页开发实践。
相关文章

原文

This is a story of how building HTML-first doubled a company’s users literally overnight.

My client was a utility company, and they had a big problem. To apply for their services, customers could either use an old ASP form on the website, or follow a manual process. The manual process was more expensive for the company, of course. Adding a lot of pressure, this was a regulated monopoly, and if their customer satisfaction dropped below 96% (if I remember correctly) it could result in millions of pounds in fines.

There were two previous failed (and very expensive) attempts to solve the problem. In the most recent, contractors in another country had built a React app. The React app was online for 3 days before being pulled because of customer complaints. I took one look at it and told my boss “we can’t take ownership of this.” It was a mess of loading spinners and global javascript states. It was not accessible. Image upload was a vital part of the form, and it attempted to store images (along with all other form data) in localstorage which has a 5mb limit!

I took a very bold decision and built a new version of the site using Astro. It was HTML-first. Javascript existed, in web components, but only to progressively-enhance a website that worked perfectly fine without it.

My logic was thus:

  1. This is a public service
  2. It should work on every machine possible
  3. It should work when connections are poor
  4. The forms must never lose data once it is entered

I was very moved by this anecdote from Terence Eden:

A few years ago I was doing policy research in a housing benefits office in London. They are singularly unlovely places. The walls are brightened up with posters offering helpful services for people fleeing domestic violence. The security guards on the door are cautiously indifferent to anyone walking in. The air is filled with tense conversations between partners - drowned out by the noise of screaming kids.

In the middle, a young woman sits on a hard plastic chair. She is surrounded by canvas-bags containing her worldly possessions. She doesn’t look like she is in a great emotional place right now. Clutched in her hands is a games console - a PlayStation Portable. She stares at it intensely; blocking out the world with Candy Crush.

Or, at least, that’s what I thought.

Walking behind her, I glance at her console and recognise the screen she’s on. She’s connected to the complementary WiFi and is browsing the GOV.UK pages on Housing Benefit. She’s not slicing fruit; she’s arming herself with knowledge.

The PSP’s web browser is - charitably - pathetic. It is slow, frequently runs out of memory, and can only open 3 tabs at a time.

But the GOV.UK pages are written in simple HTML. They are designed to be lightweight and will work even on rubbish browsers. They have to. This is for everyone.

Some requirements I derived:

  1. Each session with the form should have a unique ID
  2. At every step in the form wizard, submitted data should be stored on the backend, including uploads
  3. It should be possible to complete the form without javascript
  4. It should be possible to complete the form on outdated and crap web browsers
  5. We had to meet WCAG accessibility (the team settled on AA rather than AAA)
  6. Javascript and modern CSS should be used to enhance the experience

The basic setup ended up being that each step in the form wizard was its own page. When the user clicked next, the form would submit. If the data was judged to be valid by the API, the browser would be redirected to the next step.

A venerable web application pattern that has had a small modern renaissance thanks to Remix, form submissions and redirects took a while to explain to my colleagues, on account of everyone being used to heavily client-side web applications. I have nothing against heavily client-side applications, in their place. But this is just a big form - it’s not showing real-time data. Our user could be standing in the middle of a field on a new-build housing estate, holding a decade-old commodity android phone they bought in Tesco. Shipping them 20MB of javascript before we even render a form would be a ridiculous thing to do.

Next, I tackled one of my biggest bugbears, form validation (and form and form error rendering). I have seen teams waste person-months of effort wrangling React validation libraries. If you are a React person, you might be scoffing at this - skill issue, I guess - but it is the reality for many teams. I would like to humbly suggest that you too may be spending more time than you realise, and a lot more time than is necessary, interacting with and maintaining poor imitations of the validation system that ships with every browser.

So I built an HTML web component. These are simple custom elements that wrap around existing HTML and bring it to life. No shadow DOM, no (or little) rendering HTML in javascript. Mine wrapped around any HTML form, picked up the HTML validation, and made it look modern. It would prevent those HTML validation popup tooltips, and instead place the error in the aria-describedby element associated with the field (today, aria-errormessage is advised instead). It would clear validation while you typed, if you reached a valid state, and assess it again on blur and submit.

Exactly the user experience a form needs, delivered in under 1KB. If it failed, the form would fall back to built-in browser validation. If that failed, the backend API would handle validation. We reported validation issues to the user as early as possible given their browser, and always fell back to an acceptable experience if it failed.

I have since written a new version of this web component from scratch, aimed for general use. It’s called validation-enhancer. I have been in this industry for over 20 years, and it is the best form validation library I have ever used. I am very proud of it.

The code is so simple to work with:

<validation-enhancer>
  <form>

    <label for="my-email">Email</label>
    <input type="email" name="my-email" aria-errormessage="my-email-error" required />
    <div id="my-email-error"></div>
    
    <button type="submit">Submit</button>
  </form>
</validation-enhancer>

The results? When we launched, the number of people completing the form doubled. The analytics people didn’t even know where these users were coming from. Of course, your javascript-based analytics package doesn’t see the users you are bouncing because of javascript failures. It was a flood! We also saw my “keep a backend session, never lose user data” approach pay off. In one case, someone completed a form a month after starting it.

There was a sad coda; as is the way of contract work, I moved on. I explained what I had built to my replacement, that it always worked even without javascript. He was appalled and said, “but that’s a lot more work for us.”

It is not acceptable to bounce users on old browsers, users with bad network connections, users using assistive technologies. Certainly not from a monopoly public service. A lot of hype and noise is pressing us to extend the cowboy, wild-west phase of the software industry’s expansion. We should set that aside, and take ourselves seriously as a mature industry. Build a web application that works on a playstation portable on a 3G connection - if you do, it will work for all your users, and it will still work 30 years from now.

联系我们 contact @ memedata.com