Understating how assets in Rails 4 work in production - ruby-on-rails

When I run
rake assets:precompile
The compiled assets are written to pubic/assets:
I, [2013-07-21T02:16:00.987988 #13881] INFO -- : Writing /home/jpereira/dev/saleshub/public/assets/glyphicons-halflings-white-62b67d9edee3db90d18833087f848d6e.png
I, [2013-07-21T02:16:01.037698 #13881] INFO -- : Writing /home/jpereira/dev/saleshub/public/assets/glyphicons-halflings-c806376f05e4ccabe2c5315a8e95667c.png
I, [2013-07-21T02:16:01.053630 #13881] INFO -- : Writing /home/jpereira/dev/saleshub/public/assets/marketing/slider-base/slide-01-b85e542137a02bedd6c30dede873ef1e.jpg
I, [2013-07-21T02:16:01.066371 #13881] INFO -- : Writing /home/jpereira/dev/saleshub/public/assets/marketing/slider-base/slide-02-e5106e911d8a0289bfaf2ac64308a640.jpg
I, [2013-07-21T02:16:01.077879 #13881] INFO -- : Writing /home/jpereira/dev/saleshub/public/assets/marketing/slider-base/slide-03-990dccbed4c70f0118b7d30d98094811.jpg
I, [2013-07-21T02:16:01.965560 #13881] INFO -- : Writing /home/jpereira/dev/saleshub/public/assets/application-98713f9763bccfd6bc05dae422d3e242.js
I, [2013-07-21T02:16:02.068469 #13881] INFO -- : Writing /home/jpereira/dev/saleshub/public/assets/application-a40c2cd9b0f20b2a7f3b62d45159fbb3.css
Then, I start the application in production, with:
RAILS_ENV=production rails s
=> Booting WEBrick
=> Rails 4.0.0 application starting in production on http://0.0.0.0:3000
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
[2013-07-21 02:20:49] INFO WEBrick 1.3.1
[2013-07-21 02:20:49] INFO ruby 2.0.0 (2013-06-27) [x86_64-linux]
[2013-07-21 02:20:49] INFO WEBrick::HTTPServer#start: pid=13903 port=3000
But the rendered pages, don't look for the precompiled asses. In the rendered templates I got:
<!DOCTYPE html>
<html>
<head>
<title>App Home Pagetitle>
<link data-turbolinks-track="true" href="/stylesheets/application.css" media="all" rel="stylesheet" />
<link href="/stylesheets/application.css" media="all" rel="stylesheet" />
<link href="/stylesheets/marketing.css" media="all" rel="stylesheet" />
<script src="/javascripts/application.js"></script>
<script src="/javascripts/marketing.js"></script>
<meta content="authenticity_token" name="csrf-param" />
<meta content="8XQYBZWrTxmfdGvQYCK0JwQDfr2pt8si+FjW4a30SsA=" name="csrf-token" />
</head>
<body>
</body>
</html>
The template is the following:
!!! 5
%html
%head
%title App Home Page
=yield(:head)
= stylesheet_link_tag "application", media: "all", "data-turbolinks-track" => true
= stylesheet_link_tag "application", params[:controller], :media => "all"
= javascript_include_tag "application", params[:controller]
= csrf_meta_tags
%body
=flash_messages(flash)
= yield
My production.rb is as follows:
WebApp::Application.configure do
config.cache_classes = true
config.eager_load = true
config.consider_all_requests_local = false
config.action_controller.perform_caching = true
config.serve_static_assets = false
config.assets.js_compressor = :uglifier
config.assets.compile = false
config.assets.digest = true
config.assets.version = '1.0'
config.log_level = :info
config.i18n.fallbacks = true
config.active_support.deprecation = :notify
config.log_formatter = ::Logger::Formatter.new
end
What I need to configure to have stylesheet_link_tag and javascript_include_tag pick the right assets location on production?
Thanks

I just finished writing a similar question, when yours came up in the related sidebar :)
I found a blog that suggests this is a bug, and is inexplicably mitigated by setting
config.assets.compile = true
In the end I answered my own question.

If your creating assets in subfolders you need to prefix your files with the path to the file:
config.assets.precompile += %w( admin/admin.css admin/admin.js ). You'l find this command commented out in environments/production.rb
setting config.assets.compile = true is a bad idea, its slow and not necessary.
Once you've added your custom assets to config.assets.precompile, run:
bundle exec rake assets:precompile RAILS_ENV=production in the terminal to compile the assets.

I believe you may want to look at the following: that states: Edge Rails Guide
In Rails 4.0, precompiling assets no longer automatically copies non-JS/CSS assets from vendor/assets and lib/assets. Rails application and engine developers should put these assets in app/assets or configure config.assets.precompile.
Further to this I believe you can do specify your JS files by doing the following in your config/application.rb
config.assets.precompile += %w( 'application.js', 'application.css',
#specify CSS or JS files inside here! )
Then execute:
bundle exec rake assets:precompile RAILS_ENV=production

Related

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
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

foundation-rails, vendor/modernirz isn't precompiled

I have the same kind of issue as here :
Zurb Foundation 5, modernizr not found
I tried to fix this problem by testing multiple answers from here and from github's related threads, but nothing seems to work.
I'm using "rails", "3.2.15" and foundation-rails", "5.1.1.0"
I receive a 500 error, here is what I found in my production's log file:
Processing by HomeController#index as HTML
Rendered home/index.html.erb within layouts/application (0.9ms)
Completed 500 Internal Server Error in 6.1ms
ActionView::Template::Error (vendor/modernizr.js isn't precompiled):
8:
9: <%= stylesheet_link_tag "application" %>
10: <%= javascript_include_tag "vendor/modernizr" %>
11: <%= csrf_meta_tags %>
12: </head>
13:
14: <body id="bg">
app/views/layouts/application.html.erb:11:in `_app_views_layouts_application_html_erb___4555789472361352826_33758840'
This is very strange because early in my log file, a line tell me that modernizr is correctly compiled.
....
Compiled application.css (14023ms) (pid 4212)
Compiled vendor/modernizr.js (3ms) (pid 4212)
Compiled active_admin/print.css (307ms) (pid 4212)
...
Here is an extract of my production's environment file:
config.cache_classes = true
config.consider_all_requests_local = false
config.action_controller.perform_caching = true
config.serve_static_assets = true
config.assets.compress = true
config.assets.compile = false
config.assets.digest = true
config.assets.enabled = true
config.assets.version = '1.0'
config.assets.initialize_on_precompile = false
config.cache_store = :memory_store
config.assets.precompile += %w( vendor/modernizr.js )
Thanks in advance for your advice!
PS: everytime I use these commands
rake assets:clean
rake assets:precompile
Try this :
config.assets.compile = true
In application.rb
Then :
I advise you to include the CSS / Less / Js by yourself, this avoid to be dependent on a gem.
You can try something like this : http://www.erikminkel.com/2013/09/01/twitter-bootstrap-3-in-a-rails-4-application/
Pros : You're not dependent on a Gem, for example Foundation update to version from 4 to 5, you just have to update your CSS / LESS / JS files, and you don't have to wait for the gem to be updated.
Cons : The gem give you some helpers that save you a lot of time.
To get those helpers working with your project, you can just copy / paste the helpers from Bootstrap-Gems or Foundation-Gems in your project.

File digest/fingerprint is not always included in asset files

I precompile my assets and use following function to render html:
= stylesheet_link_tag "frontend/application", media: "all", "data-turbolinks-track" => true
= javascript_include_tag "frontend/application", "data-turbolinks-track" => true
Sometimes those methods generat correct html:
<link data-turbolinks-track="true" href="/assets/frontend/application-64cf06dd4d2386f145e00844fed60d28.css" media="all" rel="stylesheet" />
<script data-turbolinks-track="true" src="/assets/frontend/application-447281961c979f73e944369c4b832fd8.js"></script>
But on production rails sometimes (50%!) generates assets html like this
<link data-turbolinks-track="true" href="/stylesheets/frontend/application.css" media="all" rel="stylesheet" />
<script data-turbolinks-track="true" src="/javascripts/frontend/application.js"></script>
I use heroku on production. Any idea what could be wrong or how can I fix this?
Below are my production settings.
# Disable Rails's static asset server (Apache or nginx will already do this).
config.serve_static_assets = true
# Debug mode disables concatenation and preprocessing of assets.
# This option may cause significant delays in view rendering with a large
# number of complex assets.
config.assets.debug = true
# Compress JavaScripts and CSS.
config.assets.js_compressor = :uglifier
config.assets.css_compressor = :sass
# Do not fallback to assets pipeline if a precompiled asset is missed.
config.assets.compile = false
# Generate digests for assets URLs.
config.assets.digest = true
# Version of your assets, change this if you want to expire all your assets.
config.assets.version = '2'
# Specifies the header that your server uses for sending files.
# config.action_dispatch.x_sendfile_header = "X-Sendfile" # for apache
# config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for nginx
Here is also preview of the problem: view-source:http://v5.megabon.com/en
The only way to get it work on Heroku was to set compile to false
config.assets.compile = false
And delete every precompiled file in public folder. That forces Heroku to recompile assets on every git push.

stylesheet_link_tag and javascript_include_tag create tags without digest in production

My env is
rails (4.0.0)
ruby 2.0.0p247
and using nginx and unicorn.
I have the following code in app/views/layout/application.html.erb
<%= stylesheet_link_tag "application", media: "all" %>
<%= javascript_include_tag "application" %>
they create html tags without digest.
<link href="/assets/application.css" media="all" rel="stylesheet" />
<script src="/assets/application.js"></script>
I do assets:precompile like this.
$ RAILS_ENV=production RAILS_GROUPS=assets bundle exec rake assets:precompile
It create css and js with digest.
application-9746528cde7f7180a473328fd274c2ce.js
application-c0773e3ecaf5e09f2eef46885eebefe1.css
so my Rails application can't load css and js.
of course, I can access to http://example.com/assets/application-c0773e3ecaf5e09f2eef46885eebefe1.css
the following is my config/environments/production.rb
Myapp::Application.configure do
config.cache_classes = true
config.eager_load = true
config.consider_all_requests_local = false
config.action_controller.perform_caching = true
config.serve_static_assets = false
config.assets.js_compressor = :uglifier
config.assets.compile = false
config.assets.digest = true
config.assets.version = '1.0'
config.log_level = :info
config.i18n.fallbacks = true
config.active_support.deprecation = :notify
config.log_formatter = ::Logger::Formatter.new
end
Is there any mistake in my configurations?
I solved the problem.
The way to start unicorn is wrong.
I forgot to set RAILS_ENV when I start unicorn like that.
$ unicorn -c config/unicorn.rb -D
however the right way it the following
$ unicorn -c config/unicorn.rb -D -E production
thanks

Rails isn't appending cache-busting timestamp to assets in production

I'm using stylesheet_link_tag with the asset pipeline and yet I get this:
<link href="/assets/application.css" media="screen" rel="stylesheet" type="text/css" />
My production.rb
config.serve_static_assets = false
config.assets.compress = true
I'm assuming it's the settings or something simple..
Make sure this setting is true:
# config/environments/production.rb
config.assets.digest = true
and that you've run rake assets:precompile.

Resources