miracle2k/webassets — Issue #114: collectstatic, hashed file names and CachedStaticFilesStorage
Author: tgecho
Created: 2012-03-07 23:50:26+00:00
State: open

### The short version

I think I'm going to take a stab at making `AssetsFinder` properly serve up bundle files to `collectstatic` without requiring `assets rebuild`. What do you think?

### The long version

With apologies for a bit of a thought in progress.

I know you're deep in work on the Versioner stuff, but I wanted to write a bit about what I've figured out in the meantime. One thing webassets doesn't do is help with cache busting on static assets other then bundles such as images. It turns out [CachedStaticFilesStorage](https://docs.djangoproject.com/en/dev/ref/contrib/staticfiles/#django.contrib.staticfiles.storage.CachedStaticFilesStorage) already does this beautifully. Without repeating the docs too much, what it does on `collectstatic` is:

* Create a copy of each static file renamed to include a hash of the file contents
* Process css files so that any references to other static files use the hashed version

In the short term, I've found that I can actually use this almost out of the box with webassets by running `collecstatic` *again* after `assets rebuild`. It nicely creates hashed/processed versions of the bundle files and saves them into the static folder, ready for uploading/whatever. The following template code ties it all together:

{% assets "site_css" %}

{% endassets %}

There only real catch was that I had to set `ASSETS_ROOT` to something other then `STATIC_ROOT`. Since `collectstatic` makes copies of any files it finds and `AssetsFinder` will happily return anything it finds in `ASSETS_ROOT`, every run of `collectstatic` would create hashed versions of the already hashed files.

It's not a big deal, but I think this hints at a few issues I'm planning to poke at. I'm not sure of all the issues that might come from these, I'm just thinking out loud.

* Ideally `assets rebuild` would not be required. Instead `collectstatic` would just grab all of the bundled files from the `AssetsFinder` and do its hashing magic. What happens is that `collecstatic` uses `list` method to see which files a storage can offer. `AssetsFileStorage` only returns files that already exist in `ASSETS_ROOT`. Adding a custom `list` should fix this.
* Eliminating a standalone command might cause issues with the --parse-templates option. I don't use this, but I suppose you could have a command that wraps collectstatic or something. Not sure about this.

I'm not sure how this would affect (or is affected by) your versioning stuff. It turns out with this workflow I don't actually use the webassets expiring features. I know you support more then just Django, but right now webassets is still missing anything to do with non bundle assets such as images. It might not be too hard to add that functionality (renaming other assets + processing files that reference them), but until you do I'll use what's there.