Search code examples
ruby-on-railscdnproductionprecompile

Rails 4 assets - production env - precompile - CDN - Opsworks


I am attempting to get a Rails 4 application running with RAILS_ENV=production. My objective is to precompile assets and use a CDN to serve them quickly.

What I have done so far

production.rb 
  config.serve_static_assets = false
  config.action_controller.asset_host = "http://mycdn.cloudfront.net"
  config.assets.js_compressor = :uglifier
  config.assets.compile = false

Precompile step that runs correctly during opsworks deploy

"/usr/local/bin/bundle exec rake assets:precompile"

Output From Precompile

/srv/www/myapp/current/public/assets/$ ls
application-<digest>.css
application-<digest>.js
... other images, etc...

Using rails helper tags for .js and .css in /application.html.slim

= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true
= javascript_include_tag 'application', 'data-turbolinks-track' => true
= favicon_link_tag 'favicon.png'

What did I expect

On page load I would see things like (expected digest in url):

<link data-turbolinks-track="true" href="http://mycdn.cloudfront.net/stylesheets/application-<digest>.css" media="all" rel="stylesheet" />

What actually happens

On page load I see things like (NO DIGEST in url!):
The bad news is they are missing, since application.css doesnt exist anywhere in my app, the CDN cant cache it, naturally.

<link data-turbolinks-track="true" href="http://mycdn.cloudfront.net/stylesheets/application.css" media="all" rel="stylesheet" />

Any help would be much appreciated.

Random stats:
- Rails 4.1.5
- Unicorn 4.8.1
- Opswork stack as my deploy target
- Cloudfront as my CDN


Solution

  • You need to add this to your production.rb file ( and any other environnent that you want to have digests in )

       config.assets.digest = true