Rack::Bundle

Added by julioody at July 07, 2010 05:03 Star_small_grayStar_small_grayStar_small_grayStar_small_grayStar_small_gray optimization
GitHub stats:
Magnifier watcher(s)
Arrow_branch fork(s)
Wrench

About

Javascript/CSS bundling at the Rack level

Details

A middleware for packaging Javascripts together, so everything get’s served up as one single request. Does the same for CSS. Skips scripts that sit outside of the app’s domain.

Usage

Rack:

use Rack::Bundle, :public_dir => "path/to/app/public/dir"
run app

Sinatra it's almost the same as above, except you don't need to explicitly call run as Sinatra will handle that:

use Rack::Bundle, :public_dir => Sinatra::Application.public

As for Rails, google around how to add Rack middlewares to the stack. I'm too lazy right now to look it up. But as a general pointer, I know it's in ROOT/config/environment.rb.

By default, this middleware will use the file system for storing bundles. For Heroku and a few other setups where the application doesn't have permission to write to certain directories, you can store and serve bundles directly from a database. Like so:

use Rack::Bundle, :public_dir => 'path/to/public' do |rack_bundle|
  rack_bundle.storage = Rack::Bundle::DatabaseStore.new
end

DatabaseStore assumes an environment variable called DATABASE_URL exists, which is an URL that the adapter can use to connect to a database (See examples). You can alternatively supply that as parameter. For instance:

use Rack::Bundle, :public_dir => 'path/to/public' do |rack_bundle|
  rack_bundle.storage = Rack::Bundle::DatabaseStore.new "sqlite://foo.sqlite3"
end
blog comments powered by Disqus