Reputation: 646
I am going to move my assets from rails to amazon cloudfront cdn. I have moved images and give path from /assets/images to http://cdn.com/assets/iamges.
Now I would like to move my stylesheets and JS files, but in rails all stylesheets and JS files are included in application.css and applications.js and served with the asset pipeline. and it's enough
<%= stylesheet_link_tag "application" %>
<%= javascript_include_tag "application" %>
to include above lines in application.html.erb. If I move all my JS files to CDN means how to call them in application.html.erb file? Whether I have to include all the js files (they are at-least 20-25 ) using javascript_include_tag or any other way to call them in assets pipeline itself from CDN.
Upvotes: 2
Views: 5456
Reputation: 8402
from RailsGuides :
A common pattern for using a CDN is to set your production application as the "origin" server. This means when a browser requests an asset from the CDN and there is a cache miss, it will grab the file from your server on the fly and then cache it. For example if you are running a Rails application on example.com and have a CDN configured at mycdnsubdomain.fictional-cdn.com, then when a request is made to mycdnsubdomain.fictional- cdn.com/assets/smile.png, the CDN will query your server once at example.com/assets/smile.png and cache the request. The next request to the CDN that comes in to the same URL will hit the cached copy. When the CDN can serve an asset directly the request never touches your Rails server. Since the assets from a CDN are geographically closer to the browser, the request is faster, and since your server doesn't need to spend time serving assets, it can focus on serving application code as fast as possible.
So you can keep your files in your server and make the CDN to point to it.
In config/environments/production.rb set the host for your asset to be your CDN url, something like this :
config.action_controller.asset_host = 'yourcdnsubdomain.cloudfront.com'
You don't have to specify the http/https in your asset_host url, if the user access your website via https, then the asset url will be https as well, this is from the doc:
You only need to provide the "host", this is the subdomain and root domain, you do not need to specify a protocol or "scheme" such as http:// or https://. When a web page is requested, the protocol in the link to your asset that is generated will match how the webpage is accessed by default.
For more details check : http://edgeguides.rubyonrails.org/asset_pipeline.html the "CDN" part
Hope this help.
Upvotes: 4