Sha256: 6b01a5165339d5866430aafd0b005aea6de4ca81ec6584f48e3f74264b23faf8

Contents?: true

Size: 1.04 KB

Versions: 135

Compression:

Stored size: 1.04 KB

Contents

# frozen_string_literal: true

module Motor
  class AssetsController < ActionController::Metal
    CACHE_STORE = ActiveSupport::Cache::MemoryStore.new

    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

135 entries across 135 versions & 1 rubygems

Version Path
motor-admin-0.2.88 app/controllers/motor/assets_controller.rb
motor-admin-0.2.87 app/controllers/motor/assets_controller.rb
motor-admin-0.2.86 app/controllers/motor/assets_controller.rb
motor-admin-0.2.85 app/controllers/motor/assets_controller.rb
motor-admin-0.2.84 app/controllers/motor/assets_controller.rb
motor-admin-0.2.83 app/controllers/motor/assets_controller.rb
motor-admin-0.2.82 app/controllers/motor/assets_controller.rb
motor-admin-0.2.81 app/controllers/motor/assets_controller.rb
motor-admin-0.2.80 app/controllers/motor/assets_controller.rb
motor-admin-0.2.79 app/controllers/motor/assets_controller.rb
motor-admin-0.2.78 app/controllers/motor/assets_controller.rb
motor-admin-0.2.77 app/controllers/motor/assets_controller.rb
motor-admin-0.2.76 app/controllers/motor/assets_controller.rb
motor-admin-0.2.75 app/controllers/motor/assets_controller.rb
motor-admin-0.2.74 app/controllers/motor/assets_controller.rb
motor-admin-0.2.73 app/controllers/motor/assets_controller.rb
motor-admin-0.2.72 app/controllers/motor/assets_controller.rb
motor-admin-0.2.71 app/controllers/motor/assets_controller.rb
motor-admin-0.2.70 app/controllers/motor/assets_controller.rb
motor-admin-0.2.69 app/controllers/motor/assets_controller.rb