Search code examples
javascriptcachingwebserverbrowser-cache

How to deal with cached client side files after they gets updated


I have an application running on hostgator. When I make changes to the .js files my users don't see the changes until they clear their cache. Is this the only option to push changes to my application?

Basically, I'm supposed to make the change, update the files and then request all users to clear their cache?


Solution

  • You need to use versioning on your file includes. Anytime you change the URI of your file includes, the browser won't find a cache match and will re-download the include.

    For example:

    <script type="text/javascript" src="include/232/init.js"></script>
    

    Where 232 is your modifiable version number that should be changed whenever you release new code.

    Alternatively, you can use query strings:

    <script type="text/javascript" src="include/init.js?232"></script>
    

    The point is that you should change the file include URI in some manner whenever you want your visitors to re-download the file.

    If you use PHP or another server-side language, you can setup this versioning to occur automatically whenever your file includes are modified: http://derek.io/blog/2009/auto-versioning-javascript-and-css-files/