12

I'm using Ruby 2.0.0-p247 and Rails 4.0.0

If I make a minimal Rails 4 site like this:

rails new minimal
cd minimal
rails generate controller home index
tee config/routes.rb <<EOF
Minimal::Application.routes.draw do
  root 'home#index'
end
EOF

Then precompile the assets with

rake assets:precompile

It generates assets like:

I, [2013-09-04T17:05:36.992951 #3549]  INFO -- : Writing /WORKINGDIR/minimal/public/assets/application-723d1be6cc741a3aabb1cec24276d681.js
I, [2013-09-04T17:05:37.052303 #3549]  INFO -- : Writing /WORKINGDIR/minimal/public/assets/application-f1a14051f17824976271b9c0460232f0.css

But if I start the server in production mode, with

RAILS_ENV=production rails s

The generated URLs in the HTML don't point at the precompiled files:

<link data-turbolinks-track="true" href="/stylesheets/application.css" media="all" rel="stylesheet" />
<script data-turbolinks-track="true" src="/javascripts/application.js"></script>

I would expect, rather:

<link data-turbolinks-track="true" href="assets/application-f1a14051f17824976271b9c0460232f0.css" media="all" rel="stylesheet" />
<script data-turbolinks-track="true" src="/assets/application-723d1be6cc741a3aabb1cec24276d681.js"></script>

The default config/environments/production.rb settings say to use digests:

config.assets.digest = true

But it seems to be selectively ignored?

Am I missing something?

UPDATE:

I just tested this in Rails 4.2.3 and this appears to be fixed, however we need to hand a few more environment variables into the rails s command to start in production mode:

SECRET_KEY_BASE=$(rake secret) RAILS_SERVE_STATIC_FILES=true RAILS_ENV=production rails s
pix
  • 5,052
  • 2
  • 23
  • 25

2 Answers2

12

While I was writing up this question I came across this blog post which suggests it is a bug.

http://railsblog.kieser.net/2013/08/rails4-phusion-passenger-asset-pipeline.html

Of the various suggestions, just setting the compilation fallback true...

config.assets.compile = true

seems to be sufficient to kick Rails in to generating appropriately digested URLs:

<link data-turbolinks-track="true" href="/assets/application-f1a14051f17824976271b9c0460232f0.css" media="all" rel="stylesheet" />
<script data-turbolinks-track="true" src="/assets/application-723d1be6cc741a3aabb1cec24276d681.js"></script>

The other suggestion, explicitly setting RAILS_ENV and RAILS_GROUPS seemed to produce different digest strings on the precompiled files, but they always seem to match up with the generated HTML regardless.

pix
  • 5,052
  • 2
  • 23
  • 25
  • I usually set RAILS_ENV=production. This seems to be a regression from 3.2.x :( – Gabe Kopley Sep 07 '13 at 04:32
  • 3
    `config.assets.compile = true` will have performance issues in production, I think – Johann Oct 09 '13 at 08:10
  • @Johann if yuou are still doing `rake assets:precompile` when you deploy (as you would if `config.assets.compile` were `false`) the live recompilation code should never actually be hit (since it will always see the precompiled versions and serve them up). – pix Oct 10 '13 at 00:40
  • 1
    @pix - unless for some reason some assets were not getting compiled with the precompile. Ultimately, if config.assets.compile = true is helping you in production, then it's at the cost of performance. My application had MASSIVE delay issues (+35 second view rendering!!) in production thanks to config.assets.compile = true. Turns out rails was only precompiling application.css and application.js and compiled the others on the fly (causing the delay). My solution was this: config.assets.precompile += %w( application*.css application*.js application*.scss ) – joshblour Oct 24 '13 at 11:01
0

I was able to resolve asset links not including the digest by bumping the asset version number.

     # Version of your assets, change this if you want to expire all your assets
-    config.assets.version = '1.0'
+    config.assets.version = '2.0'

You'll find this either in config/application.rb if you're upgrading or in the newer location config/initializers/asset.rb

mmrobins
  • 12,809
  • 7
  • 41
  • 42