Changed static files stay cached in browser and won't be reloaded automatically

Bug #1162400 reported by Adrian Wechner
8
This bug affects 1 person
Affects Status Importance Assigned to Milestone
Errors
Triaged
Medium
Unassigned

Bug Description

FIX FOR CHANGED STATIC FILES

Symptom:
After deploying new static files like css,js,img the files sill stay cached inside the browser. The browser has no way to know that the files have changed.

Fix:
the filename of the static files must include the timestamp of last modification. The presented fix below would take care of it.

The changes were done under the instructions of:
http://www.peterbe.com/plog/serve-your-static-stuff-in-django-with-nginx

FIXING INSTRUCTIONS

1) Additional Django app which must be installed:
django-static (install command => easy_install django-static)

2) In case of using Nginx, the following lines must be added (if using Apache, tell me I'll find out what to do):

location ^~ /cache-forever/ {
    root /tmp;
    expires max;
    access_log off;
}

3) Apply Patch
file: errors-fix-changed-static-file.diff

Kind Regards,
Adrian Wechner

Revision history for this message
Adrian Wechner (adrian-wechner) wrote :
Revision history for this message
Brian Murray (brian-murray) wrote :

Thanks for this patch! For your information we are using apache as our web server for the errors site.

Changed in errors:
importance: Undecided → Medium
status: New → Confirmed
Revision history for this message
Evan (ev) wrote :

Apache (mod_wsgi with HAProxy) in production, gunicorn (with Apache and HAProxy in front) in soon-to-be-production :)

Revision history for this message
Evan (ev) wrote :

We should also minify the static content. I mention it here because we could use middleware that timestamps and minifies.

Revision history for this message
Adrian Wechner (adrian-wechner) wrote :

Question: why are you changing to gunicorn? what's the benefit?
Slim: that actually would have been even easier then to define file by file... with the django-static app, there is a call "slimall" and "endslimall".

Example:

{% slimall %}
<script src="/javascript/foo.js"></script>
<script src="/javascript/bar.js"></script>
{% endslimall %}

that will combine the files content and compress them. so you get less requests and smaller size. then question then is if you have a "compressor" installed. something like 'slimmer' => easy_install slimmer.

If that is all the idea you dont see any other necessary possible changes then I will create a new diff file for these changes.

for the apache conf file you need something like that:

<VirtualHost *:80>

   ...
   ...
   ...

    Alias /cache-forever /tmp/cache-forever

    <Directory /tmp/cache-forever>
        Order allow,deny
        Allow from all
    </Directory>

</VirtualHost>

HAProxy:

I am not sure, but I think that the HAProxy should change anything in the apache conf for this matter... except you want to handle the static file access directly with the HAProxy to safe redundancies. I guess as mentioned before that should work even witht he HAProxy.

Revision history for this message
Evan (ev) wrote :

Hi Adrian,

We're moving from mod_wsgi to gunicorn to coalesce around a single wsgi container. Other services deployed into the Canonical DC cloud (prodstack) are also using gunicorn. This reduces the number of things that the webops team have to be experts in and lets us share best practices.

It wasn't my decision to pick gunicorn, so I can't speak to any performance or debugging benefits over mod_wsgi.

Hope that helps to clarify.

Slimmer is available in universe in precise, so we could indeed use that for inline css/js (though I'm inclined to push inline content out into static files). For static js files, yuicompressor is used by at least U1. It's also pluggable into django-static, so we could use it for inline content as well at the cost of it being slower than slimmer.

U1 doesn't use django-static, but want to move in that direction:
"We do it the heavy-handed way: have a rewrite rule in apache to do /static/<revno>/ -> /static internally, and include <revno> in the url when generating the html. We should eventually move to something like django-static, but not in the short-term plans."

django-static isn't packaged, so we'd have to sort that out (for precise) if we are to use it.

Changed in errors:
status: Confirmed → Triaged
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.