RPM1984
RPM1984

Reputation: 73123

ASP.NET 4.5 Bundling in Debug Mode - Stale Resources

Is there any way we can make the ASP.NET 4.5 Bundling functionality generate GUID's as part of the querystring when running in debug mode (e.g bundling turned OFF).

The problem is when developing locally, the scripts/CSS files are generated like this:

<script type="text/javascript" src="/Content/Scripts/myscript.js" />

So if i change that file, i need to do a hard-refresh (sometimes a few times) to get the file to be picked up by the browser - annoying.

Is there any way we can make it render out like this:

<script type="text/javascript" src="/Content/Scripts/myscript.js?v=x" />

Where x is a GUID (e.g always unique).

Ideas?

I'm on ASP.NET MVC 4.

Upvotes: 4

Views: 752

Answers (3)

Rafe
Rafe

Reputation: 9295

Try HashCache: https://github.com/kemmis/System.Web.Optimization.HashCache

Execute the ApplyHashCache() extension method on the BundlesCollection Instance after all bundles have been added to the collection.

BundleTable.Bundles.ApplyHashCache();

This will add content hashes to the script/style tags output when in debug mode.

Upvotes: 0

RPM1984
RPM1984

Reputation: 73123

Until the NuGet package is patched as per the other answer above, for now i've ended up using the same wrapper code i did for the beta NuGet package:

private static IHtmlString JsUnbundled(this HtmlHelper htmlHelper, string bundlePath)
{
    var jsBuilder = new StringBuilder();
    foreach (var file in BundleResolver.Current.GetBundleContents(bundlePath))
    {
        var tagBuilder = new TagBuilder("script");
        tagBuilder.Attributes["src"] = file.AddCacheKey(); // add GUID
        tagBuilder.Attributes["type"] = "text/javascript";
        jsBuilder.AppendLine(tagBuilder.ToString());
    }
    return MvcHtmlString.Create(jsBuilder.ToString());
}

I then have another HTML helper which checks if debug, then uses the above - otherwises uses Scripts.Render.

Obviously this doesn't do any kind of hashing of the file - it will ALWAYS request the file. But i don't mind this, as it's only for debug.

Upvotes: 4

Hao Kung
Hao Kung

Reputation: 28200

We don't currently examine the contents of the files in debug mode, but we could add this feature.

I filed it as an issue on codeplex here.

Upvotes: 1

Related Questions