Configuring dynamic_host resources in Rails 3

I want Rails 3 to dynamically grab assets based on the current domain:

mysite.com - assets.mysite.com mysite.ru - assets.mysite.ru

Is this possible out of the box? Or should I execute it manually?

+4
source share
4 answers

In config / environment / production.rb etc.

config.action_controller.asset_host = "http://assets." << request.host 
-3
source

Try sending proc to asset_host, which gives both the path to the resource and the request:

 config.action_controller.asset_host = proc {|path, request| "http://assets." << request.host } 
+11
source

Pay attention to the key differences from other solutions in the following solutions:

  • Using request.domain instead of request.host, since most resource hosts will not be assets0.www.domain.com, but rather assets0.domain.com
  • Using source.dash and modulo ensures that the same asset is served from the same asset server. This is the key to page performance.
  • Filtering by asset type / path.

I always did something like this:

  config.action_controller.asset_host = Proc.new { |source, request| "http#{request.ssl? ? 's' : ''}://cdn#{(source.hash % 3)}." << request.domain # cdn0-3.domain.com } 

Or, if you have multiple resource / cdn hosts, you can choose a selective view of what assets will be served from what the host is:

 config.action_controller.asset_host = Proc.new { |source, request| case source when /^\/images/ "http#{request.ssl? ? 's' : ''}://cdn#{(source.hash % 2)}." << request.domain # cdn0-1.domain.com when /^\/javascripts/, /^\/stylesheets/ "http#{request.ssl? ? 's' : ''}://cdn#{(source.hash % 2) + 2}." << request.domain # cdn2-3.domain.com else "http#{request.ssl? ? 's' : ''}://cdn4." << request.domain # cdn4.domain.com end } 

Hope this helps!

+9
source

Do you mean subdomains ?

 subdomains(tld_length = 1) Returns all the subdomains as an array, so ["dev", "www"] would be returned for "dev.www.rubyonrails.org". You can specify a different tld_length, such as 2 to catch ["www"] instead of ["www", "rubyonrails"] in "www.rubyonrails.co.uk". 
0
source

All Articles