Sunday, May 26, 2019

Can You Disable Caching on CSS style Sheets? A True Hack to Stop Caching.

Can We Prevent CSS Caching? Can we disable Caching of CSS pages? Can we Force a browser to re-read the CSS everytime?

Those Are The Questions that broke the internet, combined with the death of Grumpy Cat, whom we could never save, not with the best in veterinary medicine. Nothing can bring her back, not even cloning or cryogenic treatments. As for CSS, we could never prevent caching. If you are a web developer, you need to refresh you pages a lot. Unfortunately, your styles sheets will never change, because your stupid browser, whether it be Chrome, Firefox, internet explorer or Edge will happily save a copy of your old CSS and keep it, and use it to render your pages.   The other unfortunate effect is that the public, i.e. your viewers and customers browsers will hang onto an old version of stylesheet.   

Can we solve this? Is there some way to prevent CSS caching? Is there some magical tag that you can add to your HTML to prevent caching? The answer is NO, Grumpy Cat would also say NO, NO, NO, if she were still with us.  No HTML tag can stop this. The makers of web browsers don't think you need this feature. So no, its impossible.

However, there is a "work-around". Everyone in management hates workarounds. Time stamp your CSS links.

Here is a trick that comes from the world of JavaScript programmers. To prevent the caching of their css files, they add a timestamp to the end of the src attribute. We can do the same by turning .
We did a quick test to see if this trickery would even work. Here is how I included the stylesheet link in my index.php
<link rel="stylesheet" type="text/css" href="/style.css?<?php echo date('U'); ?>" />
Which results in this:
<link rel="stylesheet" type="text/css" href="/style.css?1558914597" />
Alter that date format as needed. We recommend using the "U" option to just show the number of seconds since January 1, 1970.

Brought to You By

This Tech Tidbit was brought to you by your Web Design Pros at Eleven Dimensions Computer Technologies. See the new 11D website at:

Many Thanks to Our Partner companies

No comments:

Post a Comment