Jack R-G
Jack R-G

Reputation: 1896

Disable Asset Minification in Rails Production

In order to debug javascript in my heroku production environment, I need to disable asset compression (or at least compression of javascript). I tried config.assets.compress = false along with config.assets.debug = true, and the compressed assets were still used. I then deleted the compressed assets, at which point no assets were rendered at all. I added config.assets.enabled = false, which did not help. I tried copying the uncompressed assets into various directories, including the application root, public, and public/assets (the latter two using both the folders "images, "javascripts", and "stylesheets", and putting the assets directly into the folders without the three subfolders). I was eventually able to get the javascripts to work by changing the html to directly reference all of the javascript files. But the CSS and images still are not working.

I would have thought that my original config.assets.compress = false should have worked. Any ideas what I did wrong?

Upvotes: 43

Views: 21402

Answers (9)

Manish Singh
Manish Singh

Reputation: 53

Find and comment out these line in environments/production.rb:

config.assets.js_compressor = ...
config.assets.css_compressor = ...

Upvotes: 1

averageradical
averageradical

Reputation: 11

I had to update Rails.application.config.assets.version in config/initializers/assets.rb for the production.rb changes to take effect.

Upvotes: 1

dodgio
dodgio

Reputation: 2329

With Rails 4 on Heroku you need to do two things. First as @geekQ mentioned, comment out the js_compressor line in config/environments/production.rb

# config.assets.js_compressor = :uglifier

Second, you need to consider Heroku's asset pipeline cache for Rails 4. Any file with the same MD5 as the version in the cache will not be recompiled. The previous (possibly compressed) version will be served. Any file you edit will have a new MD5 and be recompiled.

You can also purge the entire asset cache with the Heroku Repo plugin to the Heroku toolbelt. Install that, then use the command

heroku repo:purge_cache

Deploy a new version after purging the cache and all your assets will be recompiled.

Upvotes: 2

user3630729
user3630729

Reputation: 81

Comment out the uglifier and add config.assets.debug = true. This worked for me.

  • Compress JavaScripts and CSS:

    config.assets.js_compressor = :uglifier

  • Debug mode disables concatenation and preprocessing of assets. But this option may cause significant delays in view rendering with a large number of complex assets:

    config.assets.debug = true

Upvotes: 8

geekQ
geekQ

Reputation: 29473

Under Rails 4 just commenting out the line

# config.assets.js_compressor = :uglifier

in config/environments/production.rb worked for me. Looks like default is no compresson.

Upvotes: 29

user2576663
user2576663

Reputation: 131

I also need to debug my js so I tried ncherro's solution. The problem was that it would still throw

rake aborted! uninitialized constant NoCompression

So I just put the NoCompression class in the production.rb file

    # Compress JavaScripts and CSS
    class NoCompression
         def compress(string)
             # do nothing
             string
         end
     end

     config.assets.compress = true
     config.assets.js_compressor = NoCompression.new
     config.assets.css_compressor = NoCompression.new

Upvotes: 13

whyvez
whyvez

Reputation: 323

Also worth noting... In addition to ncherro solution you will need to do the following:

  1. make sure to put your new module somewhere where it will be loaded by default. Was lib/extras in my case.
  2. run rake assets:clean to clean your existing assets.
  3. run rake assets:precompile to compile your assets using the new compressor.
  4. restart your app... i use touch tmp/restart.txt

Happy debugging ;)

Upvotes: 7

ncherro
ncherro

Reputation: 2614

I came up with this workaround after reading the docs:

create a module that does nothing to compress js / css here: lib/modules/no_compression.rb

class NoCompression
  def compress(string)
    # do nothing
    string
  end
end

configure your assets to (not) be compressed with your do-nothing compressor

config.assets.compress = true
config.assets.js_compressor = NoCompression.new
config.assets.css_compressor = NoCompression.new

Upvotes: 56

jrochkind
jrochkind

Reputation: 23317

Looks like this MAY have been a bug in Rails. From the changelog for upcoming rails 3.2.9, is this what you were running into?

Respect config.digest = false for asset_path

Previously, the asset_path internals only respected the :digest option, but ignored the global config setting. This meant that config.digest = false could not be used in conjunction with config.compile = false this corrects the behavior.

http://weblog.rubyonrails.org/2012/10/29/ann-rails-3-2-9-rc1-has-been-released/

Do you think that could be related?

Upvotes: 0

Related Questions