Coder Social home page Coder Social logo

cdn_helpers's Introduction

CDN asset URL helpers

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

turn an asset path like

/images/thing.png

into a CDN-pointed URL like

http://cdn.name.com/images/thing.png/{sha1}.png

Rails

Add cdn_helpers to your Gemfile:

gem 'cdn_helpers', :git => '[email protected]:alphagov/cdn_helpers.git'

In your environments/production.rb:

require 'cdn_helpers'
config.action_controller.asset_host = "http://cdn.url.gov.uk"
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.

Django

Add cdn_helpers to your requirements.txt:

-e git+ssh://[email protected]/alphagov/cdn_helpers.git#egg=cdn_helpers

in settings.py add cdn_helpers to your list of installed apps:

INSTALLED_APPS = (
    'django.contrib.auth',
    ...
    'cdn_helpers'
)

in your local-development local_settings.py:

APP_DEPLOYMENT_ENV = 'local'   # (disables CDN url generation)
CDN_HOSTS = []

in the appropriate local_settings.py:

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 manage.py. This uses the Ruby script process_css to do the heavy lifting, but relies on the MEDIA_ROOT and MEDIA_URL settings in settings.py to tell where to look and how to construct the URLs.

Running tests

bundle exec rake spec

PYTHONPATH=py nosetests -w test

cdn_helpers's People

Contributors

fidothe avatar jystewart avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.