dashing-rails icon indicating copy to clipboard operation
dashing-rails copied to clipboard

Issue while compressing JS files with the Asset Pipeline

Open jalberto opened this issue 10 years ago • 19 comments

Hi,

I'm trying to run rails-dashing under passenger-nginx and i Get this errors:

GET http://foo.com/dashing/widgets/r.html 500 (Internal Server Error) application-3587576a36c1ab2a24c5687247faf363.js:3 Uncaught Error: Could not load view from /r application-3587576a36c1ab2a24c5687247faf363.js:13 GET http://foo.com/dashing/widgets/u.html 500 (Internal Server Error) application-3587576a36c1ab2a24c5687247faf363.js:3 Uncaught Error: Could not load view from /u application-3587576a36c1ab2a24c5687247faf363.js:13

Of course widgets 'u' and 'r' doesn't exist and are not defined anywhere

I set the js compressosr as YUI and even leave raisl to serve the assets in real time (like in dev) but not lucky. I also added the options suggested to run it under apache:

rails_app_spawner_idle_time 0; passenger_min_instances 1;

Any clue will be welcome

TIA

jalberto avatar Nov 13 '13 12:11 jalberto

Yes I ran into the same issue when deploying on Heroku. I had to:

  1. Add gem 'yui-compressor' to the Gemfile
  2. Set config.assets.js_compressor = :yui in config/environments/production.rb
  3. Run RAILS_ENV=production bundle exec rake assets:precompile localy

Letting Heroku run the asset precompile command even with yui compressor set did not work. I think in you case, the issue is definitely the JS compression since the widgets' name are guessed base on their class name, which no longer works when JS compressor change the class names to shorter ones.

I don't know much passenger so I don't think I could help you more here :(

gottfrois avatar Nov 13 '13 16:11 gottfrois

thanks, but it did't works for me.

exactly same steps, but i'm precompiling in the server (is a VPS not heroku). The only that works for me is to create links to the original widget:

r.html -> /usr/local/rvm/gems/ruby-1.9.3-p448/gems/dashing-rails-2.1.1/app/views/dashing/widgets/number.html

jalberto avatar Nov 13 '13 17:11 jalberto

This is very weird, you should be able to control how the asset pipeline compress your JS files. It's not an issue directly related to dashing-rails gem sicne it would work nicely if the JS files are just compressed but variables name beeing not modified.

Please let me know if you figure out a way to make it work.

gottfrois avatar Nov 13 '13 18:11 gottfrois

Its not ideal but I had the same issue, I fixed it (temporarily with)

class NoCompressor
  def compress(string)
    string
  end
end

at the top of production.rb

then

config.assets.js_compressor = NoCompressor.new

basically, disable js compression (I didn't look very hard to try and find a proper way to disable it)

a hack for sure but it got me one step further :)

mikebaldry avatar Nov 28 '13 10:11 mikebaldry

Thanks for the feedback!

gottfrois avatar Nov 28 '13 10:11 gottfrois

Let me close the issue since it's not really a bug from the gem and we now have identified where does the issue come from.

Please keep us posted we you find anything.

gottfrois avatar Dec 01 '13 11:12 gottfrois

IMHO is not good idea to lose this bug. Is easy to find it if it is open, and this workaround disable compression in the whole rails app, not only dashing, so it is actually a requirement to use rails-dashing.

jalberto avatar Dec 02 '13 10:12 jalberto

Yes I see, i'll digg into this issue to see what can be done.

gottfrois avatar Dec 02 '13 13:12 gottfrois

Hi guys,

I've run in to this problem today. I'm working on setting up dashing-rails at the moment so if anyone can provide me with any info I can take a look at fixing it. It's the end of a long shift here so I won't be looking at the problem right away.

Ta, James.

james-ai avatar Dec 17 '13 18:12 james-ai

Basically the issue comes from the JS compressor when precompiling the assets on production. The default JS compressor seems to also change JS variables and class names (from Foo to f for example).

Since the widget's name are used to fetch their HTML template from the server (making a GET request), having their name changed cause the issue.

In order to get around this issue for the moment, you need to change the default JS compressor used when compressing the assets.

I have done the above for the heroku demo application by following the following steps:

  1. Add gem yui-compressor to the Gemfile
  2. Set config.assets.js_compressor = :yui in config/environments/production.rb
  3. Run RAILS_ENV=production bundle exec rake assets:precompile locally to force Heroku to use my local precompile assets

gottfrois avatar Dec 17 '13 23:12 gottfrois

I will look more closely to this issue since I was recently told that the default JS compressor should not change .coffee class names. I'll keep you guys posted and push any fix if necessary.

gottfrois avatar Dec 17 '13 23:12 gottfrois

If you guys want to take a look at it. Here is where I use the JS widget name to get its HTML template on the server side https://github.com/gottfrois/dashing-rails/blob/master/app/controllers/dashing/widgets_controller.rb#L29

All we have to do is to make sure the name coming from the params matches the widget HTML file name which seems to not be the case at the moment because of JS compression.

gottfrois avatar Dec 17 '13 23:12 gottfrois

Thanks @gottfrois. I'll have a look at this once I'm back in the office.

james-ai avatar Dec 17 '13 23:12 james-ai

Seems that by default uglifier will mangle variable and function names.

The documentation seems to suggest that you can do something like

config.assets.js_compressor = Uglifier.new(mangle: false) # Uglifier.new(:mangle => false) for old Ruby

https://github.com/lautis/uglifier#usage

I will run this when I get a chance but feel free to jump in there before me.

james-ai avatar Dec 19 '13 10:12 james-ai

I'll try that tonight, thanks for digging in to this issue.

gottfrois avatar Dec 19 '13 11:12 gottfrois

Ok setting Uglifier.new(mangle: false) works. Let me add this to the readme. Not sure how we should handle this though. Any ideas ?

Thanks for looking at it.

gottfrois avatar Dec 20 '13 17:12 gottfrois

Sorry for the long wait. Nothing comes to mind immediately. I won't have much time to look at this in the coming weeks but if you leave any thoughts on this thread I'll investigate them once I'm back on the dashboard project.

Thanks.

james-ai avatar Dec 29 '13 15:12 james-ai

@brightbits thanks for your tip! This was perfect for my application since it's just a rest api.

ericraio avatar Mar 28 '15 16:03 ericraio