Override cached static files on browser as it is updated on the server

Hello all,

So I update my static files (HTML, CSS, JS) somewhat frequently. The biggest problem with this is that the visitor’s browsers will still have the older versions cached, and I don’t want to have them clear it out so often (or at all ideally for something like this).

How would I go about doing this?
P.S. Enabling “ManifestStaticFilesStorage” broke my admin page and caused a 500 Internal server error. I suspect I am missing something.

1 Like

I saw this post in the forum earlier, maybe cache busting can help you:

1 Like

This is what I’d suggest using so it is worth figuring out why it is causing other issues for you.

Beyond that, you can control how long clients cache resources by having the web server set appropriate headers, e.g. Cache-Control on those static files.

1 Like

This is what I saw as well. I suppose I could just post date and timestamps into the filename, but that could get messy kinda quickly. I was sorts hoping for something automatic, which was what I discovered “ManifestStaticFilesStorage” is for.

Well, in the traceback, this was part of the error that was logged when going to the admin page:

ValueError: Missing staticfiles manifest entry for ‘admin/css/nav_sidebar.css’
Forbidden (Referer checking failed - no Referer.): /

… and I ran the collectstatic command before that point already.

I’m also trying not to be obnoxious with cache-busting so much so it degrades performance… but I have no idea what I would set the duration to.

Forbidden implies a permissions issue. The user running Django needs to be able to access and read the manifest file.

Okay… well, I attempted the setting again and may have fixed the problem. Permissions were not the issue. I think I forgot some steps the first time I did it. This time, I mentally recorded the steps while testing again:

  1. Enable the storage setting
  2. Refresh admin page as change is applied to server (resulted in error)
  3. run collectstatic
  4. Refresh (2 types: Normally, and override cache) - sometimes works, sometimes doesn’t.
  5. Restart gunicorn daemon (Python HTTP Server)
  6. Refresh and test admin pages.

Things work as they should now. Pretty sure I forgot to do one or more of these things initially. :man_facepalming:

I’ll admit I’m only recently finding this setting, and getting back to Django after a a few months due to time constraints…
Thanks for the help.

2 Likes