Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django 1.8 - how can staticfiles magically guess the hashed file name

I'm storing my static files on Amazon S3 and I'm using Django's cache-busting, my storage class look like this:

class MyStaticFilesStorage(ManifestFilesMixin, S3BotoStorage):
    ...

See: https://docs.djangoproject.com/en/1.8/ref/contrib/staticfiles/#manifeststaticfilesstorage

After running manage.py collectstatic all the static files are uploaded to S3 with the cache-busting hash in the file name.

Everything works great but I can't understand how.

  • my static folder is in .gitignore so static files never reach my server.
  • the staticfiles.json that django creates is stored on S3 only, so I went there and deleted it (so I can see if it has any effect).
  • I can ssh to the server and verify that there's no file called staticfiles.json and non of the static files are on the server.

But with all that Django still generates on the server the correct static URL with the correct hash (for example: main.c076b26cc1a7.css).

How? How does django know the hash without the mapping (staticfiles.json) and without the original file?

like image 246
Dan Bolofe Avatar asked Sep 09 '15 15:09

Dan Bolofe


1 Answers

I´m guessing that Django first gets the staticfiles.json file from the S3 and then reads the mapping from the file.

Even thou your static folder is in .gitignore, when you run the manage.py collectstatic, you are telling Django to look in the static folder, so it know it has to get them from there. Don´t forget you don´t upload the contents in the static folder using git pull,you do it using manage.py collectsatic.

like image 172
JesusAlvSoto Avatar answered Sep 22 '22 04:09

JesusAlvSoto