How to make sure that your CSS is never cached in the browser.

I was working on a simple HTML design. The IDE which I was using was a browser-based IDE named Codiad.  I was trying it out.  While I was working on a very simple design. For some reason whenever I made a change to the CSS it was not reflecting on the design I made. I was running the file directly from the live server.  After spending some time I realized that my CSS was not getting reflected because the browser was using the cached file.  I tried reloading the page with ctr+F5 but the CSS would not update. I searched the net for a solution but could not find a proper one so I just made a small hack in the CSS using PHP. This can be done using any scripting or programming language.

Below is the code:

<link rel="stylesheet" href="customStyle.css?v=<?php printf('%s.%s.%s',rand(),rand(),rand()); ?>">

Hope this might be useful for others searching for a solution.

Add a Comment

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