Dynamic Css and Javascript link

Problem

Sometimes we update our websites design(css), Javascript code or Images. Problem is we see the old content when visit the site again. This happens because browser chache those contents when we visited the website. So next time we visit the site it shows the content from cahce. Even visitors will not realize the change unless the cache is cleared or re-chache.

Solution

If we can make the css,Javascirt link or even the image link(if needed) dynamic then browser cannot cache the contents because the links are not same every  time it loads. Suppose we want to add a style.css we can add the css like this

<link rel="stylesheet" href="style.css?a_dynamic_text">

This line will alaways load the style.css does not matter what we include after ?. If we can put some dynamic text there then the href will not be same each time. So browser will load the content again from server(not form cache).

I try to set current time as dynamic text because it is always different and unique.So I use following way

<link rel="stylesheet" href="style.css?v=<?php echo time(); ?>">

Php time funciton returns current time in second so my link will look like this which always dynamic and unique

<link rel="stylesheet" href="style.css?v=1508959178">

 

Bookmark the permalink.

Leave a Reply

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