Rails asset:precompile on dev machine: How to handle assets group? - ruby-on-rails

Since my production machine is somewhat limited (raspberry pi), I want to install as few gems as possible in my production machine. In order to reduce the dependencies, I want to do the precompiling of my assets on my dev machine and deploy them to production. So I don't have to do the precompilition on my production machine and don't need all the gems which are required by the asset pipeline.
So I've moved all gems in the Gemfile to the assets group and changed my config/application.rb:
Bundler.require *Rails.groups(:assets => %w(development test))
In development and test environment everything works fine.
But if I want to do the precompilation on my dev machine via ...
RAILS_ENV='production' bundle exec rake assets:precompile
... it breaks due the gems from the assets group are missing.
But I can't simply add the assets group to production environment, since that would require to install all the assets gems in my production environment (what I don't want to do).
Does someone have an idea how to solve that?

You can compile it rake assets:precompile in dev m/c and then commit the public/assets folder to remote repo. So when the production server is started it will take the already precompiled assets from the assets folder. Make sure any js/css/image change you do, update the assets file accordingly.
Any gem mentioned in Gemfile has to be installed. In case you don't feel the need to use specific gems, remove it from Gemfile. ex. rubyracer gem is present in Gemfile which is used for compiling js. To avoid any issues, fallback in case compiled asset is not present should be false as compiling on runtime will not be possible due to absence of js compilers

Related

Compiling Assets in Ruby does it need to be done in deployment? Why not before?

So this is just a question I've been wondering about lately, I seem to spend a significant amount of time when deploying waiting for assets to compile.
Why can we not compile the assets on our development machines and submit it in the git repo?
I am currently using ruby 2.5.x and rails 5.2.4.x using the assets pipeline with uglifier.
Is there a way to do this in development and thus disable all asset compilation during deployment?
many thanks,
Simon
The solution was pretty simple
1) disable asset compiling during deployment
I'm using elastic beanstalk so just had to set this in the environment variables
2) Either
Add the /public/assets folder to git
Or for elastic beanstalk create .ebignore file and copy over the .gitignore but remove the /public/assets entry
3) before deploying run
Linux -- RAILS_ENV=development bundle exec rake assets:precompile
Windows -- set RAILS_ENV=development && bundle exec rake assets:precompile
4) deploy code to server as normal
eb deploy
1st: Why can we not compile the assets on our development machines and submit it in the git repo?
The reason why we need to precompile are:
Compressed the assets resources then cached some static content like images, css and so on.
It's help us to generates two files (.css and .js) and compressed all our's css file event it's file from vendors:
<script src="/assets/application-908e25f4bf641868d8683022a5b62f54.js"></script>
<link href="/assets/application-4dd5b109ee3439da54f5bdfd78a80473.css" media="screen"
rel="stylesheet" />
It's very helpful but It's take time and `And the resource it not live reload when you modify some code. You have to re-compile to apply the code.
-> So, That's why you should not compile the assets in DEVELOPMENT evironment.
2nd: Is there a way to do this in development and thus disable all asset compilation during deployment?
You also use precompile on DEVELOPMENT by run this command:
RAILS_ENV=development bundle exec rake assets:precompile
You can precompile assets in development environment by default using
config/development.rb
config.assets.debug = false
Thanks. Hope it's help
In my case, I precompile before deployment, commit, and push it to Github, as you mentioned.
Then, I deploy to production with capistrano gem: https://github.com/capistrano/capistrano
This is the command to precompile:
rails assets:precompile

rails assets not being properly precompiled by heroku

My assets are not being properly precompiled to my staging heroku app. I usually use rake assets:precompile before pushing but my senior developer told me not to use that and put /public/assets into gitignore saying that heroku will automatically precompile the assets. When I push, heroku says that it runs rake assets:precompile but none of my assets show up. The page is just html.
possible causes: before my senior developer told me his way, I ran rake assets:precompile and removed /public/assets from gitignore as I though it wasn't suppose to be there. But after he told me, I put /public/assets back into .gitignore and deleted all the public assets and the assets still are not working on the heroku staging app. What am I missing?
I found the solution here: https://stackoverflow.com/a/16571492/1890135
I didn't specify this in the question but I was making a 2nd staging app not called staging so I had to add the rails_12factor gem under this new staging name environment.
group :staging_new, :staging, :production do
gem 'rails_12factor' # To enable features such as static asset serving and logging on Heroku
end
Why did you include public/assets in your gitignore file anyway? There's no need for it to be in there, unless you've precompiled your assets locally & don't want to use them in Heroku?
--
A good way to test whether your app will work in staging is to run rake assets:precompile RAILS_ENV=production. This will do as Heroku would -- compiling your assets
What I would do initially is:
Precompile your assets locally (using rake assets:precompile RAILS_ENV=production)
Check in your public/assets folder for the precompiled files
If they're there, push to Heroku & do again
If not, there will be another problem to fix
Generally, though, Heroku's precompilation process is just the same as you would do locally. So maybe you could even try running your Rails server in production mode to see what the issue might be; it's probably more your app than Heroku

Rails assets precompile on development issue

I precompiled assets on my dev environment (by mistake!) now any changes done on js/css files are not reflecting on browsing site locally.
I removed assets folder from public directory but then no css/js was available.
How do I get rid of this?
As a temporary solution I just cloned project into new directory and it works.
The question is: why do you need asset precompilation in the development environment? It's not meant to work like this.
The asset pipeline allows working in development with the uncompressed, unminified versions of your JS files. It also reloads them each time you refresh your browser, so you can develop your application with ease.
In production, though, the asset pipeline precompiles the JS files / assets you have into one single, minified file. This allows for better performance on the client as the files are smaller and are fetched in one single request.
So precompiling assets in development makes no sense at all.
In case if I understood you correctly, generally, when you precompile by default assets directory is being created inside public directory. To get your assets back, you can precompile again.
There is also a cache in tmp directory that you might consider removing.
Later you could use a $ (bundle exec) rake assets:precompile in combination with $ (bundle exec) rake assets:clean instead of $ rm -r public/assets so that new assets would be in effect rake way.
A one line command to look at new changes after your commits in environment would be
$ RAILS_ENV=(environment) rake assets:clean assets:precompile
but generally in development assets are not meant to be served as in production mode, so running previous with RAILS_ENV=production and starting a local server in production mode would be considered as a way to check (but not to make sure) if your assets would be served upon deployment in real production.
I went into the exact same problem and resolved it following #dashi's advice: 'remove directory assets in public, then start server in development mode.' everything is back to normal.

Rails Assets Precompile just not working

I've pushed a Rails app to Heroku and keep on running into the following problem:
I'll save changes to my main css.scss file (in assets/stylesheets) or to images in assets/images, push to git, push that to heroku, and reload the page, only to find out that these assets haven't been loaded at all.
This was also a slight problem on the local server, but entering:
rake assets:precompile
and reloading the local server usually worked, whereas doing
heroku run rake assets:precompile
and then re-pushing does nothing. I've fished around for info and haven't found anything particularly helpful.
Of note, in my config/application.rb (some of these the result of said fishing around):
# Enable the asset pipeline
config.assets.enabled = true
if defined?(Bundler)
# If you precompile assets before deploying to production, use this line
Bundler.require(*Rails.groups(:assets => %w(development test)))
# If you want your assets lazily compiled in production, use this line
# Bundler.require(:default, :assets, Rails.env)
end
in 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.compress = true
# Fallback to assets pipeline if a precompiled asset is missed
config.assets.compile = true
# Generate digests for assets URLs
config.assets.digest = true
Of additional possible interest, when I push to heroku, it says, among other things, this:
Preparing app for Rails asset pipeline
Detected manifest.yml, assuming assets were compiled locally
-----> Rails plugin injection
Injecting rails_log_stdout
Injecting rails3_serve_static_assets
and
Installing dependencies using Bundler version 1.3.0.pre.5
Running: bundle install --without development:test --path vendor/bundle --binstubs vendor/bundle/bin --deployment
I learned with Rails 3 and don't really know how the assets pipeline differs from what was available in previous version, so sorry if I'm being an idiot and putting overlapping and/or contradictory settings in my config files.
Would appreciate any help. This has been a headache.
It looks like it could be that you are add your locally compiled assets to git and pushing them and as a result Heroku is not compiling your assets on push. Check to make sure you are not adding the public/assets directory to git.

Assets precompile not being run on heroku cedar for rails 3.2 app

Started working on a new application this week that is running the latest rails 3.2.2 with the asset pipeline. For the assets I would like the assets to be compiled when pushing the app to heroku (rather than having to manually compile and store compiled assets in the repo). The docs suggest this should happen automatically.
The problem I have is when I run git push heroku master it does not seem to run the rake assets:precompile task at all. The assets are never compiled. Here is the output I get:
-----> Heroku receiving push
-----> Ruby/Rails app detected
-----> Installing dependencies using Bundler version 1.1.rc.7
Running: bundle install --without development:test --path vendor/bundle --binstubs bin/ --deployment
Using rake (0.9.2.2)
.......
.......
Your bundle is complete! It was installed into ./vendor/bundle
Cleaning up the bundler cache.
-----> Writing config/database.yml to read from DATABASE_URL
-----> Rails plugin injection
Injecting rails_log_stdout
Injecting rails3_serve_static_assets
-----> Discovering process types
Procfile declares types -> web
Default types for Ruby/Rails -> console, rake, worker
-----> Compiled slug size is 61.7MB
-----> Launching... done, v30
[appname] deployed to Heroku
I have the asset pipeline enabled in my application.rb
require File.expand_path('../boot', __FILE__)
require "action_controller/railtie"
require "action_mailer/railtie"
require "active_resource/railtie"
require "sprockets/railtie"
Bundler.require *Rails.groups(:assets) if defined?(Bundler)
module Appname
class Application < Rails::Application
# ...
# Enable the asset pipeline
config.assets.enabled = true
# Do not boot the app when precompiling assets
config.assets.initialize_on_precompile = false
end
end
Anyone know what might be causing the problem? I'm happy to gist more code if need be. If I run heroku info it shows that i'm running on the Cedar stack.
I had to turn on config.assets.compile = true to stop receiving 500 errors in production, although this compiles the assets at runtime (which I do not want).
This seems to be solved now. I believe it was caused by the Rakefile not loading the assets:precompile task in production.
The rakefile was loading up tasks for both the cucumber and rspec gems which are not bundled into production. As a result the assets:precompile task was not available in production so heroku didn't attempt to run it.
I wrapped the test tasks in a environment check conditional like so:
if %(development test).include?(Rails.env)
require 'rspec/core'
require 'rspec/core/rake_task'
end
If there's anything wrong in your Rakefile then the precompile step will be skipped entirely. For me, I was referring to a dependency that was only being loaded in my test and development environments and not production so the rakefile was bombing.
Using the suggestion from #Lecky-Lao worked for me.
heroku run rake -T --app staging-hawkmo
Running `rake -T` attached to terminal... up, run.1
rake aborted!
cannot load such file -- jasmine-headless-webkit
Once I saw that it was as simple as wrapping the Rakefile require in an environment test.
I normally add this to production.rb:
# Precompile additional assets (application.js, application.css, and all non-JS/CSS are already added)
config.assets.precompile += %w( style.css grid.css ... geo.js jquery.flexslider-min.js)
and set:
config.assets.compile = false
It appears that this results from something dying because of a fatal error when heroku attempts to start/check for assets. This may not actually be related to Rails 3.2. I don't know.
I didn't have time to wait for heroku to help so I went through the painful process below to determine what the problem was. I recommend you do it also.
Create a new, separate rails 3.2.2 app. There is nothing in it besides the default files. Create a heroku app: heroku create --stack cedar. Just for your sanity, push out this app as it stands and see that the precompilation step happens during slug compilation.
Copy into the new app, each logical block of your application. I started with app/assets. (thinking it was something to do with assets and precompilation... nah) cp -R ../ProblemApp/app/assets/* app/assets/ Then add, commit, and push this to the heroku git remote.
At some point, you are going to copy over a group of files that will stop the assets:precompile message from appearing during slug compilation. Now, one by one, revert those files to the original raw state or comment out suspicious lines and push back out to heroku. Once you see the precompilation message again, you've found your problem.
It turned out that for us, I had several false alarms. When I copied the config directory over to the new app, the app module name was our old app and not the new one. This also prevented the precompilation from happening, but it was not the original cause I was looking for.
In the end, we had a rake task that was responsible for loading CSV data into the app during development. It had one line that was causing a problem:
require "#{Rails.root.to_s}/config/environment"
Commenting out this line in our original app fixed our problem. So is this Rails 3.2 related? Why is heroku running unrelated rake tasks? I dunno. But shit's working. :)
Just having the same issue on both Rails 3.1.3 and Rails 3.2.3. Following Mario's idea, The rake task do exist while I run "heroku run rake -T". So anyone got feedback from Heroku's feedback yet? I might just raise ticket for this as well then...
I had the same problem I following this guide https://devcenter.heroku.com/articles/rails-asset-pipeline fixed it.
Regards.
Try to add the following into production.rb
config.assets.precompile = ['*.js', '*.css']
I also found this site https://coderwall.com/p/ga9l-a
Rename your css to .css.erb and use the asset_path tag in your rule.
E.g.
background: url(<%= asset_path 'bg.png'%>);
As mentioned in 2.3.1
http://guides.rubyonrails.org/asset_pipeline.html#coding-links-to-assets

Resources