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
Related
I've tried every troubleshooting *including StackOverflow and Heroku documentations.
Tried to precompile all assets locally bundle exec rails_env=production rake assets:precompile
Added config.serve_static_assets = true in application.rb, also tried config.serve_static_files = true
Tried gem 'rails_12factor', group: :production
And other things, still didn't work. The website loads, without any assets, javascript or styles. The browser console gives this warning:
Loading failed for the with source “...
/assets/application-c2decc28eb82aba39474d1a38f2cc1f016634981895dfbe9cb87ebb12709630d.js”.
I have know idea of what can be. Is Heroku looking for assets somewhere else? How can I check it and fix it? It was working yesterday, but don't know what is causing this issue. Thank you very much.
The problem was on my environments/production.rb file.
This config was pointing to another host. Now fixed.
config.action_controller.asset_host = "https://yourhost.herokuapp.com/"
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
It very clearly says in its documentation that it would do this if I don't precompile them locally.
And truthfully, I have no interest in precompiling these locally.
What I've had in production.rb, I've duplicated in application.rb
In my production.rb :
config.serve_static_assets = false
config.assets.compile = false
config.assets.precompile << 'application.js'
config.assets.precompile << 'application.css'
config.assets.precompile << 'screen.css'
Then I deploy, and that returns :
-----> Compiled slug size: 52.4MB
-----> Launching... done, v28
http://myapp.herokuapp.com deployed to Heroku
So it "compiled" something, right? Except no go, I go to the site and the .css and .js files are blank.
In order to precompile this locally, I am required to comment out in bootstraps_and_overrides.css the line :
#import "screen.css.scss";
Then it precompiles locally, and my local machine will not load the css correctly, but remotely it will actually work correctly.
So my method of deployment now is comment out that line of code,
bundle exec rake assets:precompile
git add .
git commit -m "Adding public/assets"
git push heroku development:master
Then ( unfortunately! ) :
bundle exec rake assets:clean
And then uncomment that line of code in my .css.
Some things to check
You're on Cedar, right? Heroku only supports this behavior on Cedar.
You're on Rails 3, right? Heroku doesn't support precompiling assets on Rails 4.
You have asset pipeline turned on, right? in config/application.rb you need the line config.assets.enabled = true
You have to not have a public/assets folder. On deployment Heroku decides whether or not to precompile based on whether this folder is present (even if it's empty).
If the pipeline is on and you don't have an assets folder, then pre-compilation must be failing.
Try changing to this. I hope this will help you.
In config/environments/production.rb
config.assets.compile = true
config.assets.digest = true
You might be on the wrong Heroku stack. Make sure you specify stack Cedar when you create apps that use the asset pipeline.
heroku create -stack cedar
The reason it would not deploy was because of Google fonts. Moving the file to your application.css such as :
*= require_self
*= require_tree .
*/
#import url(http://fonts.googleapis.com/css?family=Special+Elite);
Will allow the app to deploy, and for the fonts to work.
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.