I have an app which I want to deploy to Heroku. It works in development, but not when deploying to Heroku.
I am considering this to be an asset precompile issue.
But when I run bundle exec rake assets:precompile, it doesn't generate anything.
What could be the reason for this?
Seems like you overrided config.assets.precompile option and removed default values from it. Check your config/application.rb and config/initializers/assets.rb files.
Related
I built a Rails 6 app and deployed it to Heroku. But any changes I make to the stylesheet are not reflected. All the Heroku documentation and SO questions/answers appear to no longer be relevant to the current Rails setup in this regard. I could precompile the assets before pushing to heroku but I'd prefer not to. And actually I did find a "solution" but it feels more like a hack than a real solution. If I open config/initializers/assets.rb and change the statement:
Rails.application.config.assets.version = '1.0'
to
Rails.application.config.assets.version = '1.1'
then it will update the assets. But that means if I am experimenting with the look of the site I would be changing the version all the time. I mean if that's they way it's supposed to work I'll live with it, but it doesn't seem right. Anyone know a way to get Heroku to just update it on every push?
You've to compile the assets from the heroku end(i.e. in your server on Heroku), so after pushing your code to Heroku, run:
$ heroku run rake assets:precompile
# If above doesn't work
$ heroku run RAILS_ENV=production rake assets:precompile
Also, it's a good practice to add public/assets(or whatever folder that precompiles to) folder to your .gitignore file, so incase if you precompile assets in your local environment this will not mess up with your production env when you push to Heroku. But everytime you change some CSS and push to heroku, you'll need to precomile assets on Heroku end from the above command.
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 read the docs but can't seem to understand if you have to run rake assets:precompile locally each time you change scss file or any other assets? Isn't there an automatic way to do it? One of the things I have noticed is that I forget to run it sometimes and my heroku changes do not appear. There must be a way to set it up automatically in rails?
If I change
config.assets.compile = false
to true, will do that it? Is a disadvantage of doing that?
You don't have to precompile your assets for Heroku to serve them. Heroku will precompile your assets automatically if you have not already precompiled assets locally. Read this heroku doc regarding the asset pipeline in Rails 3 (even if you are already using Rails 4). Then read this doc regarding the asset pipeline in Rails 4 on heroku.
Pay particular attention to this part:
If a public/assets/manifest.yml is detected in your app, Heroku will
assume you are handling asset compilation yourself and will not
attempt to compile your assets. Rails 4 uses a file called
public/assets/manifest-.json instead. On both versions you
can generate this file by running $ rake assets:precompile locally and
checking the resultant files into Git.
rake assets:precompile must be run for production environment. Do not need to run the command for the development environment. The command is used to collect all files into one and so be lighter to serve in production. Under development the styles they are wanted in the assets folder. After running the command, the styles are placed in the public folder.
If you forget to run rake assets:precompile Heroku should do it automatically. One reason it may not be is if you have checked your public folder into git as then during slug compilation Heroku will assume you precompiled your assets and will not do it for you.
Setting config.assets.compile = true can slow down your application by a lot, which is why it is only used in development.
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
I'm getting a 404 on all of the images included with jquery-ui-rails on Rails 4.0.1 after going production. It works fine in development environment. The site is looking for /assets/jquery-ui/ui-icons_222222_256x240.png, but only public/assets/jquery-ui/ui-icons_222222_256x240-890385424135de1513f00cbecfb7f990.png exists in the filesystem. How come production build IDs are not being appended?
I've also had this problem with some fonts. For the time being I've worked around it by just manually copying and pasting to the sought path.
First thing to try is precompiling assets specifically for the production environment:
RAILS_ENV=production rake assets:precompile
If that doesn't do anything, set the following in production.rb and precompile again
config.assets.precompile += ['*.js', '*.css']
config.assets.compile = true