Rails 4 - javascript runtime required in production - ruby-on-rails

I just updated a Rails 3.2.x app to 4.0.2.
When I deployed to production (ubuntu, MRI 2.0) I got the good old error about the lack of a javascript runtime.
I quickly fixed it by installing node, but it makes me wonder.
I prefer to precompile the assets locally, check them into git, and then push them to the production server along with the rest of the application.
With Rails 3.2 this system has always allowed me to not care about a js runtime in production, as the application doesn't need to compile coffeescript or run uglyfier.
So, the question is: what has changed with Rails 4? Is there a config option to control this behaviour?
I checked my (rails 4) config file, and I think that the production evironment is already configured to NOT fallback to live compilation.
assets-related config options:
config/application.rb
config.assets.precompile += ['html5shim.js']
config.assets.initialize_on_precompile = false
config/environments/production.rb
# Disable Rails's static asset server (Apache or nginx will already do this)
config.serve_static_assets = false
# Compress JavaScripts and CSS
config.assets.js_compressor = :uglifier
# config.assets.css_compressor = :sass
# Don't 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 = '1.0'

In case someone hit this question as I did. I had the same problem and this link pointed me to the right direction: https://mattbrictson.com/upgrading-to-rails-4-with-capistrano. Specifically the following statement: In Rails 4, the standard Gemfile no longer has an :assets group, which means asset pipeline gems are always loaded in production, on all servers. I precompile my assets locally and upload them to production, so there is no reason to have JS runtime on production server.
In my case I added group :asset to my Gemfile putting there asset-related gems. In my case it was:
group :asset do
gem 'uglifier'
gem 'execjs'
end
My capistrano tasks on production install bundle without :asset group, so after this change JS runtime is no longer required on production.

Related

Rails 5 unable to load assets in production mode

I am able to precompile assets in public/assets directory of rails application.
But when I start application in production, rails constructing wrong path and not pointing to the precompiled assets.
System environment details are as below.
rails -v
Rails 5.0.4
ruby -v
ruby 2.3.3p222 (2016-11-21 revision 56859) [i386-mingw32]
"sprockets-rails", '2.3.3'
and production.rb
# Disable serving static files from the `/public` folder by default since
# Apache or NGINX already handles this.
config.public_file_server.enabled = true
# Do not fallback to assets pipeline if a precompiled asset is missed.
config.assets.compile = false
and asset.rb
# Precompile additional assets.
# application.js, application.css, and all non-JS/CSS in app/assets folder are already added.
Rails.application.config.assets.precompile += %w( *.js *.css *.js.erb *.css.erb)
And my index.html.erb looks this way
<%=javascript_include_tag "bootstrap.min"%>
<%=stylesheet_link_tag "bootstrap.min"%>
and the rails trying to fetch assets using below paths,
and the compiled assets in folders looks as bellow.
Kindly help me to find out where I am going wrong.
Thanks in advance and any help appreciated.
Make sure the digest setting is enabled:
config.assets.digest = true

Disable Sprockets asset caching in development on Rails 4

Another question "Disable Sprockets asset caching in development" addresses how to disable Sprockets caching in Rails 3.2. How do you do the same thing on Rails 4? I am working on a gem that is deep in the asset pipeline and having to clear tmp/cache/* and restart Rails is getting tiring.
If you look at the Sprockets source, you can see that if cache_classes is true then app.assets gets set to app.assets.index, and the filesystem is no longer checked.
In order to get around this in development, you can add something similar to the following to your development.rb configuration:
# Sprockets configuration: prevent sprockets from caching assets in development
# when cache_classes is set to true
sprockets_env = nil
config.assets.configure do |env|
sprockets_env = env
# Sprockets environment configuration goes here
# env.js_compressor = :uglifier # or :closure, :yui
# env.css_compressor = :sass # or :yui
end
if config.cache_classes
config.after_initialize do
Rails.application.assets = sprockets_env
end
end
This essentially grabs a reverence to the Sprockets::Environment object before it is overwritten by the Sprockets::Index one, and allows the filesystem to be checked for new assets even when cache_classes is true. This seems to work for us in development, so hopefully it helps someone else out as well.

Bootstrap-sass asset compilation on Heroku deployment

I am currently working on a Rails application that uses the bootstrap-sass gem to help style some of my front end views. When running locally, I have no problem browsing the views. After Heroku deployment, I receive the following error:
ActionController::RoutingError (No route matches [GET] "/assets/bootstrap-responsive.css")
Here is a snippet of my application.rb file:
# Enable the asset pipeline
config.assets.enabled = true
# Version of your assets, change this if you want to expire all your assets
config.assets.version = '1.0'
config.assets.compile = true
config.assets.initialize_on_precompile = false
Any thoughts as to how to get bootstrap-sass working on a Heroku deployment with asset pre-compilation?
Did you install the 12factor gem? You'll need that with Rails
gem 'rails_12factor', group: :production
https://devcenter.heroku.com/articles/getting-started-with-rails4#heroku-gems
I had my bootstrap import statements in my application.css file. By moving to a bootstrap_and_overrides.css.scss file, my problem was solved.
This post helped: Getting bootstrap-sass bootstrap CSS into production on Heroku

Most of my assets suddenly return 404 after a push to heroku

I have deployed this app (rails 3.2.11) a million times, I haven't messed with any settings, but now I'm greeted with this:
Why did this happen out of the blue? My conents of application.rb include config.assets.enabled = true - never had any issues.
In fact running it locally on port 3000 seems to not have any issues whatsoever.
After deploying to heroku this morning, it seems that it loads nothing that's inside /assets/
Interestingly, after copying the files over to try and just make a new app, git commit results in all the stuff you'd expect as well as a LONG list of these which I think might be related:
Edit: Interestingly enough SOME of the assets have loaded, like the logo and the background, but the rest as you can see return 404.
put line in config/environments/production.rb
config.assets.compile = true
it worked as it will compile the assets on runtime, just like in development environment, but its makes application slow, the best way is to either compile the assets locally in production environment with rake task(RAILS_ENV=production bundle exec rake assets:precompile) and commit your generated assets in public/assets and then do deployment. or, heroku run rake assets:precompile
I had this problem today with rails 4 on heroku. The article provided by #Jeff is a little bit old but, the gem repository has a good readme.
Summarizing, you will need to add two gems to your Gemfile:
gem 'rails_serve_static_assets' (it will solve the static assets problem) and
gem 'rails_stdout_logging' (which the previous one depends on).
Heroku released a gem to handle the assets without needing to turn off compilation or to manually compile.
https://devcenter.heroku.com/articles/ruby-support#static-assets
Just add this to your Gemfile and redeploy.
gem 'rails_serve_static_assets', group: [:production]
For Rails 4, use:
config.serve_static_assets = true
The default was false. We needed this after removing the rails_12factor gem.
Rails recommended that this setting config.serve_static_assets by default should be disabled i.e. set to false. Here is the default configuration in config/environments/production.rb generated in rails app
Disable Rails's static asset server (Apache or nginx will already do this)
config.serve_static_assets = false
So if you are setting it to true in your local app then that's still fine. But if you are deploying your app on Apache or ngix or anything other than heroku then its not advisable to make config.serve_static_assets=true in your production.rb config file. Here is the documentation from Rails guides.
config.serve_static_files configures Rails itself to serve static
files. Defaults to true, but in the production environment is turned
off as the server software (e.g. NGINX or Apache) used to run the
application should serve static assets instead. Unlike the default
setting set this to true when running (absolutely not recommended!) or
testing your app in production mode using WEBrick. Otherwise you won't
be able use page caching and requests for files that exist regularly
under the public directory will anyway hit your Rails app.
URL - http://guides.rubyonrails.org/configuring.html
To make the assets to load with the corresponding fingerprint of each file verify the configuration config/environments/production.rb has the instruction:
ruby
# Load assets with fingerprint behavior
config.assets.digest = true

Rails compress assets even in development mode

Rails just compress javascript in development mode but i dont need to.
Here is the config/environments/development.rb
# Do not compress assets
config.assets.compress = false
# Expands the lines which load the assets
config.assets.debug = true
Rails version is 3.2.8 but also tried 3.2.9-rc3, 3.1.8 doesnt work with my application because it was created in 3.2.8
I just found that rails serves precompiled assets instead of assets from app/ catalog when they are exist. Its need to remove them to make Rails work like i need.
rake assets:clean
I belive Rails must not behave that way in development mode.

Resources