Assets & IPv6 on Ruby on Rails

All of our internal production sites starting in 2009 have been launched with dual stack IPv4/IPv6 from day one.  We typically serve static assets (images and javascript) from our content delivery network to improve load performance.  Unfortunately, the CDN we use is IPv4 only. The following Ruby on Rails code snippet is from /config/environments/production.rb that rewrites static asset paths to serve from the RoR server for IPv6 and SSL requests and the CDN for IPv4 requests.

1
2
3
4
5
6
7
8
9
ActionController::Base.asset_host = Proc.new { |source, request|
remote_ip = IPAddr.new(request.remote_ip)
if request.ssl? || remote_ip.ipv6?
"#{request.protocol}#{request.host_with_port}"
else
# replace your.cdn/path with the host + path to your IPv4-only CDN
"#{request.protocol}your.cdn/path"
end
}
  Updated
Larry Salibra

About

I'm the Founder & CEO of Pay4Bugs, the crowdsourced testing service that finds product bugs before they cost you sales. More about me.