Go slowly   About  Contact  Archives

Django staticfiles

Recently we move our sass & js complier from good ol' django-compressor and django-require (one for compile sass, one for bundle js), to webpack, a client-side bundler (it will do both sass & js for us). It’s a long process, I admit, but things go smoothly eventually. Till something pops up.

Before that, we use django-require’s OptimizedStaticFilesStorage to generate bundlers offline. This storage will generate a cache buster files side by side with original staticfiles in our STATIC_ROOT folder. Some folder will look like this after collectstatic:

# public/

cms/header.css
cms/header.0847d6eff302.css

After that, we must use aws cli to sync staticfiles manually to our S3 bucket, then set STATIC_URL to our Cloudfront endpoint. It works, till now.

I now remove both django-compressor and django-require, and use Django’s ManifestFilesMixin storage instead, with combination with S3BotoStorage.

We must change default manifest_name a little bit using git commit digest to support multiple deployments in the same bucket. The storage we use now looks like this:

class S3ManifestStaticFilesStorage(ManifestFilesMixin, S3BotoStorage):
    """
    This storage uses S3 as backend and having cache busting property of
    ManifestStaticFilesStorage
    """
    manifest_strict = False

    @property
    def manifest_name(self):
        filename = 'staticfiles-{version}.json'
        version = subprocess.check_output(['git', 'rev-parse', 'HEAD']).strip()
        return filename.format(version=version.decode('utf-8'))

Now whenever we call collectstatic, it will collect all our staticfiles to S3, no need another step to sync them manually.

Things will work beautifully if Django collect all files, instead it WILL NOT collect anything which are collected before, even when we’ve changed our storage backend.

I have done many twists back and forth but the static template tags got wrong urls all the time. Then after checking last-modified in one of response header, I find out that the file hasn’t change for very long time. Hence Django mustn’t touch these files when I change staticfiles backend storage.

Voyla!

Just copy all staticfiles form old folder into the new one, and problem solved, new files would work as expected.

Written on January 30, 2018.