1

有人可以告诉我为什么(或不)我们创建一行 html 的原因吗?

我知道只是减少文件大小(好处),但是在服务器中我们必须添加一些函数来生成一行 html(服务器成本),否则当我们需要更改一些代码时我们会遇到麻烦(编辑成本)。

4

2 回答 2

0

You don't manually create HTML like that. Personally, my CMS caches it's output as static files to be served to the user. Before it caches, it runs:

$tocache is the contents of the page to be displayed. I do this, then write it to disk. Apache then serves the static content instead avoiding the DB and PHP on subsequent access.

// Remove white space
$tocache = str_replace(array("\n", "\t","\r")," ",$tocache);

// Remove unnecessary closing tags (I know </p> could be here, but it caused problems for me)
$tocache = str_replace(array("</option>","</td>","</tr>","</th>","</dt>","</dd>","</li>","</body>","</html>"),"",$tocache);

// remove ' or " around attributes that don't have spaces
$tocache = preg_replace('/(href|src|id|class|name|type|rel|sizes|lang|title|itemtype|itemprop)=(\"|\')([^\"\'\`=<>\s]+)(\"|\')/i', '$1=$3', $tocache);

// Turn any repeated white space into one space
$tocache = preg_replace('!\s+!', ' ', $tocache);

Now, I run that once per page change, then serve up the smaller HTML to users.

This is pretty much pointless though, as the process of gzipping makes the biggest difference. I do it because I might as well – I already am caching these files, so why not make myself feel clever first!

For CSS and JS I use SASS's compressed option, and uglifyJS to get those as one small file.

That means on a page I have 1 HTML file, 1 CSS and 1 JS, minimising the number of HTTP reqs and the amount of data to be transmitted.

Gzip + ensuring 1 css and 1 js is the biggest savings though.

于 2013-01-04T17:02:08.413 回答
0

你不应该缩小你的 HTML。编辑时不值得花时间和工作。看看这个非常好的答案
不过,您应该优化您的 CSS。有一个免费程序:Scout,它会检查您的 css 文件的更改并自动创建它的缩小版本。它实现了Sass,这将使编写 CSS 变得更加容易。你应该试试看。

于 2013-01-04T16:54:22.480 回答