[RESOLVED]Why is The Browser Caching An Old Version of my JS File?

I’m working and testing strictly on the dev machine (MVC 4.0), nothing deployed as yet. I created some helpful js routines and placed them in a js file. Later I edited the file but Firefox is still using the old version (cached I surmise). I hear that MVC
was supposed to nullify this caching by computing a hash of the file, appending it to the url’s query string (serving as the new version number), thus embedding a new  <script src=url> onto the page. I looked at my rendered page hoping to see such a hash appended
to the url but all I see is this:
 <script src="/Family/Scripts/MyUtilities.js"></script>
In my bundles loader I have:
    bundles.Add(new ScriptBundle("~/bundles/MyUtilities").Include("~/Scripts/MyUtilities.js"));
and the View page invokes it like this:
    @Scripts.Render("~/bundles/MyUtilities")                  
What am I doing wrong?

I think it’s no longer cached, I don’t know if it’s because I restarted both the app and the browser,

Why don’t my rendered <script src=url> tags have a hash/version number? I’m hoping to prevent this problem recurring in the future.

Hi jal2,

jal2

Why don’t my rendered <script src=url> tags have a hash/version number? 

The browser cache files base on the URL. So you could add parameter to the URL.

<script src="~/Scripts/Files/Te.js?version=1.0"></script>

Best Regards

Starain

Ok thanks. But isn’t MVC 4 supposed  to automatically append a hash/version-no if you use the @Scripts.Render(‘my bundle’) syntax? I thought I read such somewhere.

It does, if optimizations are enabled and the argument to the Scripts.Render() method is a bundle name and it could create the bundle response to calculate the hash value for.

About the first part in that sentence: Depending on what template you used, BundleTable.EnableOptimizations may be set in BundleConfig.cs. If not set, it depends on the "debug" value from the <compilation>-node in web.config.

About the second part: Scripts.Render() has special handling when passed a bundle name (which is what you are after), but if it is a path to a file it just renders an ordinary <script>-tag without the hash.

Your description of the problem suggests you haven’t enabled optimizations, or you are debugging, or you used the path to the javascript file and not the bundle name.

I set BundleTable.EnableOptimizations = true in global.asax and got immediate success. Thanks!

Leave a Reply