Cache Bust

Intro

This is a bookmarklet to reload the current webpage with a randomized query string at the end in order to retrieve a new copy of a page that otherwise may reload from cache. It's useful when you need to check updates to a page more quickly than your CMS' normal cache time allows.

For example, if you are on www.mysite.com/foo.html and you refresh the page, you may simply get the same cached page. Click the "Cache Bust" bookmarklet and you will go to www.mysite.com/foo.html?refresh491057368, which is a different URL and therefore you will receive a new copy of the page.

How to install

Drag the bookmarklet below onto your browser's Bookmarks Toolbar. It will create a new bookmark there. Then go to any URL to test it.

[Cache Bust]

Source

/* 
 * Cache Bust
 *
 * Reload the current webpage with a randomized query string at the end
 * to break out of the current caching cycle. Useful when you need to check
 * updates to a page more quickly than your CMS' cache cycle allows.
 */

/* Set up 9-digit random number */
var max = 999999999;
var min = 100000001;

/* Get current URL as a string */
var current_url = window.location;
current_url = current_url + '';

/*
 * Generate cache-busting string.
 * This will be the word 'refresh' followed by a random 9-digit number.
 */
var rand = Math.floor(Math.random() * (max - min + 1)) + min;
var cache_bust_string = '?refresh' + rand;

/*
 * Before adding the cache-busting string to the current URL,
 * check to see if URL already contains a previous string.
 */
 
var cache_bust_check = new RegExp(/\?refresh([0-9]{9})/g);
var has_cache_bust_string = cache_bust_check.test(current_url);

if (has_cache_bust_string == true) {
  /*
   * If the URL already has one of the cache-busting strings,
   * then delete it.
   */
   var old_cache_bust_string = current_url.match(cache_bust_check);
   var current_url = current_url.replace(old_cache_bust_string, '');
}

/* Append the cache-busting string to the new URL */
var new_url = current_url + cache_bust_string;

/* Redirect browser to cache-busted URL */
window.location = new_url;

← Home