Heroku Assets Precompile - ruby-on-rails

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.

Related

Rails 6 deployed on Heroku won't update Stylesheets

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.

Do you always have to run rake assets:precompile locally?

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.

Force Heroku to Recompile All Assets

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

Ruby on Rails: Ran rake assets:precompile and now both local and heroku deployment don't include bootstrap

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)

Rails, precompiled assets; code changes not reflected in running app

I am developing an application with Rails 3.0 and Backbone and I tried
asset precompilation (rake assets:precompile).
Since then any change I made in the code is not reflected in the executed application,
in development environment.
Thank in advance
You'll have to pre-compile assets every time you make a change.
rake assets:precompile RAILS_ENV=development
I proposed a possible cause and solution to a similar question here which relates to
config/application.rb containing files to be precompiled.
I am writing with respect to Rails 3.2.22
if you are facing this problem then here is solution:-
Causes
Since you are run rake assets:precompile the script has created a folder public/assets and generated all the assets file the browser might ask for. So, when you make new changes in the js/css asset files, the requests from browser are served from public/assets directory.
Two Solutions
rm -df public/assets
rake assets:clean

Resources