Feature Request: Provide an option to disable static files caching during development

339 views
Skip to first unread message

Bharat Chauhan

unread,
Sep 27, 2021, 9:17:35 AM9/27/21
to Django developers (Contributions to Django itself)
Hello,

I recently opened a ticket (#33148) but it was closed as duplicate of (#32891) which itself is marked "wontfix".

The reasoning provided by Carlton Gibson for this is:

> I don't think this is the right approach. As far as I can see the browser behaviour is correct and as desired, serving static assets from the browser cache when possible.

At least while DEBUG is True, I see no harm in setting `Cache-Control: max-age=0` header.

This will allow browsers to cache the files while forcing them to revalidate cache using the If-Modified-Since header. This way, Django can serve updated files if they get modified or return a 304 response in which case browsers will use the cached file (conforming to Carlton's comment about using browser cache when possible).

This will aid in development as we now have to open the browser's network tab and disable the cache to refresh static files. In Mobile, disabling cache is not possible, so we have to test in Incognito Mode. It's a frustrating development experience.

Thank you.

laym...@gmail.com

unread,
Sep 28, 2021, 1:19:06 AM9/28/21
to Django developers (Contributions to Django itself)
I'm +1 on this.

In my experience teaching Django at university, this always confuses students: "why are my changes not reflected in the browser?"

Having static files caching disabled when DEBUG=True would be very useful. Thanks for bringing this up!

Sage

Bharat Chauhan

unread,
Oct 22, 2021, 9:53:44 AM10/22/21
to Django developers (Contributions to Django itself)
Really, no one is interested in making the developer experience a little less frustrating?

It's literally two lines of code and will eventually save hours trying to refresh the cache by opening dev tools,
which, btw, isn't even possible on mobile.

Besides, Django's dev server is not meant to be used in production. So why even care about caching?

Roxane Bellot

unread,
Oct 23, 2021, 4:07:39 AM10/23/21
to Django developers (Contributions to Django itself)
Hi

Wouldn't the setting "CACHE_MIDDLEWARE_SECONDS" do what you are looking for ?

Regards,

Bharat Chauhan

unread,
Oct 25, 2021, 3:38:49 AM10/25/21
to Django developers (Contributions to Django itself)
Hi,

That won't work because this is related to static files and how browsers implement caching for static files.


Basically, browsers won't revalidate the cache unless the server also sends a `Cache-Control` header. Which Django's built-in static view doesn't do. That means, if you make changes to CSS/JS files during development, browsers will serve stale version unless the cache is disabled.

Jason Johns

unread,
Oct 25, 2021, 9:02:33 AM10/25/21
to Django developers (Contributions to Django itself)

https://docs.djangoproject.com/en/3.2/ref/contrib/staticfiles/#django.contrib.staticfiles.storage.ManifestStaticFilesStorage

Are you using this?  basically, it converts static asset filenames from `styles.css` to `styles.2932y382.css` and other random-seeming hashes in the filename.  Useful for cache-busting.
Reply all
Reply to author
Forward
0 new messages