How to force cache flush

July 12, 2016 7:24 am Published by 2 Comments

I’ve updated the icon font & the icon font was not showing properly.

To force flush icon fonts after making changes to your icon fonts, you can use applicationCache.swapCache().

https://html.spec.whatwg.org/multipage/browsers.html#dom-appcache-swapcache

Below is the code snippet I’ve used to force a new cache on visitor.

// Check if a new cache is available on page load.
window.addEventListener('load', function(e) {

  window.applicationCache.addEventListener('updateready', function(e) {
    if (window.applicationCache.status == window.applicationCache.UPDATEREADY) {
      // Browser downloaded a new app cache.
      // Swap it in and reload the page to get the new hotness.
      window.applicationCache.swapCache();
      if (confirm('A new version of this site is available. Load it?')) {
        window.location.reload();
      }
    } else {
      // Manifest didn't changed. Nothing new to server.
    }
  }, false);

}, false);

Hope this helps anyone who needs to force a new cache on visitors.

Tags: , ,

Categorized in:

This post was written by hackya

2 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *