Guest User

Untitled

a guest
Jun 21st, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.81 KB | None | 0 0
  1. module Heroku
  2. class StaticAssetsMiddleware
  3. def initialize(app)
  4. @app = app
  5. end
  6.  
  7. def call(env)
  8. # call returns an array containing [response code, header, Rack::Response]
  9. reply = @app.call(env)
  10.  
  11. reply = cache_static_asset(reply)
  12.  
  13. reply
  14. end
  15.  
  16. def cache_static_asset(reply)
  17. return reply unless can_cache?(reply)
  18.  
  19. status, headers, response = reply
  20.  
  21. # static files are cacheable for 12hrs
  22. headers['Cache-Control'] = 'public, max-age=43200'
  23.  
  24. build_new_reply(status, headers, response)
  25. end
  26.  
  27. def can_cache?(reply)
  28. response = reply[2]
  29. status = reply[0]
  30. (response.kind_of?(Rack::File) || response.kind_of?(Sinatra::Helpers::StaticFile)) and status.to_i == 200
  31. end
  32.  
  33. def build_new_reply(status, headers, response)
  34. headers.delete('Etag') if headers.has_key?('Etag')
  35. [ status, headers, response ]
  36. end
  37. end
  38. end
Add Comment
Please, Sign In to add comment