Class: HerokuDeflater::SkipBinary

Inherits:
Object
  • Object
show all
Defined in:
lib/heroku-deflater/skip_binary.rb

Overview

Add a no-transform Cache-Control header to binary types, so they won’t get gzipped

Constant Summary collapse

WHITELIST =
[
  %r{^text/},
  'application/javascript',
  %r{^application/.*?json},
  %r{^application/.*?xml},
  'application/x-font-ttf',
  'font/opentype',
  'image/svg+xml'
].freeze

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ SkipBinary

Returns a new instance of SkipBinary.



4
5
6
# File 'lib/heroku-deflater/skip_binary.rb', line 4

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/heroku-deflater/skip_binary.rb', line 18

def call(env)
  status, headers, body = @app.call(env)
  headers = Rack::Utils::HeaderHash.new(headers)
  content_type = headers['Content-Type']
  cache_control = headers['Cache-Control'].to_s.downcase

  unless cache_control.include?('no-transform') || WHITELIST.any? { |type| type === content_type }
    if cache_control.empty?
      headers['Cache-Control'] = 'no-transform'
    else
      headers['Cache-Control'] += ', no-transform'
    end
  end

  body.close if body.respond_to?(:close)
  [status, headers, body]
end