Skip to content

Instantly share code, notes, and snippets.

@tatey
Created October 31, 2013 23:48
Show Gist options
  • Save tatey/7259055 to your computer and use it in GitHub Desktop.
Save tatey/7259055 to your computer and use it in GitHub Desktop.
class Deflater
def initialize app
@app = app
end
def call env
if acceptable_method?(env) && compress?(env)
Rack::Deflater.new(app).call env
else
app.call env
end
end
private
attr_reader :app
def acceptable_method? env
['GET', 'HEAD'].include? env['REQUEST_METHOD']
end
def compress? env
env['REQUEST_PATH'] !~ /\.(png|jpg|jpeg|gif)\z/
end
end
# Sets the correct headers when serving files from /public. CloudFront
# will forward these headers to the client.
config.middleware.insert 0, 'Rack::Static',
urls: Rails.root.join('public').children(false).map { |p| "/#{p}" },
root: Rails.root.join('public'),
header_rules: [
# Clients should cache assets for one year.
['/assets', {'Cache-Control' => 'public, max-age=3136000'}],
# Provide web fonts with cross-origin access-control-headers
# Firefox requires this when serving assets using a CDN.
[:fonts, {'Access-Control-Allow-Origin' => '*'}]
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment