Ruby-on-rails 如何避免在开发模式下提供预编译资产?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8013478/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to avoid precompiled assets being served in development mode?
提问by Ortwin Gentz
I prefer notto concatenate JavaScript files in development mode, but serve them as individual files. So I configured:
我不喜欢在开发模式下连接 JavaScript 文件,而是将它们作为单独的文件提供。所以我配置了:
development.rb:
开发.rb:
config.assets.compress = false
config.assets.debug = true
config.assets.compile = true
In my /app/assets/javascript directory I have:
在我的 /app/assets/javascript 目录中,我有:
- reviews.js
- reviews/
- foo.js
- bar.js
- 评论.js
- 评论/
- foo.js
- bar.js
reviews.js:
评论.js:
//= require jquery
//= require jquery_ujs
//= require_tree ./reviews
I include the JavaScript using <%= javascript_include_tag "reviews" %>in my layout. The generated page correctly references the three scripts individually and reviews.jsis essentially empty. So far so good.
我<%= javascript_include_tag "reviews" %>在我的布局中包含了 JavaScript 使用。生成的页面正确地分别引用了三个脚本,reviews.js并且基本上是空的。到现在为止还挺好。
Now when I precompile my assets for production using rake assets:precompilethe three JavaScript files are concatenated into reviews.js. This is all fine for production but now, in development mode, the concatenated reviews.jsis served in additionto the two individual files.
现在,当我使用rake assets:precompile三个 JavaScript 文件预编译我的资产以进行生产时,这三个文件被连接到reviews.js. 这对于生产来说一切都很好,但是现在,在开发模式下,除了两个单独的文件之外,reviews.js还提供连接。
Of course, this leads to all kinds of nasty bugs when developing because now, the content of foo.jsand bar.jsis served twice, one of them in a potentially older version in reviews.js.
当然,在开发这会导致各种讨厌的错误,因为现在的内容foo.js,并bar.js送达了两次,其中一人在一个潜在的旧版本reviews.js。
How can I make sure Rails doesn't use the precompiled assets in development mode?
如何确保 Rails 在开发模式下不使用预编译资产?
采纳答案by Richard Hulse
It sounds like you are precompiling locally. Because the files exist in the expected location they are being served by your dev server, and the requests are not going to Sprockets.
听起来您正在本地预编译。因为文件存在于预期位置,它们由您的开发服务器提供服务,并且请求不会发送到链轮。
The only way to stop this is delete the compiled files.
阻止这种情况的唯一方法是删除已编译的文件。
Normally you do not need to compile locally. It is expected that in almost all cases the precompile task will be run during deployment of the app. There is a Capistrano recipe for this on the asset pipeline guide page.
通常不需要在本地编译。预计在几乎所有情况下,预编译任务都将在应用程序部署期间运行。在资产管道指南页面上有一个 Capistrano 配方。
If you do need to have those files locally committed to your repo you could use a branch to avoid the problem. Reserve your master branch for production code, and make a second branch for dev. Only compile and commit assets on master. When you switch to dev, they will be gone. Merge dev into master as required.
如果您确实需要将这些文件本地提交到您的存储库,您可以使用分支来避免该问题。为生产代码保留 master 分支,并为 dev 创建第二个分支。仅在 master 上编译和提交资产。当您切换到 dev 时,它们将消失。根据需要将 dev 合并到 master 中。
Edit: Make sure you force your browser to update (control + F5) or you may find the old assets used from the browser cache!
编辑:确保您强制浏览器更新(control + F5),否则您可能会从浏览器缓存中找到使用的旧资源!
回答by Haim Lankry
In config/environments/development.rbset:
在config/environments/development.rb集:
config.assets.prefix = "/assets_dev"
so that in developmentmode Rails will look there (but it will not find anything, as you will not compile assets in development (this is indeed what you are trying to do -- not compile assets)).
以便在开发模式下 Rails 会在那里查看(但它不会找到任何东西,因为您不会在开发中编译资产(这确实是您想要做的 - 不编译资产))。
When precompiling for production, use
为生产进行预编译时,请使用
RAILS_ENV=production rake assets:precompile
so it compiles into the default assets folder, public/assets.
所以它编译到默认的资产文件夹中,public/assets.
回答by Peter Madsen
in config/environments/development.rbset:
在config/environments/development.rb集合中:
config.serve_static_assets = false
and no files from /publicwill be served
并没有从文件/public将送达
回答by Uchenna
I tried this and it worked. rake assets:precompile RAILS_ENV=production
我试过了,它奏效了。 rake assets:precompile RAILS_ENV=production
I observed that the new version of assets pipeline does this when you run rake assets:precompiledoes rake assets:precompile:all
我观察到新版本的资产管道会在您运行时执行此rake assets:precompile操作rake assets:precompile:all

