为什么许多网站缩小 CSS 和 JavaScript 而不是 HTML?

发布于 2024-08-23 05:28:00 字数 268 浏览 5 评论 0原文

可能的重复:
为什么要缩小资产而不是标记?

我见过很多网站使用缩小的 CSS 和 JavaScript 来增加网站响应时间,但我从未见过任何网站使用缩小的 HTML。为什么你不希望你的 HTML 被缩小呢?

Possible Duplicate:
Why minify assets and not the markup?

I have seen a lot of sites using minified CSS and JavaScript to increase website response time but I have never seen any sites use minified HTML. Why would you not want your HTML to be minified?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

苄①跕圉湢 2024-08-30 05:28:00

因为如果你做得正确的话,你无论如何都会提供压缩的 HTML,所以 HTML 缩小的容易实现的成果 - 空白 - 并不是那么重要。 HTML 中没有很多简单的目标(例如变量名),而 CSS 和 JavaScript 中却存在这些目标。 HTML 的大部分内容是页面的实际内容,可能无法缩小(并且,正如其他人指出的那样,几乎肯定会比 CSS 或 JS 更频繁地变化)。

Because if you're doing things properly you're serving HTML gzipped anyway, so the low-hanging fruit of HTML minification - whitespace - isn't all that relevant. There aren't lots of easy targets (e.g. variable names) for minification in HTML, which are present in CSS and JavaScript. Much of the content of HTML is the actual content of the page, which probably can't be minified (and, as others have pointed out, will almost certainly vary more frequently than your CSS or JS).

梦里寻她 2024-08-30 05:28:00

我猜大多数网站都有静态 CSS 和 Javascript。这意味着它们每次更新时都可以缩小一次。另一方面,HTML 往往是动态生成的,这意味着它必须在每个页面请求上进行缩小,这比缩小静态 CSS 和 Javascript 文件要昂贵得多。

I'd guess that most sites have static CSS and Javascript. This means they can be minified just once whenever they are updated. On the other hand, HTML tends to be dynamically generated, which means it would have to be minified on every page request, which is considerably more expensive than minifying static CSS and Javascript files.

路还长,别太狂 2024-08-30 05:28:00

我认为 HTML 中没有太多缩小的空间:您可以删除空格和换行符,但本质上,这就是它,而无需实际进入页面的结构。

JS 缩小可以缩短变量和函数名称,这可能是节省空间方面最大的净利润。由于其固定的标签集,HTML 不提供这种可能性。

gzip 压缩 HTML 的选项可能消除了大部分缩小的需要,特别是因为它通常针对 HTML 启用,而它(不必要)并不总是针对 CSS 和 JS 文件类型。

I don't think there is that much room for minification in HTML: You can remove white spaces and line breaks, but essentially, that's about it without actually getting into the page's structure.

JS minification can shorten variable and function names, probably the biggest net profit in terms of saved space. With its fixed set of tags, HTML does not provide that possibility.

The option of gzipping HTML probably eliminates much of the need to minify anyway, especially as it is usually enabled for HTML, while it (unnecessarily) not always is for the CSS and JS file types.

影子的影子 2024-08-30 05:28:00

主要是因为 Javascript 文件和 CSS 样式表通常是静态文件,在部署时不会更改。另一方面,标记通常是动态生成的(至少对于数据库驱动的 Web 应用程序而言),并且“页面”的数量通常很大且动态,这使得缩小的好处比其价值更多。

Primarily because Javascript files and CSS stylesheets are often static files that will not change upon deployment. Markup, on the other hand, is often generated on the fly (with database-driven web apps, at least), and the number of "pages" is usually large and dynamic, which makes the benefits of minification more work than it's worth.

度的依靠╰つ 2024-08-30 05:28:00

被 gzip 压缩的 Html 内容负责大部分压缩,在此基础上进行缩小不会实现太多效果或节省大量带宽。

是缩小每一部分(如果生成了怎么办?)或者让它一直缩小(做噩梦?)

你可以将 JavaScript 作为构建的一部分进行缩小,整个 HTML 内容发生这种情况的唯一方法 成本与收益,成本:边际带宽,收益:更容易处理,更容易生成,更容易调试,在我的源代码视图窗口中很漂亮。

Html content being gzipped takes care of most of the compression, minifying on top of that wouldn't accomplish much or save a great deal of bandwidth.

Javascript you can minify as part of the build, the only way this would happen with the entire HTML content would be to minify every piece (what if it's generated?) or to have it minified the whole time (nightmare to work on?)

It's cost vs benefit, cost: marginal bandwidth, benefit: easier to work on, easier to generate, easier to debug, pretty in my source view window.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文