我读到将所有的 css 文件合并成一个大文件,或者将所有的脚本文件合并到一个脚本文件中可以减少 HTTP 请求的数量,从而加快下载速度。
但是我不明白这一点。我认为如果你有多个文件(最多一个限制,我相信在现代浏览器上是 10 个),浏览器会并行下载它们,从而减少总下载时间(除以允许的连接数)。
我显然在这里遗漏了一条关键信息。有人可以开灯吗?
最佳答案
每个请求/响应都有开销。本质上就是这样。
这是向 Google 发送请求 header 的示例 ...
GET http://www.google.com/ HTTP/1.1 Accept: application/x-ms-application, image/jpeg, application/xaml+xml, image/gif, image/pjpeg, application/x-ms-xbap, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-shockwave-flash, / Accept-Language: en-US User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/4.0; GTB0.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; OfficeLiveConnector.1.4; OfficeLivePatch.1.3) Accept-Encoding: gzip, deflate Connection: Keep-Alive Host: www.google.com Cookie: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
我去年写了一篇关于这个的文章...... http://swortham.blogspot.com/2010/03/latency-requests-css-sprites-and-you.html
您说得对,可以并行下载多个文件(从一个主机名下载 2 个或更多文件,具体取决于浏览器)。这反过来又会导致页面逐渐加载,这很好。但这并不意味着你的主页就应该由20+个css、js、图片文件组成。理想情况下,您希望结合相当多的内容来优化网站。
关于html - 网页优化 : Why are Combined Files Faster?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4938610/