#33148 closed Cleanup/optimization (duplicate)
Set `Cache-Control: max-age=0` header on static files response so that files are easily refreshable during development
Reported by: | bhch | Owned by: | nobody |
---|---|---|---|
Component: | contrib.staticfiles | Version: | 3.2 |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | yes | UI/UX: | no |
Description
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. 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 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.
Change History (4)
follow-up: 2 comment:1 by , 3 years ago
Component: | Uncategorized → contrib.staticfiles |
---|---|
Resolution: | → duplicate |
Status: | new → closed |
follow-up: 3 comment:2 by , 3 years ago
comment:3 by , 3 years ago
Replying to bhch:
Replying to Mariusz Felisiak:
Duplicate of #33148.
Is this a typo? 33148 is this very ticket.
Sorry, duplicate of #32891.
Duplicate of #33148.