This literally just happened, and won't go away -- anyone had any similar experiences or a fix?
Many thanks!
You need to use heroku run to run your rake commands but don't do it with assets:precompile. That happens already when you deploy. If you need to precompile your assets, do it locally, commit the compiled files and deploy to heroku.
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.
Is there a situation where it is not necessary to run this command? i.e. I don't have static images in the assets directory
Heroku includes asset precompilation as a step in the deploy process. As I see you're using Heroku, that means that you don't have to run rake assets:precompile before committing or pushing. Just make sure that the code that needs to be preprocessed is included in the commit that you're pushing (i.e. the files in /app/assets/(javascripts|stylesheets) etc are up to date).
If you're only deploying to Heroku and working locally in the "development" environment, then you might consider adding /public/assets to your .gitignore and deleting that folder.
See the Heroku docs on this.
I updated two images and now Heroku is serving one correctly but the other is still the old image. The output of the deploy logs show both images being precompiled with new hashes but the hash used to retrieve one of them (from the application.css file) is still the old hash and it's grabbing the old image somehow.
I'd like to force Heroku to recompile every asset and restart the server (essentially a fresh deploy). Currently it seems to "intelligently" only precompile the assets that it judges as being new. I tried doing rake assets:clobber and rake assets:precompile but it changed nothing -- still using the old hash to grab the old image version for one, but successfully getting the other. Any other options to try?
Expiring the assets manually worked -- changed config.assets.version = 1.0 to 1.1 in config/production.rb. Still not sure what happened, though.
You can now recompile assets without commiting anything.
heroku plugins:install heroku-repo
and then
$ heroku repo:reset --app=appname
$ git push heroku
Source: https://stackoverflow.com/a/9736959/3034747
This command used to accomplish the same thing, but it has been removed and no longer works:
$ heroku repo:rebuild -a appname
Gross, but make a small change and redeploy.
You have to actually redeploy because that's when asset compilation happens and your slug is compiled. Just restarting the server with heroku restart, changing config variables, or pretty much anything else won't build a new slug for you.
I just ran into this problem, and this was at least what solved it for me; YMMV.
Stuff like that can happen - why don't you try using heroku run rake assets:clean and heroku run rake assets:precompile to get the assets cleaned on the server
I was having issues deploying my project to a heroku server (Precompile fail). So I found this response, https://stackoverflow.com/a/13713753/2989437, and followed up on the advice. I added one line to my application.rb file:
application.rb
module FirstEdc
class Application < Rails::Application
config.assets.initialize_on_precompile = false # I added this line
...
end
end
I then ran the precompile command, committed the changes, and managed to deploy successfully to heroku. However, now my bootstrap/css appears to have stopped functioning both on the heroku deployment, and my local deployment.
I learned that I was supposed to add another line to my deployment.rb file:
deployment.rb
FirstEdc::Application.configure do
...
# Allows for local precompilling --added by Ian
config.assets.prefix = '/dev-assets'
end
So I added this, recompiled and redeployed, but to no avail.
Finally, I ran a rake assets:clean in an attempt to at least get my local deployment back to normal, but it did not work.
Any advice would be greatly appreciated. I'm reading more into the asset pipeline now, but I feel like this could be a cache problem or something. I'll update as I figure out what's going on.
edit. Just to clarify, I've tried removing both additions, running a rake assets:clean and rake assets:clean:all, but neither fix my local deployment.
I don't use config.assets.prefix in any of my apps and they work just fine with Heroku, so not sure what that's doing.
Try removing that line, then running rake assets:clean. Now your local server should be using the files as you change them. When you want to push, run rake assets:precompile first, then push.
If you want to make changes locally after that, run rake assets:clean again to get rid of the precompiled files on your local machine.
If Heroku detects any files in public/assets it will not attempt to precompile your assets again. This is by design.
So, you need to make a decision to either always precompile your assets with rake assets:precompile, or remove any files in public/assets before pushing to Heroku.
(The recommended way is to allow Heroku to precompile them during push)
Today I was working with an application I've had running on Heroku for a few months now and in an attempt to get something working I ran in my development environment:
rake assets:precompile
When I committed my changes and pushed to Heroku, I get 500 errors on my request:
ActionView::Template::Error (jquery.flexslider-min.js isn't precompiled):
I'm at a bit of a loss as to what to do, I've tried a few things:
Lazily compile in production (which I really don't want to do):
Bundler.require(:default, :assets, Rails.env)
Specifically list all of the files that need to be "precompiled" (also don't really want to do this, doesn't seem very efficient):
config.assets.precompile += ...
So far I've simply done a rollback to my last working version. I'm currently stuck unable to push new code. Will be setting up a staging environment (like I should've done long ago) but not sure what to do next or what might fix this issue? Why didn't this throw an error before?
UPDATE
rake assets:clean
Appears to have resolved the problem, although I don't understand why. Can someone share some insight into this?
If you running Rails 4.0 or above, rake assets:clean has been replaced with rake assets:clobber.
However, there are some current problems with clean and clobber with regards to permanently deleting assets. You can follow the issue here. https://github.com/heroku/heroku-buildpack-ruby/issues/123
I'm willing to bet a compiled version/filename inside of manifest.yml in the public/assets folder was out of date/wrong.
If you've made changes to the flexslider.js file, you'll need to recompile with rake assets:precompile and push the updated version to github. I believe you can set the version of the assets inside of the manifest.yml file.
Also, I believe you can run heroku run rake assets:clean or heroku run rake assets:precompile.
I don't think it would be a good idea to precompile assets inside of heroku because of versioning and name conflicts/not stored in github.
You can clean the assets in heroku and push the repo again, so you wouldn't need to precompile locally and push to github, unless there was indeed an issue in the local compilation.
I'd also take a few minutes to read http://guides.rubyonrails.org/asset_pipeline.html
Another possibility is your file name is having issues with sprockets. Why not use the development version of the flexslider.js, rename it to something slightly more convenient, and allow sprockets to do the minification.
Found the solution in the GitHub thread:
increment the config.assets.version variable in
${project-root}/config/application.rb
Assets were refreshed after I'd added config.assets.version = '1.1' at the end of my config file.