-
Notifications
You must be signed in to change notification settings - Fork 22k
Enable gzip compression by default #16466
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Enable gzip compression by default #16466
Conversation
Shouldn't we only set Vary if a |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do headers['Cache-Control'] = ...
to avoid extra hash allocations.
@matthewd i'm not sure. Setting Vary to
So if we don't set it on the non-gzip response if a client makes a request with Here's a blog post that, to me, makes It's possible that i'm not reading or interpreting that correctly. Any sane/easy ways you can think to test this out? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can just do File.exist?(File.join(@root, "#{::Rack::Utils.unescape(env["PATH_INFO"])}.gz"))
with no return false
on next line either.
Also nice to extract gz_path
to own method:
def gz_path
File.join(@root, "#{::Rack::Utils.unescape(env["PATH_INFO"])}.gz")
end
Note I said "if a |
@matthewd ahh, thanks I didn't read close enough to your comment. I understand better now, thanks. You're correct. I'm modifying to always check presence of a gzip file and only set the header if it's available. I'm not sure if the disk hit on non-gzipped files is a net win for doing one less cache miss. It is the technically correct thing to do here, i'm pushed the change in behavior and broke out some logic into methods per @sgrif's comment |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yo dawg, I herd you like controlling caches.
If someone is using ActionDispatch::Static to serve assets and makes it past the `match?` then the file exists on disk and it will be served. This PR adds in logic that checks to see if the file being served is already compressed (via gzip) and on disk, if it is it will be served as long as the client can handle gzip encoding. If not, then a non gzip file will be served. This additional logic slows down an individual asset request but should speed up the consumer experience as compressed files are served and production applications should be delivered with a CDN. This PR allows a CDN to cache a gzip file by setting the `Vary` header appropriately. In net this should speed up a production application that are using Rails as an origin for a CDN. Non-asset request speed is not affected in this PR.
f3b7275
to
cfaaacd
Compare
Enable gzip compression by default
Turns out this gem does nothing post Rails 4.2 rails/rails#16466 This reverts commit 31f3f85.
If someone is using ActionDispatch::Static to serve assets and makes it past the
match?
then the file exists on disk and it will be served. This PR adds in logic that checks to see if the file being served is already compressed (via gzip) and on disk, it will be served as long as the client can handle gzip encoding. If not, then a non-gzip file will be served.This additional logic slows down an individual asset request but should speed up the consumer experience as compressed files are served and production applications should be delivered with a CDN. This PR allows a CDN to cache a gzip file by setting the
Vary
header appropriately. In net this should speed up a production application that are using Rails as an origin for a CDN. Non-asset request speed is not affected in this PR.