Stop loading JS from Cache

Hello Masters !

Well, I have a big online application that many user uses in my office. It has extensive code of AJAX etc Javascript and JQuery. The problem is - whenever i update some code/function - I or any other user has to press Control+F5 in order to take the new changes into effect.

I do not want this, I want that whenever I upload new files / new codes it applies without pressing Control+F5 or in simple words it should not load from cache.

What are the possibilities and different ways to tackle this problem ? Is there anything I can do with PHP or .Htaccess to ENFORCE NO CACHE policy for all pages and ajax scripts ?

Please help
ZH

I see two ways to solve that problem.

  1. Configure your server to send headers that will prevent caching for .js files (I will not help with that one :smile:)

  2. Add something random to URL of the .js file using PHP:

Formally, that method will produce a new URL for the same script each time, so browser will not use cache.
And of course, don’t forget to remove that code later on.

Even better if you add something static to the URL (like new version number) when you’re done with updates:

<script src="/path/to/script.js?v=2015-09-08"> 

That will allow users to recache your script just once, when there is a real need to do that.

1 Like

The following Leverage Browser Caching article from Google has some good details in there.

ETag (from the article) looks to be a good revalidation solution.

1 Like

Okay thanks. Nice suggestion. But what if some JS codes are not in a separate js file but in tag of a php file ?

I can’t advise anything better than move that code into separate files :wink:

1 Like

What about this :

Yes, that is forcing there to be no cache at all, which seems to be inferior to the etag solution where it’s cached only until a more recent change occurs.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.