I am attempt to set my asset host so that my images are served through a CDN. as is best practice, my CDN is pointed directly back to my app's root domain, making this apply only as a mechanism to speed up the image and font loading.
symptom: in my Rails 6.1.3.1 app
in production.rb
when I set
config.action_controller.asset_host = "xxxxxxxx.cloudfront.net"
on Heroku I get this when I deploy:
remote: rake aborted!
remote: NoMethodError: undefined method `protocol' for nil:NilClass
remote: /tmp/build_c7e7c4dd/vendor/bundle/ruby/2.6.0/gems/actionview-6.1.3.1/lib/action_view/helpers/asset_url_helper.rb:303:in `compute_asset_host'
remote: /tmp/build_c7e7c4dd/vendor/bundle/ruby/2.6.0/gems/actionview-6.1.3.1/lib/action_view/helpers/asset_url_helper.rb:212:in `asset_path'
remote: /tmp/build_c7e7c4dd/vendor/bundle/ruby/2.6.0/gems/actionview-6.1.3.1/lib/action_view/helpers/asset_url_helper.rb:231:in `asset_url'
remote: /tmp/build_c7e7c4dd/vendor/bundle/ruby/2.6.0/gems/exception_handler-0.8.0.1/app/assets/stylesheets/styles/_exception.css.erb:28:in `_evaluate_template'
remote: /tmp/build_c7e7c4dd/vendor/bundle/ruby/2.6.0/gems/exception_handler-0.8.0.1/app/assets/stylesheets/styles/_exception.css.erb:28:in `_evaluate_template'
remote: /tmp/build_c7e7c4dd/vendor/bundle/ruby/2.6.0/gems/sprockets-3.7.2/lib/sprockets/erb_processor.rb:26:in `call'
remote: /tmp/build_c7e7c4dd/vendor/bundle/ruby/2.6.0/gems/sprockets-3.7.2/lib/sprockets/erb_processor.rb:13:in `call'
remote: /tmp/build_c7e7c4dd/vendor/bundle/ruby/2.6.0/gems/sprockets-3.7.2/lib/sprockets/processor_utils.rb:75:in `call_processor'
remote: /tmp/build_c7e7c4dd/vendor/bundle/ruby/2.6.0/gems/sprockets-3.7.2/lib/sprockets/processor_utils.rb:57:in `block in call_processors'
remote: /tmp/build_c7e7c4dd/vendor/bundle/ruby/2.6.0/gems/sprockets-3.7.2/lib/sprockets/processor_utils.rb:56:in `reverse_each'
remote: /tmp/build_c7e7c4dd/vendor/bundle/ruby/2.6.0/gems/sprockets-3.7.2/lib/sprockets/processor_utils.rb:56:in `call_processors'
remote: /tmp/build_c7e7c4dd/vendor/bundle/ruby/2.6.0/gems/sprockets-3.7.2/lib/sprockets/loader.rb:134:in `load_from_unloaded'
remote: /tmp/build_c7e7c4dd/vendor/bundle/ruby/2.6.0/gems/sprockets-3.7.2/lib/sprockets/loader.rb:60:in `block in load'
remote: /tmp/build_c7e7c4dd/vendor/bundle/ruby/2.6.0/gems/sprockets-3.7.2/lib/sprockets/loader.rb:317:in `fetch_asset_from_dependency_cache'
remote: /tmp/build_c7e7c4dd/vendor/bundle/ruby/2.6.0/gems/sprockets-3.7.2/lib/sprockets/loader.rb:44:in `load'
remote: /tmp/build_c7e7c4dd/vendor/bundle/ruby/2.6.0/gems/sprockets-3.7.2/lib/sprockets/cached_environment.rb:20:in `block in initialize'
remote: /tmp/build_c7e7c4dd/vendor/bundle/ruby/2.6.0/gems/sprockets-3.7.2/lib/sprockets/cached_environment.rb:47:in `load'
remote: /tmp/build_c7e7c4dd/vendor/bundle/ruby/2.6.0/gems/sprockets-3.7.2/lib/sprockets/bundle.rb:23:in `block in call'
remote: /tmp/build_c7e7c4dd/vendor/bundle/ruby/2.6.0/gems/sprockets-3.7.2/lib/sprockets/utils.rb:200:in `dfs'
remote: /tmp/build_c7e7c4dd/vendor/bundle/ruby/2.6.0/gems/sprockets-3.7.2/lib/sprockets/bundle.rb:24:in `call'
remote: /tmp/build_c7e7c4dd/vendor/bundle/ruby/2.6.0/gems/sprockets-3.7.2/lib/sprockets/processor_utils.rb:75:in `call_processor'
remote: /tmp/build_c7e7c4dd/vendor/bundle/ruby/2.6.0/gems/sprockets-3.7.2/lib/sprockets/processor_utils.rb:57:in `block in call_processors'
remote: /tmp/build_c7e7c4dd/vendor/bundle/ruby/2.6.0/gems/sprockets-3.7.2/lib/sprockets/processor_utils.rb:56:in `reverse_each'
remote: /tmp/build_c7e7c4dd/vendor/bundle/ruby/2.6.0/gems/sprockets-3.7.2/lib/sprockets/processor_utils.rb:56:in `call_processors'
remote: /tmp/build_c7e7c4dd/vendor/bundle/ruby/2.6.0/gems/sprockets-3.7.2/lib/sprockets/loader.rb:134:in `load_from_unloaded'
remote: /tmp/build_c7e7c4dd/vendor/bundle/ruby/2.6.0/gems/sprockets-3.7.2/lib/sprockets/loader.rb:60:in `block in load'
remote: /tmp/build_c7e7c4dd/vendor/bundle/ruby/2.6.0/gems/sprockets-3.7.2/lib/sprockets/loader.rb:317:in `fetch_asset_from_dependency_cache'
remote: /tmp/build_c7e7c4dd/vendor/bundle/ruby/2.6.0/gems/sprockets-3.7.2/lib/sprockets/loader.rb:44:in `load'
remote: /tmp/build_c7e7c4dd/vendor/bundle/ruby/2.6.0/gems/sprockets-3.7.2/lib/sprockets/cached_environment.rb:20:in `block in initialize'
remote: /tmp/build_c7e7c4dd/vendor/bundle/ruby/2.6.0/gems/sprockets-3.7.2/lib/sprockets/cached_environment.rb:47:in `load'
remote: /tmp/build_c7e7c4dd/vendor/bundle/ruby/2.6.0/gems/sprockets-3.7.2/lib/sprockets/base.rb:66:in `find_asset'
remote: /tmp/build_c7e7c4dd/vendor/bundle/ruby/2.6.0/gems/sprockets-3.7.2/lib/sprockets/base.rb:73:in `find_all_linked_assets'
remote: /tmp/build_c7e7c4dd/vendor/bundle/ruby/2.6.0/gems/sprockets-3.7.2/lib/sprockets/manifest.rb:134:in `block in find'
remote: /tmp/build_c7e7c4dd/vendor/bundle/ruby/2.6.0/gems/sprockets-3.7.2/lib/sprockets/manifest.rb:133:in `each'
remote: /tmp/build_c7e7c4dd/vendor/bundle/ruby/2.6.0/gems/sprockets-3.7.2/lib/sprockets/manifest.rb:133:in `find'
remote: /tmp/build_c7e7c4dd/vendor/bundle/ruby/2.6.0/gems/sprockets-3.7.2/lib/sprockets/manifest.rb:186:in `compile'
remote: /tmp/build_c7e7c4dd/vendor/bundle/ruby/2.6.0/gems/sprockets-rails-3.2.2/lib/sprockets/rails/task.rb:67:in `block (3 levels) in define'
remote: /tmp/build_c7e7c4dd/vendor/bundle/ruby/2.6.0/gems/sprockets-3.7.2/lib/rake/sprocketstask.rb:147:in `with_logger'
remote: /tmp/build_c7e7c4dd/vendor/bundle/ruby/2.6.0/gems/sprockets-rails-3.2.2/lib/sprockets/rails/task.rb:66:in `block (2 levels) in define'
remote: Tasks: TOP => assets:precompile
This is because of the not specified protocol for the asset_host value.
config.action_controller.asset_host = "https://#{Rails.application.credentials.dig(:cdn_asset_host)}"
Move CDN value to the credentials.yml for security purposes and load asset_host as mentioned above.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With