简体   繁体   English

无法通过本地生产轨道3.1.3服务器上的管道配置资产

[英]Cannot configure assets via pipeline on local production rails 3.1.3 server

This month, I upgraded from Rails 3.0 to Rails 3.1 - this week I tried to launch the server in production mode - today I have hit a wall ! 本月,我从Rails 3.0升级到Rails 3.1--本周我尝试在生产模式下启动服务器 - 今天我已经碰壁了!

I am unable to get my production environment server to serve up my public assets (JavaScript and CSS) via the asset pipeline, unless I set config.assets.compile = true in my environment.rb file, which for speed reasons I obviously don't want to do. 我无法让我的生产环境服务器通过资产管道提供我的公共资产(JavaScript和CSS), 除非我在我的environment.rb文件中设置了config.assets.compile = true ,由于速度原因,我显然不喜欢我想做。

I have a large number of JS and CSS files, each of which tends to get used on one or two different pages. 我有大量的JS和CSS文件,每个文件都倾向于在一个或两个不同的页面上使用。 This means creating a single "manifest" file doesn't fit my usage, as each page wants something slightly different. 这意味着创建单个“清单”文件不符合我的用法,因为每个页面都需要稍微不同的东西。 I also expect some of the CSS won't sit together nicely. 我也期望一些CSS不会很好地融合在一起。 Therefore I have gone down the approach of "just get it working", looking to tidy up the large quantity of CSS / JS later. 因此,我已经采取了“只是让它工作”的方法,希望以后整理大量的CSS / JS。

Here is the production.rb file: 这是production.rb文件:

Implicit::Application.configure do
  ...

  config.consider_all_requests_local       = false
  config.action_controller.perform_caching = true

  # I set this to true, as I am testing this locally, just running a local Thin server
  config.serve_static_assets = true

  config.assets.compress = true

  # Setting this to false removes the issue - but is SLOW
  config.assets.compile = true

  config.assets.digest = true

  # This is overkill - but does get EVERYTHING precompiled for now
  config.assets.precompile += %w( *.css *.js )

  config.action_dispatch.x_sendfile_header = nil
  ...
end

This is quite a new area for me, and so I've spent much of today toggling these booleans and stop/starting the local Thin / Rails server to try them out. 这对我来说是一个相当新的领域,因此我今天花了很多时间来切换这些布尔值并停止/启动本地Thin / Rails服务器来试用它们。 But the only value that's made a solid difference is the compile flag. 但唯一的价值就是编译标志。

My application.rb file is pretty much standard, and has config.assets.enabled = true and config.assets.initialize_on_precompile = false in it, the latter from a heroku post (and a guess, again). 我的application.rb文件非常标准,并且其中包含config.assets.enabled = trueconfig.assets.initialize_on_precompile = false ,后者来自heroku帖子(再次猜测)。

I have a fully populated public/assets directory, created with the bundle exec rake assets:precompile command, that takes about 20 mins to run on my oldish laptop (5 years), probably something to do with that "catch all" precompile regex, although with that line commented it still takes over 10 mins (!) 我有一个完全填充的public/assets目录,使用bundle exec rake assets:precompile命令创建,在我的旧笔记本电脑上运行大约需要20分钟(5年),可能与“catch all”预编译正则表达式有关,虽然有这条线评论它仍然需要超过10分钟(!)

With the compile flag set to true , I can see copies of these assets getting created in my /tmp/cache directory - this is clearly the application creating it's own "compiled copy" of the assets. 将compile标志设置为true ,我可以看到这些资产的副本在我的/tmp/cache目录中创建 - 这显然是应用程序创建它自己的资产的“编译副本”。

With the compile flag set to false , I am confronted with the error message (in the logs, unless I set requests to local, then I see it on the rendered error page) of jquery.reveal isn't precompiled . 在编译标志设置为false ,我遇到了错误消息(在日志中,除非我将请求设置为本地,然后我在渲染的错误页面上看到它) jquery.reveal isn't precompiled However, when I go to http://localhost:3000/assets/jquery.reveal.js the javascript file is served up. 但是,当我转到http://localhost:3000/assets/jquery.reveal.js ,会提供javascript文件。

The line of my application layout causing this is: 我的应用程序布局导致这一点是:

<%= javascript_include_tag "application", "jquery.reveal" %>

I have tried changing that jquery.reveal to jquery.reveal.js with no change. 我已经尝试将jquery.reveal更改为jquery.reveal.js而没有任何更改。 Removing it fixes the index page, except that the jquery.reveal functionality is gone of course ! 删除它修复了索引页面,除了jquery.reveal功能当然消失了! So clearly the application.js is being served up correctly. 很明显,application.js正在正确提供。 I just can't figure out why jquery.reveal isn't, as I can see the precompiled files in the public/assets directory. 我只是无法弄清楚为什么jquery.reveal不是,因为我可以看到public / assets目录中的预编译文件。

Answering my own question here, but looks like it might be a bug in parsing assets with "periods" such as jquery.reveal 在这里回答我自己的问题,但看起来这可能是解析具有“句点”的资产的错误,例如jquery.reveal

An issue report (https://github.com/rails/rails/issues/3398) reports this and highlights a commit (https://github.com/sstephenson/sprockets/commit/4ba5b32764a9073671df5e77355df6ed2bb3d3c9) which occurs just after Sprockets 2.0.3 - the default version that rails 3.1.3 relies upon. 问题报告(https://github.com/rails/rails/issues/3398)报告了这一点,并突出显示了Sprockets 2.0之后的提交(https://github.com/sstephenson/sprockets/commit/4ba5b32764a9073671df5e77355df6ed2bb3d3c9)。 3 - rails 3.1.3依赖的默认版本。 Upgrading Sprocket would therefore involve moving onto 3.2-stable rails - a bit bleeding edge for this newbie ! 因此,升级链轮将涉及到3.2稳定的导轨 - 对于这个新手来说有点出血!

But the bug report does say this only occurs when config.assets.compile = true - and I did see whilst that was the case in my code that jquery.reveal was being dynamically compiled to jquery-8fu...8g.reveal.js (instead of jquery.reveal-8fu...8g.js). 但是错误报告确实说这只发生在config.assets.compile = true - 我确实看到了我的代码中的情况,即jquery.reveal被动态编译为jquery-8fu ... 8g.reveal.js (而不是jquery.reveal-8fu ... 8g.js)。

So perhaps this ISN'T the answer. 所以也许这不是答案。 At least I hope it isn't. 至少我希望不是。 But will certainly continue to look at this period issue, as the "non-period" containing CSS / JS files are being served up just fine, as far as I can tell. 但肯定会继续关注这个时期的问题,因为据我所知,包含CSS / JS文件的“非期间”正在提供。

This is an interesting issue, and I think there may be two bugs - the one you've linked and another: the file is being being compiled to the wrong name. 这是一个有趣的问题,我认为可能存在两个错误 - 您链接的错误和另一个错误:文件被编译为错误的名称。 It might be worth trying to generate a minimal test case that you can submit with a bug report. 可能值得尝试生成一个可以使用错误报告提交的最小测试用例。

The workaround for this - and I suspect that this is why few people seem to have the problem - is to use a secondary manifest (linking libraries only via a manifest seems to be an evolving best-practice). 对此的解决方法 - 我怀疑这就是为什么很少有人似乎遇到问题 - 是使用辅助清单(仅通过清单链接库似乎是一种不断发展的最佳实践)。

Create one called home.js and require just that one file to it. 创建一个名为home.js文件,只需要一个文件即可。

This isn't a bad approach overall. 总体而言,这不是一个糟糕的方法。 These extra manifests have to be added to the precompile array (see the guide), and allow multiple libraries to be shared over several pages or sections without having to link them each time. 这些额外的清单必须添加到预编译数组中(请参阅指南),并允许在多个页面或节上共享多个库,而无需每次都链接它们。

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

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