Skip to content

Add cache control headers to assets #104

Closed
@code-bunny

Description

@code-bunny

The assets controller as it currently works by streaming the file:

def show
  @asset = Maglev::Asset.find(resource_id)
  send_data @asset.download, filename: @asset.filename, type: @asset.content_type
end

This results in cache-control: max-age=0, private, must-revalidate

The most sensible fix is to respect the config.public_file_server.headers set by in the Rails environment.

I suggest something like below

def show
  @asset = Maglev::Asset.find(resource_id)
  send_data @asset.download, filename: @asset.filename, type: @asset.content_type

  cache_control = Rails.configuration.public_file_server.headers["cache-control"]
  if cache_control && cache_control.match(/max-age=(\d+)/)
    max_age = Regexp.last_match(1).to_i
    expires_in max_age.seconds, public: true
  end
end

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions