Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assets pipeline when updating to Rails 3.1 on Heroku

I've just upgraded my app on Heroku from Rails 3.0 to 3.1, and I'm trying to make the assets pipeline work. The main issue is that I can read from the heroku log the following kind of lines, for every asset:

2011-09-03T16:35:28+00:00 app[web.1]: cache: [GET /assets/border-a3c571a354b9381740db48aeebfaa63a.jpg] miss

If I understand the pipeline correctly, this should not be "miss" for every request I make from a browser, but it should be found in the cache.

Reading the Heroku docs you can find this explanation:

Rails 3.1 provides an assets:precompile rake task to allow the compilation penalty to be paid up front rather than each time the asset is requested. If this task exists in your app we will execute it when you push new code.

But how should that "assets:precompile" task be? I tried building a project with rails 3.1 from scratch to try to find out, but there is no such task in a bare project. Or am I missing something? How could I make that the assets are found in the cache? Maybe is just an issue with configuration.

These are the options of my production config file:

config.serve_static_assets = false
config.assets.compress = true
config.assets.compile = true # If I turn this off I get a 500 error and logs say that an  asset isn't compiled

My application.rb has this line:

config.assets.enabled = true

Thanks a lot for your help!

like image 411
alvatar Avatar asked Sep 03 '11 16:09

alvatar


People also ask

How do you Precompile assets Rails in Heroku?

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 is assets Precompile?

rails assets:precompile is the task that does the compilation (concatenation, minification, and preprocessing). When the task is run, Rails first looks at the files in the config.assets.precompile array. By default, this array includes application.js and application.css .

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.


4 Answers

Also, take a look at http://guides.rubyonrails.org/asset_pipeline.html#precompiling-assets

For faster asset precompiles, you can partially load your application by setting config.assets.initialize_on_precompile to false in config/application.rb, though in that case templates cannot see application objects or methods. Heroku requires this to be false

like image 192
user670908 Avatar answered Oct 12 '22 00:10

user670908


I was wondering the same thing, but here's a tip to help figure out if your assets are live-compiling or not

  1. run rake assets:precompile locally
  2. make some changes to your css but do not rerun the rake task
  3. git add, commit and push to heroku

If the changes you made in step 2 show up on heroku, then you know your app is live-compiling

Don't forget that you are now in charge of http caching since Varnish is no longer included on celadon, so you need to set up rack-cache and memcached yourself:

  • heroku doc on http caching
  • setup rack-cache with memcached on heroku
  • heroku docs on memcached

But yeah, I found this puzzling too

like image 20
stephenmurdoch Avatar answered Oct 11 '22 23:10

stephenmurdoch


Can you try with config.serve_static_assets set to true and

config.action_dispatch.x_sendfile_header = "X-Sendfile"

added to your config/environments/production.rb file?

When you push your code to Heroku you should see the precompiling announced by the slug compiler AFAICT.

like image 21
Franck Verrot Avatar answered Oct 12 '22 01:10

Franck Verrot


Make sure you are on the Heroku "Cedar" stack. Then Heroku will automatically precompile your assets during slug compilation.

Note: I'm still getting "cache misses" too, but I don't think that's really true because your app wouldn't work if your assets weren't compiled.

like image 41
Andrew Avatar answered Oct 11 '22 23:10

Andrew