简体   繁体   中英

How to remove old rails assets on heroku?

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:

  1. rake assets:clobber
  2. Heroku's file system
  3. 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运行耙资产:Clobber

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM