Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue while compressing JS files with the Asset Pipeline #13

Open
jalberto opened this issue Nov 13, 2013 · 19 comments
Open

Issue while compressing JS files with the Asset Pipeline #13

jalberto opened this issue Nov 13, 2013 · 19 comments

Comments

@jalberto
Copy link

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

@gottfrois
Copy link
Owner

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 :(

@jalberto
Copy link
Author

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

@gottfrois
Copy link
Owner

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.

@mikebaldry
Copy link

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

@gottfrois
Copy link
Owner

Thanks for the feedback!

@gottfrois
Copy link
Owner

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.

@jalberto
Copy link
Author

jalberto commented Dec 2, 2013

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.

@gottfrois
Copy link
Owner

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

@gottfrois gottfrois reopened this Dec 2, 2013
@james-ai
Copy link

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.

@gottfrois
Copy link
Owner

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
Copy link
Owner

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
Copy link
Owner

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.

@james-ai
Copy link

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

@james-ai
Copy link

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.

@gottfrois
Copy link
Owner

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

@gottfrois
Copy link
Owner

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.

@james-ai
Copy link

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.

@ericraio
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants
@ericraio @jalberto @mikebaldry @james-ai @gottfrois and others