Assets pipeline when updating to Rails 3.1 on Hero

2019-03-16 15:25发布

问题:

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!

回答1:

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



回答2:

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



回答3:

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.



回答4:

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.