Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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) || response.kind_of?(Sinatra::Helpers::StaticFile)) 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
Add Comment
Please, Sign In to add comment