Ruby on Rails 3 - Stylesheets and Javascripts on separate domains
I have seen many websites use a separate domain, or subdomain to serve javascript and stylesheets to speed up site load time. How do I go about implementing the same in my Rails 3 web app?
For example I want
<link href="/stylesheets/style.css?1304887417" media="screen" rel="stylesheet" type="text/css" />
to show as
<link href="http://images.domain.com/stylesheet开发者_如何学编程s/style.css?1304887417" media="screen" rel="stylesheet" type="text/css" />
I am using this tag to show the stylesheets: <%= stylesheet_link_tag 'style', :media => 'screen' %>
Supplying an absolute URL is the most direct way.
<%= stylesheet_link_tag 'http://images.domain.com/stylesheets/style.css', :media => 'screen' %>
I recommend writing your own view helper to help with this.
Edit: Hey, I just learned something today. Check out http://api.rubyonrails.org/classes/ActionView/Helpers/AssetTagHelper.html under "Using asset hosts" for info on how to set up AssetTagHelper to call a CDN, even supporting the multiple server CDN case of assets1.example.com, assets2.example.com, etc.
精彩评论