43

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?

Jack R-G
  • 1,778
  • 2
  • 19
  • 25
  • You were definitely modifying `config/environments/production.rb`? – Andrew France Mar 13 '12 at 16:24
  • 1
    Are you using Rails tag helpers to access the assets (javascript_include_tag 'script.js')? Could you focus on a single file and give us an example of how it's implemented and what's happening? – mltsy Oct 30 '12 at 17:23

9 Answers9

56

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
ncherro
  • 2,604
  • 2
  • 24
  • 20
  • Very clever! I never would have thought of that. Thanks! – Jack R-G Mar 26 '13 at 22:08
  • Great thing to use when deploying to staging servers. Saves me 100 seconds from a total 260 each time I deploy on heroku. – hakunin Apr 17 '13 at 10:04
  • Will this still add the hash to asset file names? I'm having to debug some code on a server, but in order to find the error I need to be using the correctly hashed asset names. – Ziggy Dec 15 '13 at 12:32
  • 1
    @Ziggy sorry for the late reply, but Sprockets will still append a digest created from a MD5 hash of the uncompressed files (as long as `config.assets.digest = true` in your environment.rb file) – ncherro Feb 11 '14 at 05:55
  • 1
    Removing `config.assets.js_compressor = :uglifier` is enough to fix it – Dorian Dec 20 '17 at 19:03
  • Would it be possible to modify this to affect only certain files? – Daniel Lawton Jan 23 '20 at 08:34
29

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.

geekQ
  • 29,027
  • 11
  • 62
  • 58
13

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
user2576663
  • 131
  • 2
  • 3
8

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

gavdotnet
  • 2,214
  • 1
  • 20
  • 30
user3630729
  • 81
  • 2
  • 2
7

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 ;)

whyvez
  • 303
  • 3
  • 12
2

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.

dodgio
  • 2,169
  • 21
  • 19
1

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

1

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

config.assets.js_compressor = ...
config.assets.css_compressor = ...
Dorian
  • 22,759
  • 8
  • 120
  • 116
Manish Singh
  • 53
  • 1
  • 7
0

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?

Community
  • 1
  • 1
jrochkind
  • 22,799
  • 12
  • 59
  • 74
  • I don't think that the two are related, because I believe that the change has to do with whether or not a digest was added to the asset name, while my issue had more to do with javascript compression. It's possible that the same problem exists with config.asset.compression and they just haven't found it yet, I suppose. At any rate, thanks for the heads up on this. – Jack R-G Nov 06 '12 at 00:23