On every push to Heroku, all assets are precompiled, so not only assets that have changed. Asset precompilation thus takes more than 250 seconds. I tried all suggestions I found online, but I can't seem to fix it.
I have installed the rails_12factor gem as suggested by Heroku. Static assets are hosted on Cloudfront. We use Bower-Rails for front-end dependencies. Unicorn is the server on Heroku.
These are some settings in the production.rb environment:
config.serve_static_assets = true
config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect'
config.assets.compile = true
config.static_cache_control = 'public, max-age=31536000'
config.assets.compress = true
config.assets.digest = true
Has anyone experienced this as well? Or can anyone help me?
Have you tried running this before pushing to heroku?
bundle exec rake assets:precompile RAILS_ENV=production
Then do a git commit and git push heroku master
I wanted to post this as a comment but didn't have the rep ><
Related
My rails' app has uploading images function.
Precompiled assets will be created in public/assets When I deploy rails app in production and run
RAILS_ENV=production rake assets:precompile
I will get below error messages every time user upload images successfully in admin platform and request showing that created image.
ActionView::Template::Error (The asset "course/main_photo/XXXX/Course_ss12344.png" is not present in the asset pipeline.):
In those messages, I found that images created by user didn't precompile in the public/assets.
config.assets.compile = true # when I set true for config.assets.compile, server will response assets slowly that is not good choice.
config/environments/production.rb # below is my relative code in production's config
config.assets.compress = true
config.assets.compile = false
config.assets.digest = true
config.serve_static_assets = true
Can anyone give me some advice on how to auto precompile assets or other advice when user upload images online?
Try this
Remove previous pre-compiled assets by running
"rake assets:clobber"
than do "rake assets:precompile" and Restart your server
I have a project that works in the local development environment but breaks when it is uploaded to Heroku. When visiting my project on Heroku, I notice that I get 404 responses from the server saying that it could not find my css and js files. I have done some searching and found out that Heroku is not precompiling my assets. The project will work fine until Heroku puts my project to sleep. Upon waking the project in Heroku, the css and js are broken.
The project is using Rails 4.2.4, I have made sure to to include config.serve_static_assets = true in my config/application.rb and gem 'rails_12factor', group: :production in my Gemfile.
The css and js only breaks when Heroku puts the project to sleep due to inactivity. Does anyone know how to have Heroku automatically precompile assets when it is awaken from sleep?
I had similar issues before, my best bet was to precompile in local and then push to heroku. Configure your production.rb as follows:
config.serve_static_files = false
config.assets.compile = false
then in your console precompile as follows:
rake assets:precompile RAILS_ENV=production
This will precompile everything in public/assets commit your changes and push to heroku.
Also reset your assets cache for avoid any inconsistence:
rake assets:precompile RAILS_ENV=production
The above will force all your public/assets directory to rebuild when you run precompile command.
If your issue is with assets recompilation my answer should solve it, if you still have issues then you are doing something wrong or the issue does not have anything to do with assets precompilation.
We set the configuration values of above to false because now you are sending the precompiled files to the repo, so we do not serve static files nor fallback assets pipeline if something is missing, we are going everything in local.
Gemfile
gem 'rails_12factor', group: :production
application.rb
By default Rails 4 will not serve your assets. To enable this functionality you need to go into config/application.rb and add this line:
config.serve_static_assets = true
production.rb
config.serve_static_files = true
config.assets.compile = true
Command Line
bundle install
bundle exec rake assets:precompile RAILS_ENV=production
Make sure the images are in the /public folder.
Reference here
I am deploying a RoR 3.2 app to a shared server.
When I run rake assets:precompile RAILS_ENV=production I can see at ~/public_html/my_app/assets/ that all my images are precompiled.
But when I go to mywebsite.com ActionView throws an error. The production log gives me:
ActionView::Template::Error (200/adrap21.png isn't precompiled).
Funny thing is that at ~/public_html/my_app/assets/200 I can see a precompiled image: adrap21-a5f042dd2b89a3d87eba25969495d678.png
I have run several times one of the following:
1) rake assets:precompile RAILS_ENV=production
2) RAILS_ENV=production rake assets:precompile
at my_app/config/environments/production.rb I have the following:
# 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
# Don't fallback to assets pipeline if a precompiled asset is missed
config.assets.compile = true
I have changed config.assets.compile from false to true and does not help.
I have read related answers but other people solutions are not working for me.
This one was suposed to be foolproof. Another one, more specific about not compiling images, this other prevents using config.assets.compile = true.
I have even tried the advice of using config.assets.precompile += %w[*.png *.jpg *.jpeg *.gif] , but it is not yet working. Might there be something wrong with images call? Is it a RoR 3.2 bug?
FOUND WHAT THE PROBLEM WAS.
Phusion Passenger needs to be restarted for the compiled assets to work. That is why, using Capistrano, you need to restart the server for all the compiled to work together.
Thanks again, Paritosh. I know too how slow is learning curve at beginning when you learn to program by yourself.
I'm using heroku to deploy my app in ruby on rails, but have a problem, when I deploy my app, heroku doesn't update with the modifications I made, before I compiled my assets. I solved my problem in localhost using a simple rake assets:clean but in heroku this doesn't solve what I make to back update when I modify my assets
config.assets.initialize_on_precompile = false
config.assets.initialize_on_precompile = true
Changed this but it doesn't work
There's a few things to do. The first is to redeploy
$ rake assets:clean
$ rake assets:precompile
$ git add .
... deploy to heroku
If it still doesnt work, it could be a caching issue, in which case, you can manually bump the version in config/application.rb and redeploy to expire all the previous assets
Ex:
config.assets.version = '1.2' # change to '1.3'
First of all. Heroku will pre-compile the assets for you when you deploy the app without assets as it will not find the manifest.yml. in assets.
To Make this work.
Remove all the assets files from the public folder in from local and heroku.
In production.rb
Make sure you have these code.
config.serve_static_assets = false
config.assets.compress = true
config.assets.digest = true
config.assets.compile = false
Set the assets.compile = true if you want the rails app to find asset for you if any asset is found. in precompiled. Ideally this option should be false.
config.assets.compile = true
Push these clean changes to heroku.
I had this same issue late last night, i found that after pushing to heroku it was disconnecting from git. Once I went through the GIT process again then pushed it to heroku everything updated. Not sure why it disconnects, but this has been my work around all day. Spent 2-3 hours trying to fix the root issue, but the work around is just faster.
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.