如何避免在开发模式下提供预编译资产? [英] How to avoid precompiled assets being served in development mode?

查看:119
本文介绍了如何避免在开发模式下提供预编译资产?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我更喜欢 not 在开发模式下串联JavaScript文件,但将它们作为单独的文件使用.所以我配置了:

I prefer not to concatenate JavaScript files in development mode, but serve them as individual files. So I configured:

development.rb:

development.rb:

config.assets.compress = false
config.assets.debug = true
config.assets.compile = true

在我的/app/assets/javascript目录中,我拥有:

In my /app/assets/javascript directory I have:

  • reviews.js
  • 评论/
    • foo.js
    • bar.js
    • reviews.js
    • reviews/
      • foo.js
      • bar.js

      reviews.js:

      reviews.js:

      //= require jquery
      //= require jquery_ujs
      //= require_tree ./reviews
      

      我在布局中使用<%= javascript_include_tag "reviews" %>包含了JavaScript.生成的页面正确地分别引用了三个脚本,并且reviews.js本质上是空的.到目前为止一切顺利.

      I include the JavaScript using <%= javascript_include_tag "reviews" %> in my layout. The generated page correctly references the three scripts individually and reviews.js is essentially empty. So far so good.

      现在,当我使用rake assets:precompile预编译我的资产以进行生产时,这三个JavaScript文件将连接到reviews.js中.这一切都可以很好地用于生产,但是现在处于开发模式中,除了这两个单独的文件之外,还提供了串联的reviews.js.

      Now when I precompile my assets for production using rake assets:precompile the three JavaScript files are concatenated into reviews.js. This is all fine for production but now, in development mode, the concatenated reviews.js is served in addition to the two individual files.

      当然,这会导致开发时出现各种令人讨厌的错误,因为现在foo.jsbar.js的内容被提供了两次,其中一个在reviews.js中可能是较旧的版本中.

      Of course, this leads to all kinds of nasty bugs when developing because now, the content of foo.js and bar.js is served twice, one of them in a potentially older version in reviews.js.

      如何确保Rails在开发模式下不使用预编译的资产?

      How can I make sure Rails doesn't use the precompiled assets in development mode?

      推荐答案

      听起来您正在本地进行预编译.由于文件位于预期的位置,因此将由您的开发服务器提供服务,并且请求不会发送到Sprockets.

      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.

      通常,您不需要本地编译.预计在几乎所有情况下,预编译任务都将在应用程序部署期间运行.资产管道指南页面上有一个Capistrano配方.

      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.

      如果确实需要将这些文件本地提交到您的存储库中,则可以使用分支来避免此问题.将您的master分支保留用于生产代码,并为dev创建第二个分支.仅在master上编译并提交资产.当您切换到开发人员时,他们将消失.根据需要将开发人员合并到母版中.

      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.

      请确保您强制浏览器进行更新(控制键+ F5),否则您可能会从浏览器缓存中找到使用的旧资产!

      Make sure you force your browser to update (control + F5) or you may find the old assets used from the browser cache!

      这篇关于如何避免在开发模式下提供预编译资产?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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