问题原文
原帖链接 · Omar Rahman · CC BY-SA 4.0
If website load time is a concern, which is generally better to use when referencing a JavaScript or CSS file? Would using a CDN be better or would that just add more HTTP requests?
已采纳解答
原帖链接 · Analog · CC BY-SA 3.0
The CDN should be used for all static files (.css/.js/images).
Sometimes however javascript or css files can have dynamic aspect to them such as that it would include a unique user string or something of that sort. In this situation the CDN server would have to contact the origin server on every request which would defeat the purpose.
If your CSS and Javascript is static for all users then using a CDN is the right way to go. This would not cause any extra HTTP requests because it would only load the css and js files from the CDN instead of your own webserver (unless you are using inline code). So instead of a users browser loading these requests from your server they would be loaded via the CDN, there is no additional requests you are just changing where to send these requests (again as long as you are currently not using inline code).
Other benefits from using a CDN would be that the CDN server would most likely be located closer to your end users then your origin which will benefit loading times. The CDN servers are also likely setup to serve static content much faster then your origin server by specifically catering the web server for static content.
摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。
