简体   繁体   English

网站优化-压缩.JS / .CSS文件

[英]Website Optimization - Compactation of .JS/.CSS files

Well, I didn't know Google already had a Mechanism that give to you some hints on what you can make your modifications to optimize your website. 恩,我不知道Google已经拥有一种机制 ,可以为您提供一些提示,说明您可以进行哪些修改以优化网站。 In my analyzes I saw an interesting thing like compacting js and css files. 在分析中,我看到了一个有趣的事情,例如压缩js和css文件。 I googled and found out tools to help on this such as 我在Google上搜索了一下,发现了可以帮助解决此问题的工具,例如

gzip/deflate gzip /放气

, but I have no idea how to use them. ,但我不知道如何使用它们。 Googleling, I saw something like putting in my http header a meta Accept-encoding: "gzip, deflate" - but I'm not too sure - then when done this, when your site make a request saying "Hello, I accept-encoding: gzip and deflate.", then my server would process everything needed and would return a response already containg all the files compacted (How's that?). Googleling,我看到了类似在我的http标头中放置一个元Accept-encoding的代码:“ gzip,deflate”-但是我不太确定-然后,当您这样做时,当您的网站发出一个请求说“你好,我接受-编码” :gzip和deflate。”,那么我的服务器将处理所需的所有内容,并返回已经包含所有压缩文件的响应(那是什么?)。

Some examples in topics I saw, they talked about server apache and the first reference was this one How to optimize your website , but in my case the server is written in python. 在我所看到的主题中,有一些示例涉及服务器apache,而第一个参考文献是“ 如何优化您的网站” ,但就我而言,服务器是用python编写的。 So how do I do the magical thing in compacting everything and send them all in a response? 那么,如何在压缩所有内容并将其全部发送给响应时做神奇的事情?

I know is a long way to go, and I think I need a good reference explaining how to do this. 我知道还有很长的路要走,我想我需要一个很好的参考来说明如何执行此操作。

Have you passed through this kind of thing? 你经历过这种事情了吗? If so, well, could you give some ideas? 如果是这样,那么,您能提供一些想法吗?

Thanks in advance. 提前致谢。

http://jscompress.com/ http://jscompress.com/

This will minify (compress) your files JS for you. 这将为您最小化(压缩)文件JS。

http://cssminifier.com/ http://cssminifier.com/

This will do the same for CSS 这与CSS相同

http://www.willpeavy.com/minifier/ http://www.willpeavy.com/minifier/

And why not do the HTML while we are at it 为什么不使用HTML呢?

There's two answers to your question I can think of off the top of my head: 对于您的问题,我可以想到两个答案:

  1. 'Compacting' JS/CSS probably refers to the practice of concatenating and minifying your JS/CSS payloads. “压缩”的JS / CSS可能是指串联和最小化JS / CSS负载的做法。 You can concatenate with cat and then minify with uglifyjs or yuicompressor respectively. 您可以与cat串联,然后分别与uglifyjsyuicompressor缩小。 This reduces the number of individual requests your sending, and provides great cheap gains. 这减少了您发送的单个请求的数量,并提供了可观的廉价收益。 Bear in mind that you might want to do this per-page if you have different dependencies for different views, otherwise you incur the overhead of running javascript you're not using. 请记住,如果您对不同的视图有不同的依赖关系,则可能需要按页面执行此操作,否则会产生运行不使用的javascript的开销。

  2. gzipping the content you serve has to be done carefully. gzip压缩您所提供的内容必须谨慎。 Ideally it's just something you enable on your webserver. 理想情况下,这只是您在网络服务器上启用的功能。 For example in an nginx server definition: 例如,在Nginx服务器定义中:

     server { gzip_types text/plain text/css application/json application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript; } 

    If you're doing it at the web application level, you could be introducing more latency than is really worth it to compress and decompress your data. 如果要在Web应用程序级别执行此操作,则可能会引入比压缩和解压缩数据真正值得的延迟更多的延迟。

I use this to combine around 23 or so core js files into 1 and compress it. 我用它来将大约23个核心js文件合并为1并压缩它。 https://developers.google.com/closure/compiler/ It has a handful of useful settings, like the ability to keep in tact the js files header/license text. https://developers.google.com/closure/compiler/它具有一些有用的设置,例如能够保持js文件的标头/许可文本不变。

They also have an equivalent for css https://code.google.com/p/closure-stylesheets/ (scroll down to the "minification" section). 它们也具有CSS https://code.google.com/p/closure-stylesheets/的等效项(向下滚动到“最小化”部分)。

If this is something you think you would like to try I could give you head start with the java setup and creating a batch file to do your combining/compressing. 如果您认为这是您要尝试的方法,我可以让您从Java设置入手,并创建一个批处理文件来进行合并/压缩。

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

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