Hemm K
Hemm K

Reputation: 469

Caching JS without changing the file name or clearing cache locally

I'm working on a project, which has no way to implement versioning on my js files for cache.

Therefore I need a way to update users cache from the server side, if possible.

My thinking is that I can change the name of the js temporarily, then let the browser cache the new instance. Then I will revert the file name to what it was.

At this point does this mean that the browser will still have a cache of the old filename therefore loading the old js?

e.g.

example.js > changes to > example-new.js > browser caches this new js > revert the filename back to example.js > ...... what happens?

Upvotes: 0

Views: 1500

Answers (1)

pavlos
pavlos

Reputation: 3081

If your case is to change js code on the server side and then make this changes available to the clients without the need of "clear cache" then do the following.

Instead of changing the filenames, reference them with an extra parameter, lets say using the version. So lets say you reference a js file "example.js" within your html,jsp .... page.

your tag should be like: (note the ?v1.1 at the end of the file url)

<script type="text/javascript" src="jslib/example.js?v1.1"></script>

Now next time you make changes to this file, reference it like:

<script type="text/javascript" src="jslib/example.js?v1.2"></script>

This will force client browser to get the new file instead of using the cached one.

Upvotes: 2

Related Questions