1

Working with Ruby 2.0 and Rails 4.0, I'm seeing a different behavior on my asset pipeline and I'm not sure why. Here's the gist of it...

I have in my ./app/assets/javascripts folder an application.js.coffee and google_analytics.js.coffee file.

In my app/views/layouts/application.html.haml, I have the following two lines:

= javascript_include_tag "application"
= javascript_include_tag "google_analytics"

This produces the following in the rendered page once deployed to production:

<script src="/assets/application-f15feb4200e0d1db4963a00b28fbe9ca.js"></script>
<script src="/javascripts/google_analytics.js"></script>

Note that the application include is compiled into the assets pipeline as expected, while the google_analytics script is not.

In development mode, everything's served from the assets pipe without the fingerprints as expected:

<script src="/assets/jquery.js?body=1"></script>
<!-- several other includes suppressed for brevity here -->
<script src="/assets/application.js?body=1"></script>
<script src="/assets/google_analytics.js?body=1"></script>

I also checked the production deployment and noted that google_analytics doesn't get pre-compiled and fingerprinted.

EDIT: After getting the comments and answers below, added the following to config/environments/production.rb and attempted another deploy:

ComingSoon::Application.configure do
  config.serve_static_assets = false
  config.assets.js_compressor = :uglifier
  config.assets.compile = false
  config.assets.digest = true
  config.assets.version = '1.0'

  config.assets.precompile += ['google_analytics.js.coffee']
end

Why is the sprockets assets pipeline not pre-compiling the google_analytics script and serving it up like it does the application script? how do I fix this?

Michael Lang
  • 1,028
  • 12
  • 21
  • How did you check that the google script isn't being precompiled? If its in your assets folder and you have //= require_tree . in your application.js then it defintely should be getting compiled. – DiegoSalazar Jan 30 '14 at 16:27
  • its not a require in the application.js.coffee and, as a general rule, I don't do "require_tree" -- Its separate because I want to include it only on production with a conditional include. that is "= javascript_include('google_analytics") if Rails.env == 'production'" To check that it wasn't precompiled, I noted that it did not emit a message for this file during cap production deploy but other assets did and I also logged into production to check the public/assets folder and it is missing there as well. – Michael Lang Jan 30 '14 at 16:42

2 Answers2

3

You can add the google script to the precompile list in your config/environments/production.rb file on the config.assets.precompile setting. More here: What is the purpose of config.assets.precompile?

Community
  • 1
  • 1
DiegoSalazar
  • 13,361
  • 2
  • 38
  • 55
  • According to documentation from that SO answer..."The default matcher for compiling files includes application.js, application.css and all files that do not end in js or css: [ /\w+\.(?!js|css).+/, /application.(css|js)$/ ]" which should mean "google_analytics.js.coffee" should get picked up automatically. – Michael Lang Jan 30 '14 at 16:50
  • Not if its not in your application.js manifest. When it's not you need to add it to the precompile list. – DiegoSalazar Jan 30 '14 at 16:53
  • Followed instructions to add google_analytics.js.coffee to the list of assets to precompile, but behavior is unchanged. – Michael Lang Jan 30 '14 at 18:02
  • capistrano's precompiling assets... executing "cd -- /home/deploy/sites/coming_soon/production/app/releases/20140130174823 && RAILS_ENV=production RAILS_GROUPS=assets bundle exec rake assets:precompile" -- is this not sufficient? – Michael Lang Jan 30 '14 at 18:04
  • Solved! Do not include the "coffee" extension on the file name. The setting should be: "config.assets.precompile += ['google_analytics.js']" – Michael Lang Jan 30 '14 at 18:11
1

The google_analytics file needs to be added to the precompile list:

config.assets.precompile += ['google_analytics.js.coffee', ..]

Add above to your application.rb or production.rb as desired.

Note: Unless you have required the file in the application.js manifest file, through a direct require or through a require_tree, the js doesn't get compiled.

manojlds
  • 290,304
  • 63
  • 469
  • 417
  • Followed instructions to add google_analytics.js.coffee to the list of assets to precompile, but behavior is unchanged. Updated question to show my changes. – Michael Lang Jan 30 '14 at 18:02