HTML优先
HTML First

原始链接: https://html-first.com/

HTML First 是一种简化网站建设的理念。 通过利用现代 Web 浏览器的默认功能、HTML 的简单属性语法并消除不必要的构建步骤,HTML First 促进了易于理解、增加乐趣和无缝开发。 普通 HTML 属性不采用基于 JavaScript 的框架,而是提供了包含行为和样式的简单方法,而无需第三方库。 这种方法不是严格分离关注点,而是强调行为的局部性并支持裸 HTML 而不是混淆的层。 此外,保持右键单击查看源代码的功能可以让您通过复制粘贴轻松共享和修改网站组件。 总体而言,HTML First 提供了一种独特的解决方案,可以创建更好的网络体验,同时促进该领域更广泛的可访问性。

关于有人声称有一天可能会纯粹使用 HTML、CSS、JavaScript 和 TypeScript 开发一个复杂的 Web 应用程序 – 这并不是一个不可能的任务。 然而,实现这些目标的实用性和可行性目前仍不确定。 目前,各种各样的前端框架和库为开发人员提供了更多的便利和生产力。 尽管如此,值得考虑的是,采用这种方法是否符合行业流行的范例和最佳实践,特别是考虑到有关潜在陷阱的论点,例如渲染速度较慢、文件大小增加以及错误和不一致的可能性更大。 最终,这种方法在实践中是否成功和采用仍有待确定,具体取决于技术进步、不断变化的市场条件和用户需求等因素。 尽管如此,对这些概念的持续探索为软件工程领域的新兴趋势和发展提供了宝贵的见解,应该仔细考虑。
相关文章

原文

HTML First is a set of principles that aims to make building web software easier, faster, more inclusive, and more maintainable by...

  1. Leveraging the default capabilities of modern web browsers.
  2. Leveraging the extreme simplicity of HTML's attribute syntax.
  3. Leveraging the web's ViewSource affordance.

The main goal of HTML First is to substantially widen the pool of people who can work on web software codebases. This is good from an individual perspective because it allows a greater number of people to become web programmers, to build great web software, and increase their income. It's also good from a business perspective as it decreases the cost of building software, and decreases the amount of resources required to hire - a notoriously resource intensive process.

A second goal of HTML First is to make it more enjoyable and seamless to build web software. Most web programmers are familiar with the excitement of seeing their product come together rapidly as they transition smoothly between the text editor and the browser, with very few unexpected potholes or context switches. But today it takes several years of mastering tools and frameworks to get to that stage. HTML First principles should allow people to unlock that feeling, and level of mastery, much earlier on in their coding journey.

The way we achieve these goals is by acknowledging that HTML is very easy to understand, and thus using HTML as the bedrock of our product - not only to define content and structure, but also to set styling and behaviours.

Use "vanilla" approaches to achieve desired functionality over external frameworks

The range of things that browsers support out of the box is large, and growing. Before adding a library or framework to your codebase, check whether you can achieve it using plain old html/css.
Encouraged

Click to toggle content

This is the full content that is revealed when a user clicks on the summary

Discouraged

import React, { useState } from 'react';

const DetailsComponent = () => {
  const [isContentVisible, setContentVisible] = useState(false);

  const toggleContent = () => {
    setContentVisible(!isContentVisible);
  };

  return (
    
Click to toggle content {isContentVisible &&

This is the full content that is revealed when a user clicks on the summary

}
); }; export default DetailsComponent;

Where possible, default to defining style and behaviour with inline HTML attributes

For styling this can be enabled with an SPC library like Tailwind or Tachyons. For behaviour, you can use libraries like hyperscript, Alpine, or similar. Yes, this does mean your HTML will look busy. But it also means it will be easier for other developers to find and understand behaviour, navigate it, and make changes to it.

Encouraged


Discouraged

Click Me

#results-pane.active {
  background-color: green;
}
var resultsPane = document.getElementById("myDiv");
resultsPane.addEventListener("click", function() {
    this.classList.add("active");
});

You may notice that this approach seems to violate Separation of Concerns - one of the most commonly-touted software design principles. We believe an all-or-nothing approach to SoC is flawed, and instead advocate an approach that accounts for Locality of Behaviour and acknoweldges the trade-offs between the two.

Where libraries are necessary, use libraries that leverage html attributes over libraries built around javascript or custom syntax

Encouraged



Discouraged







Steer Clear of Build Steps

Libraries that require transforming your files from one format to another add significant maintenance overhead, remove or heavily impair the ViewSource affordance , and usually dictate that developers learn new tooling in order to use them. Modern browsers don't have the same performance constraints that they did when these practices were introduced. And if we use HTML First libraries like static tailwind or htmx, the amount of additional CSS and JS needed is usually minimal.

Encouraged


Discouraged


npx css-compile -i ./src/input.css -o ./dist/output.css --watch

Aside: The build step practice is so deeply ingrained that even one year ago this opinion was considered extremely fringe. But in the last year has begun to gain significant steam. Some recent examples:

Prefer "naked" HTML to obfuscation layers that compile down to HTML

This principle is most applicable to backend implementation. The underlying idea again here is readability. If a developer who has familiarity with HTML but not with your backend framework looks through your view files, they should still be able to understand 90%+ of what they see. As with above, this means sacrificing brevity for understandability.

Encouraged


Discouraged


  

  

  

  

Where possible, maintain the right-click-view-source affordance

The beauty of the early web was that it was always possible to "peek behind the curtains" and see the code that was responsible for any part of any web page. This was a gift to aspiring developers, as it allowed us to bridge the gap between the theoretical (reading about how code works) and the practical - seeing both code and interface alongside each other. For many sites, we could copy and paste the html or css and run it in ourselves to get a close-to-identical replica. "Remixing" existing snippets was not only a way to learn, but often formed the basis of our new creations.

In the time since, the industry has adopted several "improvements" which have made this practice much rarer. For example, if we use React - the most popular frontend framework, we cannot hit "View Source", copy the code, and remix it, because 1. React has a build step, meaning the code we see in the developer tools is different to the code that the developer wrote, and 2. React code snippets must be wrapped in a react application in order for them to work.

For sites that follow HTML First principles, we regain the ViewSource affordance again. In fact, HTML First sites often go one step further. Because if you define your UI interactions using HTML attributes, you can now also preserve these interactions when copy pasting into a new codebase, (provided your destination file includes the same js library). At some point we intend to levearge this to build an HTML First code snippet library.

Wrapping Up

The practices and principles described on this site are still considered niche in the industry as a whole, and the community of people using them small. One of my hopes with creating this site is to act as a Honeypot to find and connect like minded people with whom we can discuss and sharpen these ideas. If any of this resonates with you, I'd love to hear from you.

联系我们 contact @ memedata.com