有人可以告诉我为什么(或不)我们创建一行 html 的原因吗?
我知道只是减少文件大小(好处),但是在服务器中我们必须添加一些函数来生成一行 html(服务器成本),否则当我们需要更改一些代码时我们会遇到麻烦(编辑成本)。
有人可以告诉我为什么(或不)我们创建一行 html 的原因吗?
我知道只是减少文件大小(好处),但是在服务器中我们必须添加一些函数来生成一行 html(服务器成本),否则当我们需要更改一些代码时我们会遇到麻烦(编辑成本)。
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.