The Asset Pipeline in Sinatra

The spectacular asset pipeline that shipped with Rails 3.1 is an easy feature to get used to once you've started using it, and when writing light Sinatra apps it's one feature that I miss enough to pull in. Initially, I'd boot up a Sprockets module directly in my application's rackup file (that's and map it to a particular URL path. This approach works well, but makes a mess in the rackup file and offers little in terms of fine-grained control. More recently I've found that I can get better flexibility and clarity by running it from a custom Sinatra module.

Add Sprockets and Yahoo's YUI compressor to your Gemfile:

gem "sprockets"
gem "yui-compressor"

# I find it well worth to include CoffeeScript and SASS as well
gem "coffee-script"
gem "sass"

Your assets file structure should look something like this:

+ assets
  + images
    - my-jpg.jpg
    - my-png.png
  + javascripts
    - app.js
  + stylesheets
    - app.css
    - my-styles.sass

app.js should load all other JavaScript assets in its directory (in the example structure above this will pick up

//= require_tree

app.css as well (includes my-styles.sass):

//= require_tree

The Sinatra module should look something like this:

class Assets < Sinatra::Base
  configure do
    set :assets, ( { |env|
      env.append_path(settings.root + "/assets/images")
      env.append_path(settings.root + "/assets/javascripts")
      env.append_path(settings.root + "/assets/stylesheets")

      # compress everything in production
      if ENV["RACK_ENV"] == "production"
        env.js_compressor  =
        env.css_compressor =

  get "/assets/app.js" do

  get "/assets/app.css" do

  %w{jpg png}.each do |format|
    get "/assets/:image.#{format}" do |image|

Now use the assets module as middleware in, and delegate everything else to your main app:

use Assets
run Sinatra::Application

Posted on November 10, 2012 from the De Young Museum, San Francisco

Newest Articles


My name is Brandur. I'm a polyglot software engineer and part-time designer working at Heroku in San Francisco, California. I'm a Canadian expat. My name is Icelandic. Drop me a line at [email protected].

Aside from technology, I'm interested in energy and how it relates to our society, travel, longboarding, muay thai, symphonic metal, and the guitar.

If you liked this article, consider finding me on Twitter.