Skip to content

Instantly share code, notes, and snippets.

@francirp
Last active August 29, 2015 14:16
Show Gist options
  • Save francirp/9fe05dcbcf4ffabbf0e1 to your computer and use it in GitHub Desktop.
Save francirp/9fe05dcbcf4ffabbf0e1 to your computer and use it in GitHub Desktop.
Rails: Heroku Cloudfront and Gzip

Cloudfront

First, create a new Cloudfront distribution via Amazon AWS console (gist here). Once you've done that, it's as simple as adding the following to your production environment (and staging if you run a staging environment), replacing the value with your CDN host url:

config.action_controller.asset_host = "<YOUR DISTRIBUTION SUBDOMAIN>.cloudfront.net"

See here for more info: https://devcenter.heroku.com/articles/using-amazon-cloudfront-cdn#adding-cloudfront-to-rails

Gzip

This boosts performance. Helpful post here: http://thediscoblog.com/blog/2013/05/01/the-rails-cloudfront-and-heroku-performance-hat-trick/

To add gzip via Heroku Deflater gem:

Gemfile.rb:

gem 'heroku-deflater', group: [:production, :staging]

config/environments/production.rb and config/environments/staging.rb:

config.static_cache_control = 'public, max-age=31536000'
config.middleware.delete "ActionDispatch::Static"
config.middleware.insert_before(Rack::Runtime, ActionDispatch::Static, paths["public"].first, config.static_cache_control)

You can see the result on our site: www.launchpadlab.com

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment