为什么Google PageSpeed Insights告诉我缩小javascript和CSS(使用带有JS和CSS压缩的Rails 3.2)以及如何修复?

ras*_*som 10 ruby-on-rails yui-compressor ruby-on-rails-3 google-pagespeed ruby-on-rails-3.2

我知道这不是我可以用KB保存的很多,但为了在Google PageSpeed Insights中获得更好的分数,从而可能更好的SEO排名,我该如何解决这个问题?

来自https://developers.google.com/speed/pagespeed/insights/?hl=en&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.
Run Code Online (Sandbox Code Playgroud)

它告诉我我的CSS文件同样的事情.

从我的production.rb文件:

config.assets.compress = true
config.assets.js_compressor  = Uglifier.new(:mangle => true)
config.assets.css_compressor = :yui
Run Code Online (Sandbox Code Playgroud)

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

任何人都知道如何让它压缩最后一点以删除PageSpeed关于它的通知?也许使用另一台压缩机而不是Uglifier?

谢谢 :-)

alo*_*cas 14

如果您只是检查缩小的js,您将看到代码被缩小,但是您包含的每个js lib在顶部都有自己的版权和许可证信息(这是正确的),如下面的代码片段:

/**
 * Copyright 2009 SomeThirdParty.
 * Here is the full license text and copyright
 */
Run Code Online (Sandbox Code Playgroud)

如果你真的想在你的rails应用程序上实现完全缩小并且除去页面速度2%的额外压缩通知,你可以通过以下设置来实现:

config.assets.js_compressor = Uglifier.new(copyright: false)
Run Code Online (Sandbox Code Playgroud)

要么

config.assets.js_compressor = Uglifier.new(output: { comments: :none })
Run Code Online (Sandbox Code Playgroud)

注1:以上两者都将最小化您的application.js而不做任何评论.

注意2:使用上述两种设置,uglifier会从您的缩小的js中删除所有版权信息,从而使您的应用程序达到Google网页速度所需的总缩小比例,您的得分将获得+1分.

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

如何

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

/**
 * 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]
Run Code Online (Sandbox Code Playgroud)

然后通过使用链接标记指定该文件在应用程序的html头部的位置(layouts/application.html):

<head>
  <!-- License information of used libraries -->
  <link rel="license" href="../licenses.txt">
</head>
Run Code Online (Sandbox Code Playgroud)

相对=许可证表示:当前文档的主要内容是通过参考文件中描述的版权许可覆盖.

最后,如果你想对这个更正确,你应该只对简化的application.js添加一条评论,只是为了让某人能够找到所有的版权信息(如果是......).为此,请在application.js文件的顶部添加以下注释:

/*!LC
 * Copyright and Licenses: http://www.example.com/licenses.txt
*/
Run Code Online (Sandbox Code Playgroud)

所以你的application.js可能如下所示:

/*!LC
 * Copyright and Licenses: http://www.example.com/licenses.txt
*/ 
//= require jquery
//= require jquery_ujs
//= [..rest of your requires]
Run Code Online (Sandbox Code Playgroud)

注意:我在开始时将此评论与!LC区分开来.你需要这个将正则表达式传递给uglifier,因此它只允许对缩小的js进行评论.为此,请转到production.rb并放置以下内容:

config.assets.js_compressor = Uglifier.new(output: { comments: /^!LC/ })
Run Code Online (Sandbox Code Playgroud)

Uglifier将只允许您缩小的js文件的顶部!LC评论,你不会得到关于PageSpeed Insights的警告只是一个评论.如果您完成所有这些操作,那么您将完全没问题,您可以获得有关网页摘要的全面评分,您已完全缩小.js以获得最佳交付,并且您可以在任何法律问题的地方拥有所有版权.

  • 哇!优秀而且非常彻底的答案.谢谢 :-) (3认同)