9

After I deployed my upgraded Rails 2.3.x -> 3.1 (rc4) app to our test environment this afternoon, all of our stylesheets and JavaScript files were returning 404 errors. We had added the rake assets:precompile task to our post-deploy script and it took a while to determine why the assets folder didn't have the pre-compiled files we expected.

In the end, the files weren't being compiled because apparently only application.css and application.js (+ non JS/CSS files) are processed by default.

We needed to change the following configuration value as follows:

config.assets.precompile += %w( *.js *.css )

Question: why isn't this the default?

I would have expected that anything that wasn't necessary to process as a manifest file would just get copied into public/assets. Much of what I've read on the asset pipeline is essentially "stick your assets in app/assets, configure the manifest files, and it should just work". Since the assets:precompile task didn't spit out any information about what it was doing, it took a while to determine that it just wasn't looking at the files we thought it would.

Is there any reason why this would not be a good value for the precompile configuration?

Thanks!

New Alexandria
  • 6,951
  • 4
  • 57
  • 77
shedd
  • 4,198
  • 4
  • 33
  • 42

2 Answers2

9

The idea is to have all your JavaScript and CSS always loaded in one shot, rather than loading bits and pieces as you move along. That way you always have the 'world' loaded and ready to work with, rather than having to include a whole bunch of individual files here and there.

It's a bit of a larger 'up front' load, but then the browser should keep loading all the javascript from cache. So the perceived speed of the site should speed up due to having everything cached and ready to go after the first request.

This was a controversial decision to include for Rails, but so is including CoffeeScript by default. Rails has always been an opinionated framework that way.

Christopher WJ Rueber
  • 2,141
  • 15
  • 22
  • Thanks, Chris, for the reply. I get that it makes sense to cut down on HTTP requests for efficiency, but I would think most applications have their CSS segmented into at least internal / external stylesheets and modular JavaScript files. It just seems like a real shift from the way the rest of the Rails framework deals with things, but based on the defaults, it certainly appears that must have been the thinking. – shedd Jun 20 '11 at 22:04
  • Well, one would definitely want to cut their files up in to modular ones. The idea is that the application.js and application.css have "includes" that pull in all the specific javascript and css in those directories (if you look in those files, you'll find a 'require_tree .' directive, that pulls in everything in that directory and all it's children). – Christopher WJ Rueber Jun 20 '11 at 22:14
  • Most Rails apps actually don't have CSS segmented into different stylesheets -- at least as delivered to the client. Separate source CSS (and JS) are compiled into one aggregate application.css (or .js) by the asset pipeline. Still, I tend to agree with you about what Rails defaults should be -- if (*.js *.css) were the default, it would still work fine or the 'suggested' way to do it, but would also Just Work for what you were trying to do. I think earlier versions of Rails actually did work as you suggest, but they changed it more recently, perhaps for good reasons we're not thinking of. – jrochkind Jun 07 '14 at 12:31
0

the new sprockets-based pipeline compiles all the files in /asssets/stylesheets and /assets/javascripts get compiled into application.css and application.js, respectively, by default.

In your views, you only need to link the application files, sprockets handles the rest.

Update: Well, you don't have to make it all into just one file... You could have an shared.js, secure.js and public.js and have them each include the parts they need...

Think of them not as javascript files, but manifest files that establish groups of javascript files which you can then include as a group with a single javascript_include_tag. While the default is to include everything in the folder into a single file, you can be always pick and choose what to include, and what not.

The 'precompile' task simply runs those manifest files and compiles the multiple files into one, while pre-processing and sass or coffee script it runs across.

colinross
  • 2,075
  • 13
  • 10
  • Thanks, Colin. Yep, I figured that out, but the question was probing a bit beyond that. – shedd Jun 20 '11 at 22:05