nitsujri
nitsujri

Reputation: 1591

Rails assets not precompiling in production

I'm using Datetimepicker and Slider. I include them in my Gemfile

gem 'datetimepicker-rails', github: 'zpaulovics/datetimepicker-rails', branch: 'master', submodules: true
source 'https://rails-assets.org' do
  # gem 'rails-assets-select2-bootstrap-css'
  gem 'rails-assets-seiyria-bootstrap-slider'
end

In my application.js

//= require moment
//= require bootstrap-datetimepicker
//= require pickers

//= require seiyria-bootstrap-slider

This works great in development, but when I run RAILS_ENV=production rake assets:precompile on the server (capistrano deploy or by hand) these, and others don't seem to get pulled in. Chrome complains specifically about these two first.

I know I could put the line Rails.application.config.assets.precompile += %w( *.js ) and then do a =javascript_include_tag :XXXX, but this defeats the purpose of sprockets/manifest right?

My understanding with sprockets/manifest is when I require it in my application.js it'll be included with the deploy so the client hits the server less.

Is there something I'm missing?

EDIT Traced the problem to the uglifier gem. When I remove/comment out config.assets.js_compressor = :uglifier and recompile the JS starts working again.

Any thoughts?

Upvotes: 2

Views: 11063

Answers (1)

Milind
Milind

Reputation: 5112

this is because things work differently in development as compared to production. few thing to note:-

  1. No CSS or JS files will be available to your app through the asset pipeline unless they are included in other files OR listed in the config.precompile directive.Only application.css and application.js are available by default of all the CSS and JS files.
  2. Every file that is not a Javascript file or CSS file that is in the app/assets folder will be copied by Rails into the public/assets folder when you compile your assets.So if you want to add some web fonts, you could make an app/assets/fonts/ folder and put your fonts in there, these will then be copied to public/assets/fonts folder when you compile your assets. Note that your app/assets/stylesheets/fonts.css.scss file that references those fonts will NOT be copied over unless you either added it to the config.assets.precompile directive or required it from your application.css
  3. for config.assets.compile...If it is set to "true" (which it is by default in development) then Rails will try to find a Javascript or CSS file by first looking in the public/assets directory and if it can't find it, will hunt through your app/assets folder looking for the file. If it finds it in app/assets it will go ahead and compile on the fly and then serve this asset up.

The problem with this is that you don't notice it happening in development, then you commit everything and push to production and BOOM, everything is broken with 500 errors because production has config.assets.compile set to "false".This prevents the app from "falling back" and trying to load the file directly instead of using the asset pipeline.

# Don't fallback to assets pipeline if a precompiled asset is missed
config.assets.compile = false

Why don't you just have this set to "true" in every environment? Well, because it is sloooooow. And you don't want slow in production

  1. Run RAILS_ENV=production rake assets:clean assets:precompile
  2. check public/assets directory and verify that assets are compiled..if its not empty...that means asset pipeline is working but path is not correct.use asset_helpers to set the path of assets in css files.

Upvotes: 7

Related Questions