Is this a safe, inexpensive way to do auto-versioning on an ASP.NET web page? Or will it bite me?

198 Views Asked by At

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?

0

There are 0 best solutions below