Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the way to clear not used assets?

rake assets:precompile

creates new file like application-be2b8c92856ffacee861d9e9c2935e3e, but there's old one named application-c730047bc2a5cf3a706aa3a9f998ab77.css. It will be never used any more. Is there a way to clear out changed assets? I don't want to delete all assets dir, cause that seems like a overkill for all those files which are untouched (and it looks bad in git)

like image 920
Grzegorz Avatar asked Apr 30 '12 15:04

Grzegorz


2 Answers

Actually you should generally keep some old asset versions around because they could be referenced by something that is cached, or a page left open in the user's browser. Imagine if the user visited your page a second before you purge assets. The HTML is loaded and when the browser subsequently tries to retrieve the declared stylesheets and scripts, they're gone. Maybe this is a corner case for you, but if the HTML content comes from a cache (e.g. maybe you're memcaching content), it could still happen days later. The old content might be cached somewhere in the middle, but you can't rely on that even with the aggressive caching that's used with these assets.

What you really want, then, is to keep a few latest versions around and just delete the really old stuff. And that's exactly what brake assets:clean does, which you could run every time if you want, before precompiling. Here's the source showing it retains 2 backups.

If you could change that "2" to 0, it would answer this question literally. I'm not sure how to change that value, though it's a parameter in that function, so it should be configurable. But regardless, it's not a good idea anyway.

like image 54
mahemoff Avatar answered Sep 27 '22 19:09

mahemoff


Isn't the filename based on the md5 of the original file?

That means you could delete the whole directory, then run rake assets:precompile and since the contents of the files would be the same, you'd end up with the same filenames (with the same contents). So your git status would not notice those 'new' files since they are identical to the files already existing in its store. It would only notice that some files had been deleted.

Eg:

$ ls
application-<md5-old>.css
application-<md5-current>.css
$ rm *
$ rake assets:precompile
$ ls
application-<md5-current>.css
$ git status
deleted: application-<md5-old>.css
like image 33
Soup Avatar answered Sep 27 '22 19:09

Soup