Friday, May 25, 2012

How to refresh javascript css files automatically in simple easy fast way

As I was developing and had this needed for some time but still a small enough project not to need a really big solution. When making small (or bigger) javascript changes, uploading it to the server now the users need to actually hit refresh (sometimes more than once!) I know we ask a lot of the users but here's a simple easy fast elegent way I found that I think works pretty well and someone else might find useful.

<link rel="stylesheet" type="text/css" href="css/style.css?v=<?php echo filemtime('css/style.css'); ?>" />
<script type="text/javascript" src="js/ajax.js?v=<?php echo filemtime('js/ajax.js'); ?>"></script>

As you can see it works for both css and js, very simple get type query which is meaningless to the files but just checks the when the file changed. So say you changed something recently that return of unix timestamp will change and now the browser is forced to download again instead of using the cached version.

Works pretty well so far, I will update if something changes but let me know if you found this useful/used it or anything related.

Thanks for visiting.

No comments: