网站页面加载时间和保持活动的 http 连接
背景:我更喜欢将所有 CSS 和 Javascript 保存在单独的 .css/.js 文件中。 (原因是它们被许多页面共享,因此通过这种方式,这些文件中的静态数据不会随每个页面视图一起传输)。这导致某些页面有 5-6 个“link rel”或“script type”语句。
现在,通常情况下,这意味着浏览器将为每个 css/js 文件发出单独的请求,并且有效页面加载时间会增加 - 比如说 5 个往返请求变为 5 倍(如果我在这里错了,请纠正我)。
我的问题是:
1)现代浏览器默认请求保持活动连接吗?
2)如果这样做,那么是否意味着额外的文件来源(css / js)不会增加有效加载时间?例如 - 服务器是否会假设浏览器将请求 css/js 文件,因此继续发送它(从而避免额外的请求)?
简而言之 - 谁能解释一下,通过使用单独的 css/js 文件,加载时间何时不会增加?如果它总是增加,那么加载时间的增加是否与包含的文件数量成正比?
问候,
太平绅士
Background: I prefer to keep all my CSS and Javascript in separate .css/.js files. (Reason is that they are shared by many pages so in this way, static data in those file would not get transferred with each page view). This leads to some pages having 5-6 "link rel" or "script type" statements.
Now, normally, this would mean that browser would make separate request for each of those css/js files, and effective page load time can increase - say become 5X for 5 round trip requests (please correct me if I am wrong here).
My question is:
1) Do modern browsers request keep-alive connections by default?
2) If they do, then does it mean that additional file sourcing (css/js) will not increase the effective load time? For example - will the server assume that browser is going to request the css/js files, and hence keep sending it (thus avoiding an extra request)?
In short - can anyone explain when would the load time NOT increase by having separate css/js files and if it always increases, then is the load time increase proportional to the number of files included?
regards,
JP
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,使用 HTTP/1.1 的浏览器通常应该使用 keep-alive 连接。
服务器不一定会假设它;保持活动应该意味着没有单独的 DNS 查找 - 文件仍然会被请求。
如果配置正确,您应该会发现(例如可以使用 Firefox+Firebug 进行验证)第一个请求生成这些文件的正常下载,但后续请求生成 HTTP 304(文件未修改)请求,其中内容自此以来没有更改最后一次加载,因此它应该可以从缓存中获得。
如果这是一个真正的问题,您可能需要考虑使用 Expires 标头来指定文件的长寿命,因此根本不会发出请求(甚至不会发出查看文件是否已更改的请求)
Yes, browsers using HTTP/1.1 should generally be using keep-alive connections.
The server won't necessarily assume it; the keep-alive should mean there isn't a separate DNS lookup - the file still gets requested.
If properly configured, you should find (which can be verified with Firefox+Firebug for example) that the first request generates a normal download of those files, but subsequent requests generate HTTP 304 (File Not Modified) requests where the content has not changed since the last load and as such it should be available from cache.
If it is a real concern, you may want to look into using Expires headers to specify long-life of the files, so the request never even gets made (not even the request to see if the file has changed)