Skip to content

Instantly share code, notes, and snippets.

@adamwiggins
Created July 18, 2010 23:37
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamwiggins/480819 to your computer and use it in GitHub Desktop.
Save adamwiggins/480819 to your computer and use it in GitHub Desktop.
module Heroku
class StaticAssetsMiddleware
def initialize(app)
@app = app
end
def call(env)
# call returns an array containing [response code, header, Rack::Response]
reply = @app.call(env)
reply = cache_static_asset(reply)
reply
end
def cache_static_asset(reply)
return reply unless can_cache?(reply)
status, headers, response = reply
# static files are cacheable for 12hrs
headers['Cache-Control'] = 'public, max-age=43200'
build_new_reply(status, headers, response)
end
def can_cache?(reply)
response = reply[2]
status = reply[0]
response.kind_of?(Rack::File) and status.to_i == 200
end
def build_new_reply(status, headers, response)
headers.delete('Etag') if headers.has_key?('Etag')
[ status, headers, response ]
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment