Sha256: 0d12fbd7714cb5017d81844812ca5d6d288019579713d6a0842159b8204c1239

Contents?: true

Size: 1.08 KB

Versions: 2

Compression:

Stored size: 1.08 KB

Contents

# frozen_string_literal: true

module Motor
  class AssetsController < ActionController::Metal
    CACHE_STORE = ActiveSupport::Cache::MemoryStore.new(coder: ActiveSupport::Cache::NullCoder)

    GZIP_TYPES = [
      'application/javascript',
      'text/css',
      'image/svg+xml'
    ].freeze

    MIME_TYPES = {
      '.js' => 'application/javascript',
      '.css' => 'text/css',
      '.svg' => 'image/svg+xml',
      '.woff2' => 'font/woff2'
    }.freeze

    def show
      filename = params[:filename]

      return [404, {}, ''] unless Motor::Assets.manifest.value?(filename)

      assign_headers(filename)

      self.response_body = CACHE_STORE.fetch(filename) do
        Motor::Assets.load_asset(filename, gzip: headers['Content-Encoding'] == 'gzip')
      end
    end

    private

    def assign_headers(filename)
      content_type = MIME_TYPES[File.extname(filename)]

      headers['Content-Type'] = content_type
      headers['Content-Encoding'] = 'gzip' if !Motor.development? && GZIP_TYPES.include?(content_type)
      headers['Cache-Control'] = 'max-age=31536000'
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
motor-admin-0.1.51 app/controllers/motor/assets_controller.rb
motor-admin-0.1.50 app/controllers/motor/assets_controller.rb