Action View Asset Tag Helpers
This module provides methods for generating HTML that links views to assets such as images, JavaScripts, stylesheets, and feeds. These methods do not verify the assets exist before linking to them:
image_tag("rails.png") # => <img src="/assets/rails.png" /> stylesheet_link_tag("application") # => <link href="/assets/application.css?body=1" rel="stylesheet" />
Constants
MAX_HEADER_SIZE = 1_000
Attributes
Alternative hostname generation method
Instead of using a random number to generate the hostname for the single asset, I prefer using source.hash.modulo, so that a given file is always served from the same host. This makes the content more cacheable by browsers and proxies.
ActionController::Base.asset_host = Proc.new { |source| "http://assets#{ source.hash.modulo(9) }.example.com" }
I didn’t benchmark how long it takes, but String#hash should be reasonably fast.
RE: Alternative hostname generation method
According to compute_asset_host as of rails 2.0.0 the %d expansion works in the same way, it’s not a random number generated on each call but the source hash mod 4.
In my brief testing in rails 2.3.9, %d doesn’t appear to work if you use a Proc, so manual generation is of use in that case.