Class: Rack::ImageAssetsCacheControl

Inherits:
Object
  • Object
show all
Defined in:
lib/rack_image_assets_cache_control.rb

Constant Summary collapse

ImagePostfixRegexp =
Regexp.new(
  MIME::Types.to_a.select {|t| t.media_type == 'image' }.map {|i| i.raw_sub_type }.reject {|i| i.match(/\.|\-|\+/) }.map {|i| "\.#{i}" }.join("|"),
  'i'
)

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ ImageAssetsCacheControl

Returns a new instance of ImageAssetsCacheControl.



10
11
12
13
# File 'lib/rack_image_assets_cache_control.rb', line 10

def initialize(app, options={})
  @app = app
  @max_age = options["max-age"] || 300
end

Instance Method Details

#call(env) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/rack_image_assets_cache_control.rb', line 15

def call(env)
  @status, @headers, @response = @app.call(env)

  # if @status is 304, then @headers and @response will be blank,
  # so @headers['Content-Type'].to_s.start_with?("image") will not work
  if env['REQUEST_PATH'].match(ImagePostfixRegexp)
    @headers.merge!("Cache-Control" => "public, max-age=#{@max_age}")
  end

  [@status, @headers, @response]
end