Require.js and precompiled assets with rails

I use requirejs-rails gem along with many third-party javascripts. In dev, all this seems to work ... even with the preliminary compilation of my assets, however, at the stage of production / production (heroku), he cannot find the assets.

Take jQuery for example (using jquery-rails gem). From heroku console if i do

Rails.application.assets.find_asset('jquery.min') it finds an asset (/app/vendor/bundle/ruby/1.9.1/gems/jquery-rails-2.1.1/vendor/assets/javascripts/jquery.min .js ").

However, if I try to go to it domain.com/assets/jquery.min.js , I will get 404.

My requirejs-rails configuration:

 shim: jquery: exports: '$' jquery-ujs: - jquery paths: jquery: jquery.min jquery-ujs: jquery_ujs 

in my application.js.coffee i have

 require ['jquery', 'jquery-ujs'], ($) -> // ... 
+4
source share
2 answers

The requirejs-rails path works (and indeed r.js from requirejs) is that it compiles all your javascript files that it knows about (through paths in requirejs.yml or through the actual calls to define your code) into one big a file that can then be cleaned, compressed, and cached (by including a digest in the title). Therefore, if you have a file called /assets/views/my_view.js that is available in dev on top of http, it no longer exists during production (because it is part of your application.js ).

In your case, if you look at application-<digest>.js in production (you can get the actual digest by looking at the html pages of your served application), you will see that jquery is already included in it (you can see license comments) .

So, there is a file, it is correctly served as part of application-<digest>.js , and you should not be in production, relying on the fact that it is available directly. Since the whole point of the asset pipeline and the requirejs-rails plugin is to compile all files into a single compressed file to improve page performance.

+3
source

I did not use requirejs, but you checked Rails.application.assets.paths to make sure that this asset has? Also make sure config.action_controller.asset_host is set correctly in the current environment

0
source

All Articles