just some sort of lame blog

Static Asset Versioning

One of the things that I used with suo, was versioning CSS and JS files. There are existing ways of doing this with grunt, node, or by hand. Since I'm still new to how node works I thought it would be a good learning exercise. The old way of doing this was to append a querystring to the linked file but certain firewalls don't cache files with querystrings.

I initially tried generating checksums but the resulting hashes were too huge. I didn't want to use more bytes than necessary so I used the last modified datetime of the file. I basically just converted the time in milliseconds to a custom base. Nothing too special about the custom number base code. The 66 characters I used were all ones that don't have to be escaped when they're used in URLs. If they had to be escaped it would just add bytes.

To shorten the hashes more I divided by 60,000 to use the nearest minute, and then I subtracted a more recent date that would serve as the new zero. As of right now the hashes are just 3 characters. I wrote a little helper for handlebars that uses the original filename as a key and the renamed as the value.

After all this, it turns out that Github Pages only sets the cache for 10 minutes instead of the far future which defeats the whole purpose altogether.


comments powered by Disqus