Project

cdn_tags

0.0
No commit activity in last 3 years
No release in over 3 years
Helpers to serve assets via CDN in Rails.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 0.7
~> 1.6
~> 3.0

Runtime

>= 3.0
 Project Readme

CdnTags Build Status Coverage Status Code Climate Gem Version

Using a CDN in development is kind of painful when the network is unstable, however in production common libraries should generally be served via a CDN. This gem helps to automatize this process.

This gem has been tested with Rails 3 and 4, and Ruby 1.9, 2.0 and 2.1.

Installation

Add

gem 'cdn_tags'

to your Gemfile and run

bundle install

Configuration

Create config/initializers/cdn_tags.rb and configure your assets. You can generate it by using

rails generate cdn_tags:install

Here is a sample configuration.

CdnTags.configure do |c|
  c.scripts_urls = {
    '/path/to/jquery' => '//code.jquery.com/jquery-2.1.1.min.js',
  }
  c.stylesheets_urls = {
    '/path/to/bootstrap' => '//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css'
  }
end

This will automatically add the files to Rails.application.config.assets.precompile. If you want to disable this behavior, you can set add_to_precompile to false in the configuration.

Configuration items

  • scripts_urls (Hash, default: {}): The scripts that should be served from a CDN.
  • stylesheets_urls (Hash, default: {}): The stylesheets that should be served from a CDN.
  • add_to_precompile (true|false, default: true): Automatically add assets to Rails.application.config.assets or not.
  • raise_on_missing (true|false|Array, default: false): Raise an exception or not if the asset used with CDN helper is not defined in scripts_urls or stylesheets_urls. If an array is given, an exception will be set if the current environment is in this array.
  • cdn_environments (Array, default: [:production]): The environments in which CDN should be used instead of normal asset.

Usage

Just replace javascript_include_tag and stylesheet_link_tag by javascript_cdn_include_tag and stylesheet_cdn_link_tag.

For example,

<%= javascript_include_tag '/path/to/jquery' %>
<%= stylesheet_link_tag '/path/to/bootstrap' %>

becomes

<%= javascript_cdn_include_tag '/path/to/jquery' %>
<%= stylesheet_cdn_link_tag '/path/to/bootstrap' %>

This will result in the following HTML output.

  • In development and test environments:
<script src="/assets/jquery-1.9.1.js?body=1"></script>
  • In production environment:
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>