0

We have an internal .NET web application, that has few embedded javascript files. Everytime we release a new version of the application, we face the problem where the user's browser has an older version of the js file, and we have to ask them to hit Ctrl + F5 on their browser to fetch latest.

Unfortunately, this is annoying. We were thinking of embedding a random number in the js call like this so the browser sees a changed url and fetches the new js automatically -

Has anyone achieved automating something like this without having to go in and do this manually for each changed js?

PS: Not looking to refresh every single time by appending <%DateTime.Now.Ticks%> or a similar strategy, just looking to automate appending the version for each release.

Binita Mehta
  • 339
  • 1
  • 8
  • Check this out: http://stackoverflow.com/questions/7538104/are-the-js-files-being-cached You can read more about optimizing caching here: https://developers.google.com/speed/docs/best-practices/caching?csw=1 – Alex Art. Nov 23 '13 at 16:33
  • Thanks Alex for pointing me to the resource. I was wondering more along the lines if there was a way to automate appending the random string after each release. – Binita Mehta Nov 23 '13 at 16:39
  • @BM0: instead of automating this, make it explicit. Take a look at my answer. – Wiktor Zychla Nov 23 '13 at 16:42

2 Answers2

1

There are numerous ways to force the refresh but the commonly used in practice is versioning.

In this approach, your javascript file names have version number suffixes. Anytime a file changes, it version changes and also all references change.

For example

ourapp.core.1.1.js

and then

ourapp.core.1.2.js

This also helps in reusing the same javascript files between different applications.

Wiktor Zychla
  • 47,367
  • 6
  • 74
  • 106
0

Here is one way I just thought of to achieve this:

<script type="text/javascript" src="somejsfile.js?<%MyHelperClass.GetVersion()%>

where GetVersion() returns the latest version from local.config.

In local.config, have a key like this:

<add key="Version" value="1.2"/>

One downside of this approach is that you need to remember to change the version in local.config with each release. Or better still, have your build script ask you for the version each time and then embed that in local.config.

Binita Mehta
  • 339
  • 1
  • 8