Friday, 7 December 2012

Stop CSS file caching

Websites store cache in the web browser to prevent the same files being downloaded over and over again. However, when you need to update your website with new styles, the old style sheet may still be accessed instead of the updated style sheet.

The solution is to add a query string after the name of the CSS file (styles.css?v=1). When you come to update your style sheet change the number after the ‘v=’ and the browser will download the new style sheet instead of the cached one. The browser can recognise the different file name from the cached file name and will download the new file from the web server.

For example, this is what your link tag would look like:

<link rel="stylesheet" type="text/css" href=" styles.css?v=1">
You would change the link tag like this:
<link rel="stylesheet" type="text/css" href=" styles.css?v=2">
The same trick also works for stopping Javascript files being browser cached. For example:
<script src="myscript.js?v=1"></script>


  COPYRIGHT © 2014 · ILIKE2FLASH · Theme by Ourblogtemplates

Back to TOP