[Flask] client side cached js/css files

Corey Boyle coreybrett at gmail.com
Tue Apr 26 16:41:16 EDT 2016


I took a look at the flask-webpack extension, but it seemed to do a lot of
things I wasn't really interested in at the moment.
I just didn't want to go down that road quite yet.
I ended up rolling my own solution that I would like to share in hopes it
might be helpful to someone.
Or... if I've done something dumb, maybe someone will point that out for me.

*~ Part One ~*

I added the following lines to my application factory.

from checksumdir import dirhash # imports the checksumdir module that I
installed via pip
static_hash = dirhash('static', 'md5') # creates a string of an md5 hash of
the contents of my static folder
print 'static hash is', static_hash

@app.template_global('static_hash') # creates a template global that I can
call inside url_for
def static_hash_func():
  return static_hash

Then I adjusted all my resources links inside my templates to add this hash
value as a get parameter.

{{url_for('static', filename='images/logo.png', ver=static_hash())}}

*~ Part Two ~ Only needed for dev env*

I added the following lines to my management script.

#this block creates a list of directories inside my static folder
extra_dirs = ['static/',]
extra_files = []
for extra_dir in extra_dirs:
  for dirname, dirs, files in os.walk(extra_dir):
    extra_files.append(dirname)

manager.add_command('run', Server(extra_files=extra_files)) # this line
passes in those directories to the extra_files parm of the flask srv

*~ Summary ~*
The code in part one, simply generate a hash of the entire static directory
and then uses that to add a "version" parameter to the requests for static
files.
The code in part two, simply tells the werkzeug reloader to watch for any
changes in the static folder.
I also set the SEND_FILE_MAX_AGE_DEFAULT in my config to 1 year.
My entire site is served over HTTPS, so proxies shouldn't be an issue.

--
Corey

On Sun, Apr 24, 2016 at 2:02 PM, Jeff Widman <jeff at jeffwidman.com> wrote:

> Check out flask-webpack extension... The creator has a good video and blog
> post and using it will provide an introduction to tools like webpack
> (browserify, grunt, gulp, etc)
> On Apr 24, 2016 10:32 AM, "Corey Boyle" <coreybrett at gmail.com> wrote:
>
>> Does anyone have recommendations for dealing with client side cached
>> js/css files when pushing out new versions? I was thinking about
>> embedding version numbers into the files and incrementing them
>> whenever I commit new versions. Or maybe adding something like
>> ?ver=123 to the end of all resource links and incrementing that.
>>
>> Am I even asking the right question?
>> _______________________________________________
>> Flask mailing list
>> Flask at python.org
>> https://mail.python.org/mailman/listinfo/flask
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/flask/attachments/20160426/a9c11451/attachment.html>


More information about the Flask mailing list