Package: django-ecstatic
django-ecstatic is an expansion pack fordjango.contrib.staticfiles!
Read the full documentation atreadthedocs.Here are some things it can do:Eliminate the need for network connections to calculate file hashes.Collect only the static files that have changed.Add content hashes to your filenames, without failing on non-existent files.Create static manifests to reduce network operations and ease deployment.Ecstatic’s utlities are written with the same interfaces asdjango.contrib.staticfiles, so they should be compatible with your favorite
Django storage libraries.Hashed Filenames FTWFirst of all, you should already be using Django’s CachedFilesMixin orCachedStaticFilesStorageclasses, which add a post-processing step to thecollectstaticcommand that saves a copy of your static files with a hash of
their contents in the filename. If you’re serving the files yourself, this will
allow you to setfar-future expirationsfor your assets, which will make
your site’s users happy. It also means that new versions of assets won’t
overwrite the old versions, which would break your site if the deployed code and
static files aren’t in sync.However, in order to get the content hash, these classes will open the file
using your app’sSTATICFILES_STORAGE. If you’re using a CDN, this means
they’ll be performing network operations. But those static files are saved on
the local filesystem, too—after all, they were collected from somewhere. That’s
whereecstatic.storage.CachedStaticFilesMixinandecstatic.storage.CachedStaticFilesStoragecome in. Instead of using the
storage class to get the hash, they’ll use your app’s staticfiles finders to
find the local version and use its hash. (They also have a couple of other handy
features.) Use the mixin with the storage class of your choice to get the
benefits:fromecstatic.storageimportCachedStaticFilesMixinfromcumulus.storageimportCloudFilesStaticStorageclassMyStaticFilesStorage(CachedStaticFilesMixin,CloudFilesStaticStorage):passHashed Filenames and Built FilesRemember when I mentioned howecstatic.storage.CachedStaticFilesMixinandecstatic.storage.CachedStaticFilesStorageworked? They calculate the hashes
of the local versions of the static files. Obviously, then, the local
versions—that is, the static files on your app server—need to be the same as the
ones you collected to your CDN. Otherwise, the app server would get different
hashes and use the wrong URL! So if your project requires a build step, you need
to make sure that the built files are on your app server. There are two ways
to do this:Include your built files in your package and deploy them with the rest of
your application code.Re-build the static files on the app server.Alternatively, you can go back to usingdjango.contrib.staticfiles.storage.CachedFilesMixinordjango.contrib.staticfiles.storage.CachedStaticFilesStorage, though then
you’re back in the situation of using network operations to get the hash.All of the above options have pros and cons. If you deploy directly from
version control, option 1 would mean committing compiled files to your
repository, which you may consider bloat. On the other hand, option 2 means that
your app server needs to have all of your build tools installed. It also means
that there will be some time while new code is deployed, but it’s referencing
old assets (until the build completes so the storage can get the new hash).Luckily, Ecstatic has another solution:ecstatic.storage.StaticManifestMixin. This mixin is used just likeecstatic.storage.CachedStaticFilesMixin, but it looks up your static files
URLs in a manifest file—completely sidestepping the need to calculate the hash
of the local files.fromecstatic.storageimportCachedStaticFilesMixin,StaticManifestMixinfromcumulus.storageimportCloudFilesStaticStorageclassMyStaticFilesStorage(StaticManifestMixin,CachedStaticFilesMixin,CloudFilesStaticStorage):passNoteNotice that we’re still includingCachedStaticFilesMixin. It (or
Django’s version) is still needed for the post-processing, and to figure out
which URL should be inserted into the manifest.With this mixin, the storage no longer needs access to the built files to
determine their hashes (and therefore URLs); it only needs to access the
manifest file. That means:You don’t need to package your built static files with your app.You don’t need to install your build tools on your app server.The storage class can lookup the new URL as soon as new code is deployed
(along with a new staticfiles manifest).We still aren’t performing network operations to get hashes/URLs.In other words, we’ve solved all of our issues. Yay!So how do you create this manifest? First, you need to add a variable to your
settings.py file to let Ecstatic know where to create it:ECSTATIC_MANIFEST_FILE=os.path.join(os.path.dirname(__file__),'staticmanifest.json')Then just run thecreatestaticmanifestmanagement command:./manage.pycreatestaticmanifestNoteWhen you runcreatestaticmanifest, make sure that the Django settings
you’re using contain the correctSTATICFILES_STORAGE. If you have a
local_settings.py that sets a differentSTATICFILES_STORAGE, the
manifest will contain the URLs that it reports!