0

Possible Duplicate:
Rails 3.1 asset precompilation

I've been strugling lately with an application I'm building using Rails 3.1 and Ruby 1.9.2.

My problem is that sometimes I add a specific JS file to the system, but when I push it into Heroku I'd see the "yourfile.js isn't precompiled" error.

I solve this by adding my file into config.assets.precompile on my application.rb file. But is there a way to automate this process? I currently have a lot of js files, and might add a lot more. Any gems available for this? Maybe some configuration I'm missing?

Community
  • 1
  • 1
Deleteman
  • 8,500
  • 6
  • 25
  • 39
  • This isn't an answer, why I generally just use application.js, and include it on every page. this way the javascript is loaded and cached just once. – Matthew Rudy Apr 25 '12 at 15:03
  • Yes, but if your site is big enough, you'll have JS code on pages that don't really need it. If that's the way to go, I don't really think it's the cleanest one. – Deleteman Apr 25 '12 at 15:55
  • But you only load it once. And the browser caches it. So the only delay is the javascript parse time. Depends how much you have. – Matthew Rudy Apr 26 '12 at 08:43

1 Answers1

0

You should be adding these files to application.js and including on every page.

It does not really matter if it is not needed on some pages; if the server is setup correctly the application.js file will be cached by the browser and not downloaded again as the user browses the site. A second advantage is that you js files could be cached by a transparent proxy somewhere, reducing you bandwidth costs, and speeding up response times for people on the other side of the proxy.

If you want to only include files on an as-needed basis, then they will have to be included on the precompile array - that is the only way the precompile task knows to make them available in the production manifest.

In practice you could include js that is common to many pages in application.js and if there is a large bit if js for one page the put that only on that one page.

Richard Hulse
  • 10,383
  • 2
  • 33
  • 37
  • So there is no other way? I know that if I include everything on the application.js file, it'll be cached and only downloaded once, but my gut tells me that including all of the interface code on all pages is wrong. I mean, I should be able to only load the code I need for a specific interface, shouldn't I? – Deleteman Apr 25 '12 at 20:41
  • The default way is to require tree, which pulls all the code in to application.js. There is nothing wrong with having js on a per page basis, it's just not the 'golden path' which is why you have to add files to the precompile array. It is a balancing act, from a performance perspective. The caching only occurs if you have the correct server headers set, BTW. – Richard Hulse Apr 25 '12 at 21:03