0

I've got an ASP.NET web page where I want to ensure users get the latest version of the javascript file each time they load the page, but I do not want them to download the file unless it's actually changed. It is a continually evolving web app and the js files get modified frequently. I have a version number in the url that I update manually. But I keep forgetting to update it when I change the js file.

<script type="text/javascript" src="js/Sched.js?v=1"></script>

So doing some searching for auto-versioning methods, I found one using re-writes and php, which made me think about my environment. The only thing I came up with was to use the LastWriteTime of the js file for the version. So I built a function:

    protected string GetFileTime(string file)
    {
        FileInfo fi = new FileInfo(Server.MapPath(file));
        if (fi.Exists)
        {
            Debug.WriteLine(fi.FullName);
            return fi.LastWriteTimeUtc.ToFileTime().ToString();
        }
        else
        {
            Debug.WriteLine("0");
            return "0";
        }
    }

And changed my script url in the page:

<script type="text/javascript" src="js/Sched.js?dev=<%= GetFileTime("js/Sched.js") %>"></script>

It seems to work great on my dev server. The browser sees this:

<script type="text/javascript" src="js/Sched.js?dev=131969009464369343"></script>

I've tested it with many page loads and the number remains the same until I make a change to the file.

So it seems like a great solution. I've looked at FileInfo and it reads the file meta data from the disk and does not open or lock the file. But when I put it live on the production server with about 1000 users, is there a possibility of performance or other issues?

6
  • Are you updating JavaScript but not any of the server side code? I only inquire because even with my SPA's my package.json is keeping track and versioning everything.
    – Greg
    Mar 12, 2019 at 22:20
  • @Greg Yes. Mostly updating the js file and possibly html. But server-side code only occasionally gets changed and recompiled if I have to add a new WebMethod for an ajax call.
    – tolsen64
    Mar 12, 2019 at 22:41
  • And you are not using a package.json?
    – Greg
    Mar 12, 2019 at 22:45
  • Never heard of it before. I just looked it up and it has something to do with node.js and npm, neither of which i'm familiar with. This is just a plain-jane asp.net website with a few js and css files.
    – tolsen64
    Mar 12, 2019 at 23:07
  • 1
    1,000 users won't be a problem IMO. However from server perspective I will try to avoid touching hard drive as much as I can. You can always add a cache/use FileMonitor if it becomes a problem later. FYI, ASP.NET Core asp-append-version uses hash code + file change monitor.
    – Luke Vo
    Mar 13, 2019 at 0:09

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.