LocalAssets

LocalAssets facilitates loading of assets normally served by various content delivery networks (CDNs) from local sources in development so your site loads faster, and works even when you don’t have an Internet connection (eg, on an airplane).

To set up a local CDN mirror, see: github.com/alexreisner/cdn_mirror

Rails

Add the gem to your Gemfile. Then add the following to your config/environments/development.rb file:

config.middleware.use "Rack::LocalAssets", {
  "//ajax.googleapis.com/ajax/libs/" => "//localhost/cdn_mirror/google/"
}

The configuration hash should contain any URL fragments that should be replaced with local alternatives in the development environment. The above example is for assets normally served by Google’s CDN.

Sinatra

require ‘local_assets’

use Rack::LocalAssets,

"//ajax.googleapis.com/ajax/libs/" => "//localhost/cdn_mirror/google/"

To-do List

  • require replaced URLs to be in src or href attributes

  • allow external config file which does not get committed to repo

    • allows different local URLs on different development machines

  • implement as a railtie and load middleware automatically on app_middleware config hook

Copyright © 2009-10 Alex Reisner, released under the MIT license