require 'action_dispatch/middleware/static' | |
module Middleware | |
class FileHandler < ActionDispatch::FileHandler | |
def initialize(root, assets_path, cache_control) | |
@assets_path = assets_path.chomp('/') + '/' | |
super(root, cache_control) | |
end | |
def match?(path) | |
path.start_with?(@assets_path) && super(path) | |
end | |
end | |
class CompressedStaticAssets | |
def initialize(app, path, assets_path, cache_control=nil) | |
@app = app | |
@file_handler = FileHandler.new(path, assets_path, cache_control) | |
end | |
def call(env) | |
if env['REQUEST_METHOD'] == 'GET' | |
request = Rack::Request.new(env) | |
encoding = Rack::Utils.select_best_encoding( | |
%w(gzip identity), request.accept_encoding) | |
if encoding == 'gzip' | |
pathgz = env['PATH_INFO'] + '.gz' | |
if match = @file_handler.match?(pathgz) | |
# Get the filehandler to serve up the gzipped file, | |
# then strip the .gz suffix | |
env["PATH_INFO"] = match | |
status, headers, body = @file_handler.call(env) | |
path = env["PATH_INFO"] = env["PATH_INFO"].chomp('.gz') | |
# Set the Vary HTTP header. | |
vary = headers["Vary"].to_s.split(",").map { |v| v.strip } | |
unless vary.include?("*") || vary.include?("Accept-Encoding") | |
headers["Vary"] = vary.push("Accept-Encoding").join(",") | |
end | |
headers['Content-Encoding'] = 'gzip' | |
headers['Content-Type'] = | |
Rack::Mime.mime_type(File.extname(path), 'text/plain') | |
headers.delete('Content-Length') | |
return [status, headers, body] | |
end | |
end | |
end | |
@app.call(env) | |
end | |
end | |
end |
# Serve pre-gzipped static assets | |
middleware.insert_after( | |
'Rack::Cache', Middleware::CompressedStaticAssets, | |
paths["public"].first, config.assets.prefix, config.static_cache_control) |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
Show comment Hide comment
neersighted
commented
Sep 26, 2012
This is awesome! Mind if I gemify it? |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
Show comment Hide comment
EduBcn
Dec 2, 2012
Hi! Newbie question here: Where do I put the first file? I created a folder "app/middleware" but I'm getting an "uninitialized constant error Middleware" in production.rb...
EduBcn
commented
Dec 2, 2012
Hi! Newbie question here: Where do I put the first file? I created a folder "app/middleware" but I'm getting an "uninitialized constant error Middleware" in production.rb... |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
Show comment Hide comment
theo-bittencourt
commented
Feb 10, 2013
@EduBcn - Try config/initializers/ |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
Show comment Hide comment
loicginoux
Feb 23, 2013
I have an error when startng the app:
"/app/config/environments/staging.rb:33:in `block in <top (required)>': uninitialized constant Middleware (NameError)"
any idea?
loicginoux
commented
Feb 23, 2013
I have an error when startng the app: any idea? |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
Show comment Hide comment
bensheldon
Feb 24, 2013
FYI - this functionality can now be achieved by using the heroku-deflater gem.
bensheldon
commented
Feb 24, 2013
FYI - this functionality can now be achieved by using the heroku-deflater gem. |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
Show comment Hide comment
marcgg
Feb 26, 2013
@bensheldon the gem doesn't seem very followed and is maintained by a person and not an organization. Are you sure about it? Have you used it in production?
marcgg
commented
Feb 26, 2013
@bensheldon the gem doesn't seem very followed and is maintained by a person and not an organization. Are you sure about it? Have you used it in production? |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
Show comment Hide comment
luizfonseca
Mar 31, 2013
Hey @marcgg, I'm using it in production and yep, it works like a charm. Some pages went from 2mb/1mb when cached to 12kb when properly cached.
luizfonseca
commented
Mar 31, 2013
Hey @marcgg, I'm using it in production and yep, it works like a charm. Some pages went from 2mb/1mb when cached to 12kb when properly cached. |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
Show comment Hide comment
mattolson
Apr 2, 2013
@bensheldon @marcgg FYI the heroku-deflater gem is slightly different than this gist. The gem will use Rack::Deflater for every response, but tell it to skip binary files via the no-transform Cache-control directive. This gist takes advantage of the higher compression ratio for precompiled assets by serving up the .gz version, but by itself doesn't do anything with other types of requests. I think a combination of the two approaches is the ideal.
mattolson
commented
Apr 2, 2013
@bensheldon @marcgg FYI the heroku-deflater gem is slightly different than this gist. The gem will use Rack::Deflater for every response, but tell it to skip binary files via the no-transform Cache-control directive. This gist takes advantage of the higher compression ratio for precompiled assets by serving up the .gz version, but by itself doesn't do anything with other types of requests. I think a combination of the two approaches is the ideal. |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
Show comment Hide comment
mattolson
Apr 3, 2013
@guyboltonking @neersighted @bensheldon @marcgg @runeroniek I have taken the liberty of gemifying this code (with changes to make it play nice with Rack::Deflate). It is available here. It's been working well for me. Let me know if you have any questions.
mattolson
commented
Apr 3, 2013
@guyboltonking @neersighted @bensheldon @marcgg @runeroniek I have taken the liberty of gemifying this code (with changes to make it play nice with Rack::Deflate). It is available here. It's been working well for me. Let me know if you have any questions. |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
Show comment Hide comment
eliotsykes
Jul 21, 2013
Another option for serving gzipped assets using Rack::Rewrite https://gist.github.com/eliotsykes/6049536
eliotsykes
commented
Jul 21, 2013
Another option for serving gzipped assets using Rack::Rewrite https://gist.github.com/eliotsykes/6049536 |
This is awesome! Mind if I gemify it?