[Django] #33148: Set `Cache-Control: max-age=0` header on static files response so that files are easily refreshable during development

82 views
Skip to first unread message

Django

unread,
Sep 27, 2021, 1:25:55 AM9/27/21
to django-...@googlegroups.com
#33148: Set `Cache-Control: max-age=0` header on static files response so that
files are easily refreshable during development
------------------------------------------------+------------------------
Reporter: bhch | Owner: nobody
Type: Cleanup/optimization | Status: new
Component: Uncategorized | Version: 3.2
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 1
UI/UX: 0 |
------------------------------------------------+------------------------
**Problem**

Currently, the view (`django.views.static.serve`) which serves static
files in development only sets a `Last-Modified` header.

The problem this produces is that Firefox and Chrome don't send the cache
revalidation requests (`If-Modified-Since` header), and will keep on
serving stale files even after modifications.
[https://engineering.fb.com/2017/01/26/web/this-browser-tweak-saved-60-of-
requests-to-facebook/, Here's a good article on this from Facebook
Engineering].

So, during development, when we make changes to the static files (CSS/JS),
we need to open the `Network` tab and disable the cache to refresh the
static files.

On Mobile, disabling cache is not possible so we have to test in Incognito
mode.

**Solution**

Set `Cache-Control: max-age=0` header on the response.

As per [https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-
Control#requiring_revalidation this MDN page on Cache-Control] and some
tests I did on my computer, browsers can be forced to revalidate the cache
using `Cache-Control: max-age=0` header.

This will force the browsers to send the `If-Modified-Since` header and
Django can then send a 304 response if unmodified or serve the fresh file
if modified.

--
Ticket URL: <https://code.djangoproject.com/ticket/33148>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Sep 27, 2021, 1:43:48 AM9/27/21
to django-...@googlegroups.com
#33148: Set `Cache-Control: max-age=0` header on static files response so that
files are easily refreshable during development
-------------------------------------+-------------------------------------
Reporter: bhch | Owner: nobody
Type: | Status: closed
Cleanup/optimization |
Component: contrib.staticfiles | Version: 3.2
Severity: Normal | Resolution: duplicate

Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* status: new => closed
* resolution: => duplicate
* component: Uncategorized => contrib.staticfiles


Comment:

Duplicate of #33148.

--
Ticket URL: <https://code.djangoproject.com/ticket/33148#comment:1>

Django

unread,
Sep 27, 2021, 1:52:22 AM9/27/21
to django-...@googlegroups.com
#33148: Set `Cache-Control: max-age=0` header on static files response so that
files are easily refreshable during development
-------------------------------------+-------------------------------------
Reporter: bhch | Owner: nobody

Type: | Status: closed
Cleanup/optimization |
Component: contrib.staticfiles | Version: 3.2
Severity: Normal | Resolution: duplicate
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by bhch):

Replying to [comment:1 Mariusz Felisiak]:
> Duplicate of #33148.

Is this a typo? 33148 is this very ticket.

--
Ticket URL: <https://code.djangoproject.com/ticket/33148#comment:2>

Django

unread,
Sep 27, 2021, 2:01:20 AM9/27/21
to django-...@googlegroups.com
#33148: Set `Cache-Control: max-age=0` header on static files response so that
files are easily refreshable during development
-------------------------------------+-------------------------------------
Reporter: bhch | Owner: nobody

Type: | Status: closed
Cleanup/optimization |
Component: contrib.staticfiles | Version: 3.2
Severity: Normal | Resolution: duplicate
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Mariusz Felisiak):

Replying to [comment:2 bhch]:


> Replying to [comment:1 Mariusz Felisiak]:
> > Duplicate of #33148.
>
> Is this a typo? 33148 is this very ticket.

Sorry, duplicate of #32891.

--
Ticket URL: <https://code.djangoproject.com/ticket/33148#comment:3>

Django

unread,
Feb 4, 2023, 9:57:16 AM2/4/23
to django-...@googlegroups.com
#33148: Set `Cache-Control: max-age=0` header on static files response so that
files are easily refreshable during development
-------------------------------------+-------------------------------------
Reporter: bhch | Owner: nobody

Type: | Status: closed
Cleanup/optimization |
Component: contrib.staticfiles | Version: 3.2
Severity: Normal | Resolution: duplicate
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Kevin Christopher Henry):

This issue is being discussed in the [https://forum.djangoproject.com/t
/static-files-served-in-development-should-not-be-cached/18579 Django
Forum].

--
Ticket URL: <https://code.djangoproject.com/ticket/33148#comment:4>

Reply all
Reply to author
Forward
0 new messages