Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you catch errors in the rails asset pipeline before production?

I'm just getting acquainted with Rails 3.1, and I burned some time updating an old project and trying to work out how the new asset pipeline behaves in development mode versus production.

The default config.assets.precompile setting blesses only application.css and application.js, with the intention that everything should be served as a single stylesheet and a single javascript file.

Obviously there are situations when we don't want that, so we can add items to the list in that config variable...

Here's the situation I ran into with my sandbox project when going to production:

  1. Browsed the site in development, saw that everything was working. The assets were linked as separate files and the site displayed correctly.
  2. Uploaded the site to my server, and tried to get it working in production. The first error was saying that "ie.css" (a conditional stylesheet) isn't precompiled. (I was in Safari and this stylesheet wouldn't even be downloaded: the error was raised from the stylesheet_link_tag helper before rendering the page.)
  3. Ran rake assets:precompile and tried again.
  4. Added the offending item to config.assets.precompile and tried again.
  5. Kicked the error down the curb until it hit another asset error.
  6. GOTO 3.

Not knowing how to address this, I went around in circles a few times until I thought I got all the assets and the site was rendering in production. Then I tried it in MSIE and hit another error 500: "belated_png_fix.js" was being conditionally loaded, and it didn't crop up until then.

So my question is, other than trial and error or a heavy dependence on integration testing, how can I predict that my site isn't going to bomb out when the asset pipeline discovers that some stylesheet or javascript wasn't added to the precompile list?

I'm also curious why a missing stylesheet asset should cause the whole page to error 500 instead of just compiling it on-demand or serving a 404 when that asset is requested. Is this a deliberate design to "fail early"?

like image 890
Andrew Vit Avatar asked Sep 02 '11 02:09

Andrew Vit


People also ask

How do you Precompile rails assets?

To compile your assets locally, run the assets:precompile task locally on your app. Make sure to use the production environment so that the production version of your assets are generated. A public/assets directory will be created. Inside this directory you'll find a manifest.

What does rake assets Clean do?

rake assets:clean Only removes old assets (keeps the most recent 3 copies) from public/assets . Useful when doing rolling deploys that may still be serving old assets while the new ones are being compiled.

How does Rails asset pipeline work?

The asset pipeline provides a framework to concatenate and minify or compress JavaScript and CSS assets. It also adds the ability to write these assets in other languages such as CoffeeScript, Sass and ERB. Prior to Rails 3.1 these features were added through third-party Ruby libraries such as Jammit and Sprockets.


2 Answers

I've just released a gem called assets_precompile_enforcer, which ensures that developers won't forget to add assets to config.assets.precompile while they are developing. An exception will be raised if you include an asset via javascript_include_tag or stylesheet_link_tag, and it doesn't match a filter in config.assets.precompile.

This means that asset errors will be caught during development, instead of being discovered after deploying to production.

like image 82
ndbroadbent Avatar answered Sep 30 '22 19:09

ndbroadbent


I had similar problems with rails 3.1 as well. The best thing you could do is to install capistrano multi stage and get a staging server.

If for any reasons this is not possible, install a virtual machine on your computer and try to replicate your servers environment.

like image 40
vise Avatar answered Sep 30 '22 19:09

vise