Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 3 - Is there any fallback for when serving assets from an asset server fails?

In my production.rb I've set this:

# Enable serving of images, stylesheets, and JavaScripts from an asset server
  config.action_controller.asset_host = "http://myassets.com"

And the images, js and stylesheets are loading fine from my CDN (assets server)

but what if someday this asset servers fails? and it returns a 404?

Because in my assets server (CDN) I have configure a pull zone, the content is still available from /assets/...

Is there any fallback or how can I make a fallback so when my assets server fails or return error my application loads assets from /assets/ inside the application?

like image 991
Mr_Nizzle Avatar asked Aug 17 '12 17:08

Mr_Nizzle


People also ask

What does rake assets Clean do?

The clean it removes the old versions of the precompiled assets while leaving the new assets in place. Show activity on this post. rake assets:clean removes compiled assets. It is run by cap deploy:assets:clean to remove compiled assets, generally from a remote server.

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 and pre-processors such as CoffeeScript, Sass, and ERB.

What is the command for Precompiling assets before deploying a Rails app?

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 are Rails Sprockets?

Sprockets is a Ruby library for compiling and serving web assets. Sprockets allows to organize an application's JavaScript files into smaller more manageable chunks that can be distributed over a number of directories and files.


1 Answers

production.rb

config.action_controller.asset_host = Proc.new { |source, request, asset_path|
  if some_condition
    "http://myassets.com"
  else
    asset_path
  end
}

For more info, see AssetTagHelper

Edit

I don't think this precaution is worth the extra load/added requests to your application. If you were building a large application with failover servers to provide high availability, assets would be yet another thing you'd accommodate for with that redundancy. If you're hosting your stuff on the cloud through a service like AWS or Rackspace, I think you're good on availability and you shouldn't worry about the issue. This approach almost completely negates the benefit of caching assets.

like image 186
anxiety Avatar answered Sep 22 '22 01:09

anxiety