views:

114

answers:

4

I'm working with someone else's code, so I don't know the whole picture, and I don't even know MVC that well, but here's the problem...

In Site.Master there's a

<%= Html.IncludeJs("ProductPartial")%>

which produces this line in the final mark-up

<script type="text/javascript" src="/Scripts/release/ProductPartial.js"></script>

I made some changes in the JS file, but the old one is obviously cached by the browser, so the changes won't show up until the user refreshes. The usual workaround is to add a version tag at the end of the script source path, but I'm not sure how to do that in this case.

Any suggestions?

A: 

Your version strategy really isn't important. As long as the file name is different, the browser will be forced to get the new script. So even this would work:

<%= Html.IncludeJs("ProductPartialv1")%>

ProductPartialv1.js

I have been using this technique for important JavaScript and CSS changes (CSS is also cached by the browser) - so I update the template to use the newer version and I'm safe in the knowledge that if the new HTML is used, so is the new script and CSS file.

It is "in action" on http://www.the-mag.me.uk/ - where I just increment a numeric suffix on the files.

Sohnee
That would mean I have to rename the files every time something changes and update all references. Not a perfect solution.
Ed
Or you can delete the file from your browser cache.. which browser are you using?
moi_meme
This is about forcing users' browsers to update, not my own.
Ed
On the site mentioned in my example, I rename the file and update the reference once in code. When you say "update all references" - it sounds like you should be holding the script name as a config item, rather than hard-coding it in many places inside your app. This is the only way to force a browser to use the new file.
Sohnee
+1  A: 

Here are some links already on this topic:

http://stackoverflow.com/questions/2204107/why-do-some-websites-access-specific-versions-of-a-css-or-javascript-file-using-g

http://stackoverflow.com/questions/2185872/force-browsers-to-get-latest-js-and-css-files-in-asp-net-application

David Liddle
Not the same thing. I know how to version normal script includes, but the question is about Html.IncludeJs.
Ed
+2  A: 

Why not write your own Html helper extension method, and make it output the version number of your application assembly? Something along these lines should do the trick:

public static MvcHtmlString IncludeVersionedJs(this HtmlHelper helper, string filename)
{
    var version = Assembly.GetExecutingAssembly().GetName().Version;
    return MvcHtmlString.Create(filename + "?v=" + version);
}

You can then increment the version number of the assembly whenever you release a new version to your users, and their caches will be invalidated across the application.

Jonas H
Might be useful. Will see if I can use that instead of Html.IncludeJs.
Ed
A: 

It turns out IncludeJs is a helper method for automatically including compressed JS files when in release mode: LINK.

So I just have to modify that method a bit to include a version number. Sorry about the confusion.

Ed