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?
javascript_include_tag inconsistent behavior
-
05-10-2022 - |
Question
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?
Solution
OTHER TIPS
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.