alexspeller / non-stupid-digest-assets

Fix the Rails 4 asset pipeline to generate non-digest along with digest assets
MIT License
493 stars 87 forks source link

Alternative: controller proxy #46

Closed sfcgeorge closed 7 years ago

sfcgeorge commented 7 years ago

I came up with this rather simple solution: add a controller action that proxies the file. Seems to work great! Obviously it only works with precompiled files so run rake assets:precompile to test in development.

# config/routes.rb
get "integrations/assets/:id", to: "assets#show"
# app/controllers/assets_controller.rb
class AssetsController < ApplicationController
  skip_before_filter :verify_authenticity_token, only: [:show]

  def show
    file_name = "#{params[:id]}.#{params[:format]}"
    asset_path = ActionController::Base.helpers.asset_path(file_name)
    file_path = "#{Rails.root}/public#{asset_path}"
    mime_type = Mime::Type.lookup_by_extension(params[:format])

    send_file(file_path, type: mime_type, disposition: "inline")
  end
end

Posting here as a different solution to https://github.com/rails/sprockets-rails/issues/49 because that thread is locked.