Rails asset_host, cloudfront and heroku

此生再无相见时 提交于 2019-12-10 22:41:54

问题


I'm running a rails 4.0 app on heroku and for the life of me I can't get my asset urls using the host I've set in asset_host.

I believe my cloudfront setup is good because I can substitute in my cloudfront url for any of my asset urls and the file is picked up from heroku and cached on cloudfront.

So https://xxxxxxxxxxxx.cloudfront.net/assets/application-xxxxxxxx.js is caching https://myapp.com/assets/application-xxxxxxxxx.js correctly.

The issue seems to be my assets helper, such as javascript_include_tag, are never using the asset_host setting in staging.rb.

All I see when I load my page is all my js and css files being served from https://myapp.com/assets/

My staging setup looks like this:

  # Full error reports are disabled and caching is turned on
  config.consider_all_requests_local       = false
  config.action_controller.perform_caching = true

  # Disable Rails's static asset server (Apache or nginx will already do this)
  config.serve_static_assets = true

  # Compress JavaScripts and CSS
  config.assets.compress = true

  # Don't fallback to assets pipeline if a precompiled asset is missed
  config.assets.compile = true

  # Generate digests for assets URLs
  config.assets.digest = true
  #config.assets.digest = false

  #config.assets.initialize_on_precompile = true

  config.static_cache_control = "public, max-age=31536000"

  # user Amazon Cloudfront for asset hosting
  config.action_controller.asset_host = "https://xxxxxxxxxxxx.cloudfront.net"

Is there a magic combination of config settings that has somehow eluded me?


回答1:


Ok, finally figured out what was going on - my app was using the rails_api gem, which removes a whole bunch of middleware from the standard rails stack.

Changing my application controller to class ApplicationController < ActionController::Base added back in the required middleware, and asset_host started working immediately. I'll investigate further to determine which middleware it is later and decide whether I want to go back to rails_api.

One more issue I then discovered was that rack-mini-profiler rewrites cache headers to always revalidate, thus negating most of the benefit of the cdn. I've now disabled it in staging and prod and my app is running a lot more snappily!



来源:https://stackoverflow.com/questions/22571375/rails-asset-host-cloudfront-and-heroku

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!