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
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
Hope this helps!
source share