Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/52.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 为什么谷歌PageSpeed Insights告诉我缩小javascript&;CSS(使用Rails3.2和JS&;CSS压缩)以及如何修复?_Ruby On Rails_Ruby On Rails 3_Ruby On Rails 3.2_Yui Compressor_Google Pagespeed - Fatal编程技术网

Ruby on rails 为什么谷歌PageSpeed Insights告诉我缩小javascript&;CSS(使用Rails3.2和JS&;CSS压缩)以及如何修复?

Ruby on rails 为什么谷歌PageSpeed Insights告诉我缩小javascript&;CSS(使用Rails3.2和JS&;CSS压缩)以及如何修复?,ruby-on-rails,ruby-on-rails-3,ruby-on-rails-3.2,yui-compressor,google-pagespeed,Ruby On Rails,Ruby On Rails 3,Ruby On Rails 3.2,Yui Compressor,Google Pagespeed,我知道我可以节省的KB不多,但要在Google PageSpeed Insights中获得更好的分数,从而可能获得更好的SEO排名,我该如何解决这个问题 发件人: 对于我的CSS文件,它告诉我同样的事情 从my production.rb文件: config.assets.compress = true config.assets.js_compressor = Uglifier.new(:mangle => true) config.assets.css_compressor = :y

我知道我可以节省的KB不多,但要在Google PageSpeed Insights中获得更好的分数,从而可能获得更好的SEO排名,我该如何解决这个问题

发件人:

对于我的CSS文件,它告诉我同样的事情

从my production.rb文件:

config.assets.compress = true
config.assets.js_compressor  = Uglifier.new(:mangle => true)
config.assets.css_compressor = :yui
看看那些丑陋的文档/选项,我不知道如何配置它来获得最后2KB

有人知道如何让它压缩最后一小段以删除关于它的PageSpeed通知吗?也许用另一台比丑陋的压缩机


谢谢:-)

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

/**
 * Copyright 2009 SomeThirdParty.
 * Here is the full license text and copyright
 */
如果您确实希望在rails应用程序上实现完全缩小,并摆脱pagespeed insights 2%的额外压缩通知,您可以通过以下设置来实现:

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

注1:以上两种方法都会在不添加任何注释的情况下最小化application.js

注2:使用上述两种设置,uglifier将从缩小的js中删除所有版权信息,从而使您的应用程序实现google pagespeed所需的总体缩小,您将获得+1分

但是,您不应该避免包含您正在使用的代码的版权。大多数许可证(MITBSDGPL…)要求您在重新分发库时保留版权和许可信息。当您允许浏览器从服务器或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]
/*!LC
 * Copyright and Licenses: http://www.example.com/licenses.txt
*/ 
//= require jquery
//= require jquery_ujs
//= [..rest of your requires]
config.assets.js_compressor = Uglifier.new(output: { comments: /^!LC/ })
然后通过使用链接标记指定此文件在应用程序(layouts/application.html)的html头上的位置:

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

/**
 * 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]
/*!LC
 * Copyright and Licenses: http://www.example.com/licenses.txt
*/ 
//= require jquery
//= require jquery_ujs
//= [..rest of your requires]
config.assets.js_compressor = Uglifier.new(output: { comments: /^!LC/ })
注意:我已将此评论与区分开来!LC开始时。你需要用这个来传递一个regexp给丑八怪,这样它就只允许在缩小的js上发表这个评论。为此,请转到production.rb并放置以下内容:

/**
 * 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]
/*!LC
 * Copyright and Licenses: http://www.example.com/licenses.txt
*/ 
//= require jquery
//= require jquery_ujs
//= [..rest of your requires]
config.assets.js_compressor = Uglifier.new(output: { comments: /^!LC/ })

丑八怪只会允许你这么做!LC注释在缩小的js文件上,你不会因为一条注释而在页面上得到警告。如果你做了所有这些,你会很好,你在pagespeed insights上得到满分,你已经完全缩小了.js以获得最佳的交付,并且你拥有所有的版权,可以处理任何法律问题。

不用担心。页面速度是错误的,试图删除该通知是毫无意义的。谢谢,尽管我猜谷歌使用它作为搜索引擎优化排名的一部分。你确定他们没有?这是速度优化,不是搜索引擎优化。我非常怀疑谷歌会因为你发送了额外的2kb而惩罚你。哇!回答得非常好,非常彻底。谢谢:-)非常有趣-回答得很好。尤其是铁路线。谢谢