HTML, CSS, JavaScript
How to reload Java Script file
Gravatar is a globally recognized avatar based on your email address. How to reload Java Script file
  Craig Tucker
  All
  Apr 28, 2023 @ 06:04am

Hi All, We have added some functions to a client facing asp.net application associated javascript file. However, we are having to ask clients to clear their cache prior to using the application because new methods are not visible.

Question is what is the cleanest way to automatically reload new javascript files in an asp.net aspx page?

Thank you, Craig

Gravatar is a globally recognized avatar based on your email address. re: How to reload Java Script file
  Rick Strahl
  Craig Tucker
  Apr 29, 2023 @ 06:04am

Add a version number in the ASP.NET pages and the URLs you're using to load them.

In your main app add a version number (or some other unique value) that retrieves the current app's version:

public class App 
{
     public static string Version { get; set; }
     public static App() 
     {
          Version = typeof(App).GetType().Assembly.GetName().Version.ToString();
          // Version = DataUtils.GetUniqueId();
     }
}

If you're incrementing build numbers (recommended) you can just use Version.Build rather than the full version string which is less obvious and also saves a few bytes in content.

Then in your Razor code:

<script src="/scripts/SomeJsFile.js?v=@App.Version"></script>

Using the version number can be more effective as it will cache based on the version. Using a random unique value ends up busting the cache every time the app is restarted so the version number (or some part of it like the Build number) is often preferrable as global caching can kick in.

The downside is that you have to ensure that update the version number changes so you need to probably use auto-increment build numbers and ensure you compile before every publish rather than just publishing an updated script file.

+++ Rick ---

Gravatar is a globally recognized avatar based on your email address. re: How to reload Java Script file
  Craig Tucker
  Rick Strahl
  May 1, 2023 @ 03:58am

Perfect!
Thank you, Craig

© 1996-2023