为什么Google PageSpeed Insights告诉我缩小javascript& CSS(将Rails 3.2与JS和CSS压缩一起使用)以及如何解决? [英] Why is Google PageSpeed Insights telling me to minify javascript & CSS (using Rails 3.2 with JS & CSS compression) and how to fix?

查看:144
本文介绍了为什么Google PageSpeed Insights告诉我缩小javascript& CSS(将Rails 3.2与JS和CSS压缩一起使用)以及如何解决?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我可以节省很多钱,但是要在Google PageSpeed Insights中获得更高的得分,从而可能获得更好的SEO排名,我该如何解决呢?

I know it's not a lot I could save in KB, but to achieve a better score in Google PageSpeed Insights, and thus probably better SEO ranking, how can I fix this?

来自 https://developers.google.com/speed/pagespeed/insights/?hl=zh-CN&url=www.tradebench.com :

Minify JavaScript for the following resources to reduce their size by 2.8KiB (2% reduction). Minifying http://d2bfamm4k6zojq.cloudfront.net/…tion-ea806932c941fb875b7512a557ebead3.js could save 2.8KiB (2% reduction) after compression.

它告诉我CSS文件的内容.

It tells me the same thing for my CSS file.

从我的production.rb文件中:

From my production.rb file:

config.assets.compress = true
config.assets.js_compressor  = Uglifier.new(:mangle => true)
config.assets.css_compressor = :yui

看着uglifier docs/options,我看不到如何配置它以获取最后的2KB.

Looking at the uglifier docs/options, I don't see how I could configure it to get the last 2KB.

任何人都有一个主意,该如何压缩最后一点以删除有关它的PageSpeed通知?也许会使用除Uglifier以外的其他压缩机?

Anyone has an idea how to get it to compress the last tiny bit to remove the PageSpeed notice about it? Maybe use another compressor than Uglifier?

谢谢:-)

推荐答案

如果仅检查缩小的js,您将看到代码已缩小,但是您包含的每个js库的顶部都有其自己的版权和许可信息(正确),例如以下代码段:

If you just inspect your minified js, you will see that the code is minified but every js lib you include has its own copyright and license information at the top(which is correct), like the following snippet:

/**
 * Copyright 2009 SomeThirdParty.
 * Here is the full license text and copyright
 */

如果您确实希望在Rails应用程序上实现完全的缩小并摆脱Pagespeed见解的2%额外压缩通知,则可以通过以下设置来做到这一点:

If you really want to achieve complete minification on your rails app and get rid of the 2% extra compression notice of pagespeed insights you could do so by having the following setting:

config.assets.js_compressor = Uglifier.new(copyright: false)

config.assets.js_compressor = Uglifier.new(output: { comments: :none })

注释1::以上两种都将使您的application.js最小化,而无需任何评论.

NOTE1: Both of the above will minimize your application.js without any comments.

注意2::使用上述两项设置,uglifier会从缩小的js中删除所有版权信息,因此您的应用达到了Google Pagespeed所需的总缩小量,并且获得了 +1分.

NOTE2: With both of the settings above, uglifier removes all the copyright information from your minified js and thus your app achieve the total minification required from google pagespeed and you gain +1 point at your score.

但是,您不应该避免包括所使用代码的版权.大多数许可证( MIT BSD GPL ...)要求您在重新分发库时保留版权和许可信息.当您允许浏览器从服务器或CDN下载库的压缩副本时,在重新分配库时的计数.因此,您应在您应用的某些位置包含版权和许可信息.

However, you should not avoid to include the copyrights of the code you are using. Most of the licenses (MIT, BSD, GPL...) require that you keep the copyright and licensing information whenever you redistribute the library. When you allow browsers to download a compressed copy of the library from your server or from your CDN counts as you redistributing the library. Thus you SHOULD INCLUDE the copyright and license information at some place of your app.

您可以做的一件事是收集您在应用程序上使用的js库的所有版权信息,并将它们全部添加到licenses.txt中,并将其放置在公用文件夹中.您的 public/licenses.txt 应该如下所示:

One thing you could do is to collect all the copyright information of your js libraries you are using on your app and add them all in a licenses.txt and place it at your public folder. Your public/licenses.txt should look like the following:

/**
 * Unobtrusive scripting adapter for jQuery
 * https://github.com/rails/jquery-ujs
 *
 * Requires jQuery 1.8.0 or later.
 *
 * Released under the MIT license
 *
*/
[...and the rest copyrights here one after the other]

然后使用 link标记指定该文件在应用程序html头(layouts/application.html)上的位置:

Then by using a link tag specify the location of this file on the html head of the app(layouts/application.html):

<head>
  <!-- License information of used libraries -->
  <link rel="license" href="../licenses.txt">
</head>

rel = license 表示:当前文档的主要内容由引用文档描述的版权许可覆盖.

最后,如果您想对此做得更正确,则应仅在缩小的application.js上添加一条评论,以使某个人可以确切地找到所有版权信息(如果有的话..).为此,请在application.js文件顶部添加以下注释:

Finally, if you want to be more correct with this one, you should include just one comment on the minified application.js just to let someone where exactly can find all the copyright information(in case of whatever..). In order to do that, add the following comment on top of your application.js file:

/*!LC
 * Copyright and Licenses: http://www.example.com/licenses.txt
*/

因此您的application.js可能如下所示:

so your application.js might look like the following:

/*!LC
 * Copyright and Licenses: http://www.example.com/licenses.txt
*/ 
//= require jquery
//= require jquery_ujs
//= [..rest of your requires]

注意::我首先以!LC 来区分此评论.您需要使用此代码将 regexp 传递给uglifier,以便允许对小型化的JS 仅此注释.为此,请转到您的production.rb并放置以下内容:

NOTE: I have distinguish this comment with an !LC at the start. You need this one to pass a regexp to the uglifier so it will allow ONLY THIS comment on the minified js. To do that, go to your production.rb and place the following:

config.assets.js_compressor = Uglifier.new(output: { comments: /^!LC/ })

Uglifier只会在缩小的js文件的顶部仅包含!LC注释,您将不会对仅一个注释的pagespeed洞察力发出警告.如果您完成所有这些操作,您将完全没问题, 您将获得关于页面速度洞察力的最高分,并且完全精简了.js以实现最佳投放效果,并且所有版权均已保存在任何法律问题的地方.

Uglifier will allow only the !LC comment on top of your minified js file, you wont get warning on the pagespeed insights for just one comment. If you do all these you will be totally fine, you get full score on pagespeed insights, you have completelly minified .js for optimal delivery and you have all your copyrights at a place for any legal issues.

这篇关于为什么Google PageSpeed Insights告诉我缩小javascript&amp; CSS(将Rails 3.2与JS和CSS压缩一起使用)以及如何解决?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆