I have a rails app hosted on Heroku. I added a "Audio" folder in Assets. The "Audio" folder is not included into my git pushes. My app generates some audio files that are automatically added to the assets/audio folder. That's why I don't want Heroku to clean the assets inside this particular folder each time I push a modification.
One solution would be to host the generated audio files with AWS S3 but it's quite a lot of work to set up.
I wonder if, instead, there is a way to tell Heroku not to clean the assets in the "Audio" folder?
I searched on the internet but didn't find anything so far...
So far I know Heroku will clean you temporary assets everytime you deploy your application and the build starts.
Check this answer: Store file in directory tmp on heroku Rails
If your application is Rails 5.2 based you can implement your upload to AWS via Active Storage, check: https://evilmartians.com/chronicles/rails-5-2-active-storage-and-beyond
I am working on a legacy app, and I just found out that config.assets.compile was set to true in production.rb
Now, while we'll test the hell out of it on staging, I want to find all the assets which should be part of the precompile array if I can.
What I did was to go into the tmp/cache directory and have a look around:
rails#foobar-production-app:/srv/www/app/shared/tmp/cache$ find . -type f | grep foobar
./57A/651/http%3A%2F%2Fwww.foobar.com%2Fassets%2Fmathquill.css%3F
./AEE/971/http%3A%2F%2Fwww.foobar.com%2Fassets%2Fmathquill%2Fmathquill.min.js%3F
./222/C12/http%3A%2F%2Fwww.foobar.com%2Fassets%2Flegacy-b56a7fd21baf98f1ac20cd77ca77a184.js%3F
./C96/561/http%3A%2F%2Fwww.foobar.com%2Fassets%2Fmathquill%2Fmathquill.min.js%3Fv2.6%3D
And I found the file I was expecting to legacy.js which I knew was missing from precompile. Is it likely that I just need to add the files matching my domain name to the assets precompile and then it should all be OK to push to production with config.assets.compile = false or are there other places / files I should look for to fix up.
I did find a heap of assets files - like this:
./assets/D1D/0F0/sprockets%2F663a54e3907dfc958d784233a6dd0e9a
and the contents of which do contain links to images etc, but I can see those fingerprinted assets in public/assets (yes, we are actually precompiling the assets in production). Or is the presence of any of these sprockets file pointing to something that is an issue and should be fixed?
I guess my question is, how can I most easily find any missing assets, other than just setting the flag, pushing to staging, and clicking around.
I'm trying to make a Rails app server its assets at S3. I'm not using any gems because want to do it manually to better understand the process. My questions regarding serving the static assets are:
Which assets should I upload to my S3 bucket: from my_app/assets or from my_app/public/assets?
If the second option, it means I have to precompile them first, right? And then upload the whole folder my_app/public (or my_app/public/assets?) ?
Since right now I'm deploying the website at heroku and the max size of the repo at heroku is 300Mb and the size of my assets is much bigger, how and should I at all make the app somehow "understand" that its assets are located at S3 so that when I say "git push heroku master" it won't upload all them from its my_app/assets or my_app/public/ folder?
You will need to change the config.action_controller.asset_host option on the Rails Configuration. Take a look at http://guides.rubyonrails.org/configuring.html#rails-general-configuration
You will indeed need to precompile the assets and sync them to S3. Remember to use RAILS_ENV=production. A good place to look at and then replicate manually would be in the asset_sync gem.
My rails app is working but its slug size is huge (220mb).
I am storing all assets on cloudfront with config.assets.digest = true and config.action_controller.asset_host = "something.cloudfront.net"
heroku run bash
In the slug, there is 30 times application-onedigest.js in public/assets, and same thing for stylesheets and images, so now the slug size is 220 mb.
Maybe one of my gem is doing middleware things (Rails asset_host, cloudfront and heroku) or maybe heroku is trying to optimize things (https://devcenter.heroku.com/changelog-items/328), but i don't think so. I don't want to exceed the maximum heroku slug size that is 300 mb, and i feel that is not normal.
How to remove old rails assets on heroku ?
EDIT
I made little commits in a css file, push to heroku, and compared slug. In the deploy process, there is those lines:
Running: rake assets:precompile
INFO -- : Writing /tmp/build_ff4eb6d7-303e-444d-9c88-938ab504ea8a/public/assets/application-700c7e1849c55312a94a353e60312500.css
Asset precompilation completed (9.60s)
Cleaning assets
Running: rake assets:clean
INFO -- : Removed application-48375ba5495e14b36afab9d4b9d97033.css
This is what i want. But when i compare the old and the new slug, there is the new application-700....css, and an another new mysterious application-a9b33ae58934ad161038cb3ebcee146c.css
And this one is actually used is the heroku webapp (when i explore css resources from the browser, after clearing cache).
So i guess precompile is done twice somewhere?
Temporary solution : heroku fork the app, i am back to 40 mb but there is still the problem.
EDIT
It seems it works when i precompile locally, but i don't like it as it violates 12factors.
Some things you'll want to consider:
rake assets:clobber
Heroku's file system
CDN
rake assets:clobber is an inbuilt way to remove old asset files from your public/assets directory. By running this command, either locally or on Heroku, you'll be able to actively remove the precompiled asset files on your system, allowing you to repopulate the folder with new files
Secondly, you have to remember Heroku runs an ephemeral file system. This means that each time you push your application to Heroku, it will just overwrite any of the files you had before, meaning (importantly), that it's not going to store your last assets. Each time you push, it will precompile the assets for you - which should ensure minimal space taken up with them.
--
Finally, if you find that your assets are taking up too much space on your application server, you may consider using a content delivery network (CDN) to serve the assets you need. You can use the asset_sync gem to push your assets to your CDN of choice
Most people tend to use a CDN with storage system to serve their assets. We use S3 with Cloudfront currently, but are looking to move to Rackspace's file serve system!
You can use the Heroku repo plugin (https://github.com/heroku/heroku-repo) and the repo:purge command to clean all the build assets out of your slug.
Using asset_sync gem is not ideal as you can run into deploy timeouts pushing all the assets over to S3 plus you end up with assets in two places. Remember S3 is not a content delivery network, you're better off using a CloudFront distribution and have it cache the assets directly from your application. See https://devcenter.heroku.com/articles/using-amazon-cloudfront-cdn for more details.
This solve it for me, in production.rb :
config.assets.compile = false
Heroku is no longer generating useless assets. I don't know how to remove the old assets in the same heroku app, but i used "heroku fork" to create a brand new heroku app with the same config / database and without old assets.
heroku run rake assets:clobber
I have a number of assets in my rails public/assets directory that I do not want compiled - specifically all the image files I want to use in my ember app. However, I cannot figure out how to get capistrano to deploy them. They are checked in to the public/assets directory but they do not appear in the deployed shared or release directories on my server. In fact I searched the whole capistrano directory tree on my server and the assets are nowhere to be found. I have confirmed that the assets are in git for the branch that capistrano is deploying. Any suggestions would be much appreciated.
I couldn't find a way to configure capistrano / capistrano-rails to allow me to use git source controlled files under public/assets, however, it did work to create an img directory under public and put the images there in git. That avoids the asset compilation process and the md5 hashes for the images and works well with ember.