简体   繁体   English

包括CSS文件和Javascript文件的加载-如何进行优化

[英]Including loads of css files and Javascript file - how do I optimize

Duplicate: Multiple javascript/css files: best practices? 重复: 多个javascript / css文件:最佳做法?

Hi guys, my application is almost done but the thing is that I've noticed that I'm including a lot of external javascript files and css. 大家好,我的应用程序即将完成,但事实是我注意到我包含了很多外部javascript文件和CSS。 I'm using a lot of third party free plugins here and didnt want to touch the code for fear of messing something up. 我在这里使用了许多第三方免费插件,并且不想触摸代码,以免弄乱了某些东西。

But the result is that I notice that I have now included 8 external css files as well as a whopping 20 external Javascript files :O - I don't recall having seen any major website include more than 2 or 3 css_ js files combined so I definitely must be doing something wrong here. 但是结果是,我注意到我现在已经包含了8个外部CSS文件以及多达20个外部Javascript文件:O-我不记得有哪个主要网站包含超过2个或3个CSS_ JS文件,所以我肯定在这里做错了。 How do I sort this out - I read somewhere that one large js file is more quicker to load than 2 or 3 files which are even half the accumulated size. 我该如何解决-我读到某个地方,一个大型js文件的加载速度比2或3个文件(甚至是累加大小的一半)要快。

Any help would be greatly appreciated 任何帮助将不胜感激

One big file is better than a bunch of small because in this case web browser makes one request to the web server instead of, say, 8 requests. 一个大文件优于一堆小文件,因为在这种情况下,Web浏览器向Web服务器发出一个请求,而不是8个请求。 What counts more is not the slight differences in total size, but the total latency of the requests. 更重要的不是总大小的细微差别,而是请求的总延迟

Imagine two scenarios: you download one file of 8 kB and 8 files of 1kB each. 想象一下两种情况:分别下载一个8 kB的文件和8个1kB的文件。

  1. In the first scenario total time is smth like 80 msec (transfer time) + 50 msec (latency) = 130 msec 在第一种情况下,总时间为80毫秒(传输时间)+ 50毫秒(延迟)= 130毫秒

  2. In the second scenario you have smth like 8x10 msec (transfer time) + 8x50 msec (!) of latency = 480 msec (!) 在第二种情况下,您有8x10毫秒(传输时间)+ 8x50毫秒(!)的延迟= 480毫秒(!)

You see the difference. 您会看到差异。 This is by no means a comprehensive example, but you get the idea. 这绝不是一个全面的示例,但是您可以理解。

So, if possible, merge files together. 因此,如果可能,将文件合并在一起。 Compress content to decrease the amount of data to transfer. 压缩内容以减少要传输的数据量。 And use caching to get rid of repetitive requests. 并使用缓存摆脱重复的请求。

If you combine your CSS files into one file (and same for JS) in the order they are currently loaded to the page then they will work exactly the same as before; 如果按当前将CSS文件加载到页面的顺序将CSS文件组合到一个文件(对于JS同样),则它们的工作方式将与以前完全相同。 it's when you start changing the order that rules can unexpectedly override ones they didn't used to. 当您开始更改规则的顺序时,规则可能会意外覆盖它们不习惯的规则。

Most of your files should wind up in the client side cache anyway so I wouldn't be too worried about it. 无论如何,您的大多数文件都应该存储在客户端缓存中,因此我不必太担心。 Just make sure you're setting the right headers. 只要确保您设置了正确的标题即可。 Of course if this is the front page of your site then yeah, you probably should be optimising further. 当然,如果这是您网站的首页,是的,您可能应该进一步优化。

In past projects, I used SmartOptimizer for compressing JS and CSS files on the fly. 在过去的项目中,我使用SmartOptimizer即时压缩JS和CSS文件。 It's a PHP based file minifier. 这是一个基于PHP的文件压缩程序。

By using a just in time minifier frees you from having to keep seperate source and minified files. 通过使用及时的缩小器,您无需保留单独的源文件和缩小的文件。 Of course, there's a little processing power needed to keep minifying the files. 当然,保持文件最小化只需要一点处理能力。 Also, loading a single minified file is better with the following advantages: 此外,加载单个压缩文件更好,它具有以下优点:

  1. Smaller file size (for sure) 较小的文件大小(确定)
  2. No component parallel download delay because fewer scripts/stylesheets. 没有组件并行下载延迟,因为更少的脚本/样式表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM