Occasionally, occasionally!, I’ll post techy/codey things here, though it’s an activity I usually refrain from because a) I never ever wanted to be a tech nerd blogger, because b) tech nerds are the fucking worst, and c) my greatest fear in life is being Patient Zero on a reblog chain that ultimately devolves into a treatment of the merits and demerits of semaphores vs. message passing and/or the righteousness of Ayn Rand’s John Galt, because d) seriously what the fuck is with that anyway? why is every coder so hot for Ayn Rand? and also e) “tech nerd” totally conflicts with my Personal Brandâ„¢, but anyway!, sometimes I do like to give a little something back to the Google and maybe help a bro out who’s been headdesking over something I’ve already been through all seven stages of.
So!
Let’s talk static asset versioning. What a pain in the ass! Because as we all know you can’t just link to /static/js/boom.js and be aggressive with your cache headers because then the next time you :w && git add static/js/boom.js && git commit -m “boom!” && git push && fab -R prod deploy some poor schmuck is going to hit your site and still have the old boom.js sitting in his browser cache and all your shit’s gonna be totally effed.
And while there are a number of open source strategies out there for solving this they’re all a little heavy-handed for my tastes (and the asset versioning support in the forthcoming Django 1.3 looks to be particularly so). But this morning I eureka!‘d in a big way and nailed an extremely lightweight method that works wonderfully for my needs. And hey, maybe yours too! Assuming at least that you work in a Django environment and your deployment strategy involves pulling from a remote Git repo.
etc/templatetags/tags.py
from django import template
from django.conf import settings
@register.simple_tag
def static_url(token):
return '%s/static%s%s?v=%s' % (
settings.STATIC_URL,
('' if token[0] == '/' else '/'),
token,
settings.ASSET_HASH
)
settings.py
settings.py
import subprocess
STATIC_URL = 'http://path.to.your.cdn/'
ASSET_HASH = subprocess.Popen(
"git log -1 --abbrev-commit",
stdout=subprocess.PIPE,
shell = True).communicate()[0].split(' ')[1].split('\n')[0]
templates/sample.html
{% load tags %}
<script type="text/javascript" src="{% static_url 'js/base.src.js' %}">
And it’s that easy: embed all static files in your templates using this {%static_url%} tag, then every time you kick Apache/nginx/etc and Python does its thing settings.ASSET_HASH will be assigned the latest abbreviated Git commit hash and you’ll have lovely cache-safe URLs like http://path.to.your.cdn.com/static/js/boom.js?v=288c616.
Enjoy!
-
jasper-whetham likes this
-
maricela-gorum likes this
-
baobabtic likes this
-
justine likes this
-
oversets said:
I AM NOT READING THIS.
-
nikography said:
THE ABSOLUTE WORST OF THE WORST :3
-
inky likes this
-
nostrich said:
This verbose bullshit is what you call giving back to Google? Google fucking hates yr guts.
-
eoporto said:
dork
-
dwineman said:
Rails does something like this automatically when you use the asset tag helpers, but as far as I know it doesn’t use the git hash. That’s a cool idea.
-
boutofcontext likes this
-
thingsthatscarelaurenleto likes this
-
mwunsch likes this
-
wnstn likes this
-
roxanstone said:
at least i understand 1/3 of the post. it’s great that you didnt alienate your audience.
-
paulstraw likes this
-
jhnbrssndn likes this
-
banterability likes this
-
langer posted this