#29277: Add a bust_browser_cache flag to the static template tag
-------------------------------------+-------------------------------------
Reporter: David Kerkeslager | Owner: nobody
Type: New feature | Status: new
Component: Template system | Version: 2.0
Severity: Normal | Resolution:
Keywords: browser-cache- | Triage Stage:
busting static-files | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Old description:
> The proposal is to add a `bust_browser_cache` flag to the `static`
> template tag using a similar syntax to passing context variables to the
> `include` template tag. It would look something like this:
>
> {{{
> {% static 'css/style.css' with bust_browser_cache=True %}
> }}}
>
> The flag would default to `False` to avoid breaking existing usages of
> the `static` tag.
>
> What flag does is add a `cache_hash` get parameter to the URL of the
> static file, with a hash of the static file. So if the content of
> `css/style.css` looks like:
>
> {{{
> p {
> color: magenta;
> }
> }}}
>
> The generated URL will look like
> `/static/css/style.css?cache_hash=0756793258f5ea00cda3e445de6bb595`.
>
> If the content of `css/style.css` changes to:
>
> {{{
> p {
> color: magenta;
> }
> p::after {
> content: "Sorry about this awful color.";
> }
> }}}
>
> Then the link would be
> `/static/css/style.css?cache_hash=c3aebd6286841ce8f4d76aa8d3eba487`.
>
> This is not a security feature, so I think it would be best to use MD5
> because it is fast.
>
> Instead, the purpose of this is to allow us to serve static files with
> `Cache-Control: immutable` or `Cache-Control: max-age=<some large
> number>` without fear. As long as the content of the file does not
> change, the URL's `cache_hash` will not change, so the browser will use
> the cached version. But if the content of the static file changes, the
> `cache_hash` parameter will change, causing the browser to request the
> new version of the file. This gets us the best of both worlds: we
> maximize usage of browser cache for static files, while never having to
> worry that our users are seeing stale versions of static files.
New description:
The proposal is to add a `bust_browser_cache` flag to the `static`
template tag using a similar syntax to passing context variables to the
`include` template tag. It would look something like this:
{{{
{% static 'css/style.css' with bust_browser_cache=True %}
}}}
The flag would default to `False` to avoid breaking existing usages of the
`static` tag.
What flag does is add a `cache_hash` get parameter to the URL of the
static file, with a hash of the static file. So if the content of
`css/style.css` looks like:
{{{
p {
color: magenta;
}
}}}
The generated URL will look like
`/static/css/style.css?cache_hash=0756793258f5ea00cda3e445de6bb595`.
If the content of `css/style.css` changes to:
{{{
p {
color: magenta;
}
p::after {
content: "Sorry about this awful color.";
}
}}}
Then the link would be
`/static/css/style.css?cache_hash=c3aebd6286841ce8f4d76aa8d3eba487`.
This is not a security feature, so I think it would be best to use MD5
because it is fast.
Instead, the purpose of this is to allow us to serve static files with
`Cache-Control: immutable` or `Cache-Control: max-age=<some large number>`
without fear. As long as the content of the file does not change, the
URL's `cache_hash` will not change, so the browser will use the cached
version. But if the content of the static file changes, the `cache_hash`
parameter will change, causing the browser to request the new version of
the file. This gets us the best of both worlds: we maximize usage of
browser cache for static files, while never having to worry that our users
are seeing stale versions of static files.
It may make sense to store the MD5 hash in Django's cache so that it
doesn't get calculated every time.
--
Comment (by David Kerkeslager):
I can implement this ticket, but I haven't contributed to Django before,
and looking at the relevant source code it's going to take me a little
while to figure out how to do it. If someone else wants to do it who is
more familiar with the codebase, it would probably get done quicker.
--
Ticket URL: <https://code.djangoproject.com/ticket/29277#comment:1>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/069.d5aef712e2bc3d8a8080711631afa179%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.