Service assets on a "static" subdomain

How to set up rails for servicing assets on different subdomains? Basically I want view / asset helpers to use a subdomain for all static files, for example:

  • instead of example.com/application.css -> static.example.com/application.css
  • instead of example.com/application.js -> static.example.com/application.js
  • instead of example.com/logo.jpg -> static.example.com/logo.jpg
+6
source share
2 answers

Do you know about asset_host option?

 # config/environments/production.rb config.action_controller.asset_host = "static.example.com" 

You can also execute dynamic names:

 ActionController::Base.asset_host = Proc.new { |source| "http://assets#{Digest::MD5.hexdigest(source).to_i(16) % 2 + 1}.example.com" } 

http://api.rubyonrails.org/classes/ActionView/Helpers/AssetTagHelper.html

+14
source

You can also try the stone rack-cors for sharing Cross-Origin resources. https://github.com/cyu/rack-cors

I used this gem in a Rails 4 application when my beautiful font icons won’t appear when I start using subdomains. This wiki set me on the right path: https://github.com/bokmann/font-awesome-rails/wiki/CORS-Cross-Domain-Support

Besides changing my Gemfile, I also put the following code in config/application.rb at the beginning according to this guide: https://github.com/cyu/rack-cors/blob/master/examples/rails4/config/application. rb

 config.middleware.insert_before 0, "Rack::Cors", :debug => true, :logger => (-> { Rails.logger }) do allow do origins '*' resource '/cors', :headers => :any, :methods => [:post], :max_age => 0 resource '*', :headers => :any, :methods => [:get, :post, :delete, :put, :patch, :options, :head], :max_age => 0 end end 
+1
source

Source: https://habr.com/ru/post/906932/


All Articles