Automatic Cache Buster

This is just a quick post to share a JavaScript snippet I came up with.

I’m currently working on a project with very aggressive server-side caching. Every requested URL is cached, so if I make any changes and want to see them, I have to tweak the query string to create a unique URL. Very tedious.

So I wrote a script that attaches a keydown listener to the window and listens for F5 or ctrl + r to be pressed. It intercepts the browser’s usual reload behavior, sticks a unique timestamp into the current URL’s query string, and requests that new URL instead. This sidesteps the server-side caching and lets me check rapid-fire changes much more quickly.

The URL parsing isn’t 100% bullet-proof, but more than enough for my needs. I went ahead and created a GitHub repo in case I decide to take this thing further.

JavaScript Function Methods: bind() Browser History