Find file
Fetching contributors…
Cannot retrieve contributors at this time
70 lines (41 sloc) 2 KB

CDN asset URL helpers

This library contains a Rubygem and Django custom templatetag library to do the following:

turn an asset path like


into a CDN-pointed URL like{sha1}.png


Add cdn_helpers to your Gemfile:

gem 'cdn_helpers', :git => ''

In your environments/production.rb:

require 'cdn_helpers'
config.action_controller.asset_host = ""
config.action_controller.asset_path = CdnHelpers::AssetPath

The gem also make the rake task cdn:css_urls available to you. This is for post-deployment rewriting of URLs within CSS files. It's destructive - it rewrites the files in place - but it doesn't require that the config vars above are set since it leaves URLs as absolute local URLs.


Add cdn_helpers to your requirements.txt:

-e git+ssh://

in add cdn_helpers to your list of installed apps:


in your local-development

APP_DEPLOYMENT_ENV = 'local'   # (disables CDN url generation)

in the appropriate

APP_DEPLOYMENT_ENV = 'dev'   # (or staging, production)
CDN_HOSTS = ['cdn1', 'cdn2'] # (as appropriate for the environment)
SHARED_PUBLIC_ROOT = "/path/to/shared/assets" # (i.e. where Static's public dir is)

in templates use the asset_url helper:

<img src="{% asset_url "/images/thing.png" %}">
<img src="{% asset_url thing.image.url %}">

The app provides the cdn_css command for This uses the Ruby script process_css to do the heavy lifting, but relies on the MEDIA_ROOT and MEDIA_URL settings in to tell where to look and how to construct the URLs.

Running tests

bundle exec rake spec

PYTHONPATH=py nosetests -w test